本文整理汇总了Python中shadowsocks.eventloop.errno_from_exception函数的典型用法代码示例。如果您正苦于以下问题:Python errno_from_exception函数的具体用法?Python errno_from_exception怎么用?Python errno_from_exception使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了errno_from_exception函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _handle_remote
def _handle_remote(self, sock):
try:
data, addr = sock.recvfrom(BUF_SIZE)
except (OSError, IOError) as e:
logging.error(e)
if eventloop.errno_from_exception(e) == errno.ECONNRESET:
# just for Windows lol
self._rebuild_sockets()
return
if data:
try:
header = asyncdns.parse_header(data)
if header:
req_id = header[0]
res = asyncdns.parse_response(data)
logging.info('response %s', res)
addr = self._id_to_addr.get(req_id, None)
if addr:
for answer in res.answers:
if answer and answer[0] in GFW_LIST:
return
self._local_sock.sendto(data, addr)
del self._id_to_addr[req_id]
except Exception as e:
import traceback
traceback.print_exc()
logging.error(e)
if eventloop.errno_from_exception(e) == errno.EACCES:
# when we have changed our ip
self._rebuild_sockets()
示例2: _handle_stage_connecting
def _handle_stage_connecting(self, data):
if self._is_local:
data = self._encryptor.encrypt(data)
self._data_to_write_to_remote.append(data)
if self._is_local and not self._fastopen_connected and self._config["fast_open"]:
# for sslocal and fastopen, we basically wait for data and use
# sendto to connect
try:
# only connect once
self._fastopen_connected = True
remote_sock = self._create_remote_socket(self._chosen_server[0], self._chosen_server[1])
self._loop.add(remote_sock, eventloop.POLL_ERR)
data = b"".join(self._data_to_write_to_remote)
l = len(data)
s = remote_sock.sendto(data, MSG_FASTOPEN, self._chosen_server)
if s < l:
data = data[s:]
self._data_to_write_to_remote = [data]
else:
self._data_to_write_to_remote = []
self._update_stream(STREAM_UP, WAIT_STATUS_READWRITING)
except (OSError, IOError) as e:
if eventloop.errno_from_exception(e) == errno.EINPROGRESS:
# in this case data is not sent at all
self._update_stream(STREAM_UP, WAIT_STATUS_READWRITING)
elif eventloop.errno_from_exception(e) == errno.ENOTCONN:
logging.error("fast open not supported on this OS")
self._config["fast_open"] = False
self.destroy()
else:
logging.error(e)
if self._config["verbose"]:
traceback.print_exc()
self.destroy()
示例3: _handle_stage_reply
def _handle_stage_reply(self, data):
if self._is_local:
data = self._encryptor.encrypt(data)
self._data_to_write_to_remote.append(data)
if self._is_local and not self._fastopen_connected and \
self._config['fast_open']:
try:
self._fastopen_connected = True
remote_sock = \
self._create_remote_socket(self._chosen_server[0],
self._chosen_server[1])
self._loop.add(remote_sock, eventloop.POLL_ERR)
data = b''.join(self._data_to_write_to_local)
l = len(data)
s = remote_sock.sendto(data, MSG_FASTOPEN, self._chosen_server)
if s < l:
data = data[s:]
self._data_to_write_to_local = [data]
self._update_stream(STREAM_UP, WAIT_STATUS_READWRITING)
else:
self._data_to_write_to_local = []
self._update_stream(STREAM_UP, WAIT_STATUS_READING)
self._stage = STAGE_STREAM
except (OSError, IOError) as e:
if eventloop.errno_from_exception(e) == errno.EINPROGRESS:
self._update_stream(STREAM_UP, WAIT_STATUS_READWRITING)
elif eventloop.errno_from_exception(e) == errno.ENOTCONN:
logging.error('fast open not supported on this OS')
self._config['fast_open'] = False
self.destroy()
else:
logging.error(e)
if self._config['verbose']:
traceback.print_exc()
self.destroy()
示例4: main
def main():
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
parser = argparse.ArgumentParser(description='Forward DNS requests.')
parser.add_argument('-b', '--local_address', metavar='BIND_ADDR', type=str,
help='address that listens, default: 127.0.0.1',
default='127.0.0.1')
parser.add_argument('-p', '--local_port', metavar='BIND_PORT', type=int,
help='port that listens, default: 53', default=53)
parser.add_argument('-s', '--dns', metavar='DNS', type=str,
help='DNS server to use, default: '
'114.114.114.114,208.67.222.222,8.8.8.8',
default='114.114.114.114,208.67.222.222,8.8.8.8')
config = vars(parser.parse_args())
logging.info("starting dns at %s:%d",
config['local_address'], config['local_port'])
loop = eventloop.EventLoop()
try:
udprelay = UDPDNSRelay(config)
udprelay.add_to_loop(loop)
tcprelay = TCPDNSRelay(config)
tcprelay.add_to_loop(loop)
loop.run()
except (OSError, IOError) as e:
logging.error(e)
if eventloop.errno_from_exception(e) == errno.EACCES:
logging.info('please use sudo to run this program')
sys.exit(1)
示例5: handle_event
def handle_event(self, sock, fd, event):
# handle events and dispatch to handlers
if sock:
logging.log(shell.VERBOSE_LEVEL, 'fd %d %s', fd,
eventloop.EVENT_NAMES.get(event, event))
if sock == self._server_socket:
if event & eventloop.POLL_ERR:
# TODO
raise Exception('server_socket error')
try:
logging.debug('accept')
conn = self._server_socket.accept()
TCPRelayHandler(self, self._fd_to_handlers,
self._eventloop, conn[0], self._config,
self._dns_resolver, self._is_local)
except (OSError, IOError) as e:
error_no = eventloop.errno_from_exception(e)
if error_no in (errno.EAGAIN, errno.EINPROGRESS,
errno.EWOULDBLOCK):
return
else:
shell.print_exception(e)
if self._config['verbose']:
traceback.print_exc()
else:
if sock:
handler = self._fd_to_handlers.get(fd, None)
if handler:
handler.handle_event(sock, event)
else:
logging.warn('poll removed fd')
示例6: _on_remote_read
def _on_remote_read(self):
logging.debug("Running in the TCPRelayHandler class. [_on_remote_read]")
# handle all remote read events
data = None
try:
data = self._remote_sock.recv(BUF_SIZE)
except (OSError, IOError) as e:
if eventloop.errno_from_exception(e) in (errno.ETIMEDOUT, errno.EAGAIN, errno.EWOULDBLOCK):
return
if not data:
self.destroy()
return
self._update_activity(len(data))
if self._is_local:
data = self._encryptor.decrypt(data)
else:
data = self._encryptor.encrypt(data)
try:
self._write_to_sock(data, self._local_sock)
# if not self._is_local:
# if self._config.has_key('port_limit') and self._config['port_limit'] != "" and os.path.exists(self._config['port_limit']):
# port_limits = json.loads(open(self._config['port_limit']).read())
# if str(self._server._listen_port) in port_limits:
# port_limits['%s' % self._server._listen_port]['used'] = port_limits['%s' % self._server._listen_port]['used'] + len(data) + BUF_SIZE
# open('%s' % self._config['port_limit'],"w").write("%s" % json.dumps(port_limits,indent=4,ensure_ascii=False,sort_keys=True))
except Exception as e:
shell.print_exception(e)
if self._config['verbose']:
traceback.print_exc()
# TODO use logging when debug completed
self.destroy()
示例7: _handle_local
def _handle_local(self, sock):
try:
data, addr = sock.recvfrom(BUF_SIZE)
except (OSError, IOError) as e:
logging.error(e)
if eventloop.errno_from_exception(e) == errno.ECONNRESET:
# just for Windows lol
self._rebuild_sockets()
return
header = asyncdns.parse_header(data)
if header:
try:
req_id = header[0]
req = asyncdns.parse_response(data)
logging.info('--- request %s', req.hostname)
if req.hostname in self._hosts:
response = self.build_response(data,
self._hosts[req.hostname])
if response:
logging.info('%s hit /etc/hosts', req.hostname)
self._local_sock.sendto(response, addr)
return
self._id_to_addr[req_id] = addr
for remote_addr in self._remote_addrs:
self._remote_sock.sendto(data, remote_addr)
except Exception as e:
import traceback
traceback.print_exc()
logging.error(e)
示例8: _on_local_read
def _on_local_read(self):
self._update_activity()
if not self._local_sock:
return
is_local = self._is_local
data = None
try:
data = self._local_sock.recv(BUF_SIZE)
except (OSError, IOError) as e:
if eventloop.errno_from_exception(e) in \
(errno.ETIMEDOUT, errno.EAGAIN, errno.EWOULDBLOCK):
return
if not data:
self.destroy()
return
if not is_local:
data = self._encryptor.decrypt(data)
if not data:
return
if self._stage == STAGE_STREAM:
if self._is_local:
data = self._encryptor.encrypt(data)
self._write_to_sock(data, self._remote_sock)
return
elif is_local and self._stage == STAGE_INIT:
# TODO check auth method
self._write_to_sock(b'\x05\00', self._local_sock)
self._stage = STAGE_HELLO
return
elif self._stage == STAGE_REPLY:
self._handle_stage_reply(data)
elif (is_local and self._stage == STAGE_HELLO) or \
(not is_local and self._stage == STAGE_INIT):
self._handle_stage_hello(data)
示例9: _on_local_read
def _on_local_read(self):
# handle all local read events and dispatch them to methods for
# each stage
if not self._local_sock:
return
is_local = self._is_local
data = None
try:
data = self._local_sock.recv(BUF_SIZE)
except (OSError, IOError) as e:
if eventloop.errno_from_exception(e) in \
(errno.ETIMEDOUT, errno.EAGAIN, errno.EWOULDBLOCK):
return
if not data:
self.destroy()
return
self._update_activity(len(data))
if not is_local:
data = self._encryptor.decrypt(data)
if not data:
return
if self._stage == STAGE_STREAM:
self._handle_stage_stream(data)
return
elif is_local and self._stage == STAGE_INIT:
self._handle_stage_init(data)
elif self._stage == STAGE_CONNECTING:
self._handle_stage_connecting(data)
elif (is_local and self._stage == STAGE_ADDR) or \
(not is_local and self._stage == STAGE_INIT):
self._handle_stage_addr(data)
示例10: _on_local_read
def _on_local_read(self):
# handle all local read events and dispatch them to methods for
# each stage
self._update_activity()
if not self._local_sock:
return
is_local = self._is_local
data = None
try:
data = self._local_sock.recv(BUF_SIZE)
except (OSError, IOError) as e:
if eventloop.errno_from_exception(e) in (errno.ETIMEDOUT, errno.EAGAIN, errno.EWOULDBLOCK):
return
if not data:
self.destroy()
return
if not is_local:
data = self._encryptor.decrypt(data)
if not data:
return
if self._stage == STAGE_STREAM:
if self._is_local:
data = self._encryptor.encrypt(data)
self._write_to_sock(data, self._remote_sock)
return
elif is_local and self._stage == STAGE_INIT:
# TODO check auth method
self._write_to_sock(b"\x05\00", self._local_sock)
self._stage = STAGE_ADDR
return
elif self._stage == STAGE_CONNECTING:
self._handle_stage_connecting(data)
elif (is_local and self._stage == STAGE_ADDR) or (not is_local and self._stage == STAGE_INIT):
self._handle_stage_addr(data)
示例11: _handle_conn
def _handle_conn(self, sock):
try:
local, addr = sock.accept()
addrs = socket.getaddrinfo(self._remote_addr[0],
self._remote_addr[1], 0,
socket.SOCK_STREAM, socket.SOL_TCP)
if len(addrs) == 0:
raise Exception("can't get addrinfo for %s:%d" %
self._remote_addr)
af, socktype, proto, canonname, sa = addrs[0]
remote = socket.socket(af, socktype, proto)
local.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
remote.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
self._local_to_remote[local] = remote
self._remote_to_local[remote] = local
self._loop.add(local, 0)
self._loop.add(remote, eventloop.POLL_OUT)
try:
remote.connect(self._remote_addr)
except (OSError, IOError) as e:
if eventloop.errno_from_exception(e) in (errno.EINPROGRESS,
errno.EAGAIN):
pass
else:
raise
except (OSError, IOError) as e:
logging.error(e)
示例12: _on_remote_read
def _on_remote_read(self):
# handle all remote read events
data = None
try:
data = self._remote_sock.recv(BUF_SIZE)
except (OSError, IOError) as e:
if eventloop.errno_from_exception(e) in \
(errno.ETIMEDOUT, errno.EAGAIN, errno.EWOULDBLOCK):
return
if not data:
self.destroy()
return
self._update_activity(len(data))
if self._is_local:
data = self._encryptor.decrypt(data)
else:
data = self._encryptor.encrypt(data)
try:
self._write_to_sock(data, self._local_sock)
except Exception as e:
shell.print_exception(e)
if self._config['verbose']:
traceback.print_exc()
# TODO use logging when debug completed
self.destroy()
示例13: _on_remote_read
def _on_remote_read(self):
logging.debug('on remote read')
# handle all remote read events
data = None
try:
data = self._remote_sock.recv(BUF_SIZE)
except (OSError, IOError) as e:
if eventloop.errno_from_exception(e) in \
(errno.ETIMEDOUT, errno.EAGAIN, errno.EWOULDBLOCK):
return
if not data:
self.destroy()
return
self._update_activity(len(data))
if self._is_local:
data = self._encryptor.decrypt(data)
else:
# logging.debug('received data:[%s]' % data)
# data = 'HTTP/1.1 302 Found\nLocation: https://ashadowsocks.com/'
data = self._encryptor.encrypt(data)
try:
self._write_to_sock(data, self._local_sock)
except Exception as e:
shell.print_exception(e)
if self._config['verbose']:
traceback.print_exc()
# TODO use logging when debug completed
self.destroy()
示例14: _handle_stage_connecting
def _handle_stage_connecting(self, data):
# 如果是本地端,加密数据
if self._is_local:
data = self._encryptor.encrypt(data)
self._data_to_write_to_remote.append(data)
# 若本地端设置了fast_open却没有fast_open连接
if self._is_local and not self._fastopen_connected and \
self._config['fast_open']:
# for sslocal and fastopen, we basically wait for data and use
# sendto to connect
try:
# only connect once
self._fastopen_connected = True
remote_sock = \
self._create_remote_socket(self._chosen_server[0],
self._chosen_server[1])
self._loop.add(remote_sock, eventloop.POLL_ERR)
# 发送二进制流
data = b''.join(self._data_to_write_to_local)
l = len(data)
# 发送给服务端
s = remote_sock.sendto(data, MSG_FASTOPEN, self._chosen_server)
# 若发送尚未完成,转入读写状态
if s < l:
data = data[s:]
self._data_to_write_to_local = [data]
self._update_stream(STREAM_UP, WAIT_STATUS_READWRITING)
# 发送完毕,转入读状态
else:
self._data_to_write_to_local = []
self._update_stream(STREAM_UP, WAIT_STATUS_READING)
self._stage = STAGE_STREAM
except (OSError, IOError) as e:
# EINPROGRESS错误,表示连接操作正在进行中,但是仍未完成,常见于非阻塞的socket连接中
# stream流状态更新为读写
if eventloop.errno_from_exception(e) == errno.EINPROGRESS:
self._update_stream(STREAM_UP, WAIT_STATUS_READWRITING)
# ENOTCONN指定的socket是一个未连接成功的socket
elif eventloop.errno_from_exception(e) == errno.ENOTCONN:
logging.error('fast open not supported on this OS')
self._config['fast_open'] = False
self.destroy()
else:
logging.error(e)
if self._config['verbose']:
traceback.print_exc()
self.destroy()
示例15: _handle_stage_connecting
def _handle_stage_connecting(self, data):
# further process data
# encrypt it and add to buffer
if self._is_local:
data = self._encryptor.encrypt(data)
self._data_to_write_to_remote.append(data)
# for fastopen, we basically
# wait for data and use sendto to connect
# only connect once
if self._is_local and not self._fastopen_connected and \
self._config['fast_open']:
try:
self._fastopen_connected = True
# 1. create _remote_sock
# 2. add it to eventloop, set event as ERR
# 3. send data to ssremote
# 4. reset buffer with unsent data
# 5. sslocal relays up stream
remote_sock = \
self._create_remote_socket(self._chosen_server[0],
self._chosen_server[1])
self._loop.add(remote_sock, eventloop.POLL_ERR, self._server)
data = b''.join(self._data_to_write_to_remote)
l = len(data)
s = remote_sock.sendto(data, MSG_FASTOPEN, self._chosen_server)
if s < l:
data = data[s:]
self._data_to_write_to_remote = [data]
else:
self._data_to_write_to_remote = []
self._update_stream(STREAM_UP, WAIT_STATUS_READWRITING)
except (OSError, IOError) as e:
if eventloop.errno_from_exception(e) == errno.EINPROGRESS:
self._update_stream(STREAM_UP, WAIT_STATUS_READWRITING)
elif eventloop.errno_from_exception(e) == errno.ENOTCONN:
logging.error('fast open not supported on this OS')
self._config['fast_open'] = False
self.destroy()
else:
shell.print_exception(e)
if self._config['verbose']:
traceback.print_exc()
self.destroy()