This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_mesh.py
221 lines (174 loc) · 6.04 KB
/
test_mesh.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
from mesh import *
import os
import pytest
import tempfile
import time
def test_Key():
with tempfile.TemporaryDirectory() as tmp_dir:
p = os.path.join(tmp_dir, "tmp.key")
# new the key
k = Key(None)
pk = k.pk
sk = k.sk
k.dump(p)
del k
# reload teh ky
k1 = Key(p)
assert(k1.pk == pk)
assert(k1.sk == sk)
def test_veth():
left_ns = NS("left")
right_ns = NS("right")
veth = Veth("veth", "10.1.1.1/24", "10.1.1.2/24", left_ns, right_ns)
left_ns.up()
right_ns.up()
veth.up()
assert(os.system(left_ns.gen_cmd("timeout 0.2 ping 10.1.1.2 -c 1")) == 0)
veth.down()
right_ns.down()
left_ns.down()
def test_wg():
left_key = Key(None)
right_key = Key(None)
left_ns = NS("left")
right_ns = NS("right")
veth = Veth("veth", "10.1.1.1/24", "10.1.1.2/24", left_ns, right_ns)
left_wg, right_wg = gen_wg("wg0", left_key, right_key, right_wan_ip="10.1.1.2",
link_cidr="192.168.1.8/30", port="1234", mtu=1420, left_ns=left_ns, right_ns=right_ns)
left_ns.up()
right_ns.up()
veth.up()
left_wg.up()
right_wg.up()
assert(os.system(left_ns.gen_cmd("ping 192.168.1.10 -c 1")) == 0)
assert(os.system(left_ns.gen_cmd("ping 192.168.1.10 -c 2")) == 0)
right_wg.down()
left_wg.down()
veth.down()
right_ns.down()
left_ns.down()
def test_IPTableRule():
net = ConfSet()
ns = NS("ns")
net.add(ns)
net.add(Veth("veth", "10.1.1.1/24", "10.1.1.2/24", global_ns, ns))
rule = IPTableRule("filter", "OUTPUT", "-d 10.1.1.1 -j DROP", ns)
net.up()
assert(os.system(ns.gen_cmd("timeout 0.2 ping 10.1.1.1 -c 1")) == 0)
rule.up()
assert(os.system(ns.gen_cmd("timeout 0.2 ping 10.1.1.1 -c 1")) != 0)
rule.down()
net.down()
def test_IPSet():
def is_in_ipset(ipset_name, ip):
return os.system(f"sudo ipset test {ipset_name} {ip}") == 0
s1 = IPSet("private_ip", privateip_list(), global_ns)
s1.up()
assert(is_in_ipset(s1.name, "10.1.1.1"))
s1.down()
cip = IPSet("china_ip", chinaip_list(), global_ns)
cip.up()
assert(is_in_ipset(cip.name, "114.114.114.114") == True)
assert(is_in_ipset(cip.name, "8.8.8.8") == False)
cip.down()
def test_IPSetBundle():
a = IPSet("a", ["192.168.1.0/24"], global_ns)
b = IPSet("b", ["192.168.2.0/24"], global_ns)
c = IPSet("c", ["192.168.3.0/24"], global_ns)
bundle = IPSetBundle(match=[a, b], not_match=[c])
assert(bundle.gen_iptables_condition() == "-m set --match-set a dst -m set --match-set b dst -m set ! --match-set c dst")
def test_RouteRule():
net = ConfSet()
a = NS("a")
b = NS("b")
net.add(a)
net.add(b)
net.add([
Veth("ab1", "192.168.1.1/24", "192.168.1.2/24", a, b),
Veth("ab2", "192.168.10.1/24", "192.168.10.2/24", a, b),
Route("192.168.1.2", "192.168.10.2", "1", a),
IPTableRule("filter", "INPUT", "-i ab1-right -j DROP", b),
])
a_iptable = IPTableRule(
"mangle", "OUTPUT", "-d 192.168.0.0/16 -j MARK --set-mark 1", a)
a_rule = RouteRule("1", "1", a)
net.up()
assert(os.system(a.gen_cmd("timeout 0.2 ping 192.168.1.2 -c 1")) != 0)
a_iptable.up()
a_rule.up()
assert(os.system(a.gen_cmd("timeout 0.2 ping 192.168.1.2 -c 1")) == 0)
a_iptable.down()
a_rule.down()
net.down()
def test_AnyProxy():
# somke test
pass
# ap = AnyProxy()
# ap.up()
# time.sleep(1)
# ap.down()
# log_path = os.path.join(
# os.path.dirname(os.path.realpath(__file__)),
# "bin",
# "any_proxy.log",
# )
# assert(os.path.exists(log_path))
# os.remove(log_path)
def test_FreeDNS():
f = FreeDNS("-l 127.0.0.1:5353", False, global_ns)
f.up()
time.sleep(2)
f.down()
def test_ConfSet():
net = ConfSet()
ns = NS("ns")
net.add(ns)
net.add(Route("1.1.1.1", "192.168.1.1", "main", ns)) # error: no gateway 192.168.1.1
with pytest.raises(Exception) as e:
net.up()
print(e)
# ns should be deleted by net due to the exception
assert(os.system(global_ns.gen_cmd("ip netns exec ns ip addr")) != 0)
def test_Network():
net = Network(mock_net = True)
net.add_host("a", "40.0.1.23", Key(None))
net.add_host("b", "50.0.1.23", Key(None))
net.add_host("c", "60.0.1.23", Key(None))
net.add_host("d", "70.0.1.23", Key(None))
net.connect("a", "b", "10.0.0.0/30", 50000)
net.connect("b", "c", "10.0.0.4/30", 50001)
net.connect("c", "d", "10.0.0.8/30", 50002)
net.connect("d", "a", "10.0.0.12/30", 50003)
# routing all the wan traffic to `c`
wan = IPSet("wan", ["40.0.1.23", "50.0.1.23", "60.0.1.23", "70.0.1.23"], global_ns)
pri = IPSet("pir", privateip_list(), global_ns)
bundle = IPSetBundle(match=[wan], not_match=[pri])
net.output_to_nat_gateway(bundle, "a", "c")
net.up_mock_net()
for h in ["a", "b", "c", "d"]:
net.up(h)
# case 1:
# 10.0.0.6 is c's ip
# if it is reachable, then it means Network() can automatically compute routes to all local ips in the network
assert(os.system(NS("a").gen_cmd("ping 10.0.0.6 -c 1")) == 0)
assert(os.system(NS("b").gen_cmd("ping 10.0.0.13 -c 1")) == 0)
assert(os.system(NS("c").gen_cmd("ping 10.0.0.1 -c 1")) == 0)
# case 2:
# test if the nat works
assert(os.system(NS("a").gen_cmd("ping 70.0.1.23 -c 1"))==0)
p = subprocess.run(["sh", "-c", NS("a").gen_cmd("traceroute 70.0.1.23")], stdout=subprocess.PIPE)
assert(p.returncode == 0)
print(p.stdout.decode())
assert("10.0.0" in p.stdout.decode())
# case 3:
# test tcp connections
p = subprocess.Popen(NS("d").gen_cmd("python -m SimpleHTTPServer 8088"), shell=True)
time.sleep(0.5)
assert(p.poll() == None)
# how to debug why does it not work on the CI machine?
if os.environ.get("CI") == None:
assert(os.system(NS("a").gen_cmd("timeout 3 curl 70.0.1.23:8088"))==0)
p.terminate()
for h in ["a", "b", "c", "d"]:
net.down(h)
net.down_mock_net()