Skip to content

Commit 71d65f3

Browse files
vieirabrianmay
authored andcommitted
Fixes some style issues and minor bugs
1 parent 9f238eb commit 71d65f3

File tree

17 files changed

+78
-86
lines changed

17 files changed

+78
-86
lines changed

.prospector.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ pylint:
1010
- bare-except
1111
- protected-access
1212
- no-else-return
13+
- unused-argument
14+
- method-hidden
15+
- arguments-differ
16+
- wrong-import-position
17+
- raising-bad-type
1318

1419
pep8:
1520
options:

sshuttle/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ def daemon_cleanup():
112112

113113
class MultiListener:
114114

115-
def __init__(self, type=socket.SOCK_STREAM, proto=0):
116-
self.type = type
115+
def __init__(self, kind=socket.SOCK_STREAM, proto=0):
116+
self.type = kind
117117
self.proto = proto
118118
self.v6 = None
119119
self.v4 = None
@@ -746,22 +746,22 @@ def main(listenip_v6, listenip_v4,
746746
# Last minute sanity checks.
747747
# These should never fail.
748748
# If these do fail, something is broken above.
749-
if len(subnets_v6) > 0:
749+
if subnets_v6:
750750
assert required.ipv6
751751
if redirectport_v6 == 0:
752752
raise Fatal("IPv6 subnets defined but not listening")
753753

754-
if len(nslist_v6) > 0:
754+
if nslist_v6:
755755
assert required.dns
756756
assert required.ipv6
757757
if dnsport_v6 == 0:
758758
raise Fatal("IPv6 ns servers defined but not listening")
759759

760-
if len(subnets_v4) > 0:
760+
if subnets_v4:
761761
if redirectport_v4 == 0:
762762
raise Fatal("IPv4 subnets defined but not listening")
763763

764-
if len(nslist_v4) > 0:
764+
if nslist_v4:
765765
if dnsport_v4 == 0:
766766
raise Fatal("IPv4 ns servers defined but not listening")
767767

sshuttle/cmdline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def main():
4545
if opt.listen:
4646
ipport_v6 = None
4747
ipport_v4 = None
48-
list = opt.listen.split(",")
49-
for ip in list:
48+
lst = opt.listen.split(",")
49+
for ip in lst:
5050
family, ip, port = parse_ipport(ip)
5151
if family == socket.AF_INET6:
5252
ipport_v6 = (ip, port)

sshuttle/firewall.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import socket
33
import signal
44
import sshuttle.ssyslog as ssyslog
5+
import sshuttle.sdnotify as sdnotify
56
import sys
67
import os
78
import platform
@@ -164,7 +165,7 @@ def main(method_name, syslog):
164165
_, _, ports = line.partition(" ")
165166
ports = ports.split(",")
166167
if len(ports) != 4:
167-
raise Fatal('firewall: expected 4 ports but got %n' % len(ports))
168+
raise Fatal('firewall: expected 4 ports but got %d' % len(ports))
168169
port_v6 = int(ports[0])
169170
port_v4 = int(ports[1])
170171
dnsport_v6 = int(ports[2])
@@ -203,14 +204,14 @@ def main(method_name, syslog):
203204
try:
204205
debug1('firewall manager: setting up.\n')
205206

206-
if len(subnets_v6) > 0 or len(nslist_v6) > 0:
207+
if subnets_v6 or nslist_v6:
207208
debug2('firewall manager: setting up IPv6.\n')
208209
method.setup_firewall(
209210
port_v6, dnsport_v6, nslist_v6,
210211
socket.AF_INET6, subnets_v6, udp,
211212
user)
212213

213-
if len(subnets_v4) > 0 or len(nslist_v4) > 0:
214+
if subnets_v4 or nslist_v4:
214215
debug2('firewall manager: setting up IPv4.\n')
215216
method.setup_firewall(
216217
port_v4, dnsport_v4, nslist_v4,
@@ -249,7 +250,7 @@ def main(method_name, syslog):
249250
pass
250251

251252
try:
252-
if len(subnets_v6) > 0 or len(nslist_v6) > 0:
253+
if subnets_v6 or nslist_v6:
253254
debug2('firewall manager: undoing IPv6 changes.\n')
254255
method.restore_firewall(port_v6, socket.AF_INET6, udp, user)
255256
except:
@@ -262,7 +263,7 @@ def main(method_name, syslog):
262263
pass
263264

264265
try:
265-
if len(subnets_v4) > 0 or len(nslist_v4) > 0:
266+
if subnets_v4 or nslist_v4:
266267
debug2('firewall manager: undoing IPv4 changes.\n')
267268
method.restore_firewall(port_v4, socket.AF_INET, udp, user)
268269
except:

sshuttle/hostwatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def _enqueue(op, *args):
251251

252252

253253
def _stdin_still_ok(timeout):
254-
r, w, x = select.select([sys.stdin.fileno()], [], [], timeout)
254+
r, _, _ = select.select([sys.stdin.fileno()], [], [], timeout)
255255
if r:
256256
b = os.read(sys.stdin.fileno(), 4096)
257257
if not b:

sshuttle/methods/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ def assert_features(self, features):
7474
"Feature %s not supported with method %s.\n" %
7575
(key, self.name))
7676

77-
def setup_firewall(self, port, dnsport, nslist, family, subnets, udp, user):
77+
def setup_firewall(self, port, dnsport, nslist, family, subnets, udp,
78+
user):
7879
raise NotImplementedError()
7980

8081
def restore_firewall(self, port, family, udp, user):

sshuttle/methods/ipfw.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import os
2-
import sys
3-
import struct
42
import subprocess as ssubprocess
53
from sshuttle.methods import BaseMethod
64
from sshuttle.helpers import log, debug1, debug3, \
@@ -31,9 +29,9 @@
3129
if recvmsg == "python":
3230
def recv_udp(listener, bufsize):
3331
debug3('Accept UDP python using recvmsg.\n')
34-
data, ancdata, msg_flags, srcip = listener.recvmsg(4096, socket.CMSG_SPACE(4))
32+
data, ancdata, _, srcip = \
33+
listener.recvmsg(4096, socket.CMSG_SPACE(4))
3534
dstip = None
36-
family = None
3735
for cmsg_level, cmsg_type, cmsg_data in ancdata:
3836
if cmsg_level == socket.SOL_IP and cmsg_type == IP_RECVDSTADDR:
3937
port = 53
@@ -44,13 +42,13 @@ def recv_udp(listener, bufsize):
4442
elif recvmsg == "socket_ext":
4543
def recv_udp(listener, bufsize):
4644
debug3('Accept UDP using socket_ext recvmsg.\n')
47-
srcip, data, adata, flags = listener.recvmsg((bufsize,), socket.CMSG_SPACE(4))
45+
srcip, data, adata, _ = \
46+
listener.recvmsg((bufsize,), socket.CMSG_SPACE(4))
4847
dstip = None
49-
family = None
5048
for a in adata:
5149
if a.cmsg_level == socket.SOL_IP and a.cmsg_type == IP_RECVDSTADDR:
5250
port = 53
53-
ip = socket.inet_ntop(socket.AF_INET, cmsg_data[0:4])
51+
ip = socket.inet_ntop(socket.AF_INET, a.cmsg_data[0:4])
5452
dstip = (ip, port)
5553
break
5654
return (srcip, dstip, data[0])
@@ -75,7 +73,7 @@ def ipfw_rule_exists(n):
7573
if not ('ipttl 42' in line or 'check-state' in line):
7674
log('non-sshuttle ipfw rule: %r\n' % line.strip())
7775
raise Fatal('non-sshuttle ipfw rule #%d already exists!' % n)
78-
found = True
76+
found = True
7977
rv = p.wait()
8078
if rv:
8179
raise Fatal('%r returned %d' % (argv, rv))
@@ -193,7 +191,8 @@ def setup_udp_listener(self, udp_listener):
193191
#if udp_listener.v6 is not None:
194192
# udp_listener.v6.setsockopt(SOL_IPV6, IPV6_RECVDSTADDR, 1)
195193

196-
def setup_firewall(self, port, dnsport, nslist, family, subnets, udp, user):
194+
def setup_firewall(self, port, dnsport, nslist, family, subnets, udp,
195+
user):
197196
# IPv6 not supported
198197
if family not in [socket.AF_INET]:
199198
raise Exception(
@@ -224,31 +223,22 @@ def setup_firewall(self, port, dnsport, nslist, family, subnets, udp, user):
224223

225224
ipfw_noexit('table', '124', 'flush')
226225
dnscount = 0
227-
for f, ip in [i for i in nslist if i[0] == family]:
226+
for _, ip in [i for i in nslist if i[0] == family]:
228227
ipfw('table', '124', 'add', '%s' % (ip))
229228
dnscount += 1
230229
if dnscount > 0:
231230
ipfw('add', '1', 'fwd', '127.0.0.1,%d' % dnsport,
232231
'udp',
233232
'from', 'any', 'to', 'table(124)',
234233
'not', 'ipttl', '42')
235-
"""if udp:
236-
ipfw('add', '1', 'skipto', '2',
237-
'udp',
238-
'from', 'any', 'to', 'table(125)')
239-
ipfw('add', '1', 'fwd', '127.0.0.1,%d' % port,
240-
'udp',
241-
'from', 'any', 'to', 'table(126)',
242-
'not', 'ipttl', '42')
243-
"""
244-
ipfw('add', '1', 'allow',
234+
ipfw('add', '1', 'allow',
245235
'udp',
246236
'from', 'any', 'to', 'any',
247237
'ipttl', '42')
248238

249239
if subnets:
250240
# create new subnet entries
251-
for f, swidth, sexclude, snet \
241+
for _, swidth, sexclude, snet \
252242
in sorted(subnets, key=lambda s: s[1], reverse=True):
253243
if sexclude:
254244
ipfw('table', '125', 'add', '%s/%s' % (snet, swidth))
@@ -265,4 +255,3 @@ def restore_firewall(self, port, family, udp, user):
265255
ipfw_noexit('table', '124', 'flush')
266256
ipfw_noexit('table', '125', 'flush')
267257
ipfw_noexit('table', '126', 'flush')
268-

sshuttle/methods/nat.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class Method(BaseMethod):
1212
# the multiple copies shouldn't have overlapping subnets, or only the most-
1313
# recently-started one will win (because we use "-I OUTPUT 1" instead of
1414
# "-A OUTPUT").
15-
def setup_firewall(self, port, dnsport, nslist, family, subnets, udp, user):
15+
def setup_firewall(self, port, dnsport, nslist, family, subnets, udp,
16+
user):
1617
# only ipv4 supported with NAT
1718
if family != socket.AF_INET:
1819
raise Exception(
@@ -50,7 +51,7 @@ def _ipm(*args):
5051
_ipt('-I', 'PREROUTING', '1', *args)
5152

5253
# create new subnet entries.
53-
for f, swidth, sexclude, snet, fport, lport \
54+
for _, swidth, sexclude, snet, fport, lport \
5455
in sorted(subnets, key=subnet_weight, reverse=True):
5556
tcp_ports = ('-p', 'tcp')
5657
if fport:
@@ -65,7 +66,7 @@ def _ipm(*args):
6566
'--dest', '%s/%s' % (snet, swidth),
6667
*(tcp_ports + ('--to-ports', str(port))))
6768

68-
for f, ip in [i for i in nslist if i[0] == family]:
69+
for _, ip in [i for i in nslist if i[0] == family]:
6970
_ipt_ttl('-A', chain, '-j', 'REDIRECT',
7071
'--dest', '%s/32' % ip,
7172
'-p', 'udp',
@@ -97,8 +98,8 @@ def _ipm(*args):
9798
# basic cleanup/setup of chains
9899
if ipt_chain_exists(family, table, chain):
99100
if user is not None:
100-
nonfatal(_ipm, '-D', 'OUTPUT', '-m', 'owner', '--uid-owner', str(user),
101-
'-j', 'MARK', '--set-mark', str(port))
101+
nonfatal(_ipm, '-D', 'OUTPUT', '-m', 'owner', '--uid-owner',
102+
str(user), '-j', 'MARK', '--set-mark', str(port))
102103
args = '-m', 'mark', '--mark', str(port), '-j', chain
103104
else:
104105
args = '-j', chain

sshuttle/methods/pf.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,21 @@ def add_anchors(self, anchor, status=None):
115115
if ('\nanchor "%s"' % anchor).encode('ASCII') not in status:
116116
self._add_anchor_rule(self.PF_PASS, anchor.encode('ASCII'))
117117

118-
def _add_anchor_rule(self, type, name, pr=None):
118+
def _add_anchor_rule(self, kind, name, pr=None):
119119
if pr is None:
120120
pr = self.pfioc_rule()
121121

122122
memmove(addressof(pr) + self.ANCHOR_CALL_OFFSET, name,
123-
min(self.MAXPATHLEN, len(name))) # anchor_call = name
123+
min(self.MAXPATHLEN, len(name))) # anchor_call = name
124124
memmove(addressof(pr) + self.RULE_ACTION_OFFSET,
125-
struct.pack('I', type), 4) # rule.action = type
125+
struct.pack('I', kind), 4) # rule.action = kind
126126

127127
memmove(addressof(pr) + self.ACTION_OFFSET, struct.pack(
128-
'I', self.PF_CHANGE_GET_TICKET), 4) # action = PF_CHANGE_GET_TICKET
128+
'I', self.PF_CHANGE_GET_TICKET), 4) # action = PF_CHANGE_GET_TICKET
129129
ioctl(pf_get_dev(), pf.DIOCCHANGERULE, pr)
130130

