示例#1
0
def check_xtunnel_http1(ssl_sock, host):
    start_time = time.time()
    request_data = 'GET / HTTP/1.1\r\nHost: %s\r\n\r\n' % host
    ssl_sock.send(request_data.encode())

    response = simple_http_client.Response(ssl_sock)
    response.begin(timeout=5)

    server_type = response.getheader('Server', "")
    xlog.debug("status:%d", response.status)
    xlog.debug("Server type:%s", server_type)
    if response.status == 403:
        xlog.warn("check status:%d", response.status)
        return False

    if response.status != 200:
        xlog.warn("ip:%s status:%d", ssl_sock.ip, response.status)
        return False

    content = response.read(timeout=1)
    if "X_Tunnel OK" not in content:
        xlog.warn("app check content:%s", content)
        return False

    time_cost = (time.time() - start_time) * 1000
    ssl_sock.request_time = time_cost
    xlog.info("check_xtunnel ok, time:%d", time_cost)

    return True
示例#2
0
def check_xtunnel_http1(ssl_sock, host):
    xlog.warn("ip:%s use http/1.1", ssl_sock.ip)
    start_time = time.time()

    try:
        request_data = 'GET / HTTP/1.1\r\nHost: %s\r\n\r\n' % host
        ssl_sock.send(request_data.encode())

        response = simple_http_client.Response(ssl_sock)
        response.begin(timeout=5)

        server_type = response.getheader('Server', "")
        xlog.debug("status:%d", response.status)
        xlog.debug("Server type:%s", server_type)
        if response.status == 403:
            xlog.warn("check status:%d", response.status)
            return ssl_sock

        if response.status != 200:
            xlog.warn("ip:%s status:%d", ssl_sock.ip, response.status)
            return False

        content = response.read(timeout=1)
        if content != "OK":
            xlog.warn("app check content:%s", content)
            return ssl_sock
    except Exception as e:
        xlog.debug("check ip %s http1 e:%r", ssl_sock.ip, e)
        return ssl_sock

    time_cost = (time.time() - start_time) * 1000
    ssl_sock.request_time = time_cost
    xlog.info("check_xtunnel ok, time:%d", time_cost)
    ssl_sock.support_xtunnel = True
    return ssl_sock
示例#3
0
    def check_http1(self, ssl_sock, host):
        self.logger.info("ip:%s use http/1.1", ssl_sock.ip_str)

        try:
            request_data = 'GET %s HTTP/1.1\r\nHost: %s\r\n\r\n' % (self.config.check_ip_path, host)
            ssl_sock.send(request_data.encode())

            response = simple_http_client.Response(ssl_sock)
            response.begin(timeout=5)
            return response
        except Exception as e:
            self.logger.debug("check ip %s http1 e:%r", ssl_sock.ip_str, e)
            return False
示例#4
0
    def head_request(self):
        # for keep alive, not work now.
        self.accept_task = False
        self.request_onway = True
        self.record_active("head")
        start_time = time.time()
        # xlog.debug("head request %s", self.ip)
        request_data = 'GET / HTTP/1.1\r\nHost: %s\r\n\r\n' % self.ssl_sock.host

        try:
            data = request_data.encode()
            ret = self.ssl_sock.send(data)
            if ret != len(data):
                xlog.warn("h1 head send len:%r %d %s", ret, len(data), self.ip)
                xlog.warn('%s trace:%s', self.ip, self.get_trace())
                return False
            response = simple_http_client.Response(self.ssl_sock)
            response.begin(timeout=30)

            status = response.status
            if status != 200:
                xlog.warn("%s host:%s head fail status:%d", self.ip,
                          self.ssl_sock.host, status)
                return False

            content = response.readall(timeout=5)

            time_now = time.time()
            self.record_active("head end")
            self.rtt = (time_now - start_time) * 1000
            self.last_active_time = start_time
            ip_manager.update_ip(self.ip, self.rtt)
            self.accept_task = True
            return True
        except httplib.BadStatusLine as e:
            time_now = time.time()
            inactive_time = time_now - self.last_active_time
            head_timeout = time_now - start_time
            xlog.warn("%s keep alive fail, inactive_time:%d head_timeout:%d",
                      self.ssl_sock.ip, inactive_time, head_timeout)
            xlog.warn('%s trace:%s', self.ip, self.get_trace())
            ip_manager.report_connect_closed(self.ip, "down fail")
            self.close("head fail")
        except Exception as e:
            xlog.warn("h1 %s HEAD keep alive request fail:%r",
                      self.ssl_sock.ip, e)
            xlog.warn('%s trace:%s', self.ip, self.get_trace())
            ip_manager.report_connect_closed(self.ip, "down fail")
            self.close("head fail")
        finally:
            self.request_onway = False
