• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python encrypt.encrypt_all函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中shadowsocks.encrypt.encrypt_all函数的典型用法代码示例。如果您正苦于以下问题:Python encrypt_all函数的具体用法?Python encrypt_all怎么用?Python encrypt_all使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了encrypt_all函数的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: _handle_client

 def _handle_client(self, sock):
     data, r_addr = sock.recvfrom(BUF_SIZE)
     if not data:
         logging.debug('UDP handle_client: data is empty')
         return
     if not self._is_local:
         addrlen = len(r_addr[0])
         if addrlen > 255:
             # drop
             return
         data = pack_addr(r_addr[0]) + struct.pack('>H', r_addr[1]) + data
         response = encrypt.encrypt_all(self._password, self._method, 1,
                                        data)
         if not response:
             return
     else:
         data = encrypt.encrypt_all(self._password, self._method, 0,
                                    data)
         if not data:
             return
         header_result = parse_header(data)
         if header_result is None:
             return
         # connecttype, dest_addr, dest_port, header_length = header_result
         response = b'\x00\x00\x00' + data
     client_addr = self._client_fd_to_server_addr.get(sock.fileno())
     if client_addr:
         self._server_socket.sendto(response, client_addr)
     else:
         # this packet is from somewhere else we know
         # simply drop that packet
         pass
开发者ID:sanan2015,项目名称:shadowsocks,代码行数:32,代码来源:udprelay.py


示例2: _handle_client

    def _handle_client(self, sock):
        data, r_addr = sock.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_client: data is empty')
            return
        # 如果是服务端接收到的udp,(例如来自youtube)
        if not self._is_local:
            addrlen = len(r_addr[0])
            # 域名规范:域名不能超过255个字符。其中顶级域名不能超过63字符
            if addrlen > 255:
                # drop
                return
            # pack_addr(r_addr[0]):把r_addr[0]打包成shadowvpn的专用的地址header,追加到r_addr[0]头部。
            # struct.pack('>H', r_addr[1]):打包成Big-Endian格式
            data = pack_addr(r_addr[0]) + struct.pack('>H', r_addr[1]) + data
            # 加密
            response = encrypt.encrypt_all(self._password, self._method, 1,
                                           data)
            if not response:
                return
            
        # 本地端收到服务端发来的加密udp
        else:
            # 解密
            data = encrypt.encrypt_all(self._password, self._method, 0,
                                       data)
            if not data:
                return
            header_result = parse_header(data)
            if header_result is None:
                return
            # addrtype, dest_addr, dest_port, header_length = header_result
            # 还原为标准的udp数据报格式,加上首3个字节
            response = b'\x00\x00\x00' + data
            # data: raw data
            # +------+----------+----------+----------+
            # | ATYP | DST.ADDR | DST.PORT |   DATA   |
            # +------+----------+----------+----------+
            # |  1   | Variable |    2     | Variable |
            # +------+----------+----------+----------+
            # response: true udp packet
            # +----+------+------+----------+----------+----------+
            # |RSV | FRAG | ATYP | DST.ADDR | DST.PORT |   DATA   |
            # +----+------+------+----------+----------+----------+
            # | 2  |  1   |  1   | Variable |    2     | Variable |
            # +----+------+------+----------+----------+----------+

        client_addr = self._client_fd_to_server_addr.get(sock.fileno())
        if client_addr:
            # 同样的,完美无瑕。。
            self._server_socket.sendto(response, client_addr)
        else:
            # this packet is from somewhere else we know
            # simply drop that packet
            pass
开发者ID:zhenpingfeng,项目名称:shadowsocks_analysis,代码行数:55,代码来源:udprelay.py


