Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #10 from fsgmhoward/dev
Browse files Browse the repository at this point in the history
Bug Fixes and Performance Improvements
  • Loading branch information
fsgmhoward authored Jun 26, 2016
2 parents 4ad1564 + c7243f9 commit b565b2b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 40 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ Install instruction for MU API user
2. copy `config_example.py` to `config.py` and edit it following the notes inside (but DO NOT delete the example file). You do not need to edit the MySQL Database section.
3. TestRun `cd shadowsocks && python servers.py` (not server.py)

Reminders for Windows User
--------------------------
1. install pyuv by `pip install pyuv`
2. if git is not configured in your `%PATH%` environmental variable, you can create a file named `.nogit` to avoid using `git describe`

if no exception the server will startup. By default logging is enabled.
if no exception thown the server will startup. By default logging is enabled.
You should be able to see this kind of thing in `shadowsocks.log`(default log file name)
```
Jun 24 01:06:08 INFO -----------------------------------------
Expand Down
61 changes: 30 additions & 31 deletions shadowsocks/dbtransfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
# under the License.

import logging
import cymysql
import time
import socket
import config
import json
import urllib
# TODO: urllib2 does not exist in python 3.5+
import urllib2

if not config.API_ENABLED:
import cymysql

class DbTransfer(object):

Expand Down Expand Up @@ -81,17 +81,22 @@ def push_db_all_user(self):
i = 0
if config.SS_VERBOSE:
logging.info('api upload: pushing transfer statistics')
users = DbTransfer.pull_api_user()
for port in dt_transfer.keys():
user = DbTransfer.pull_api_user(port)
user = None
for result in users:
if str(result[0]) == port:
user = result[9]
break
if not user:
logging.warn('U[%s] User Not Found', port)
server = json.loads(DbTransfer.get_instance().send_command(
'stat: {"server_port":%s}' % port))
if server['stat'] != 'ko':
logging.info(
'U[%d] Server has been stopped: user is removed' % port)
'U[%s] Server has been stopped: user is removed' % port)
DbTransfer.send_command(
'remove: {"server_port":%d}' % port)
'remove: {"server_port":%s}' % port)
continue
if config.SS_VERBOSE:
logging.info('U[%s] User ID Obtained:%s' % (port, user))
Expand Down Expand Up @@ -277,34 +282,28 @@ def pull_db_all_user():
return rows

@staticmethod
def pull_api_user(port=None):
def pull_api_user():
# Node parameter is not included for the ORIGINAL version of SS-Panel V3
url = config.API_URL + '/users?key=' + config.API_PASS + '&node=' + config.NODE_ID
f = urllib.urlopen(url)
data = json.load(f)
f.close()
if port:
for user in data['data']:
if user['port'] == port:
return user['id']
# User not found!
return None
else:
rows = []
for user in data['data']:
if user['port'] in config.SS_SKIP_PORTS:
if config.SS_VERBOSE:
logging.info('api skipped port %d' % user['port'])
else:
rows.append([
user['port'],
user['u'],
user['d'],
user['transfer_enable'],
user['passwd'],
user['switch'],
user['enable'],
user['method'],
user['email']
])
return rows
rows = []
for user in data['data']:
if user['port'] in config.SS_SKIP_PORTS:
if config.SS_VERBOSE:
logging.info('api skipped port %d' % user['port'])
else:
rows.append([
user['port'],
user['u'],
user['d'],
user['transfer_enable'],
user['passwd'],
user['switch'],
user['enable'],
user['method'],
user['email'],
user['id']
])
return rows
2 changes: 1 addition & 1 deletion shadowsocks/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import manager
from dbtransfer import DbTransfer

if os.path.isdir('../.git'):
if os.path.isdir('../.git') and not os.path.isdir('../.nogit'):
import subprocess
if "check_output" not in dir(subprocess):
# Compatible with Python < 2.7
Expand Down
14 changes: 7 additions & 7 deletions shadowsocks/tcprelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ def __init__(self, server, fd_to_handlers, loop, local_sock, config,
self._data_to_write_to_remote = []
self._upstream_status = WAIT_STATUS_READING
self._downstream_status = WAIT_STATUS_INIT
self._client_address = local_sock.getpeername()[:2]
self._remote_address = None
if 'forbidden_ip' in config:
self._forbidden_iplist = config['forbidden_ip']
Expand Down Expand Up @@ -287,6 +286,7 @@ def _handle_stage_connecting(self, data):

def _handle_stage_addr(self, data):
try:
addr, port = self._local_sock.getsockname()[:2]
if self._is_local:
cmd = common.ord(data[1])
if cmd == CMD_UDP_ASSOCIATE:
Expand All @@ -296,7 +296,6 @@ def _handle_stage_addr(self, data):
header = b'\x05\x00\x00\x04'
else:
header = b'\x05\x00\x00\x01'
addr, port = self._local_sock.getsockname()[:2]
# TODO: inet_pton is added for windows in Py 3.4
addr_to_send = socket.inet_pton(self._local_sock.family,
addr)
Expand Down Expand Up @@ -332,13 +331,13 @@ def _handle_stage_addr(self, data):
if firewall_blocked:
logging.warning('U[%d] TCP PORT BANNED: RP[%d] A[%s-->%s]' % (
self._config['server_port'], remote_port,
self._client_address[0], common.to_str(remote_addr)
addr, common.to_str(remote_addr)
))
return
else:
logging.info('U[%d] TCP CONN: RP[%d] A[%s-->%s]' % (
self._config['server_port'], remote_port,
self._client_address[0], common.to_str(remote_addr)
addr, common.to_str(remote_addr)
))

if self._is_local is False:
Expand Down Expand Up @@ -601,13 +600,13 @@ def _on_remote_write(self):
def _on_local_error(self):
logging.debug('got local error')
if self._local_sock:
logging.error(eventloop.get_sock_error(self._local_sock))
logging.error('U[%d] %s' % (self._config['server_port'], eventloop.get_sock_error(self._local_sock)))
self.destroy()

def _on_remote_error(self):
logging.debug('got remote error')
if self._remote_sock:
logging.error(eventloop.get_sock_error(self._remote_sock))
logging.error('U[%d] %s' % (self._config['server_port'], eventloop.get_sock_error(self._remote_sock)))
self.destroy()

def handle_event(self, sock, event):
Expand Down Expand Up @@ -642,8 +641,9 @@ def handle_event(self, sock, event):
logging.warn('unknown socket')

def _log_error(self, e):
addr, port = self._local_sock.getsockname()[:2]
logging.error('U[%d] %s when handling connection from %s:%d' %
(self._config['server_port'], e, self._client_address[0], self._client_address[1]))
(self._config['server_port'], e, addr, port))

def destroy(self):
# destroy the handler and release any resources
Expand Down

0 comments on commit b565b2b

Please sign in to comment.