示例#5
0
    def head_request(self):
        if not self.ssl_sock.host:
            # self.logger.warn("try head but no host set")
            return True

        # for keep alive, not work now.
        self.request_onway = True
        self.record_active("head")
        start_time = time.time()
        # self.logger.debug("head request %s", self.ip)
        request_data = b'GET / HTTP/1.1\r\nHost: %s\r\n\r\n' % utils.to_bytes(
            self.ssl_sock.host)

        try:
            data = request_data
            ret = self.ssl_sock.send(data)
            if ret != len(data):
                self.logger.warn("h1 head send len:%r %d %s", ret, len(data),
                                 self.ip_str)
                self.logger.warn('%s trace:%s', self.ip_str, self.get_trace())
                return False
            response = simple_http_client.Response(self.ssl_sock)
            response.begin(timeout=5)

            status = response.status
            if status != 200:
                self.logger.warn("%s host:%s head fail status:%d", self.ip_str,
                                 self.ssl_sock.host, status)
                return False

            content = response.readall(timeout=5)
            self.record_active("head end")
            self.rtt = (time.time() - start_time) * 1000
            #self.ip_manager.update_ip(self.ip, self.rtt)
            return True
        except Exception as e:
            self.logger.warn("h1 %s HEAD keep alive request fail:%r",
                             self.ssl_sock.ip_str, e)
            self.logger.warn('%s trace:%s', self.ip_str, self.get_trace())
            self.close("down fail")
        finally:
            self.request_onway = False
示例#6
0
文件: http1.py 项目: zuodian/XX-Net
    def head_request(self):
        # for keep alive

        start_time = time.time()
        # xlog.debug("head request %s", host)
        request_data = 'GET /_gh/ HTTP/1.1\r\nHost: %s\r\n\r\n' % self.ssl_sock.host

        try:
            data = request_data.encode()
            ret = self.ssl_sock.send(data)
            if ret != len(data):
                xlog.warn("h1 head send len:%r %d %s", ret, len(data), self.ip)
                return False

            response = simple_http_client.Response(self.ssl_sock)
            response.begin(timeout=15)

            status = response.status
            if status != 200:
                xlog.debug("%s appid:%s head fail status:%d", self.ip,
                           self.ssl_sock.appid, status)
                return False

            content = response.readall(timeout=15)
            self.rtt = (time.time() - start_time) * 1000
            self.ssl_sock.last_use_time = start_time
            google_ip.update_ip(self.ip, self.rtt)
            return True
        except httplib.BadStatusLine as e:
            time_now = time.time()
            inactive_time = time_now - self.last_active_time
            head_timeout = time_now - start_time
            xlog.debug("%s keep alive fail, inactive_time:%d head_timeout:%d",
                       self.ssl_sock.ip, inactive_time, head_timeout)
        except Exception as e:
            inactive = time.time() - self.ssl_sock.last_use_time
            xlog.debug(
                "h1 %s appid:%s HEAD keep alive request, inactive time:%d fail:%r",
                self.ssl_sock.ip, self.ssl_sock.appid, inactive, e)
示例#7
0
def test_appid_exist(ssl_sock, appid):
    request_data = 'GET /_gh/ HTTP/1.1\r\nHost: %s.appspot.com\r\n\r\n' % appid
    ssl_sock.send(request_data.encode())
    response = simple_http_client.Response(ssl_sock)

    response.begin()
    if response.status == 404:
        # xlog.warn("app check %s status:%d", appid, response.status)
        return False

    if response.status == 503:
        # out of quota
        return True

    if response.status != 200:
        xlog.warn("test appid %s status:%d", appid, response.status)

    content = response.read()
    if b"GoAgent" not in content:
        # xlog.warn("app check %s content:%s", appid, content)
        return False

    return True
