move config into a JSON file; lint code

This commit is contained in:
clowwindy 2012-11-02 17:17:40 +08:00
parent 04c04ce40c
commit 2f67cabfe0
3 changed files with 54 additions and 41 deletions

7
config.json Normal file
View file

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

View file

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

View file

@ -20,8 +20,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
PORT = 8388
KEY = "barfoo!"
import socket
import select
@ -30,6 +28,8 @@ import struct
import string
import hashlib
import sys
import json
import logging
def get_table(key):
m = hashlib.md5()
@ -69,7 +69,6 @@ class Socks5Server(SocketServer.StreamRequestHandler):
def handle(self):
try:
print 'socks connection from ', self.client_address
sock = self.connection
addrtype = ord(self.decrypt(sock.recv(1)))
if addrtype == 1:
@ -79,31 +78,37 @@ class Socks5Server(SocketServer.StreamRequestHandler):
self.rfile.read(ord(self.decrypt(sock.recv(1)))))
else:
# not support
print 'server: not support'
logging.warn('addr_type not support')
return
port = struct.unpack('>H', self.decrypt(self.rfile.read(2)))
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.connect((addr, port[0]))
local = remote.getsockname()
except socket.error:
except socket.error as e:
# Connection refused
logging.warn('socket error ' + str(e))
return
self.handle_tcp(sock, remote)
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']
def main():
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:]:
ThreadingTCPServer.address_family = socket.AF_INET6
server = ThreadingTCPServer(('', PORT), Socks5Server)
server.allow_reuse_address = True
print "starting server at port %d ..." % PORT
logging.info("starting server at port %d ..." % PORT)
server.serve_forever()
if __name__ == '__main__':
encrypt_table = ''.join(get_table(KEY))
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', ''))
main()