131131
memmove(addressof(pr) + self.ACTION_OFFSET, struct.pack(
132-
'I', self.PF_CHANGE_ADD_TAIL), 4) # action = PF_CHANGE_ADD_TAIL
132+
'I', self.PF_CHANGE_ADD_TAIL), 4) # action = PF_CHANGE_ADD_TAIL
133133
ioctl(pf_get_dev(), pf.DIOCCHANGERULE, pr)
134134

135135
@staticmethod
@@ -176,9 +176,6 @@ class pfioc_natlook(Structure):
176176
freebsd.pfioc_natlook = pfioc_natlook
177177
return freebsd
178178

179-
def __init__(self):
180-
super(FreeBsd, self).__init__()
181-
182179
def enable(self):
183180
returncode = ssubprocess.call(['kldload', 'pf'])
184181
super(FreeBsd, self).enable()
@@ -197,14 +194,14 @@ def add_anchors(self, anchor):
197194
self._add_anchor_rule(self.PF_RDR, anchor.encode('ASCII'))
198195
super(FreeBsd, self).add_anchors(anchor, status=status)
199196

200-
def _add_anchor_rule(self, type, name):
201-
pr = self.pfioc_rule()
197+
def _add_anchor_rule(self, kind, name, pr=None):
198+
pr = pr or self.pfioc_rule()
202199
ppa = self.pfioc_pooladdr()
203200