示例#8
0
文件: http1.py 项目: zuodian/XX-Net
    def request_task(self, task):
        task.set_state("h1_req")

        self.ssl_sock.last_use_time = time.time()

        task.headers['Host'] = self.ssl_sock.host
        request_data = 'POST /_gh/ HTTP/1.1\r\n'
        request_data += ''.join('%s: %s\r\n' % (k, v)
                                for k, v in task.headers.items())
        request_data += '\r\n'

        try:
            self.ssl_sock.send(request_data.encode())
            payload_len = len(task.body)
            start = 0
            while start < payload_len:
                send_size = min(payload_len - start, 65535)
                sended = self.ssl_sock.send(task.body[start:start + send_size])
                start += sended

            response = simple_http_client.Response(self.ssl_sock)
            response.begin(timeout=task.timeout)

        except Exception as e:
            xlog.warn("%s h1_request:%r", self.ip, e)
            google_ip.report_connect_closed(self.ssl_sock.ip, "request_fail")
            self.task = task
            self.close("request fail")
            return

        task.set_state("h1_get_head")
        body_length = int(response.getheader("Content-Length", "0"))
        task.content_length = body_length

        task.responsed = True
        response.worker = self
        response.task = task
        response.ssl_sock = self.ssl_sock
        task.queue.put(response)

        if body_length == 0:
            self.accept_task = True
            self.processed_tasks += 1
            return

        # read response body,
        try:
            start = 0
            end = body_length
            time_response = last_read_time = time.time()
            while True:
                if start >= end:
                    self.ssl_sock.received_size += body_length
                    time_cost = (time.time() - time_response)
                    if time_cost != 0:
                        speed = body_length / time_cost
                        task.set_state("h1_finish[SP:%d]" % speed)
                        self.report_speed(speed, body_length)
                        self.transfered_size += body_length
                    task.finish()
                    self.task = None
                    self.accept_task = True
                    self.idle_cb()
                    self.processed_tasks += 1
                    self.last_active_time = time.time()
                    return

                data = response.read()
                # task.set_state("read body:%d" % len(data))
                if not data:
                    if time.time() - last_read_time > 20:
                        xlog.warn("%s read timeout t:%d expect:%d left:%d ",
                                  self.ip,
                                  (time.time() - time_response) * 1000,
                                  body_length, (end - start))
                        break
                    else:
                        time.sleep(0.1)
                        continue

                last_read_time = time.time()
                data_len = len(data)
                start += data_len
                task.put_data(data)

        except Exception as e:
            xlog.warn("%s h1_request:%r", self.ip, e)

        task.finish()
        google_ip.report_connect_closed(self.ssl_sock.ip, "down fail")
        self.close("request body fail")
示例#9
0
    def request_task(self, task):
        timeout = task.timeout
        self.request_onway = True
        start_time = time.time()

        self.record_active("request")
        task.set_state("h1_req")

        task.headers['Host'] = self.task.host
        task.headers["Content-Length"] = len(task.body)
        request_data = '%s %s HTTP/1.1\r\n' % (task.method, task.path)
        request_data += ''.join('%s: %s\r\n' % (k, v)
                                for k, v in task.headers.items())
        request_data += '\r\n'

        try:
            self.ssl_sock.send(request_data.encode())
            payload_len = len(task.body)
            start = 0
            while start < payload_len:
                send_size = min(payload_len - start, 65535)
                sended = self.ssl_sock.send(task.body[start:start + send_size])
                start += sended

            task.set_state("h1_req_sended")

            response = simple_http_client.Response(self.ssl_sock)

            response.begin(timeout=timeout)
            task.set_state("response_begin")

        except Exception as e:
            xlog.exception("%s h1_request:%r inactive_time:%d task.timeout:%d",
                           self.ip, e,
                           time.time() - self.last_active_time, task.timeout)
            xlog.warn('%s trace:%s', self.ip, self.get_trace())

            ip_manager.report_connect_closed(self.ip, "down fail")
            self.retry_task_cb(task)
            self.task = None
            self.close("request fail")
            return

        task.set_state("h1_get_head")

        time_left = timeout - (time.time() - start_time)

        try:
            data = response.readall(timeout=time_left)
        except Exception as e:
            xlog.exception(
                "read fail, ip:%s, chunk:%d url:%s task.timeout:%d e:%r",
                self.ip, response.chunked, task.url, task.timeout, e)
            xlog.warn('%s trace:%s', self.ip, self.get_trace())
            ip_manager.report_connect_closed(self.ip, "down fail")
            self.close("read fail")
            return

        response.worker = self
        response.task = task
        response.ssl_sock = self.ssl_sock

        length = len(data)
        task.content_length = length
        task.put_data(data)
        task.responsed = True
        task.queue.put(response)

        self.ssl_sock.received_size += length
        time_cost = (time.time() - start_time)
        if time_cost != 0:
            speed = length / time_cost
            task.set_state("h1_finish[SP:%d]" % speed)

        self.transfered_size += len(request_data) + length
        self.task = None
        self.accept_task = True
        self.idle_cb()
        self.processed_tasks += 1
        self.last_active_time = time.time()
        self.record_active("Res")
