Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Sep 4, 2021

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:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from ziozzang September 4, 2021 16:40
@sourcery-ai sourcery-ai bot force-pushed the sourcery/master branch 4 times, most recently from b0fe3d7 to 1ae5799 Compare September 4, 2021 16:41
Comment on lines -28 to +66
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
Copy link
Author

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:

Comment on lines -70 to +112
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
Copy link
Author

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:

This removes the following comments ( why? ):

# Success

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Sep 4, 2021

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.95%.

Quality metrics Before After Change
Complexity 29.72 😞 27.38 😞 -2.34 👍
Method Length 99.80 🙂 99.00 🙂 -0.80 👍
Working memory 11.64 😞 11.62 😞 -0.02 👍
Quality 38.09% 😞 39.04% 😞 0.95% 👍
Other metrics Before After Change
Lines 103 100 -3
Changed files Quality Before Quality After Quality Change
socks5proxy.py 38.09% 😞 39.04% 😞 0.95% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
socks5proxy.py Socks5Server.handle_tcp 50 ⛔ 209 ⛔ 15 😞 19.70% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
socks5proxy.py Socks5Server.handle 10 🙂 231 ⛔ 10 😞 46.61% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants