Browse Source

move config into a JSON file; lint code

1.4
clowwindy 12 years ago
parent
commit
2f67cabfe0
  1. 7
      config.json
  2. 41
      local.py
  3. 33
      server.py

7
config.json

@ -0,0 +1,7 @@
{
"server":"127.0.0.1",
"server_port":8388,
"local_port":1080,
"password":"barfoo!",
"timeout":60
}

41
local.py

@ -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,14 +91,13 @@ 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)
@ -109,27 +105,32 @@ class Socks5Server(SocketServer.StreamRequestHandler):
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)
print 'Tcp connect to', addr, port[0] logging.info('connecting %s:%d' % (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))
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+')
def main(): 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()

33
server.py

@ -20,8 +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.
PORT = 8388
KEY = "barfoo!"
import socket import socket
import select import select
@ -30,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()
@ -69,7 +69,6 @@ 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
addrtype = ord(self.decrypt(sock.recv(1))) addrtype = ord(self.decrypt(sock.recv(1)))
if addrtype == 1: if addrtype == 1:
@ -79,31 +78,37 @@ class Socks5Server(SocketServer.StreamRequestHandler):
self.rfile.read(ord(self.decrypt(sock.recv(1))))) self.rfile.read(ord(self.decrypt(sock.recv(1)))))
else: else:
# not support # not support
print 'server: not support' logging.warn('addr_type not support')
return return
port = struct.unpack('>H', self.decrypt(self.rfile.read(2))) port = struct.unpack('>H', self.decrypt(self.rfile.read(2)))
try: try:
print 'Tcp connecting to', addr, port[0] logging.info('connecting %s:%d' % (addr, port[0]))
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM) remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote.connect((addr, port[0])) remote.connect((addr, port[0]))
local = remote.getsockname() except socket.error as e:
except socket.error:
# Connection refused # Connection refused
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' logging.warn('socket error ' + str(e))
if __name__ == '__main__':
with open('config.json', 'rb') as f:
config = json.load(f)
SERVER = config['server']
PORT = config['server_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+')
def main(): 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()

Loading…
Cancel
Save