示例3: _handle_client

    def _handle_client(self, sock):

        # receive data from given sock
        data, r_addr = sock.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_client: data is empty')
            return
        if self._stat_callback:
            self._stat_callback(self._listen_port, len(data))

        # server prepares response to relay
        # response is composed by data received
        # a remote server relays response to local server
        # 1. use common.pack_addr() to compose udp request header
        # this is because 'Each UDP datagram carries a UDP request header with it'
        # 2. add header before data to compose response
        # 3. encrypt all
        if not self._is_local:
            addrlen = len(r_addr[0])
            if addrlen > 255:
                # drop
                return
            data = pack_addr(r_addr[0]) + struct.pack('>H', r_addr[1]) + data
            response = encrypt.encrypt_all(self._password, self._method, 1,
                                           data)
            if not response:
                return

        # a local server relays response to local client
        # 1. decrypt data
        # 2. add first 3 bytes to compose a complete udp request header so as to compose response
        # this is because 'Each UDP datagram carries a UDP request header with it'
        else:
            data = encrypt.encrypt_all(self._password, self._method, 0,
                                       data)
            if not data:
                return
            header_result = parse_header(data)
            if header_result is None:
                return
            # addrtype, dest_addr, dest_port, header_length = header_result
            response = b'\x00\x00\x00' + data

        # relay the response
        # note how self._client_fd_to_server_addr is composed in _handle_server()
        # to conclude, client_addr is where a server receives request from
        # a local server receives request from local client
        # a remote server receives request from local server
        client_addr = self._client_fd_to_server_addr.get(sock.fileno())
        if client_addr:
            self._server_socket.sendto(response, client_addr)
        else:
            # this packet is from somewhere else we know
            # simply drop that packet
            pass
开发者ID:whenhecry,项目名称:shadowsocks,代码行数:55,代码来源:udprelay.py


示例4: _handle_client

    def _handle_client(self, sock):
        data, r_addr = sock.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_client: data is empty')
            return
        # If it is from remote
        if not self._is_local:
            addrlen = len(r_addr[0])
            if addrlen > 255:
                # drop
                return
            data = pack_addr(r_addr[0]) + struct.pack('>H', r_addr[1]) + data
            # Encrypt the data
            response = encrypt.encrypt_all(self._password, self._method, 1,
                                           data)
            if not response:
                return
        else:
            # Decrypt the data
            data = encrypt.encrypt_all(self._password, self._method, 0,
                                       data)
            if not data:
                return
            header_result = parse_header(data)
            if header_result is None:
                return
            # addrtype, dest_addr, dest_port, header_length = header_result
            response = b'\x00\x00\x00' + data

# 两个报文差3个字节的数据怎么办?加上去!客户端是有构造和识别SOCK5报文的能力的
# If the the message lost 3 bytes data, the client will add it.
# Because the client has the ability to recognize and construct SOCKS5 message
# +----+------+------+----------+----------+----------+
# |RSV | FRAG | ATYP | DST.ADDR | DST.PORT |   DATA   |
# +----+------+------+----------+----------+----------+
# | 2  |  1   |  1   | Variable |    2     | Variable |
# +----+------+------+----------+----------+----------+
# +------+----------+----------+----------+
# | ATYP | DST.ADDR | DST.PORT |   DATA   |
# +------+----------+----------+----------+
# |  1   | Variable |    2     | Variable |
# +------+----------+----------+----------+

            # Here is the real data
        client_addr = self._client_fd_to_server_addr.get(sock.fileno())
        if client_addr:
            self._server_socket.sendto(response, client_addr)
        else:
            # this packet is from somewhere else we know
            # simply drop that packet
            pass
开发者ID:Toma62299781,项目名称:shadowsocks_analysis,代码行数:51,代码来源:udprelay.py


示例5: _handle_client

    def _handle_client(self, sock):
        data, r_addr = sock.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_client: data is empty')
            return
        # 如果是远程
        if not self._is_local:
            addrlen = len(r_addr[0])
            if addrlen > 255:
                # drop
                return
            data = pack_addr(r_addr[0]) + struct.pack('>H', r_addr[1]) + data
            # 加密内容
            response = encrypt.encrypt_all(self._password, self._method, 1,
                                           data)
            if not response:
                return
        else:
            # 解密
            data = encrypt.encrypt_all(self._password, self._method, 0,
                                       data)
            if not data:
                return
            header_result = parse_header(data)
            if header_result is None:
                return
            # addrtype, dest_addr, dest_port, header_length = header_result
            response = b'\x00\x00\x00' + data

# 两个报文差3个字节的数据怎么办?加上去!客户端是有构造和识别SOCK5报文的能力的
# +----+------+------+----------+----------+----------+
# |RSV | FRAG | ATYP | DST.ADDR | DST.PORT |   DATA   |
# +----+------+------+----------+----------+----------+
# | 2  |  1   |  1   | Variable |    2     | Variable |
# +----+------+------+----------+----------+----------+
# +------+----------+----------+----------+
# | ATYP | DST.ADDR | DST.PORT |   DATA   |
# +------+----------+----------+----------+
# |  1   | Variable |    2     | Variable |
# +------+----------+----------+----------+

            # 这里是真正的数据 
        client_addr = self._client_fd_to_server_addr.get(sock.fileno())
        if client_addr:
            # 同样的,完美无瑕。。
            self._server_socket.sendto(response, client_addr)
        else:
            # this packet is from somewhere else we know
            # simply drop that packet
            pass
