Example #1
0
    def update(self, user_id, client_id, connection_id):
        if user_id not in self.user_id:
            self.user_id[user_id] = lru_cache.LRUCache()
        local_client_id = self.user_id[user_id]

        if client_id in local_client_id:
            local_client_id[client_id].update()
Example #2
0
    def insert(self, user_id, client_id, connection_id):
        if user_id not in self.user_id:
            self.user_id[user_id] = lru_cache.LRUCache()
        local_client_id = self.user_id[user_id]

        if local_client_id.get(
                client_id,
                None) is None or not local_client_id[client_id].enable:
            if local_client_id.first() is None or len(
                    local_client_id) < self.max_client:
                if client_id not in local_client_id:
                    #TODO: check
                    local_client_id[client_id] = client_queue(connection_id)
                else:
                    local_client_id[client_id].re_enable(connection_id)
                return local_client_id[client_id].insert(connection_id)

            if not local_client_id[local_client_id.first()].is_active():
                del local_client_id[local_client_id.first()]
                if client_id not in local_client_id:
                    #TODO: check
                    local_client_id[client_id] = client_queue(connection_id)
                else:
                    local_client_id[client_id].re_enable(connection_id)
                return local_client_id[client_id].insert(connection_id)

            logging.warn(self.name + ': no inactive client')
            return False
        else:
            return local_client_id[client_id].insert(connection_id)
Example #3
0
class UDPAsyncDNSHandler(object):
    dns_cache = lru_cache.LRUCache(timeout=1800)

    def __init__(self, params):
        self.params = params
        self.remote_addr = None
        self.call_back = None

    def resolve(self, dns_resolver, remote_addr, call_back):
        if remote_addr in UDPAsyncDNSHandler.dns_cache:
            if call_back:
                call_back("", remote_addr,
                          UDPAsyncDNSHandler.dns_cache[remote_addr],
                          self.params)
        else:
            self.call_back = call_back
            self.remote_addr = remote_addr
            dns_resolver.resolve(remote_addr[0], self._handle_dns_resolved)
            UDPAsyncDNSHandler.dns_cache.sweep()

    def _handle_dns_resolved(self, result, error):
        if error:
            logging.error("%s when resolve DNS" % (error, ))  #drop
            return self.call_back(error, self.remote_addr, None, self.params)
        if result:
            ip = result[1]
            if ip:
                return self.call_back("", self.remote_addr, ip, self.params)
        logging.warning("can't resolve %s" % (self.remote_addr, ))
        return self.call_back("fail to resolve", self.remote_addr, None,
                              self.params)
Example #4
0
 def __init__(self):
     self._loop = None
     self._hosts = {}
     self._hostname_status = {}
     self._hostname_to_cb = {}
     self._cb_to_hostname = {}
     self._cache = lru_cache.LRUCache(timeout=300)
     self._sock = None
     self._servers = None
     self._parse_resolv()
     self._parse_hosts()
Example #5
0
 def __init__(self):
     self.client_id = lru_cache.LRUCache()
     self.local_client_id = b''
     self.connection_id = 0
     self.set_max_client(64)  # max active client count
Example #6
0
    def __init__(self,
                 config,
                 dns_resolver,
                 is_local,
                 stat_callback=None,
                 stat_counter=None):
        self._sock_close = list()
        self._config = config
        if config.get('connect_verbose_info', 0) > 0:
            common.connect_log = logging.info
        if is_local:
            self._listen_addr = config['local_address']
            self._listen_port = config['local_port']
            self._remote_addr = config['server']
            self._remote_port = config['server_port']
        else:
            self._listen_addr = config['server']
            self._listen_port = config['server_port']
            self._remote_addr = None
            self._remote_port = None
        self._dns_resolver = dns_resolver
        self._password = common.to_bytes(config['password'])
        self._method = config['method']
        self._timeout = config['timeout']
        self._is_local = is_local
        self._udp_cache_size = config['udp_cache']
        self._cache = lru_cache.LRUCache(
            timeout=config['udp_timeout'],
            close_callback=self._close_client_pair)
        self._cache_dns_client = lru_cache.LRUCache(
            timeout=10, close_callback=self._close_client_pair)
        self._client_fd_to_server_addr = {}
        #self._dns_cache = lru_cache.LRUCache(timeout=1800)
        self._eventloop = None
        self._closed = False
        self.server_transfer_ul = 0
        self.server_transfer_dl = 0
        self.server_users = {}
        self.server_user_transfer_ul = {}
        self.server_user_transfer_dl = {}

        if common.to_bytes(config['protocol']) in obfs.mu_protocol():
            self._update_users(None, None)

        self.protocol_data = obfs.obfs(config['protocol']).init_data()
        self._protocol = obfs.obfs(config['protocol'])
        server_info = obfs.server_info(self.protocol_data)
        server_info.host = self._listen_addr
        server_info.port = self._listen_port
        server_info.users = self.server_users
        server_info.protocol_param = config['protocol_param']
        server_info.obfs_param = ''
        server_info.iv = b''
        server_info.recv_iv = b''
        server_info.key_str = common.to_bytes(config['password'])
        server_info.key = encrypt.encrypt_key(self._password, self._method)
        server_info.head_len = 30
        server_info.tcp_mss = 1452
        server_info.buffer_size = BUF_SIZE
        server_info.overhead = 0
        self._protocol.set_server_info(server_info)

        self._sockets = set()
        self._fd_to_handlers = {}
        self._reqid_to_hd = {}
        self._data_to_write_to_server_socket = []

        self._timeout_cache = lru_cache.LRUCache(
            timeout=self._timeout, close_callback=self._close_tcp_client)

        self._bind = config.get('out_bind', '')
        self._bindv6 = config.get('out_bindv6', '')
        self._ignore_bind_list = config.get('ignore_bind', [])

        if 'forbidden_ip' in config:
            self._forbidden_iplist = config['forbidden_ip']
        else:
            self._forbidden_iplist = None
        if 'forbidden_port' in config:
            self._forbidden_portset = config['forbidden_port']
        else:
            self._forbidden_portset = None

        addrs = socket.getaddrinfo(self._listen_addr, self._listen_port, 0,
                                   socket.SOCK_DGRAM, socket.SOL_UDP)
        if len(addrs) == 0:
            raise Exception("can't get addrinfo for %s:%d" %
                            (self._listen_addr, self._listen_port))
        af, socktype, proto, canonname, sa = addrs[0]
        server_socket = socket.socket(af, socktype, proto)
        server_socket.bind((self._listen_addr, self._listen_port))
        self._sock_close.append(server_socket)
        server_socket.setblocking(False)
        self._server_socket = server_socket
        self._stat_callback = stat_callback
Example #7
0
 def __init__(self):
     self.client_data = lru_cache.LRUCache(60 * 5)
     self.client_id = os.urandom(32)
     self.startup_time = int(time.time() - 60 * 30) & 0xFFFFFFFF
     self.ticket_buf = {}