-
Notifications
You must be signed in to change notification settings - Fork 3
Sourcery refactored master branch #1
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
Open
sourcery-ai
wants to merge
1
commit into
master
Choose a base branch
from
sourcery/master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,94 +25,91 @@ | |
| class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): pass | ||
| class Socks5Server(SocketServer.StreamRequestHandler): | ||
| def handle_tcp(self, sock, remote): | ||
| 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 | ||
| def handle(self): | ||
| 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 | ||
|
Comment on lines
-70
to
+112
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| #print 'socket error' | ||
| def main(): | ||
| server = ThreadingTCPServer(('', 18080), Socks5Server) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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_tcprefactored with the following changes:merge-else-if-into-elif)