Commit 8d504357 authored by nanahira's avatar nanahira

update so many things

parent ea668061
...@@ -110,6 +110,7 @@ dist ...@@ -110,6 +110,7 @@ dist
/lists/chnroutes* /lists/chnroutes*
/lists/gfwiplist* /lists/gfwiplist*
/lists/result.yaml /lists/result.yaml
/build
*.retry *.retry
*-setconf.conf.j2 *-setconf.conf.j2
......
...@@ -49,7 +49,7 @@ protocol kernel { ...@@ -49,7 +49,7 @@ protocol kernel {
{% for plan in route_plans %} {% for plan in route_plans %}
ipv4 table {{plan.name}}; ipv4 table {{plan.name}};
{% for net in route_lists[plan.list] %} {% for net in routeLists[plan.list] %}
protocol static { protocol static {
ipv4 { ipv4 {
table {{plan.name}}; table {{plan.name}};
......
--- ---
- hosts: wg - hosts: wg
become: true vars:
services: []
tasks: tasks:
- name: directory
file:
path: '{{ansible_user_dir}}/nextgen-network/{{item}}'
state: directory
recurse: true
with_items:
- scripts
- services
- ipsets
- name: post scripts - name: post scripts
template: template:
src: scripts/{{item}}.sh.j2 src: scripts/{{item}}.sh.j2
dest: '/etc/wireguard/{{item}}.sh' dest: '{{ansible_user_dir}}/nextgen-network/scripts/{{item}}.sh'
mode: a+x mode: a+x
with_items: with_items:
- postup - postup
- predown - predown
- global-postup - global-postup
- global-postdown
- name: ipset files - name: ipset files
template: template:
src: scripts/ipset.j2 src: scripts/ipset.j2
dest: '/etc/wireguard/{{item}}.ipset' dest: '{{ansible_user_dir}}/nextgen-network/scripts/{{item}}.ipset'
with_items: '{{route_list_names}}' with_items: '{{routeListNames}}'
- name: global-postup - name: global-postup
shell: /etc/wireguard/global-postup.sh become: true
shell: '{{ansible_user_dir}}/nextgen-network/scripts/global-postup.sh'
args: args:
creates: /tmp/mycard_global_postup_done creates: /tmp/mycard_global_postup_done
- name: mycard ipset - name: mycard ipset
become: true
shell: 'ipset add mycard {{item}} || true' shell: 'ipset add mycard {{item}} || true'
with_items: '{{route_lists.mycard}}' with_items: '{{routeLists.mycard}}'
- name: 鸟配置 (Debian) - name: table pref 81 lookup main
template: become: true
src: bird.conf.j2 shell: 'ip -4 rule add pref 81 to {{item}} lookup main || true'
dest: '/etc/bird/bird.conf'
when: ansible_distribution == "Debian"
notify: restart_bird
- name: 鸟配置 (Ubuntu)
template:
src: bird.conf.j2
dest: '/usr/local/etc/bird.conf'
when: ansible_distribution == "Ubuntu"
notify: restart_bird
- name: 鸟配置 (RHEL)
template:
src: bird.conf.j2
dest: '/etc/bird.conf'
when: ansible_os_family == "RedHat"
notify: restart_bird
- name: start bird
systemd:
name: bird
state: started
enabled: yes
# 为了提高测试时候的性能,不改动wg的时候注释掉这段 # 为了提高测试时候的性能,不改动wg的时候注释掉这段
- name: 'clean up null connections first' - name: 'clean up null connections first'
include_tasks: 'protocols/{{item.protocol}}/configure.yaml' include_tasks: 'protocols/{{item.protocol}}/configure.yaml'
...@@ -60,11 +51,45 @@ ...@@ -60,11 +51,45 @@
conn: '{{item}}' conn: '{{item}}'
with_items: '{{ connections }}' with_items: '{{ connections }}'
when: "item.protocol != 'null'" when: "item.protocol != 'null'"
- name: 'frps' # end
include_tasks: 'protocols/wgfrp/frps.yaml' - name: services conf
when: 'frps_needed is defined' copy:
content: |
{{dockerServices | to_yaml}}
dest: '{{ansible_user_dir}}/nextgen-network/services/docker-compose.yml'
- name: bird conf
template:
src: bird.conf.j2
dest: '{{ansible_user_dir}}/nextgen-network/services/bird.conf'
notify: restart_bird
- name: frps conf
template:
src: protocols/wgfrp/frps.ini.j2
dest: '{{ansible_user_dir}}/nextgen-network/services/frps.ini'
when: frpsNeeded is defined
notify: restart_frps
- name: frpc conf
template:
src: protocols/wgfrp/frpc.ini.j2
dest: '{{ansible_user_dir}}/nextgen-network/services/frpc_{{item.name}}.ini'
with_items: '{{ connections }}'
when: item.type == "wgfrp" and item.frpType == "frpc"
notify:
- name: start services
docker_compose:
project_src: '{{ansible_user_dir}}/nextgen-network/services'
remove_orphans: true
handlers: handlers:
- name: restart_bird - name: restart_bird
systemd: docker_compose:
name: bird project_src: '{{ansible_user_dir}}/nextgen-network/services'
state: reloaded restarted: true
services:
- bird
- name: restart_frps
docker_compose:
project_src: '{{ansible_user_dir}}/nextgen-network/services'
restarted: true
services:
- frps
...@@ -8,8 +8,8 @@ FwMark = {{conn.localGatewayMark}} ...@@ -8,8 +8,8 @@ FwMark = {{conn.localGatewayMark}}
MTU = {{conn.mtu|int - 80}} MTU = {{conn.mtu|int - 80}}
Table = off Table = off
PostUp = dev=%i localPeerAddress={{conn.localPeerAddress}} remotePeerAddress={{conn.remotePeerAddress}} remoteNextMark={{conn.remoteNextMark}} remoteDestMark={{conn.remoteDestMark}} inbound={{conn.inbound}} outbound={{conn.outbound}} mtu={{conn.mtu|int - 80}} /etc/wireguard/postup.sh PostUp = dev=%i localPeerAddress={{conn.localPeerAddress}} remotePeerAddress={{conn.remotePeerAddress}} remoteNextMark={{conn.remoteNextMark}} remoteDestMark={{conn.remoteDestMark}} inbound={{conn.inbound}} outbound={{conn.outbound}} mtu={{conn.mtu|int - 80}} {{ansible_user_dir}}/nextgen-network/scripts/postup.sh
PreDown = dev=%i localPeerAddress={{conn.localPeerAddress}} remotePeerAddress={{conn.remotePeerAddress}} remoteNextMark={{conn.remoteNextMark}} remoteDestMark={{conn.remoteDestMark}} inbound={{conn.inbound}} outbound={{conn.outbound}} mtu={{conn.mtu|int - 80}} /etc/wireguard/predown.sh PreDown = dev=%i localPeerAddress={{conn.localPeerAddress}} remotePeerAddress={{conn.remotePeerAddress}} remoteNextMark={{conn.remoteNextMark}} remoteDestMark={{conn.remoteDestMark}} inbound={{conn.inbound}} outbound={{conn.outbound}} mtu={{conn.mtu|int - 80}} {{ansible_user_dir}}/nextgen-network/scripts/predown.sh
[Peer] [Peer]
PublicKey = {{conn.wgPublicKey}} PublicKey = {{conn.wgPublicKey}}
......
...@@ -2,27 +2,9 @@ ...@@ -2,27 +2,9 @@
- name: '{{conn.name}}: frpc template' - name: '{{conn.name}}: frpc template'
template: template:
src: 'frpc.ini.j2' src: 'frpc.ini.j2'
dest: '/etc/frp/{{conn.name}}.ini' dest: '{{ansible_user_dir}}/nextgen-network/services/frpc_{{item.name}}.ini'
when: 'conn.frpType == "frpc"' when: 'conn.frpType == "frpc"'
register: frpc_config_result notify: restart_frpc
- name: '{{conn.name}}: start frpc'
systemd:
name: 'frpc@{{conn.name}}'
state: started
enabled: true
register: frpc_launch_result
when: 'conn.frpType == "frpc"'
- name: '{{conn.name}}: restart frpc'
systemd:
name: 'frpc@{{conn.name}}'
state: reloaded
when: 'conn.frpType == "frpc" and frpc_config_result.changed and not frpc_launch_result.changed'
# frps
- name: '{{conn.name}}: register frps'
set_fact:
frps_needed: 1
when: 'conn.frpType == "frps"'
# wg # wg
- name: '{{conn.name}}: wg conf' - name: '{{conn.name}}: wg conf'
......
...@@ -5,12 +5,12 @@ PrivateKey = {{key}} ...@@ -5,12 +5,12 @@ PrivateKey = {{key}}
ListenPort = {{conn.localPort}} ListenPort = {{conn.localPort}}
{% endif %} {% endif %}
{% if conn.localGatewayMark != "" %} {% if conn.localGatewayMark != "" %}
FwMark = {{conn.localGatewayMark}} # FwMark = {{conn.localGatewayMark}}
{% endif %} {% endif %}
MTU = {{conn.mtu|int - 80}} MTU = {{conn.mtu|int - 80}}
Table = off Table = off
PostUp = dev=%i localPeerAddress={{conn.localPeerAddress}} remotePeerAddress={{conn.remotePeerAddress}} remoteNextMark={{conn.remoteNextMark}} remoteDestMark={{conn.remoteDestMark}} inbound={{conn.inbound}} outbound={{conn.outbound}} mtu={{conn.mtu|int - 80}} /etc/wireguard/postup.sh PostUp = dev=%i localPeerAddress={{conn.localPeerAddress}} remotePeerAddress={{conn.remotePeerAddress}} remoteNextMark={{conn.remoteNextMark}} remoteDestMark={{conn.remoteDestMark}} inbound={{conn.inbound}} outbound={{conn.outbound}} mtu={{conn.mtu|int - 80}} {{ansible_user_dir}}/nextgen-network/scripts/postup.sh
PreDown = dev=%i localPeerAddress={{conn.localPeerAddress}} remotePeerAddress={{conn.remotePeerAddress}} remoteNextMark={{conn.remoteNextMark}} remoteDestMark={{conn.remoteDestMark}} inbound={{conn.inbound}} outbound={{conn.outbound}} mtu={{conn.mtu|int - 80}} /etc/wireguard/predown.sh PreDown = dev=%i localPeerAddress={{conn.localPeerAddress}} remotePeerAddress={{conn.remotePeerAddress}} remoteNextMark={{conn.remoteNextMark}} remoteDestMark={{conn.remoteDestMark}} inbound={{conn.inbound}} outbound={{conn.outbound}} mtu={{conn.mtu|int - 80}} {{ansible_user_dir}}/nextgen-network/scripts/predown.sh
[Peer] [Peer]
PublicKey = {{conn.wgPublicKey}} PublicKey = {{conn.wgPublicKey}}
AllowedIPs = 0.0.0.0/0, ::/0 AllowedIPs = 0.0.0.0/0, ::/0
......
#!/usr/bin/env bash
# 临时梯子
iptables -t mangle -D PREROUTING -m set --match-set ladder_needed src -m set --match-set gfwiplist dst -j MARK --set-xmark 812/0xffffffff
rm -rf /tmp/mycard_global_postup_done
exit 0
#!/usr/bin/env bash #!/usr/bin/env bash
echo "running" > /tmp/mycard_global_postup_done
# ipset # ipset
{% for list in route_list_names %} {% for list in routeListNames %}
ipset restore -f /etc/wireguard/{{list}}.ipset || true ipset restore -f {{ansible_user_dir}}/nextgen-network/ipsets/{{list}}.ipset || true
{% endfor %}
# ip rule
{% for subnet in routeLists.mycard %}
ip rule add pref 81 to {{subnet}} lookup main || true
{% endfor %} {% endfor %}
# 临时梯子 # gateway things
# iptables -t mangle -A PREROUTING -m set --match-set ladder_needed src -m set --match-set gfwiplist dst -j MARK --set-xmark 812/0xffffffff {% for gateway in gateways %}
echo 1 > /tmp/mycard_global_postup_done echo "finished" > /tmp/mycard_global_postup_done
exit 0 exit 0
create {{item}} hash:net family inet create {{item}} hash:net family inet
{% for subnet in route_lists[item] %} {% for subnet in routeLists[item] %}
add {{item}} {{subnet}} add {{item}} {{subnet}}
{% endfor %} {% endfor %}
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"scripts": { "scripts": {
"inventory": "node -r ts-node/register --unhandled-rejections=strict src/inventory.ts " "build": "tsc",
"start": "node -r ts-node/register --unhandled-rejections=strict src/inventory.ts "
}, },
"dependencies": { "dependencies": {
"@types/lodash": "^4.14.149", "@types/lodash": "^4.14.149",
......
...@@ -11,7 +11,7 @@ class InventoryBuilder { ...@@ -11,7 +11,7 @@ class InventoryBuilder {
hosts: { [key: string]: any }; hosts: { [key: string]: any };
gateways: any; gateways: any;
connections: string[]; connections: string[];
route_lists: any; routeLists: any;
async load(sheetName) { async load(sheetName) {
const data = await fs.promises.readFile(path.join('data', `内网互联计划 - ${sheetName}.csv`)); const data = await fs.promises.readFile(path.join('data', `内网互联计划 - ${sheetName}.csv`));
...@@ -21,12 +21,15 @@ class InventoryBuilder { ...@@ -21,12 +21,15 @@ class InventoryBuilder {
async loadGateways() { async loadGateways() {
const gateways = await this.load('gateways2'); const gateways = await this.load('gateways2');
for (let gateway of gateways) {
gateway.selectionMark = gateway.mark + 50;
}
return gateways; return gateways;
} }
async main() { async main() {
this.hosts = _.keyBy(await this.load('nextgen2'), 'name'); this.hosts = _.keyBy(await this.load('nextgen2'), 'name');
this.gateways = _.mapValues(_.groupBy(await this.loadGateways(), 'name'), g => _.keyBy(g, 'isp')); this.gateways = _.mapValues(_.groupBy(await this.loadGateways(), 'router'), g => _.keyBy(g, 'isp'));
//console.log(this.gateways); //console.log(this.gateways);
this.connections = _.intersection(Object.keys(this.hosts), Object.keys(_.find(this.hosts))); this.connections = _.intersection(Object.keys(this.hosts), Object.keys(_.find(this.hosts)));
...@@ -43,29 +46,23 @@ class InventoryBuilder { ...@@ -43,29 +46,23 @@ class InventoryBuilder {
async loadUtilities(hosts) { async loadUtilities(hosts) {
const raw_utility = _.keyBy(await this.load('configurations'), 'key'); const raw_utility = _.keyBy(await this.load('configurations'), 'key');
//let route_plans = await this.load('route tables'); this.routeLists = YAML.parse(fs.readFileSync(path.join('lists', 'result.yaml'), "utf8"));
this.route_lists = YAML.parse(fs.readFileSync(path.join('lists', 'result.yaml'), "utf8"));
/*for (let plan of route_plans) {
plan.name = plan.list + "_" + plan.gateway.replace(/-/g, "_").split(".")[0]
//plan.list = JSON.parse(JSON.stringify(this.route_lists[plan.list]));
plan.gatewayAddress = hosts[plan.gateway].address;
}*/
// 所有内网网段 // 所有内网网段
this.route_lists.mycard = ["10.198.0.0/16", "10.200.0.0/15", "172.16.0.0/12"]; this.routeLists.mycard = ["10.198.0.0/16", "10.200.0.0/15", "172.16.0.0/12"];
for (const h in this.hosts) { for (const h in this.hosts) {
const host = this.hosts[h] const host = this.hosts[h]
for (const c of host.subnets.split(",")) { for (const c of host.subnets.split(",")) {
if (!c.length) { if (!c.length) {
continue; continue;
} }
this.route_lists.mycard.push(c); this.routeLists.mycard.push(c);
} }
} }
// temp user before gateways // temp user before gateways
this.route_lists.ladder_needed = raw_utility.ladderNeeded.value.split(",").map((m) => { return m.trim() }); this.routeLists.ladder_needed = raw_utility.ladderNeeded.value.split(",").map((m) => { return m.trim() });
const vars = { const vars = {
route_lists: this.route_lists, routeLists: this.routeLists,
route_list_names: Object.keys(this.route_lists), routeListNames: Object.keys(this.routeLists),
//route_plans //route_plans
}; };
for (let col in raw_utility) { for (let col in raw_utility) {
...@@ -76,9 +73,21 @@ class InventoryBuilder { ...@@ -76,9 +73,21 @@ class InventoryBuilder {
} }
host_vars(host) { host_vars(host) {
const connections = []; const connections = [];
host.dockerServices = {
version: '2.4',
services: {
bird: {
restart: "always",
image: "git-registry.mycard.moe/nanahira/docker-bird",
network_mode: "host",
cap_add: ["NET_ADMIN"],
volumes: ["./bird.conf:/etc/bird/bird.conf:ro"]
}
}
};
host.frpsNeeded = false;
const null_connection = "10000,null"; const null_connection = "10000,null";
const lanInterfaces = host.lanInterfaces.length > 0 ? host.lanInterfaces.split(",") : []; const lanInterfaces = host.lanInterfaces.length > 0 ? host.lanInterfaces.split(",") : [];
const dns = host.dns;
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];
...@@ -104,14 +113,12 @@ class InventoryBuilder { ...@@ -104,14 +113,12 @@ class InventoryBuilder {
address: host.address, address: host.address,
isCN: host.location.startsWith("CN"), isCN: host.location.startsWith("CN"),
key: host.wgPrivateKey, key: host.wgPrivateKey,
frpsNeeded: host.frpsNeeded,
frpsPort: host.frpsPort, frpsPort: host.frpsPort,
chinaMirror: host.chinaMirror,
//frpToken: host.frpToken,
//gateways: _.mapValues(this.gateways[host.name], gw => _.pick(gw, ['mark_gateway'])),
gateways: _.values(this.gateways[host.name]), gateways: _.values(this.gateways[host.name]),
connections, connections,
lanInterfaces, lanInterfaces,
dns dockerServices: host.dockerServices,
}; };
} }
...@@ -126,7 +133,7 @@ class InventoryBuilder { ...@@ -126,7 +133,7 @@ class InventoryBuilder {
const name = `mc${!outbound ? 'i' : '-'}${remote.name}`; const name = `mc${!outbound ? 'i' : '-'}${remote.name}`;
const localGatewayName = (cis ? params.lif : params.rif) || params.if; const localGatewayName = (cis ? params.lif : params.rif) || params.if;
const localGateway = localGatewayName ? this.gateways[local.name][localGatewayName] : _.find(this.gateways[local.name]); const localGateway = localGatewayName ? this.gateways[local.name][localGatewayName] : _.find(this.gateways[local.name]);
const localGatewayMark = localGatewayName ? localGateway.mark : ""; const localGatewayMark = localGatewayName ? localGateway.selectionMark : "";
const remoteGatewayName = (cis ? params.rif : params.lif) || params.if; const remoteGatewayName = (cis ? params.rif : params.lif) || params.if;
const remoteGateway = remoteGatewayName ? this.gateways[remote.name][remoteGatewayName] : _.find(this.gateways[remote.name]); const remoteGateway = remoteGatewayName ? this.gateways[remote.name][remoteGatewayName] : _.find(this.gateways[remote.name]);
//const remoteGatewayMark = remoteGatewayName ? remoteGateway.mark : undefined; //const remoteGatewayMark = remoteGatewayName ? remoteGateway.mark : undefined;
...@@ -144,6 +151,27 @@ class InventoryBuilder { ...@@ -144,6 +151,27 @@ class InventoryBuilder {
const frpType = protocol === 'wgfrp' ? (this.gatewayCompare(localGateway, remoteGateway) ? 'frps' : 'frpc') : undefined; const frpType = protocol === 'wgfrp' ? (this.gatewayCompare(localGateway, remoteGateway) ? 'frps' : 'frpc') : undefined;
if (frpType === "frps" && !local.dockerServices.services.frps) {
local.frpsNeeded = true;
local.dockerServices.services.frps = {
restart: "always",
image: "fatedier/frps:v0.34.2",
network_mode: "host",
command: "-c /frps.ini",
volumes: ["./frps.ini:/frps.ini:ro"]
}
}
if (frpType === "frpc") {
local.dockerServices.services[`frpc_${remote.name}`] = {
restart: "always",
image: "fatedier/frpc:v0.34.2",
network_mode: "host",
command: "-c /frpc.ini",
volumes: [`./frpc_${remote.name}.ini:/frpc.ini:ro`]
}
}
const mtu = Math.min(localGateway ? localGateway.mtu : 1500, remoteGateway ? remoteGateway.mtu : 1500); const mtu = Math.min(localGateway ? localGateway.mtu : 1500, remoteGateway ? remoteGateway.mtu : 1500);
//console.log(local.name, name, mtu); //console.log(local.name, name, mtu);
...@@ -153,7 +181,6 @@ class InventoryBuilder { ...@@ -153,7 +181,6 @@ class InventoryBuilder {
protocol, protocol,
params, params,
localGatewayMark, localGatewayMark,
//remoteGatewayMark,
remoteNextMark, remoteNextMark,
remoteDestMark, remoteDestMark,
remoteAddress, remoteAddress,
......
...@@ -8,7 +8,7 @@ set -e ...@@ -8,7 +8,7 @@ set -e
mkdir -p result mkdir -p result
npm run inventory npm start
cd ansible || exit cd ansible || exit
......
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