|
@ -20,10 +20,6 @@ |
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
|
# SOFTWARE. |
|
|
# SOFTWARE. |
|
|
|
|
|
|
|
|
SERVER = '127.0.0.1' |
|
|
|
|
|
REMOTE_PORT = 8388 |
|
|
|
|
|
PORT = 1080 |
|
|
|
|
|
KEY = "barfoo!" |
|
|
|
|
|
|
|
|
|
|
|
import socket |
|
|
import socket |
|
|
import select |
|
|
import select |
|
@ -32,6 +28,8 @@ import struct |
|
|
import string |
|
|
import string |
|
|
import hashlib |
|
|
import hashlib |
|
|
import sys |
|
|
import sys |
|
|
|
|
|
import json |
|
|
|
|
|
import logging |
|
|
|
|
|
|
|
|
def get_table(key): |
|
|
def get_table(key): |
|
|
m = hashlib.md5() |
|
|
m = hashlib.md5() |
|
@ -74,14 +72,13 @@ class Socks5Server(SocketServer.StreamRequestHandler): |
|
|
|
|
|
|
|
|
def handle(self): |
|
|
def handle(self): |
|
|
try: |
|
|
try: |
|
|
print 'socks connection from ', self.client_address |
|
|
|
|
|
sock = self.connection |
|
|
sock = self.connection |
|
|
sock.recv(262) |
|
|
sock.recv(262) |
|
|
sock.send("\x05\x00") |
|
|
sock.send("\x05\x00") |
|
|
data = self.rfile.read(4) |
|
|
data = self.rfile.read(4) |
|
|
mode = ord(data[1]) |
|
|
mode = ord(data[1]) |
|
|
if mode != 1: |
|
|
if mode != 1: |
|
|
print 'mode != 1' |
|
|
logging.warn('mode != 1') |
|
|
return |
|
|
return |
|
|
addrtype = ord(data[3]) |
|
|
addrtype = ord(data[3]) |
|
|
addr_to_send = data[3] |
|
|
addr_to_send = data[3] |
|
@ -94,42 +91,46 @@ class Socks5Server(SocketServer.StreamRequestHandler): |
|
|
addr = self.rfile.read(ord(addr_len)) |
|
|
addr = self.rfile.read(ord(addr_len)) |
|
|
addr_to_send += addr_len + addr |
|
|
addr_to_send += addr_len + addr |
|
|
else: |
|
|
else: |
|
|
print 'not support' |
|
|
logging.warn('addr_type not support') |
|
|
# not support |
|
|
# not support |
|
|
return |
|
|
return |
|
|
addr_port = self.rfile.read(2) |
|
|
addr_port = self.rfile.read(2) |
|
|
addr_to_send += addr_port |
|
|
addr_to_send += addr_port |
|
|
port = struct.unpack('>H', addr_port) |
|
|
port = struct.unpack('>H', addr_port) |
|
|
try: |
|
|
try: |
|
|
if mode == 1: |
|
|
reply = "\x05\x00\x00\x01" |
|
|
reply = "\x05\x00\x00\x01" |
|
|
reply += socket.inet_aton('0.0.0.0') + struct.pack(">H", 2222) |
|
|
reply += socket.inet_aton('0.0.0.0') + struct.pack(">H", 2222) |
|
|
sock.send(reply) |
|
|
sock.send(reply) |
|
|
# reply immediately |
|
|
# reply immediately |
|
|
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
|
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
|
remote.connect((SERVER, REMOTE_PORT)) |
|
|
remote.connect((SERVER, REMOTE_PORT)) |
|
|
self.send_encrypt(remote, addr_to_send) |
|
|
self.send_encrypt(remote, addr_to_send) |
|
|
logging.info('connecting %s:%d' % (addr, port[0])) |
|
|
print 'Tcp connect to', addr, port[0] |
|
|
|
|
|
else: |
|
|
|
|
|
print 'command not supported' |
|
|
|
|
|
return |
|
|
|
|
|
except socket.error as e: |
|
|
except socket.error as e: |
|
|
print 'socket error ' + str(e) |
|
|
logging.warn('socket error ' + str(e)) |
|
|
return |
|
|
return |
|
|
self.handle_tcp(sock, remote) |
|
|
self.handle_tcp(sock, remote) |
|
|
except socket.error as e: |
|
|
except socket.error as e: |
|
|
print 'socket error ' + str(e) |
|
|
logging.warn('socket error ' + str(e)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(): |
|
|
if __name__ == '__main__': |
|
|
|
|
|
with open('config.json', 'rb') as f: |
|
|
|
|
|
config = json.load(f) |
|
|
|
|
|
SERVER = config['server'] |
|
|
|
|
|
REMOTE_PORT = config['server_port'] |
|
|
|
|
|
PORT = config['local_port'] |
|
|
|
|
|
KEY = config['password'] |
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', |
|
|
|
|
|
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') |
|
|
|
|
|
|
|
|
|
|
|
encrypt_table = ''.join(get_table(KEY)) |
|
|
|
|
|
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', '')) |
|
|
if '-6' in sys.argv[1:]: |
|
|
if '-6' in sys.argv[1:]: |
|
|
ThreadingTCPServer.address_family = socket.AF_INET6 |
|
|
ThreadingTCPServer.address_family = socket.AF_INET6 |
|
|
server = ThreadingTCPServer(('', PORT), Socks5Server) |
|
|
server = ThreadingTCPServer(('', PORT), Socks5Server) |
|
|
server.allow_reuse_address = True |
|
|
server.allow_reuse_address = True |
|
|
print "starting server at port %d ..." % PORT |
|
|
logging.info("starting server at port %d ..." % PORT) |
|
|
server.serve_forever() |
|
|
server.serve_forever() |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
encrypt_table = ''.join(get_table(KEY)) |
|
|
|
|
|
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', '')) |
|
|
|
|
|
main() |
|
|
|
|
|