Skip to content

Commit

Permalink
add some status protection
Browse files Browse the repository at this point in the history
  • Loading branch information
clowwindy committed Jun 6, 2014
1 parent d88d280 commit a185e16
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="shadowsocks",
version="2.0",
version="2.0.1",
license='MIT',
description="A fast tunnel proxy that help you get through firewalls",
author='clowwindy',
Expand Down
6 changes: 4 additions & 2 deletions shadowsocks/eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ def run(self):
logging.error(e)
continue
for handler in self._handlers:
# no exceptions should be raised by users
# TODO when there are a lot of handlers
handler(events)
try:
handler(events)
except (OSError, IOError) as e:
logging.error(e)


# from tornado
Expand Down
14 changes: 13 additions & 1 deletion shadowsocks/tcprelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
STAGE_UDP_ASSOC = 2
STAGE_REPLY = 4
STAGE_STREAM = 5
STAGE_DESTROYED = -1

# stream direction
STREAM_UP = 0
Expand Down Expand Up @@ -137,7 +138,7 @@ def _update_stream(self, stream, status):

def _write_to_sock(self, data, sock):
if not data or not sock:
return
return False
uncomplete = False
try:
l = len(data)
Expand All @@ -152,6 +153,7 @@ def _write_to_sock(self, data, sock):
else:
logging.error(e)
self.destroy()
return False
if uncomplete:
if sock == self._local_sock:
self._data_to_write_to_local.append(data)
Expand All @@ -168,6 +170,7 @@ def _write_to_sock(self, data, sock):
self._update_stream(STREAM_UP, WAIT_STATUS_READING)
else:
logging.error('write_all_to_sock:unknown socket')
return True

def _handle_stage_reply(self, data):
if self._is_local:
Expand Down Expand Up @@ -367,17 +370,23 @@ def _on_remote_error(self):
self.destroy()

def handle_event(self, sock, event):
if self._stage == STAGE_DESTROYED:
return
# order is important
if sock == self._remote_sock:
if event & eventloop.POLL_IN:
self._on_remote_read()
if self._stage == STAGE_DESTROYED:
return
if event & eventloop.POLL_OUT:
self._on_remote_write()
if event & eventloop.POLL_ERR:
self._on_remote_error()
elif sock == self._local_sock:
if event & eventloop.POLL_IN:
self._on_local_read()
if self._stage == STAGE_DESTROYED:
return
if event & eventloop.POLL_OUT:
self._on_local_write()
if event & eventloop.POLL_ERR:
Expand All @@ -386,6 +395,9 @@ def handle_event(self, sock, event):
logging.warn('unknown socket')

def destroy(self):
if self._stage == STAGE_DESTROYED:
return
self._stage = STAGE_DESTROYED
if self._remote_address:
logging.debug('destroy: %s:%d' %
self._remote_address)
Expand Down

0 comments on commit a185e16

Please sign in to comment.