-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.txt
58 lines (48 loc) · 1.85 KB
/
file.txt
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
root@test-virtual-machine:~# cat API.rb
require 'sinatra'
def random_int(min, max)
rand(max - min) + min
end
def safeport()
portcheck = true
while portcheck == true
port = random_int(10000, 60000)
portcheck = system("iptables -L -vt nat | grep dpt | awk '{print $11}' | awk -F ':' '{print $2}' | grep #{port}")
end
return port
end
def getIP(deviceid)
ip = `lxc-info -n #{deviceid} -iH`
return ip
end
get '/enrol/:deviceid' do
deviceid = params['deviceid']
system( "lxc-create --template ubuntu --name #{params['deviceid']}" )
system( "lxc-start --name #{params['deviceid']} -d" )
sleep 30
Cip = getIP(deviceid).strip
Cport = safeport()
system( "iptables -t nat -A PREROUTING -p tcp -i eth0 --dport #{Cport} -j DNAT --to-destination #{Cip}:22")
system ("/etc/init.d/iptables-persistent save")
system (` echo "# Autostart" >> /var/lib/lxc/#{params['deviceid']}/config `)
system (` echo "lxc.start.auto = 1" >> /var/lib/lxc/#{params['deviceid']}/config `)
system (` echo "lxc.start.delay = 5" >> /var/lib/lxc/#{params['deviceid']}/config `)
"Box " + params['deviceid'] + " created and started. IP: " + Cip.to_s + " Port: " + Cport.to_s
end
get '/start/:deviceid' do
system( "lxc-start --name #{params['deviceid']}" )
"Box "+ params['deviceid'] + " started."
end
get '/stop/:deviceid' do
system( "lxc-stop --name #{params['deviceid']}" )
"Box "+ params['deviceid'] + " stopped."
end
get '/destroy/:deviceid/:port' do
getRule = `iptables -L -vt nat --line-numbers | grep #{params['port']} | awk '{print $1}'`
Rule = getRule.strip
system("iptables -t nat -D PREROUTING #{Rule}")
system ("/etc/init.d/iptables-persistent save")
system( "lxc-stop --name #{params['deviceid']}" )
system( "lxc-destroy --name #{params['deviceid']}" )
"Box " + params['deviceid'] + " destroyed. Iptables for " + params['port'] + " has been removed."
end