-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestVPN.py
172 lines (130 loc) · 5.01 KB
/
testVPN.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
#!/usr/bin/python
""" Script that tests RTT in a mininet deployment with IPSEC tunnels
/etc directory content must be copied to host's local /etc directory before executing the script.
Usage: ./test_VPN.py [Options]
Available options:
* '-v', '--verbose': provide higher detail for logs
* '-o', '--output': set output path
* '-t', '--time': Duration of test (in minutes)
* '-H', '--n_hosts': set number of hosts
"""
__author__ = "Javier Ramos & David Muelas ({dav.muelas,javier.ramos}@uam.es)"
__version__ = "$2.0$"
__date__ = "$Date: 2017/05/10$"
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel
from mininet.link import TCLink
from mininet.node import Host
from mininet.cli import CLI
from functools import partial
import time
import sys
import getopt
H = 2
TIME=2
class SingleSwitchTopo(Topo):
"Single switch connected to n hosts."
def build(self, n=2):
switch = self.addSwitch('s1')
switchOut= self.addSwitch('s2')
privateDirs=[( '/var/log', '/tmp/%(name)s/var/log' ),( '/var/run', '/tmp/%(name)s/var/run' ),( '/etc', '/tmp/%(name)s/etc' )]
for h in range(n):
host = self.addHost('h%s' % (h + 1),privateDirs=privateDirs)
linkopts = dict(bw=20)
self.addLink(host, switch,**linkopts)
endHost=self.addHost('endHost',privateDirs=privateDirs)
gateway=self.addHost('gateway',privateDirs=privateDirs)
linkopts = dict(bw=1000)
self.addLink(switch, gateway,**linkopts)
self.addLink(gateway, switchOut,**linkopts)
self.addLink(endHost, switchOut,**linkopts)
def testConnection(net,output_path,n_minutes,n=2):
for t in range (n_minutes):
time.sleep(60)
for h in range(n):
hn='h%s' % (h + 1)
host = net.get(hn)
host.cmd('ping -D -c 1 10.0.0.2 >> %s/%s' % (output_path,hn+'.results'))
def configureHosts(net,n=2):
confGwBase='config setup\\nconn %%default\\n\\tikelifetime=60m\\n\\tkeylife=20m\\n\\trekeymargin=3m\\n\\tkeyingtries=1\\n\\tkeyexchange=ikev2\\n\\tauthby=secret\\n\\nconn rw\\n\\tleft=%s\\n\\tleftsubnet=10.0.0.0/8\\n\\tleftfirewall=yes\\n\\tright=%%any\\n\\tauto=add\\n'
confHost='config setup\\nconn %%default\\n\\tikelifetime=60m\\n\\tkeylife=20m\\n\\trekeymargin=3m\\n\\tkeyingtries=1\\n\\tkeyexchange=ikev2\\n\\tauthby=secret\\nconn host-host\\n\\tleft=%s\\n\\tleftfirewall=yes\\n\\tright=%s\\n\\trightsubnet=10.0.0.0/8\\n\\tauto=add\\n'
subnetgw1='192.168.1.%s/24' % (n+1)
ipgw1='192.168.1.%s' % (n+1)
subnetgw2='10.0.0.1/8'
pskLine='%s : PSK %s\\n'
gateway = net.get('gateway')
for h in range(n):
hn='h%s' % (h + 1)
host = net.get(hn)
res=host.cmd('ipsec stop')
subnet='192.168.1.%s/24' % (h + 1)
ip='192.168.1.%s' % (h + 1)
host.setIP(subnet)
# Create isec.conf file for each host
hostConf=confHost % (ip,ipgw1)
host.cmd('echo -e "%s" > /etc/ipsec.conf' % (hostConf))
psk=host.cmd('openssl rand -base64 48')
host.cmd('echo -e "%s" > /etc/ipsec.secrets' % (pskLine % (ip,psk)))
res=host.cmd('ipsec start')
print res
if (h==0):
gateway.cmd('echo -e "%s" > /etc/ipsec.secrets' % (pskLine % (ip,psk)))
else:
gateway.cmd('echo -e "%s" >> /etc/ipsec.secrets' % (pskLine % (ip,psk)))
intf = gateway.intf('gateway-eth0')
intf.setIP(subnetgw1)
intf = gateway.intf('gateway-eth1')
intf.setIP(subnetgw2)
endHost = net.get('endHost')
intf = endHost.intf('endHost-eth0')
intf.setIP('10.0.0.2/8')
#create ipsec.conf file for gw
confGw=confGwBase % (ipgw1)
gateway.cmd('echo -e "%s" > /etc/ipsec.conf' % (confGw))
gateway.cmd('sysctl -w net.ipv4.ip_forward=1')
gateway.cmd('iptables -t nat -A POSTROUTING -o gateway-eth1 -j MASQUERADE')
gateway.cmd('iptables -t nat -A POSTROUTING -o gateway-eth0 -j MASQUERADE')
gateway.cmd('ipsec stop')
gateway.cmd('ipsec start')
time.sleep(2)
print 'Setting Tunnels'
for h in range(n):
hn='h%s' % (h + 1)
print 'Host:%s' %hn
host = net.get(hn)
res=host.cmd('ipsec up host-host')
print res
def simpleTest(output_path,n_minutes,n=1):
"Create and test a simple network"
topo =SingleSwitchTopo(n)
privateDirs = [( '/var/log', '/tmp/%(name)s/var/log' ),( '/var/run', '/tmp/%(name)s/var/run' ),( '/etc', '/tmp/%(name)s/etc' )]
host = partial( Host, privateDirs=privateDirs)
net = Mininet(topo,link=TCLink)
net.start()
print "Dumping host connections"
dumpNodeConnections(net.hosts)
print "Testing network connectivity"
configureHosts(net,n)
net.pingAll()
testConnection(net,output_path,n_minutes,n)
net.stop()
if __name__ == '__main__':
setLogLevel('error')
options, remainder = getopt.getopt(sys.argv[1:], 'vo:H:C:t:', ['verbose','output','n_hosts','time'])
verbose=False
udp_op=''
output_path=''
H = 2
S = 1
for opt, arg in options:
if opt in ('-v', '--verbose'):
verbose=True
if opt in ('-o', '--output'):
output_path=arg+'/'
if opt in ('-H', '--n_hosts'):
H = int(arg)
if opt in ('-t', '--time'):
TIME=int(arg)
simpleTest(output_path,TIME,n=H)