-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxc-nat.rb
70 lines (61 loc) · 1.42 KB
/
lxc-nat.rb
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
#!/usr/bin/env ruby
#
## lxc-nat.rb
#
# Simple ruby script to create port-forwarding rules based on a static table.
#
## Configuration
#
# Create /etc/lxc/lxc-nat.conf with rules such as the following:
#
# src_ip:port -> lxc_container:port
# 10.0.0.1:80 -> www:80
# 10.0.0.1:3306 -> mysql_server:3306
#
## TODO
#
# * better cli arg handling
# * daemon/event mode
noop = false
flush = false
ARGV.each do |arg|
if arg == '-f'
flush = true
end
if arg == '-n'
noop = true
end
end
forwards = []
containers = {}
lxc_output = `lxc-ls --fancy | grep RUN`.split("\n")
lxc_output.each do |line|
cols = line.split(/\s+/)
containers[cols[0]] = cols[2]
end
File.open('/etc/lxc/lxc-nat.conf').each do |line|
line.chomp!
if line =~ /([0-9\.]+):(\d+)\s+->\s+(.*):(\d+)/
src_ip = $1
src_port = $2
lxc_name = $3
lxc_port = $4
if containers.include?(lxc_name)
if noop
puts "#{src_ip}:#{src_port} -> #{containers[lxc_name]}:#{lxc_port}"
else
forwards << "iptables -t nat -A lxc-nat -d #{src_ip} -p tcp --dport #{src_port} -j DNAT --to #{containers[lxc_name]}:#{lxc_port}"
end
end
end
end
system('iptables -t nat -F lxc-nat')
system('iptables -t nat -D PREROUTING -j lxc-nat')
system('iptables -t nat -X lxc-nat')
unless flush or noop
system('iptables -t nat -N lxc-nat')
system('iptables -t nat -A PREROUTING -j lxc-nat')
forwards.each do |f|
system(f)
end
end