Commit 2e919a0b authored by nanahira's avatar nanahira

implant of ladder

parent 43df9b4e
...@@ -107,6 +107,9 @@ dist ...@@ -107,6 +107,9 @@ dist
.vscode-test .vscode-test
/data/ /data/
/result/ /result/
/lists/chnroutes*
/lists/gfwiplist*
/lists/result.yaml
*.retry *.retry
*-setconf.conf.j2 *-setconf.conf.j2
......
...@@ -46,40 +46,26 @@ protocol kernel { ...@@ -46,40 +46,26 @@ protocol kernel {
{% endif %} {% endif %}
{% endfor %} {% endfor %}
ipv4 table gfwiplist;
ipv4 table chnroute_reverse; {% for plan in route_tables %}
{% for net in gfwiplist %} ipv4 table {{plan.name}};
protocol static { {% for net in plan.list %}
ipv4 {
table gfwiplist;
};
igp table master4;
route {{net}} recursive 10.199.0.12;
}
{% endfor %}
{% for net in chnroute_reverse %}
protocol static { protocol static {
ipv4 { ipv4 {
table chnroute_reverse; table {{plan.name}};
}; };
igp table master4; igp table master4;
route {{net}} recursive 10.199.0.12; route {{net}} recursive {{plan.gatewayAddress}};
} }
{% endfor %} {% endfor %}
protocol kernel { protocol kernel {
ipv4 { ipv4 {
table gfwiplist; table {{plan.name}};
export all; export all;
}; };
kernel table 401; kernel table {{plan.table}};
}
protocol kernel {
ipv4 {
table chnroute_reverse;
export all;
};
kernel table 402;
} }
{% endfor %}
protocol ospf v2 { protocol ospf v2 {
ipv4 { ipv4 {
......
netaddr==0.7.19 netaddr==0.7.19
pyyaml
#!/usr/bin/env python3
from netaddr import *
universe = IPSet(['0.0.0.0/0'])
special = IPSet([line.strip() for line in open('special.txt')])
chnroutes = IPSet([line.strip() for line in open('chnroutes.txt') if not line.startswith('#')])
result = universe - special - chnroutes
for network in result.iter_cidrs():
print(network)
#!/usr/bin/env python3
from netaddr import *
import yaml
def read_yaml_file(name):
file = open(name, 'r', encoding="utf-8")
data = yaml.load(file, Loader=yaml.SafeLoader)
file.close()
return data
def write_yaml_file(name, data):
file = open(name, 'w', encoding="utf-8")
yaml.dump(data, file)
file.close()
universe = IPSet(['0.0.0.0/0'])
special = IPSet([line.strip() for line in open('special.txt')])
chnroutes = IPSet([line.strip() for line in open('chnroutes.txt') if not line.startswith('#')])
gfwiplist = IPSet([line.strip() for line in open('gfwiplist.txt') if not line.startswith('#')])
result = {'chnroute_reverse': [], 'gfwiplist': []}
chnroute_reverse = universe - special - chnroutes
for route in chnroute_reverse.iter_cidrs():
result['chnroute_reverse'].append(str(route))
gfwiplist_patches = read_yaml_file('patch-gfwiplist.yaml')
gfwiplist_add = IPSet(gfwiplist_patches['add'])
gfwiplist_remove = IPSet(gfwiplist_patches['remove'])
gfwiplist_patched = (gfwiplist | gfwiplist_add) - gfwiplist_remove - special
for route in gfwiplist_patched.iter_cidrs():
result['gfwiplist'].append(str(route))
write_yaml_file("result.yaml", result)
...@@ -5,7 +5,12 @@ if [ -d "chnroutes2" ]; then ...@@ -5,7 +5,12 @@ if [ -d "chnroutes2" ]; then
else else
git clone https://github.com/misakaio/chnroutes2.git git clone https://github.com/misakaio/chnroutes2.git
fi fi
ln -sf chnroutes2/chnroutes.txt chnroutes.txt ln -sf chnroutes2/chnroutes.txt chnroutes.txt
if [ -d "gfwiplist" ]; then
(cd gfwiplist && git pull)
else
git clone https://github.com/SteamedFish/gfwiplist.git
fi
ln -sf gfwiplist/gfwiplist.txt gfwiplist.txt
#pip3 install -r requirements.txt #pip3 install -r requirements.txt
python3 reverse.py python3 route_helper.py
...@@ -40,18 +40,26 @@ class InventoryBuilder { ...@@ -40,18 +40,26 @@ class InventoryBuilder {
// console.log(Object.values(this.hosts)); // console.log(Object.values(this.hosts));
const hosts = Object.fromEntries(Object.values(this.hosts).map(h => [h.host, this.host_vars(h)])); const hosts = Object.fromEntries(Object.values(this.hosts).map(h => [h.host, this.host_vars(h)]));
// console.log(hosts); // console.log(hosts);
const vars = await this.loadUtilities(); const vars = await this.loadUtilities(hosts);
const result = YAML.stringify({ wg: { hosts, vars } }); const result = YAML.stringify({ wg: { hosts, vars } });
return fs.promises.writeFile('result/inventory.yaml', result); return fs.promises.writeFile('result/inventory.yaml', result);
} }
async loadUtilities() { async loadUtilities(hosts) {
const raw_data = _.keyBy(await this.load('configurations'), 'key'); const raw_utility = _.keyBy(await this.load('configurations'), 'key');
let route_tables = await this.load('route tables')
const route_lists = YAML.parse(fs.readFileSync(path.join('lists', 'result.yaml'), "utf8"));
for (let plan of route_tables) {
plan.name = plan.list + "_" + plan.gateway.replace(/-/g, "_").split(".")[0]
plan.list = JSON.parse(JSON.stringify(route_lists[plan.list]));
plan.gatewayAddress = hosts[plan.gateway].address;
}
const vars = { const vars = {
all_subnets: JSON.parse(JSON.stringify(this.all_subnets)) all_subnets: this.all_subnets,
route_tables
}; };
for (let col in raw_data) { for (let col in raw_utility) {
vars[col] = raw_data[col].value; vars[col] = raw_utility[col].value;
} }
return vars; return vars;
...@@ -59,7 +67,7 @@ class InventoryBuilder { ...@@ -59,7 +67,7 @@ class InventoryBuilder {
host_vars(host) { host_vars(host) {
const connections = []; const connections = [];
const null_connection = "10000,null"; const null_connection = "10000,null";
const lan_interfaces = host.lan_interfaces.split(","); const lan_interfaces = host.lan_interfaces.length > 0 ? host.lan_interfaces.split(",") : [];
for (const h of this.connections) { for (const h of this.connections) {
if (h != host.name) { if (h != host.name) {
const to = host[h]; const to = host[h];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment