Skip to content

Commit 4ee0516

Browse files
committed
Support port based exclude
1 parent 9ce2fa0 commit 4ee0516

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ def check(self):
151151

152152
def start(self):
153153
self.pfile.write('ROUTES\n')
154-
for (ip,width) in self.subnets_include+self.auto_nets:
155-
self.pfile.write('%d,0,%s\n' % (width, ip))
156-
for (ip,width) in self.subnets_exclude:
157-
self.pfile.write('%d,1,%s\n' % (width, ip))
154+
for (ip,width,port) in self.subnets_include+self.auto_nets:
155+
self.pfile.write('%d,%d,0,%s\n' % (width, port, ip))
156+
for (ip,width,port) in self.subnets_exclude:
157+
self.pfile.write('%d,%d,1,%s\n' % (width, port, ip))
158158
self.pfile.write('GO\n')
159159
self.pfile.flush()
160160
line = self.pfile.readline()

firewall.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# vim: set tabstop=4 expandtab :
2+
13
import re, errno, socket, select, signal, struct
24
import compat.ssubprocess as ssubprocess
35
import helpers, ssyslog
@@ -92,17 +94,24 @@ def do_iptables(port, dnsport, subnets):
9294
# to least-specific, and at any given level of specificity, we want
9395
# excludes to come first. That's why the columns are in such a non-
9496
# intuitive order.
95-
for swidth,sexclude,snet in sorted(subnets, reverse=True):
97+
for swidth,sport,sexclude,snet in sorted(subnets, reverse=True):
9698
if sexclude:
97-
ipt('-A', chain, '-j', 'RETURN',
98-
'--dest', '%s/%s' % (snet,swidth),
99-
'-p', 'tcp')
99+
if sport > 0:
100+
ipt('-A', chain, '-j', 'RETURN',
101+
'--dest', '%s/%s' % (snet,swidth),
102+
'-m', 'tcp',
103+
'--dport', '%d' % sport,
104+
'-p', 'tcp')
105+
else:
106+
ipt('-A', chain, '-j', 'RETURN',
107+
'--dest', '%s/%s' % (snet,swidth),
108+
'-p', 'tcp')
100109
else:
101110
ipt_ttl('-A', chain, '-j', 'REDIRECT',
102111
'--dest', '%s/%s' % (snet,swidth),
103112
'-p', 'tcp',
104113
'--to-ports', str(port))
105-
114+
106115
if dnsport:
107116
nslist = resolvconf_nameservers()
108117
for ip in nslist:
@@ -508,10 +517,10 @@ def main(port, dnsport, syslog):
508517
elif line == 'GO\n':
509518
break
510519
try:
511-
(width,exclude,ip) = line.strip().split(',', 2)
520+
(width,dport,exclude,ip) = line.strip().split(',', 3)
512521
except:
513522
raise Fatal('firewall: expected route or GO but got %r' % line)
514-
subnets.append((int(width), bool(int(exclude)), ip))
523+
subnets.append((int(width), int(dport), bool(int(exclude)), ip))
515524

516525
try:
517526
if line:

main.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# vim: set tabstop=4 expandtab :
12
import sys, os, re
23
import helpers, options, client, server, firewall, hostwatch
34
import compat.ssubprocess as ssubprocess
@@ -9,11 +10,16 @@
910
def parse_subnets(subnets_str):
1011
subnets = []
1112
for s in subnets_str:
12-
m = re.match(r'(\d+)(?:\.(\d+)\.(\d+)\.(\d+))?(?:/(\d+))?$', s)
13+
m = re.match(r'(\d+)?(?:\.(\d+)\.(\d+)\.(\d+))?(?:/(\d+))?(:\d+)?$', s)
1314
if not m:
1415
raise Fatal('%r is not a valid IP subnet format' % s)
15-
(a,b,c,d,width) = m.groups()
16+
(a,b,c,d,width,port) = m.groups()
1617
(a,b,c,d) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0))
18+
if port == None:
19+
port = 0
20+
else:
21+
port = int(re.sub('^:','',port))
22+
1723
if width == None:
1824
width = 32
1925
else:
@@ -22,7 +28,10 @@ def parse_subnets(subnets_str):
2228
raise Fatal('%d.%d.%d.%d has numbers > 255' % (a,b,c,d))
2329
if width > 32:
2430
raise Fatal('*/%d is greater than the maximum of 32' % width)
25-
subnets.append(('%d.%d.%d.%d' % (a,b,c,d), width))
31+
if port > 65535:
32+
raise Fatal('*:%d is greater than the maximum of 65535' % port)
33+
subnets.append(('%d.%d.%d.%d' % (a,b,c,d), width, port))
34+
2635
return subnets
2736

2837

0 commit comments

Comments
 (0)