示例#10
0
    def request_task(self, task):
        timeout = task.timeout
        self.request_onway = True
        start_time = time.time()

        self.record_active("request")
        task.set_state("h1_req")

        task.headers[b'Host'] = self.get_host(task.host)

        task.headers[b"Content-Length"] = len(task.body)
        request_data = b'%s %s HTTP/1.1\r\n' % (task.method, task.path)
        request_data += pack_headers(task.headers)
        request_data += b'\r\n'

        try:
            self.ssl_sock.send(request_data)
            payload_len = len(task.body)
            start = 0
            while start < payload_len:
                send_size = min(payload_len - start, 65535)
                sended = self.ssl_sock.send(task.body[start:start + send_size])
                start += sended

            task.set_state("h1_req_sended")

            response = simple_http_client.Response(self.ssl_sock)

            response.begin(timeout=timeout)
            task.set_state("response_begin")

        except Exception as e:
            self.logger.warn(
                "%s h1_request:%r inactive_time:%d task.timeout:%d",
                self.ip_str, e,
                time.time() - self.last_recv_time, task.timeout)
            self.logger.warn('%s trace:%s', self.ip_str, self.get_trace())

            self.retry_task_cb(task)
            self.task = None
            self.close("down fail")
            return

        task.set_state("h1_get_head")

        time_left = timeout - (time.time() - start_time)

        if task.method == b"HEAD" or response.status in [204, 304]:
            response.content_length = 0

        response.ssl_sock = self.ssl_sock
        response.task = task
        response.worker = self
        task.content_length = response.content_length
        task.responsed = True
        task.queue.put(response)

        try:
            read_target = int(response.content_length)
        except:
            read_target = 0

        data_len = 0
        while True:
            try:
                data = response.read(timeout=time_left)
                if not data:
                    break
            except Exception as e:
                self.logger.warn(
                    "read fail, ip:%s, chunk:%d url:%s task.timeout:%d e:%r",
                    self.ip_str, response.chunked, task.url, task.timeout, e)
                self.logger.warn('%s trace:%s', self.ip_str, self.get_trace())
                self.close("down fail")
                return

            task.put_data(data)
            length = len(data)
            data_len += length
            if read_target and data_len >= read_target:
                break

        if read_target > data_len:
            self.logger.warn(
                "read fail, ip:%s, chunk:%d url:%s task.timeout:%d ",
                self.ip_str, response.chunked, task.url, task.timeout)
            self.ip_manager.recheck_ip(self.ssl_sock.ip_str)
            self.close("down fail")

        task.finish()

        self.ssl_sock.received_size += data_len
        time_cost = (time.time() - start_time)
        if time_cost != 0:
            speed = data_len / time_cost
            task.set_state("h1_finish[SP:%d]" % speed)

        self.transfered_size += len(request_data) + data_len
        self.task = None
        self.accept_task = True
        self.idle_cb()
        self.processed_tasks += 1
        self.last_recv_time = time.time()
        self.record_active("Res")