-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery refactored master branch #1
base: master
Are you sure you want to change the base?
Conversation
b0fe3d7
to
1ae5799
Compare
1ae5799
to
f7ca7d9
Compare
fdset = [sock, remote] | ||
self.marked = False | ||
self.sent = False | ||
self.reqtype = None | ||
self.cbuf = None | ||
fdset = [sock, remote] | ||
self.marked = False | ||
self.sent = False | ||
self.reqtype = None | ||
self.cbuf = None | ||
|
||
# Basic Loop for contents passing. | ||
while True: | ||
r, w, e = select.select(fdset, [], []) | ||
if sock in r: | ||
buf = sock.recv(4096) | ||
if not self.marked: | ||
# 1st packet marking | ||
self.marked = True | ||
if len(buf) == 0: | ||
break | ||
if buf[0] == '\x16': | ||
# TLS start | ||
p = min(len(buf) / 5, 100) # Tricky Code. split by specific size or split by some... | ||
self.sent = True | ||
remote.send(buf[:p]) | ||
if remote.send(buf[p:]) <= 0: break | ||
elif buf[0].lower() >= ord('a') or buf[0].lower() <= ord('z'): | ||
# Generic HTTP? | ||
lp = buf.lower().find("host:") | ||
if lp != -1: | ||
self.sent = True | ||
remote.send(buf[:lp+4]) | ||
if remote.send(buf[lp+4:]) <= 0: break | ||
if not self.sent: | ||
self.sent = True | ||
if remote.send(buf) <= 0: break | ||
else: | ||
if remote.send(buf) <= 0: break | ||
|
||
if remote in r: | ||
buf = remote.recv(4096) | ||
# no filtering | ||
while True: | ||
r, w, e = select.select(fdset, [], []) | ||
if sock in r: | ||
buf = sock.recv(4096) | ||
if not self.marked: | ||
# 1st packet marking | ||
self.marked = True | ||
if len(buf) == 0: | ||
break | ||
if buf[0] == '\x16': | ||
# TLS start | ||
p = min(len(buf) / 5, 100) # Tricky Code. split by specific size or split by some... | ||
self.sent = True | ||
remote.send(buf[:p]) | ||
if remote.send(buf[p:]) <= 0: break | ||
elif buf[0].lower() >= ord('a') or buf[0].lower() <= ord('z'): | ||
# Generic HTTP? | ||
lp = buf.lower().find("host:") | ||
if lp != -1: | ||
self.sent = True | ||
remote.send(buf[:lp+4]) | ||
if remote.send(buf[lp+4:]) <= 0: break | ||
if not self.sent: | ||
self.sent = True | ||
if remote.send(buf) <= 0: break | ||
elif remote.send(buf) <= 0: break | ||
if remote in r: | ||
buf = remote.recv(4096) | ||
# no filtering | ||
|
||
if not buf: break | ||
if sock.send(buf) <= 0: break | ||
if not buf: break | ||
if sock.send(buf) <= 0: break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Socks5Server.handle_tcp
refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif
)
addr = "" | ||
addr = "" | ||
try: | ||
#>> 'socks connection from ', self.client_address | ||
sock = self.connection | ||
# 1. Version | ||
sock.recv(262) | ||
sock.send(b"\x05\x00"); | ||
# 2. Request | ||
data = self.rfile.read(4) | ||
print(">> LEN:",len(data)) | ||
hexdump.hexdump(data) | ||
if len(data) < 4: | ||
return | ||
mode = data[1] | ||
addrtype = data[3] | ||
print("> mode:", mode, " / addrtype:", addrtype) | ||
if addrtype == 1: # IPv4 | ||
addr = socket.inet_ntop(socket.AF_INET, self.rfile.read(4)) | ||
elif addrtype == 3: # Domain name | ||
addr = self.rfile.read(ord(sock.recv(1)[0])) | ||
elif addrtype == 4: # IPv6 | ||
addr = socket.inet_ntop(socket.AF_INET6, self.rfile.read(16)) | ||
print("-> IPv6:", addr) | ||
port = struct.unpack('>H', self.rfile.read(2)) | ||
reply = b"\x05\x00\x00\x01" | ||
try: | ||
#>> 'socks connection from ', self.client_address | ||
sock = self.connection | ||
# 1. Version | ||
sock.recv(262) | ||
sock.send(b"\x05\x00"); | ||
# 2. Request | ||
data = self.rfile.read(4) | ||
print(">> LEN:",len(data)) | ||
hexdump.hexdump(data) | ||
if len(data) < 4: | ||
return | ||
mode = data[1] | ||
addrtype = data[3] | ||
print("> mode:", data[1]," / addrtype:", data[3]) | ||
if addrtype == 1: # IPv4 | ||
addr = socket.inet_ntop(socket.AF_INET, self.rfile.read(4)) | ||
elif addrtype == 3: # Domain name | ||
addr = self.rfile.read(ord(sock.recv(1)[0])) | ||
elif addrtype == 4: # IPv6 | ||
addr = socket.inet_ntop(socket.AF_INET6, self.rfile.read(16)) | ||
print("-> IPv6:", addr) | ||
port = struct.unpack('>H', self.rfile.read(2)) | ||
reply = b"\x05\x00\x00\x01" | ||
try: | ||
if mode == 1: # 1. Tcp connect | ||
self.addr = addr | ||
self.port = port[0] | ||
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
remote.connect((addr, port[0])) | ||
print(">> 'Tcp connect to'", addr,":", port[0]) | ||
else: | ||
reply = b"\x05\x07\x00\x01" # Command not supported | ||
local = remote.getsockname() | ||
reply += socket.inet_aton(local[0]) + struct.pack(">H", local[1]) | ||
except socket.error: | ||
# Connection refused | ||
reply = '\x05\x05\x00\x01\x00\x00\x00\x00\x00\x00' | ||
sock.send(reply) | ||
# 3. Transfering | ||
if reply[1] == '\x00': # Success | ||
if mode == 1: # 1. Tcp connect | ||
self.handle_tcp(sock, remote) | ||
if mode == 1: # 1. Tcp connect | ||
self.addr = addr | ||
self.port = port[0] | ||
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
remote.connect((addr, port[0])) | ||
print(">> 'Tcp connect to'", addr,":", port[0]) | ||
else: | ||
reply = b"\x05\x07\x00\x01" # Command not supported | ||
local = remote.getsockname() | ||
reply += socket.inet_aton(local[0]) + struct.pack(">H", local[1]) | ||
except socket.error: | ||
pass | ||
# Connection refused | ||
reply = '\x05\x05\x00\x01\x00\x00\x00\x00\x00\x00' | ||
sock.send(reply) | ||
# 3. Transfering | ||
if reply[1] == '\x00' and mode == 1: # 1. Tcp connect | ||
self.handle_tcp(sock, remote) | ||
except socket.error: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Socks5Server.handle
refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs
) - Use previously assigned local variable (
use-assigned-variable
)
This removes the following comments ( why? ):
# Success
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.95%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
Branch
master
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
master
branch, then run:Help us improve this pull request!