开发者ID:Ben20082010,项目名称:shadowsocks_analysis,代码行数:50,代码来源:udprelay.py


示例6: _handle_client

 def _handle_client(self, sock):
     data, r_addr = sock.recvfrom(BUF_SIZE)
     if not data:
         logging.debug('UDP handle_client: data is empty')
         return
     if self._stat_callback:
         self._stat_callback(self._listen_port, len(data))
     if not self._is_local:
         addrlen = len(r_addr[0])
         if addrlen > 255:
             # drop
             return
         data = pack_addr(r_addr[0]) + struct.pack('>H', r_addr[1]) + data
         response = encrypt.encrypt_all(self._password, self._method, 1,
                                        data)
         if not response:
             return
     else:
         data = encrypt.encrypt_all(self._password, self._method, 0,
                                    data)
         if not data:
             return
         header_result = parse_header(data)
         if header_result is None:
             return
         addrtype, dest_addr, dest_port, header_length = header_result
         # spec https://shadowsocks.org/en/spec/one-time-auth.html
         if self._one_time_auth_enable or addrtype & ADDRTYPE_AUTH:
             if len(data) < header_length + ONETIMEAUTH_BYTES:
                 logging.warn('one time auth header is too short')
                 return None
             if onetimeauth_verify(data[-ONETIMEAUTH_BYTES:],
                                   data[header_length: -ONETIMEAUTH_BYTES],
                                   self._encryptor.decipher_iv + self._encryptor.key) is False:
                 logging.warn('one time auth fail')
                 return None
             self._one_time_authed = True
             header_length += ONETIMEAUTH_BYTES
         response = b'\x00\x00\x00' + data
     client_addr = self._client_fd_to_server_addr.get(sock.fileno())
     if client_addr:
         self._server_socket.sendto(response, client_addr)
     else:
         # this packet is from somewhere else we know
         # simply drop that packet
         pass
开发者ID:wzj5530,项目名称:Vision,代码行数:46,代码来源:udprelay.py


示例7: _handle_client

 def _handle_client(self, sock):
     data, r_addr = sock.recvfrom(BUF_SIZE)
     if not data:
         logging.debug('UDP handle_client: data is empty')
         return
     if not self._is_local:
         addrlen = len(r_addr[0])
         if addrlen > 255:
             # drop
             return
         data = pack_addr(r_addr[0]) + struct.pack('>H', r_addr[1]) + data
         response = encrypt.encrypt_all(self._password, self._method, 1,
                                        data)
         if not response:
             return
     else:
         data = encrypt.encrypt_all(self._password, self._method, 0,
                                    data)
         if not data:
             return
         header_result = parse_header(data,self._config)
         if header_result is None:
             return
         # addrtype, dest_addr, dest_port, header_length = header_result
         response = b'\x00\x00\x00' + data
     client_addr = self._client_fd_to_server_addr.get(sock.fileno())
     if client_addr:
         self._server_socket.sendto(response, client_addr)
         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._listen_port) in port_limits:
                     port_limits['%s' % self._listen_port]['used'] = port_limits['%s' % self._listen_port]['used'] + len(response) + BUF_SIZE
                     open('%s' % self._config['port_limit'],"w").write("%s" % json.dumps(port_limits,indent=4,ensure_ascii=False,sort_keys=True))
     else:
         # this packet is from somewhere else we know
         # simply drop that packet
         pass
开发者ID:JustAFakeName,项目名称:ss,代码行数:38,代码来源:udprelay.py


示例8: _handle_local

 def _handle_local(self, sock):
     data, addr = sock.recvfrom(BUF_SIZE)
     header = asyncdns.parse_header(data)
     if header:
         try:
             req_id = header[0]
             req = asyncdns.parse_response(data)
             self._id_to_addr[req_id] = addr
             data = self._address_to_send + data
             data = encrypt.encrypt_all(self._config['password'],
                                        self._config['method'], 1, data)
             self._remote_sock.sendto(data, self._remote_addr)
             logging.info('request %s', req.hostname)
         except Exception as e:
             import traceback
             traceback.print_exc()
             logging.error(e)
开发者ID:skcks,项目名称:ShadowDNS,代码行数:17,代码来源:dnsrelay.py