204201
ioctl(pf_get_dev(), self.DIOCBEGINADDRS, ppa)
205202
# pool ticket
206203
memmove(addressof(pr) + self.POOL_TICKET_OFFSET, ppa[4:8], 4)
207-
super(FreeBsd, self)._add_anchor_rule(type, name, pr=pr)
204+
super(FreeBsd, self)._add_anchor_rule(kind, name, pr=pr)
208205

209206
def add_rules(self, anchor, includes, port, dnsport, nslist, family):
210207
inet_version = self._inet_version(family)
@@ -224,7 +221,7 @@ def add_rules(self, anchor, includes, port, dnsport, nslist, family):
224221
for exclude, subnet in includes
225222
]
226223

227-
if len(nslist) > 0:
224+
if nslist:
228225
tables.append(
229226
b'table <dns_servers> {%s}' %
230227
b','.join([ns[1].encode("ASCII") for ns in nslist]))
@@ -294,7 +291,7 @@ def add_rules(self, anchor, includes, port, dnsport, nslist, family):
294291
for exclude, subnet in includes
295292
]
296293

297-
if len(nslist) > 0:
294+
if nslist:
298295
tables.append(
299296
b'table <dns_servers> {%s}' %
300297
b','.join([ns[1].encode("ASCII") for ns in nslist]))
@@ -440,24 +437,21 @@ def get_tcp_dstip(self, sock):
440437

441438
return sock.getsockname()
442439

443-
def setup_firewall(self, port, dnsport, nslist, family, subnets, udp, user):
444-
tables = []
445-
translating_rules = []
446-
filtering_rules = []
447-
440+
def setup_firewall(self, port, dnsport, nslist, family, subnets, udp,
441+
user):
448442
if family not in [socket.AF_INET, socket.AF_INET6]:
449443
raise Exception(
450444
'Address family "%s" unsupported by pf method_name'
451445
% family_to_string(family))
452446
if udp:
453447
raise Exception("UDP not supported by pf method_name")
454448

455-
if len(subnets) > 0:
449+
if subnets:
456450
includes = []
457451
# If a given subnet is both included and excluded, list the
458452
# exclusion first; the table will ignore the second, opposite
459453
# definition
460-
for f, swidth, sexclude, snet, fport, lport \
454+
for _, swidth, sexclude, snet, fport, lport \
461455
in sorted(subnets, key=subnet_weight, reverse=True):
462456
includes.append((sexclude, b"%s/%d%s" % (
463457
snet.encode("ASCII"),

0 commit comments

Comments
 (0)