Exemplo n.º 1
0
    def __init__(self,
                 family=socket.AF_INET,
                 type=socket.SOCK_STREAM,
                 proto=0,
                 _sock=None):
        if type not in {socket.SOCK_STREAM, socket.SOCK_DGRAM}:
            msg = "Socket type must be stream or datagram, not {!r}"
            raise ValueError(msg.format(type))

        self._proxyconn = None  # TCP connection to keep UDP relay alive
        self.resolve_dest = True

        if self.default_proxy:
            self.proxy = self.default_proxy
            proxy_host = self.proxy[1]
            if utils.check_ip_valid6(proxy_host):
                family = socket.AF_INET6
            elif utils.check_ip_valid4(proxy_host):
                family = socket.AF_INET

        else:
            self.proxy = (None, None, None, None, None, None)

        _BaseSocket.__init__(self, family, type, proto, _sock)

        self.proxy_sockname = None
        self.proxy_peername = None
Exemplo n.º 2
0
    def _write_SOCKS5_address(self, addr, file):
        """
        Return the host and port packed for the SOCKS5 protocol,
        and the resolved address as a tuple object.
        """
        host, port = addr
        host = utils.to_str(host)
        proxy_type, _, _, rdns, username, password = self.proxy

        if utils.check_ip_valid6(host):
            addr_bytes = inet_pton(socket.AF_INET6, host)
            file.write(b"\x04" + addr_bytes)
        elif utils.check_ip_valid4(host):
            addr_bytes = socket.inet_aton(host)
            file.write(b"\x01" + addr_bytes)
        else:
            if rdns:
                # Resolve remotely
                host_bytes = host.encode("idna")
                file.write(b"\x03" + chr(len(host_bytes)).encode() +
                           host_bytes)
            else:
                # Resolve locally
                addr_bytes = socket.inet_aton(socket.gethostbyname(host))
                file.write(b"\x01" + addr_bytes)
                host = socket.inet_ntoa(addr_bytes)

        file.write(struct.pack(">H", port))
        return host, port
Exemplo n.º 3
0
    def on_udp_query(self, rsock, req_data, addr):
        start_time = time.time()
        try:
            request = DNSRecord.parse(req_data)
            if len(request.questions) != 1:
                xlog.warn("query num:%d %s", len(request.questions), request)
                return

            domain = utils.to_bytes(str(request.questions[0].qname))

            if domain.endswith(b"."):
                domain = domain[:-1]

            type = request.questions[0].qtype
            xlog.debug("DNS query:%s type:%d from %s", domain, type, addr)

            ips = g.dns_query.query(domain, type)
            if not ips:
                xlog.debug("query:%s type:%d from:%s, get fail, cost:%d",
                           domain, type, addr,
                           (time.time() - start_time) * 1000)

            reply = DNSRecord(DNSHeader(id=request.header.id,
                                        qr=1,
                                        aa=1,
                                        ra=1,
                                        auth=1),
                              q=request.q)
            ips = utils.to_bytes(ips)
            for ip_cn in ips:
                ipcn_p = ip_cn.split(b"|")
                ip = ipcn_p[0]
                if utils.check_ip_valid4(ip) and type == 1:
                    reply.add_answer(RR(domain, ttl=60, rdata=A(ip)))
                elif utils.check_ip_valid6(ip) and type == 28:
                    reply.add_answer(
                        RR(domain, rtype=type, ttl=60, rdata=AAAA(ip)))
            res_data = reply.pack()

            rsock.sendto(res_data, addr)
            xlog.debug("query:%s type:%d from:%s, return ip num:%d cost:%d",
                       domain, type, addr, len(reply.rr),
                       (time.time() - start_time) * 1000)
        except Exception as e:
            xlog.exception("on_query except:%r", e)