示例9: _handle_remote

 def _handle_remote(self, sock):
     data, addr = sock.recvfrom(BUF_SIZE)
     if data:
         try:
             data = encrypt.encrypt_all(self._config['password'],
                                        self._config['method'], 0, data)
             header_result = parse_header(data)
             if header_result is None:
                 return None, None
             addrtype, dest_addr, dest_port, header_length = header_result
             data = data[header_length:]
             header = asyncdns.parse_header(data)
             if header:
                 req_id = header[0]
                 res = asyncdns.parse_response(data)
                 addr = self._id_to_addr.get(req_id, None)
                 if addr:
                     self._local_sock.sendto(data, addr)
                     del self._id_to_addr[req_id]
                 logging.info('response %s', res)
         except Exception as e:
             import traceback
             traceback.print_exc()
             logging.error(e)
开发者ID:skcks,项目名称:ShadowDNS,代码行数:24,代码来源:dnsrelay.py


示例10: _handle_server

    def _handle_server(self):
        server = self._server_socket

        # receive request
        # note that no listen()/accept() is used as TCP
        # this is because udp doesn't require long-lived connection
        # so the server does not need to listen for and accept connections
        # It only needs to use bind() to associate its socket with a port
        # and then wait for individual messages
        # _server_socket has been bound in __init__()
        data, r_addr = server.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_server: data is empty')
        if self._stat_callback:
            self._stat_callback(self._listen_port, len(data))

        # for a local server, request is from local client
        # if the datagram isnot standalone, drop it
        if self._is_local:

            frag = common.ord(data[2])
            # FRAG    Current fragment number
            # ord() converts it back to number
            # The FRAG field indicates whether or not this datagram is one of a number of fragments.
            # Implementation of fragmentation is optional; an implementation that
            # does not support fragmentation MUST drop any datagram whose FRAG field is other than X'00'.

            if frag != 0:
                logging.warn('drop a message since frag is not 0')
                return
            else:
                data = data[3:]

        # for a remote server, request is from local server
        # decrypt it
        # note that no removal of first 3 bytes canbe found here, guess in decryption?
        else:
            data = encrypt.encrypt_all(self._password, self._method, 0, data)
            # decrypt data
            if not data:
                logging.debug('UDP handle_server: data is empty after decrypt')
                return

        # for both local and remote server, analyse request header
        header_result = parse_header(data)
        if header_result is None:
            return
        addrtype, dest_addr, dest_port, header_length = header_result

        # set a place where a server relays request to
        # a local server relays request to remote server
        if self._is_local:
            server_addr, server_port = self._get_a_server()
        # a remote server relays request to dest, extracted from request header
        else:
            server_addr, server_port = dest_addr, dest_port

        # try to find send-socket from cache
        addrs = self._dns_cache.get(server_addr, None)
        if addrs is None:
            addrs = socket.getaddrinfo(server_addr, server_port, 0,
                                       socket.SOCK_DGRAM, socket.SOL_UDP)
            if not addrs:
                # drop
                return
            else:
                self._dns_cache[server_addr] = addrs
        af, socktype, proto, canonname, sa = addrs[0]
        key = client_key(r_addr, af)
        client = self._cache.get(key, None)

        # if not found in the cache, init a new send-socket
        # add it to the eventloop
        if not client:
            # TODO async getaddrinfo
            if self._forbidden_iplist:
                if common.to_str(sa[0]) in self._forbidden_iplist:
                    logging.debug('IP %s is in forbidden list, drop' %
                                  common.to_str(sa[0]))
                    # drop
                    return
            client = socket.socket(af, socktype, proto)
            client.setblocking(False)
            self._cache[key] = client
            self._client_fd_to_server_addr[client.fileno()] = r_addr

            self._sockets.add(client.fileno())
            self._eventloop.add(client, eventloop.POLL_IN, self)

        # process the request before relay
        # a local server relays request to remote server
        # so request should be encrypted
        if self._is_local:
            data = encrypt.encrypt_all(self._password, self._method, 1, data)
            if not data:
                return
        # a remote server relays request to dest
        # note that request has been decrypted before
        # so just relays it
        # note that udp request header is removed
#.........这里部分代码省略.........
开发者ID:whenhecry,项目名称:shadowsocks,代码行数:101,代码来源:udprelay.py


示例11: test

