-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathstart.py
executable file
·265 lines (209 loc) · 8 KB
/
start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python3
import os
import sys
import subprocess
import configparser
from optparse import OptionParser
from logging import getLogger, DEBUG, StreamHandler, Formatter
from logging.handlers import SysLogHandler
logger = getLogger(__name__)
logger.setLevel(DEBUG)
stream = StreamHandler()
syslog = SysLogHandler(address = "/dev/log")
syslog.setFormatter(Formatter("nanate-wan: %(message)s"))
logger.addHandler(stream)
logger.addHandler(syslog)
logger.propagate = False
ipcmd = "/bin/ip"
iwcmd = "/sbin/iw"
iptables = "/sbin/iptables"
docker = "/usr/bin/docker"
def run_cmds(cmds):
for cmd in cmds :
logger.info(" ".join(list(map(str, cmd))))
subprocess.check_output(list(map(str, cmd)))
def setup_gre(config) :
wan_interface = config.get("routing", "wan_interface")
dmvpn_interface = config.get("routing", "dmvpn_interface")
dmvpn_addr = config.get("general", "dmvpn_addr")
gre_key = config.get("routing", "gre_key")
gre_ttl = config.get("routing", "gre_ttl")
logger.info("# Setup GRE Interface")
logger.info("# wan_interface : %s" % wan_interface)
logger.info("# dmvpn_interface : %s" % dmvpn_interface)
logger.info("# dmvpn_addr : %s" % dmvpn_addr)
cmds = [
[ "modprobe", "af_key" ],
[ ipcmd, "tunnel", "add", dmvpn_interface , "mode", "gre",
"key", gre_key, "ttl", gre_ttl, "dev", wan_interface ],
[ ipcmd, "addr", "flush", dmvpn_interface ],
[ ipcmd, "addr", "flush", dmvpn_interface ],
[ ipcmd, "addr", "add", "%s/32" % dmvpn_addr, "dev", dmvpn_interface ],
[ ipcmd, "link", "set", dmvpn_interface, "up" ],
]
# XXX: ip addr flush gre1 sometimes does not flush addr, so, try twice
if os.path.exists("/sys/class/net/%s" % dmvpn_interface) :
logger.error("# '%s' exists. delete and recreate." % dmvpn_interface)
cmds.insert(0, [ ipcmd, "tunnel", "del", dmvpn_interface])
run_cmds(cmds)
def setup_bridge(config) :
br_interface = config.get("portconfig", "br_interface")
logger.info("# Setup Bridge Interface")
logger.info("# br_interface : %s" % br_interface)
cmds = [
[ ipcmd, "link", "add", br_interface, "type", "bridge",
"vlan_filtering", 1 ],
[ ipcmd, "link", "set", "dev", br_interface, "up" ]
]
if os.path.exists("/sys/class/net/%s" % br_interface) :
logger.error("# '%s' exists. delete and recreate." % br_interface)
cmds.insert(0, [ ipcmd, "link", "del", "dev", br_interface ])
run_cmds(cmds)
def iptables_ver() :
return subprocess.getoutput(["%s -V" % iptables]).strip().split(" ")[1]
def setup_nflog(config) :
logger.info("# Setup NFLOG")
dmvpn_interface = config.get("routing", "dmvpn_interface")
cmds = [
[
iptables, "-A", "FORWARD",
"-i", dmvpn_interface, "-o", dmvpn_interface,
"-m", "hashlimit",
"--hashlimit-upto", "4/minute",
"--hashlimit-burst", 1,
"--hashlimit-mode", "srcip,dstip",
"--hashlimit-srcmask", 16,
"--hashlimit-name", "loglimit-0",
"-j", "NFLOG", "--nflog-group", 1,
"--nflog-size" if iptables_ver() > "v1.6.0" else "--nflog-range",
128
],
[
iptables, "-P", "FORWARD", "ACCEPT"
]
]
nflog = "iptables -nL --line-numbers | grep NFLOG"
for line in subprocess.getoutput([nflog]).split("\n") :
if not line : continue
logger.error("# NFLOG rule '%s' exists. delete it." % line)
rulenum = line.split()[0]
cmds.insert(0, [ iptables, "-D", "FORWARD", rulenum])
run_cmds(cmds)
def setup_mss_clamp(config) :
logger.info("# Setup TCP MSS Clamp")
cmds = [
[
iptables, "-A", "FORWARD", "-p", "tcp",
"--tcp-flags", "SYN,RST", "SYN",
"-j", "TCPMSS", "--set-mss", 1340
],
]
nflog = "iptables -nL --line-numbers | grep TCPMSS"
for line in subprocess.getoutput([nflog]).split("\n") :
if not line : continue
logger.error("# TCPMSS rule '%s' exists. delete it." % line)
rulenum = line.split()[0]
cmds.insert(0, [ iptables, "-D", "FORWARD", rulenum])
run_cmds(cmds)
def run_containers(option, config, configpath) :
logger.info("# Start Nante-WAN Docker Containers")
cmds = []
if option.route_server :
# run route server container
cmds += [
[ docker, "run", "-dt", "--privileged", "--net=host",
"-v", "%s:/etc/nante-wan.conf" % configpath,
"-v", "/dev/log:/dev/log",
"upaa/nante-wan-route-server"
]
]
else :
# run as edge device
cmds += [
[ docker, "run", "-dt", "--privileged", "--net=host",
"-v", "%s:/etc/nante-wan.conf" % configpath,
"-v", "/dev/log:/dev/log",
"upaa/nante-wan-routing"
],
[ docker, "run", "-dt", "--privileged", "--net=host",
"-v", "%s:/etc/nante-wan.conf" % configpath,
"-v", "/dev/log:/dev/log",
"upaa/nante-wan-portconfig"
],
]
if option.config_server :
# run config server container
cmds += [
[ docker, "run", "-dt", "--net=host",
"-v", "%s:/etc/nante-wan.conf" % configpath,
"-v", "/dev/log:/dev/log",
"-v", "%s:/var/www/html" % os.path.abspath(option.config_dir),
"upaa/nante-wan-config-server"
]
]
if option.enable_ebconfig :
# run ebconfig container
cmds += [
[ docker, "run", "-dt", "--privileged", "--net=host",
"-v", "%s:/etc/nante-wan.conf" % configpath,
"-v", "/dev/log:/dev/log",
"upaa/nante-wan-ebconfig"
]
]
dockerps = "docker ps | grep upaa/nante-wan"
for line in subprocess.getoutput([dockerps]).split("\n") :
if not line : continue
c_id = line.split()[0]
c_name = line.split()[1]
logger.error("%s is working as %s. delete and re-run" % (c_name, c_id))
cmds.insert(0, [ docker, "rm", "-f", c_id ])
run_cmds(cmds)
if __name__ == "__main__" :
desc = "usage: %prog [options] nante-wan.conf"
parser = OptionParser(desc)
parser.add_option(
"--route-server", action = "store_true", default = False,
dest = "route_server",
help = "Run as a route server (BGP RR and NHRP NHS)"
)
parser.add_option(
"--config-server", action = "store_true", default = False,
dest = "config_server",
help = "Run as a config server (HTTP server)"
)
parser.add_option(
"--config-dir", type = "string", default = False,
dest = "config_dir",
help = "config directory for config server (DocumentRoot)"
)
parser.add_option(
"--container-only", action = "store_true", default = False,
dest = "container_only",
help = "(stop and) start containers (not config network stack)"
)
parser.add_option(
"--network-only", action = "store_true", default = False,
dest = "network_only",
help = "reset network configuration (not start containers)"
)
parser.add_option(
"--enable-ebconfig", action = "store_true", default = False,
dest = "enable_ebconfig",
help = "run ebconfig container for firewalling"
)
(option, args) = parser.parse_args()
if not args :
print("Usage: %s [Nante-WAN Config]" % sys.argv[0])
sys.exit(1)
if option.config_server and not option.config_dir :
print("--config-dir is required to run as config-server")
sys.exit(1)
config = configparser.ConfigParser()
config.read(args[0])
if not option.container_only :
setup_gre(config)
setup_bridge(config)
setup_nflog(config)
setup_mss_clamp(config)
if not option.network_only :
run_containers(option, config, os.path.abspath(args[0]))