def test():
    import time
    import threading
    import struct
    from shadowsocks import encrypt

    logging.basicConfig(level=5,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    enc = []
    eventloop.TIMEOUT_PRECISION = 1

    def run_server():
        config = {
            'server': '127.0.0.1',
            'local_port': 1081,
            'port_password': {
                '8381': 'foobar1',
                '8382': 'foobar2'
            },
            'method': 'aes-256-cfb',
            'manager_address': '127.0.0.1:6001',
            'timeout': 60,
            'fast_open': False,
            'verbose': 2
        }
        manager = Manager(config)
        enc.append(manager)
        manager.run()

    t = threading.Thread(target=run_server)
    t.start()
    time.sleep(1)
    manager = enc[0]
    cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    cli.connect(('127.0.0.1', 6001))

    # test add and remove
    time.sleep(1)
    cli.send(b'add: {"server_port":7001, "password":"asdfadsfasdf"}')
    time.sleep(1)
    assert 7001 in manager._relays
    data, addr = cli.recvfrom(1506)
    assert b'ok' in data

    cli.send(b'remove: {"server_port":8381}')
    time.sleep(1)
    assert 8381 not in manager._relays
    data, addr = cli.recvfrom(1506)
    assert b'ok' in data
    logging.info('add and remove test passed')

    # test statistics for TCP
    header = common.pack_addr(b'google.com') + struct.pack('>H', 80)
    data = encrypt.encrypt_all(b'asdfadsfasdf', 'aes-256-cfb', 1,
                               header + b'GET /\r\n\r\n')
    tcp_cli = socket.socket()
    tcp_cli.connect(('127.0.0.1', 7001))
    tcp_cli.send(data)
    tcp_cli.recv(4096)
    tcp_cli.close()

    data, addr = cli.recvfrom(1506)
    data = common.to_str(data)
    assert data.startswith('stat: ')
    data = data.split('stat:')[1]
    stats = shell.parse_json_in_str(data)
    assert '7001' in stats
    logging.info('TCP statistics test passed')

    # test statistics for UDP
    header = common.pack_addr(b'127.0.0.1') + struct.pack('>H', 80)
    data = encrypt.encrypt_all(b'foobar2', 'aes-256-cfb', 1,
                               header + b'test')
    udp_cli = socket.socket(type=socket.SOCK_DGRAM)
    udp_cli.sendto(data, ('127.0.0.1', 8382))
    tcp_cli.close()

    data, addr = cli.recvfrom(1506)
    data = common.to_str(data)
    assert data.startswith('stat: ')
    data = data.split('stat:')[1]
    stats = json.loads(data)
    assert '8382' in stats
    logging.info('UDP statistics test passed')

    manager._loop.stop()
    t.join()
开发者ID:starsingchow,项目名称:shadowsocks,代码行数:88,代码来源:manager.py


示例12: _handle_server

    def _handle_server(self):
        server = self._server_socket
        data, r_addr = server.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_server: data is empty')
        if self._is_local:
            # if local receives this request, then:
            frag = common.ord(data[2])
# this is no classic UDP
# +----+------+------+----------+----------+----------+
# |RSV | FRAG | ATYP | DST.ADDR | DST.PORT |   DATA   |
# +----+------+------+----------+----------+----------+
# | 2  |  1   |  1   | Variable |    2     | Variable |
# +----+------+------+----------+----------+----------+
            if frag != 0:
                logging.warn('drop a message since frag is not 0')
                return
            else:
                data = data[3:]
                # after [3:]
# +------+----------+----------+----------+
# | ATYP | DST.ADDR | DST.PORT |   DATA   |
# +------+----------+----------+----------+
# |  1   | Variable |    2     | Variable |
# +------+----------+----------+----------+
# 就是shadowsocks那段
        else:
            # If remote receives
            data = encrypt.encrypt_all(self._password, self._method, 0, data)
            # decrypt the data
            if not data:
                logging.debug('UDP handle_server: data is empty after decrypt')
                return
        header_result = parse_header(data)
        if header_result is None:
            return
        addrtype, dest_addr, dest_port, header_length = header_result

        if self._is_local:
            # 如果是local收到,则server_addr server_port都是远程的
            server_addr, server_port = self._get_a_server()
        else:
            # 如果远程收到,则将server_addr这些改成dest_addr dest_port,方便操作
            server_addr, server_port = dest_addr, dest_port

        key = client_key(r_addr[0], r_addr[1], dest_addr, dest_port)
        client = self._cache.get(key, None)
        if not client:
            # TODO async getaddrinfo
            # 根据server_addr, server_port等的类型决定选用的协议类型
            # Translate the host/port argument into a sequence of 5-tuples
            addrs = socket.getaddrinfo(server_addr, server_port, 0,
                                       socket.SOCK_DGRAM, socket.SOL_UDP)
            if addrs:
                af, socktype, proto, canonname, sa = addrs[0]
                # 根据上面的server_addr, server_port建立相应的连接,一环扣一环
                # 这里是主动发出请求,所以要新建一个socket

                # 这里根据上面得到的不同的端口类型就新建不同类型的socket:用于tcp的和同于udp的
                client = socket.socket(af, socktype, proto)
                client.setblocking(False)
                self._cache[key] = client
                self._client_fd_to_server_addr[client.fileno()] = r_addr
            else:
                # drop
                return
            self._sockets.add(client.fileno())
            self._eventloop.add(client, eventloop.POLL_IN)

        if self._is_local:
            # 如果是local,要向远程发,要过墙,所以要加密
            data = encrypt.encrypt_all(self._password, self._method, 1, data)
            if not data:
                return
        else:
            # 如果是远程,要向dest发请求,所以把除数据的部分除去
            data = data[header_length:]
        if not data:
            return
        try:
            # Send the data
            # 这个sendto同时有udp的和tcp的两种,sendto函数主要用于UDP,但这里两种都用了
            client.sendto(data, (server_addr, server_port))
        except IOError as e:
            err = eventloop.errno_from_exception(e)
            if err in (errno.EINPROGRESS, errno.EAGAIN):
                pass
            else:
                logging.error(e)
开发者ID:Toma62299781,项目名称:shadowsocks_analysis,代码行数:89,代码来源:udprelay.py


示例13: _handle_server

    def _handle_server(self):
        server = self._server_socket
        data, r_addr = server.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_server: data is empty')
        if self._stat_callback:
            self._stat_callback(self._listen_port, len(data))
        if self._is_local:
            frag = common.ord(data[2])
            if frag != 0:
                logging.warn('UDP drop a message since frag is not 0')
                return
            else:
                data = data[3:]
        else:
            data = encrypt.encrypt_all(self._password, self._method, 0, data)
            # decrypt data
            if not data:
                logging.debug(
                    'UDP handle_server: data is empty after decrypt'
                )
                return
        header_result = parse_header(data)
        if header_result is None:
            return
        addrtype, dest_addr, dest_port, header_length = header_result

        if self._is_local:
            server_addr, server_port = self._get_a_server()
        else:
            server_addr, server_port = dest_addr, dest_port
            # spec https://shadowsocks.org/en/spec/one-time-auth.html
            if self._one_time_auth_enable or addrtype & ADDRTYPE_AUTH:
                if len(data) < header_length + ONETIMEAUTH_BYTES:
                    logging.warn('UDP one time auth header is too short')
                    return
                _hash = data[-ONETIMEAUTH_BYTES:]
                _data = data[header_length: -ONETIMEAUTH_BYTES]
                _key = self._encryptor.decipher_iv + self._encryptor.key
                if onetimeauth_verify(_hash, _data, _key) is False:
                    logging.warn('UDP one time auth fail')
                    return
                self._one_time_authed = True
                header_length += ONETIMEAUTH_BYTES
        addrs = self._dns_cache.get(server_addr, None)
        if addrs is None:
            addrs = socket.getaddrinfo(server_addr, server_port, 0,
                                       socket.SOCK_DGRAM, socket.SOL_UDP)
            if not addrs:
                # drop
                return
            else:
                self._dns_cache[server_addr] = addrs

        af, socktype, proto, canonname, sa = addrs[0]
        key = client_key(r_addr, af)
        client = self._cache.get(key, None)
        if not client:
            # TODO async getaddrinfo
            if self._forbidden_iplist:
                if common.to_str(sa[0]) in self._forbidden_iplist:
                    logging.debug('IP %s is in forbidden list, drop' %
                                  common.to_str(sa[0]))
                    # drop
                    return
            client = socket.socket(af, socktype, proto)
            client.setblocking(False)
            self._cache[key] = client
            self._client_fd_to_server_addr[client.fileno()] = r_addr

            self._sockets.add(client.fileno())
            self._eventloop.add(client, eventloop.POLL_IN, self)

        if self._is_local:
            # spec https://shadowsocks.org/en/spec/one-time-auth.html
            if self._one_time_auth_enable:
                data = self._one_time_auth_chunk_data_gen(data)
            data = encrypt.encrypt_all(self._password, self._method, 1, data)
            if not data:
                return
        else:
            data = data[header_length:]
        if not data:
            return
        try:
            client.sendto(data, (server_addr, server_port))
        except IOError as e:
            err = eventloop.errno_from_exception(e)
            if err in (errno.EINPROGRESS, errno.EAGAIN):
                pass
            else:
                shell.print_exception(e)
开发者ID:tahaalibra,项目名称:shadowsocks,代码行数:92,代码来源:udprelay.py


示例14: _handle_server

    def _handle_server(self):
        server = self._server_socket
        data, r_addr = server.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_server: data is empty')
        if self._is_local:
            frag = common.ord(data[2])
            if frag != 0:
                logging.warn('drop a message since frag is not 0')
                return
            else:
                data = data[3:]
        else:
            data = encrypt.encrypt_all(self._password, self._method, 0, data)
            # decrypt data
            if not data:
                logging.debug('UDP handle_server: data is empty after decrypt')
                return

        if not self._is_local:
            data = pre_parse_header(data)
            if data is None:
                return

        header_result = parse_header(data)
        if header_result is None:
            return
        connecttype, dest_addr, dest_port, header_length = header_result

        if self._is_local:
            server_addr, server_port = self._get_a_server()
            key = client_key(r_addr[0], r_addr[1], dest_addr, dest_port)
        else:
            server_addr, server_port = dest_addr, dest_port
            addrs = socket.getaddrinfo(dest_addr, dest_port, 0, socket.SOCK_DGRAM, socket.SOL_UDP)
            if addrs:
                af, socktype, proto, canonname, sa = addrs[0]
                key = client_key(r_addr[0], r_addr[1], af, 0)
            else:
                key = None

        client = self._cache.get(key, None)
        if not client:
            # TODO async getaddrinfo
            #logging.info('UDP handle_server %s:%d from %s:%d' % (common.to_str(server_addr), server_port, self._listen_addr, self._listen_port))
            addrs = socket.getaddrinfo(server_addr, server_port, 0,
                                       socket.SOCK_DGRAM, socket.SOL_UDP)
            if addrs:
                af, socktype, proto, canonname, sa = addrs[0]
                if self._forbidden_iplist:
                    if common.to_str(sa[0]) in self._forbidden_iplist:
                        logging.debug('IP %s is in forbidden list, drop' %
                                      common.to_str(sa[0]))
                        # drop
                        return
                client = socket.socket(af, socktype, proto)
                client.setblocking(False)
                self._cache[key] = client
                self._client_fd_to_server_addr[client.fileno()] = r_addr
            else:
                # drop
                return
            self._sockets.add(client.fileno())
            self._eventloop.add(client, eventloop.POLL_IN)

        if self._is_local:
            data = encrypt.encrypt_all(self._password, self._method, 1, data)
            if not data:
                return
        else:
            data = data[header_length:]
        if not data:
            return
        try:
            client.sendto(data, (server_addr, server_port))
        except IOError as e:
            err = eventloop.errno_from_exception(e)
            if err in (errno.EINPROGRESS, errno.EAGAIN):
                pass
            else:
                shell.print_exception(e)
开发者ID:sanan2015,项目名称:shadowsocks,代码行数:81,代码来源:udprelay.py


示例15: _handle_server

    def _handle_server(self):
        server = self._server_socket
        data, r_addr = server.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_server: data is empty')
        if self._is_local:
            frag = common.ord(data[2])
            if frag != 0:
                logging.warn('drop a message since frag is not 0')
                common.error_to_file('drop a message since frag is not 0',self._config)
                return
            else:
                data = data[3:]
        else:
            data = encrypt.encrypt_all(self._password, self._method, 0, data)
            # decrypt data
            if not data:
                logging.debug('UDP handle_server: data is empty after decrypt')
                return
        header_result = parse_header(data,self._config)
        if header_result is None:
            return
        addrtype, dest_addr, dest_port, header_length = header_result

        if self._is_local:
            server_addr, server_port = self._get_a_server()
        else:
            server_addr, server_port = dest_addr, dest_port

        addrs = self._dns_cache.get(server_addr, None)
        if addrs is None:
            addrs = socket.getaddrinfo(server_addr, server_port, 0,
                                       socket.SOCK_DGRAM, socket.SOL_UDP)
            if not addrs:
                # drop
                return
            else:
                self._dns_cache[server_addr] = addrs

        af, socktype, proto, canonname, sa = addrs[0]
        key = client_key(r_addr, af)

        client = self._cache.get(key, None)
        if not client:
            # TODO async getaddrinfo
            if self._forbidden_iplist:
                if common.to_str(sa[0]) in self._forbidden_iplist:
                    logging.debug('IP %s is in forbidden list, drop' %
                                  common.to_str(sa[0]))
                    # drop
                    return
            client = socket.socket(af, socktype, proto)
            client.setblocking(False)
            self._cache[key] = client
            self._client_fd_to_server_addr[client.fileno()] = r_addr

            self._sockets.add(client.fileno())
            self._eventloop.add(client, eventloop.POLL_IN, self)
        if self._stat_callback:
            self._stat_callback(self._listen_port, len(data))
        if self._is_local:
            data = encrypt.encrypt_all(self._password, self._method, 1, data)
            if not data:
                return
        else:
            data = data[header_length:]
        if not data:
            return
        try:
            client.sendto(data, (server_addr, server_port))
            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._listen_port) in port_limits:
                        port_limits['%s' % self._listen_port]['used'] = port_limits['%s' % self._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 IOError as e:
            err = eventloop.errno_from_exception(e)
            if err in (errno.EINPROGRESS, errno.EAGAIN):
                pass
            else:
                shell.print_exception(e)
开发者ID:JustAFakeName,项目名称:ss,代码行数:82,代码来源:udprelay.py


示例16: _handle_server

    def _handle_server(self):
        """
        服务器有UDP连接
        :return:
        """
        server = self._server_socket
        data, r_addr = server.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_server: data is empty')
        if self._stat_callback:
            self._stat_callback(self._listen_port, len(data))
        if self._is_local:
            frag = common.ord(data[2])
            if frag != 0:
                logging.warn('drop a message since frag is not 0')
                return
            else:
                data = data[3:]
        else:
            data = encrypt.encrypt_all(self._password, self._method, 0, data)
            # decrypt data
            if not data:
                logging.debug('UDP handle_server: data is empty after decrypt')
                return
        header_result = parse_header(data)
        if header_result is None:
            return
        addrtype, dest_addr, dest_port, header_length = header_result

        if self._is_local:
            server_addr, server_port = self._get_a_server()
        else:
            server_addr, server_port = dest_addr, dest_port

        addrs = self._dns_cache.get(server_addr, None)
        if addrs is None:
            addrs = socket.getaddrinfo(server_addr, server_port, 0,
                                       socket.SOCK_DGRAM, socket.SOL_UDP)
            if not addrs:
                # drop
                return
            else:
                self._dns_cache[server_addr] = addrs

        af, socktype, proto, canonname, sa = addrs[0]
        # 给这个客户端返回一个唯一值而已,一个字符串
        key = client_key(r_addr, af)
        client = self._cache.get(key, None)
        if not client:
            # TODO async getaddrinfo
            if self._forbidden_iplist:
                if common.to_str(sa[0]) in self._forbidden_iplist:
                    logging.debug('IP %s is in forbidden list, drop' %
                                  common.to_str(sa[0]))
                    # drop
                    return
            client = socket.socket(af, socktype, proto)
            client.setblocking(False)
            # 记录client的关键字
            self._cache[key] = client
            # 记录client的文件描述符对应的转发地址,若client收到响应udp包则转发到这个地址
            self._client_fd_to_server_addr[client.fileno()] = r_addr

            self._sockets.add(client.fileno())
            self._eventloop.add(client, eventloop.POLL_IN, self)

        if self._is_local:
            data = encrypt.encrypt_all(self._password, self._method, 1, data)
            if not data:
                return
        else:
            data = data[header_length:]
        if not data:
            return
        try:
            client.sendto(data, (server_addr, server_port))
        except IOError as e:
            err = eventloop.errno_from_exception(e)
            if err in (errno.EINPROGRESS, errno.EAGAIN):
                pass
            else:
                shell.print_exception(e)
开发者ID:ncs19960831,项目名称:shadowsocks-analysis,代码行数:82,代码来源:udprelay.py


示例17: _handle_server

    def _handle_server(self):
        server = self._server_socket
        # r_addr是发送者的地址
        data, r_addr = server.recvfrom(BUF_SIZE)
        if not data:
            logging.debug('UDP handle_server: data is empty')
            
        # 若本地端从监 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python eventloop.errno_from_exception函数代码示例发布时间:2022-05-27
下一篇:
Python util.run_cipher函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap