diff --git a/gae_proxy/local/appids_manager.py b/gae_proxy/local/appids_manager.py index 428ea8acc3..03e8198bb3 100644 --- a/gae_proxy/local/appids_manager.py +++ b/gae_proxy/local/appids_manager.py @@ -6,7 +6,8 @@ import time from config import config -from proxy import xlog +from xlog import getLogger +xlog = getLogger("gae_proxy") import check_ip diff --git a/gae_proxy/local/cert_util.py b/gae_proxy/local/cert_util.py index 3bf174b2f7..c6e9f82443 100644 --- a/gae_proxy/local/cert_util.py +++ b/gae_proxy/local/cert_util.py @@ -12,7 +12,8 @@ import threading import subprocess -from proxy import xlog +from xlog import getLogger +xlog = getLogger("gae_proxy") current_path = os.path.dirname(os.path.abspath(__file__)) python_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir, 'python27', '1.0')) diff --git a/gae_proxy/local/check_ip.py b/gae_proxy/local/check_ip.py index b3c9b2dd76..eab2938702 100644 --- a/gae_proxy/local/check_ip.py +++ b/gae_proxy/local/check_ip.py @@ -5,26 +5,42 @@ import httplib import time import socket -import threading import struct -import socks +import binascii current_path = os.path.dirname(os.path.abspath(__file__)) +if __name__ == "__main__": + python_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir, 'python27', '1.0')) + + noarch_lib = os.path.abspath( os.path.join(python_path, 'lib', 'noarch')) + sys.path.append(noarch_lib) + + if sys.platform == "win32": + win32_lib = os.path.abspath( os.path.join(python_path, 'lib', 'win32')) + sys.path.append(win32_lib) + elif sys.platform.startswith("linux"): + linux_lib = os.path.abspath( os.path.join(python_path, 'lib', 'linux')) + sys.path.append(linux_lib) + import OpenSSL SSLError = OpenSSL.SSL.WantReadError - +import socks +import check_local_network from config import config import cert_util from openssl_wrap import SSLConnection -from appids_manager import appid_manager -from proxy import xlog +from xlog import getLogger +xlog = getLogger("gae_proxy") g_cacertfile = os.path.join(current_path, "cacert.pem") -openssl_context = SSLConnection.context_builder(ca_certs=g_cacertfile) # check cacert cost too many cpu, 100 check thread cost 60%. +openssl_context = SSLConnection.context_builder(ca_certs=g_cacertfile) +openssl_context.set_session_id(binascii.b2a_hex(os.urandom(10))) +if hasattr(OpenSSL.SSL, 'SESS_CACHE_BOTH'): + openssl_context.set_session_cache_mode(OpenSSL.SSL.SESS_CACHE_BOTH) max_timeout = 5 @@ -49,137 +65,6 @@ def load_proxy_config(): load_proxy_config() -##################################### -# Checking network ok - -checking_lock = threading.Lock() -checking_num = 0 -network_stat = "unknown" -last_check_time = 0 -continue_fail_count = 0 - - -def report_network_ok(): - global network_stat, last_check_time, continue_fail_count - network_stat = "OK" - last_check_time = time.time() - continue_fail_count = 0 - -def check_worker(): - global checking_lock, checking_num, network_stat, last_check_time - time_now = time.time() - if config.PROXY_ENABLE: - socket.socket = socks.socksocket - xlog.debug("patch socks") - - checking_lock.acquire() - checking_num += 1 - checking_lock.release() - try: - conn = httplib.HTTPSConnection("github.com", 443, timeout=30) - header = {"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36", - "accept":"application/json, text/javascript, */*; q=0.01", - "accept-encoding":"gzip, deflate, sdch", - "accept-language":'en-US,en;q=0.8,ja;q=0.6,zh-CN;q=0.4,zh;q=0.2', - "connection":"keep-alive" - } - conn.request("HEAD", "/", headers=header) - response = conn.getresponse() - if response.status: - last_check_time = time.time() - report_network_ok() - xlog.debug("network is ok, cost:%d ms", 1000*(time.time() - time_now)) - return True - except Exception as e: - xlog.warn("network fail:%r", e) - network_stat = "Fail" - last_check_time = time.time() - return False - finally: - checking_lock.acquire() - checking_num -= 1 - checking_lock.release() - - if config.PROXY_ENABLE: - socket.socket = default_socket - xlog.debug("restore socket") - - -def simple_check_worker(): - global checking_lock, checking_num, network_stat, last_check_time - time_now = time.time() - if config.PROXY_ENABLE: - socket.socket = socks.socksocket - xlog.debug("patch socks") - - checking_lock.acquire() - checking_num += 1 - checking_lock.release() - try: - conn = httplib.HTTPConnection("www.baidu.com", 80, timeout=3) - header = {"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36", - "accept":"application/json, text/javascript, */*; q=0.01", - "accept-encoding":"gzip, deflate, sdch", - "accept-language":'en-US,en;q=0.8,ja;q=0.6,zh-CN;q=0.4,zh;q=0.2', - "connection":"keep-alive" - } - conn.request("HEAD", "/", headers=header) - response = conn.getresponse() - if response.status: - last_check_time = time.time() - report_network_ok() - xlog.debug("network is ok, cost:%d ms", 1000*(time.time() - time_now)) - return True - except Exception as e: - xlog.warn("network fail:%r", e) - network_stat = "Fail" - last_check_time = time.time() - return False - finally: - checking_lock.acquire() - checking_num -= 1 - checking_lock.release() - - if config.PROXY_ENABLE: - socket.socket = default_socket - xlog.debug("restore socket") - -simple_check_worker() - - -def triger_check_network(force=False): - global checking_lock, checking_num, network_stat, last_check_time - time_now = time.time() - if not force: - if checking_num > 0: - return - - if network_stat == "OK": - if time_now - last_check_time < 10: - return - else: - # Fail or unknown - if time_now - last_check_time < 3: - return - - last_check_time = time_now - th = threading.Thread(target=simple_check_worker) - th.start() - - -###################################### -# about ip connect time and handshake time -# handshake time is double of connect time in common case. -# after connect and handshaked, http get time is like connect time -# -# connect time is zero if you use socks proxy. -# -# -# most case, connect time is 300ms - 600ms. -# good case is 60ms -# bad case is 1300ms and more. - - def connect_ssl(ip, port=443, timeout=5, openssl_context=None, check_cert=True): ip_port = (ip, port) @@ -193,6 +78,8 @@ def connect_ssl(ip, port=443, timeout=5, openssl_context=None, check_cert=True): sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # set struct linger{l_onoff=1,l_linger=0} to avoid 10048 socket error sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) + # resize socket recv buffer 8K->32K to improve browser releated application performance + sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 32*1024) sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True) sock.settimeout(timeout) @@ -205,11 +92,10 @@ def connect_ssl(ip, port=443, timeout=5, openssl_context=None, check_cert=True): ssl_sock.do_handshake() time_handshaked = time.time() - #report_network_ok - global network_stat, last_check_time, continue_fail_count - network_stat = "OK" - last_check_time = time_handshaked - continue_fail_count = 0 + # report network ok + check_local_network.network_stat = "OK" + check_local_network.last_check_time = time_handshaked + check_local_network.continue_fail_count = 0 cert = ssl_sock.get_peer_certificate() if not cert: @@ -217,6 +103,8 @@ def connect_ssl(ip, port=443, timeout=5, openssl_context=None, check_cert=True): if check_cert: issuer_commonname = next((v for k, v in cert.get_issuer().get_components() if k == 'CN'), '') + if __name__ == "__main__": + xlog.debug("issued by:%s", issuer_commonname) if not issuer_commonname.startswith('Google'): raise socket.error(' certficate is issued by %r, not Google' % ( issuer_commonname)) @@ -225,7 +113,7 @@ def connect_ssl(ip, port=443, timeout=5, openssl_context=None, check_cert=True): #xlog.debug("conn: %d handshake:%d", connct_time, handshake_time) # sometimes, we want to use raw tcp socket directly(select/epoll), so setattr it to ssl socket. - ssl_sock.sock = sock + ssl_sock._sock = sock ssl_sock.connct_time = connct_time ssl_sock.handshake_time = handshake_time @@ -239,37 +127,47 @@ def get_ssl_cert_domain(ssl_sock): #issuer_commonname = next((v for k, v in cert.get_issuer().get_components() if k == 'CN'), '') ssl_cert = cert_util.SSLCert(cert) - #xlog.info("%s CN:%s", ip, ssl_cert.cn) + if __name__ == "__main__": + xlog.info("%s CN:%s", ip, ssl_cert.cn) ssl_sock.domain = ssl_cert.cn -def check_appid(ssl_sock, appid, ip): +def check_goagent(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 = httplib.HTTPResponse(ssl_sock, buffering=True) response.begin() if response.status == 404: - #xlog.warn("app check %s status:%d", appid, response.status) + if __name__ == "__main__": + xlog.warn("app check %s status:%d", appid, response.status) return False if response.status == 503: # out of quota server_type = response.getheader('Server', "") if "gws" not in server_type and "Google Frontend" not in server_type and "GFE" not in server_type: + if __name__ == "__main__": + xlog.warn("503 but server type:%s", server_type) return False else: + if __name__ == "__main__": + xlog.info("503 server type:%s", server_type) return True if response.status != 200: - #xlog.warn("app check %s ip:%s status:%d", appid, ip, response.status) + if __name__ == "__main__": + xlog.warn("app check %s ip:%s status:%d", appid, ip, response.status) return False content = response.read() if "GoAgent" not in content: - #xlog.warn("app check %s content:%s", appid, content) + if __name__ == "__main__": + xlog.warn("app check %s content:%s", appid, content) return False + if __name__ == "__main__": + xlog.info("check_goagent ok") return True @@ -280,42 +178,25 @@ def test_gae_ip(ip, appid=None): get_ssl_cert_domain(ssl_sock) if not appid: - appid = appid_manager.get_appid() - if appid.startswith("xxnet-"): - appid = "xxnet-check" - if not check_appid(ssl_sock, appid, ip): + appid = "xxnet-1" + if not check_goagent(ssl_sock, appid): return False return ssl_sock - except Exception as e: - #xlog.exception("test_gae_ip %s e:%r",ip, e) + except socket.timeout: + if __name__ == "__main__": + xlog.warn("connect timeout") return False - -#=========================================== - - -def check_ipv6_host(host): - try: - conn = httplib.HTTPConnection(host, 80, timeout=5) - header = {"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36", - "accept":"application/json, text/javascript, */*; q=0.01", - "accept-encoding":"gzip, deflate, sdch", - "accept-language":'en-US,en;q=0.8,ja;q=0.6,zh-CN;q=0.4,zh;q=0.2', - "connection":"keep-alive" - } - conn.request("HEAD", "/", headers=header) - response = conn.getresponse() - if response.status: - return True - else: - return False except Exception as e: + xlog.exception("test_gae_ip %s e:%r",ip, e) return False -def check_ipv6(): - hosts = ["www.6rank.edu.cn", "v6.testmyipv6.com", ] - for host in hosts: - if check_ipv6_host(host): - return True - return False +if __name__ == "__main__": + if len(sys.argv) > 1: + ip = sys.argv[1] + xlog.info("test ip:%s", ip) + res = test_gae_ip(ip) + print res + else: + xlog.info("check_ip ") \ No newline at end of file diff --git a/gae_proxy/local/check_local_network.py b/gae_proxy/local/check_local_network.py new file mode 100644 index 0000000000..5e40b1c3ed --- /dev/null +++ b/gae_proxy/local/check_local_network.py @@ -0,0 +1,202 @@ + +import sys +import os + +import httplib +import time +import socket +import threading + +current_path = os.path.dirname(os.path.abspath(__file__)) + +if __name__ == "__main__": + python_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir, 'python27', '1.0')) + + noarch_lib = os.path.abspath( os.path.join(python_path, 'lib', 'noarch')) + sys.path.append(noarch_lib) + + if sys.platform == "win32": + win32_lib = os.path.abspath( os.path.join(python_path, 'lib', 'win32')) + sys.path.append(win32_lib) + elif sys.platform.startswith("linux"): + linux_lib = os.path.abspath( os.path.join(python_path, 'lib', 'linux')) + sys.path.append(linux_lib) + +import OpenSSL +SSLError = OpenSSL.SSL.WantReadError + +import socks +from config import config + +from xlog import getLogger +xlog = getLogger("gae_proxy") + + +max_timeout = 5 + +default_socket = socket.socket + + +def load_proxy_config(): + global default_socket + if config.PROXY_ENABLE: + + if config.PROXY_TYPE == "HTTP": + proxy_type = socks.HTTP + elif config.PROXY_TYPE == "SOCKS4": + proxy_type = socks.SOCKS4 + elif config.PROXY_TYPE == "SOCKS5": + proxy_type = socks.SOCKS5 + else: + xlog.error("proxy type %s unknown, disable proxy", config.PROXY_TYPE) + raise + + socks.set_default_proxy(proxy_type, config.PROXY_HOST, config.PROXY_PORT, config.PROXY_USER, config.PROXY_PASSWD) +load_proxy_config() + +##################################### +# Checking network ok + +_checking_lock = threading.Lock() +_checking_num = 0 +network_stat = "unknown" +last_check_time = 0 +continue_fail_count = 0 + + +def report_network_ok(): + global network_stat, last_check_time, continue_fail_count + network_stat = "OK" + last_check_time = time.time() + continue_fail_count = 0 + +def _check_worker(): + global _checking_lock, _checking_num, network_stat, last_check_time + time_now = time.time() + if config.PROXY_ENABLE: + socket.socket = socks.socksocket + xlog.debug("patch socks") + + _checking_lock.acquire() + _checking_num += 1 + _checking_lock.release() + try: + conn = httplib.HTTPSConnection("github.com", 443, timeout=30) + header = {"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36", + "accept":"application/json, text/javascript, */*; q=0.01", + "accept-encoding":"gzip, deflate, sdch", + "accept-language":'en-US,en;q=0.8,ja;q=0.6,zh-CN;q=0.4,zh;q=0.2', + "connection":"keep-alive" + } + conn.request("HEAD", "/", headers=header) + response = conn.getresponse() + if response.status: + last_check_time = time.time() + report_network_ok() + xlog.debug("network is ok, cost:%d ms", 1000*(time.time() - time_now)) + return True + except Exception as e: + xlog.warn("network fail:%r", e) + network_stat = "Fail" + last_check_time = time.time() + return False + finally: + _checking_lock.acquire() + _checking_num -= 1 + _checking_lock.release() + + if config.PROXY_ENABLE: + socket.socket = default_socket + xlog.debug("restore socket") + + +def _simple_check_worker(): + global _checking_lock, _checking_num, network_stat, last_check_time + time_now = time.time() + if config.PROXY_ENABLE: + socket.socket = socks.socksocket + xlog.debug("patch socks") + + _checking_lock.acquire() + _checking_num += 1 + _checking_lock.release() + try: + conn = httplib.HTTPConnection("www.baidu.com", 80, timeout=3) + header = {"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36", + "accept":"application/json, text/javascript, */*; q=0.01", + "accept-encoding":"gzip, deflate, sdch", + "accept-language":'en-US,en;q=0.8,ja;q=0.6,zh-CN;q=0.4,zh;q=0.2', + "connection":"keep-alive" + } + conn.request("HEAD", "/", headers=header) + response = conn.getresponse() + if response.status: + last_check_time = time.time() + report_network_ok() + xlog.debug("network is ok, cost:%d ms", 1000*(time.time() - time_now)) + return True + except Exception as e: + xlog.warn("network fail:%r", e) + network_stat = "Fail" + last_check_time = time.time() + return False + finally: + _checking_lock.acquire() + _checking_num -= 1 + _checking_lock.release() + + if config.PROXY_ENABLE: + socket.socket = default_socket + xlog.debug("restore socket") + +_simple_check_worker() + + +def triger_check_network(force=False): + global _checking_lock, _checking_num, network_stat, last_check_time + time_now = time.time() + if not force: + if _checking_num > 0: + return + + if network_stat == "OK": + if time_now - last_check_time < 10: + return + else: + # Fail or unknown + if time_now - last_check_time < 3: + return + + last_check_time = time_now + th = threading.Thread(target=_simple_check_worker) + th.start() + + +#=========================================== + + +def _check_ipv6_host(host): + try: + conn = httplib.HTTPConnection(host, 80, timeout=5) + header = {"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36", + "accept":"application/json, text/javascript, */*; q=0.01", + "accept-encoding":"gzip, deflate, sdch", + "accept-language":'en-US,en;q=0.8,ja;q=0.6,zh-CN;q=0.4,zh;q=0.2', + "connection":"keep-alive" + } + conn.request("HEAD", "/", headers=header) + response = conn.getresponse() + if response.status: + return True + else: + return False + except Exception as e: + return False + + +def check_ipv6(): + hosts = ["www.6rank.edu.cn", "v6.testmyipv6.com", ] + for host in hosts: + if _check_ipv6_host(host): + return True + return False diff --git a/gae_proxy/local/config.py b/gae_proxy/local/config.py index ea47baf870..ea91118098 100644 --- a/gae_proxy/local/config.py +++ b/gae_proxy/local/config.py @@ -7,7 +7,9 @@ import re import io -import logging as xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") diff --git a/gae_proxy/local/connect_control.py b/gae_proxy/local/connect_control.py index daee087a4a..107647a864 100644 --- a/gae_proxy/local/connect_control.py +++ b/gae_proxy/local/connect_control.py @@ -1,11 +1,14 @@ import time import threading -from proxy import xlog import sys from config import config import env_info + +from xlog import getLogger +xlog = getLogger("gae_proxy") + # change to False when exit: system tray exit menu, or Ctrl+C in console # then GoAgent will quit # Every long running thread should check it and exit when False diff --git a/gae_proxy/local/connect_manager.py b/gae_proxy/local/connect_manager.py index 7e4d2ba4a5..545da11631 100644 --- a/gae_proxy/local/connect_manager.py +++ b/gae_proxy/local/connect_manager.py @@ -2,19 +2,19 @@ # coding:utf-8 import os -import errno import binascii import time import socket -import select -import Queue import struct import threading import operator import httplib + import socks -from proxy import xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") current_path = os.path.dirname(os.path.abspath(__file__)) import OpenSSL @@ -239,7 +239,7 @@ def head_request(self, ssl_sock): response = None try: ssl_sock.settimeout(10) - ssl_sock.sock.settimeout(10) + ssl_sock._sock.settimeout(10) data = request_data.encode() ret = ssl_sock.send(data) diff --git a/gae_proxy/local/direct_handler.py b/gae_proxy/local/direct_handler.py index 3481863c82..361e3a0b08 100644 --- a/gae_proxy/local/direct_handler.py +++ b/gae_proxy/local/direct_handler.py @@ -18,7 +18,9 @@ from gae_handler import return_fail_message from google_ip import google_ip from config import config -from proxy import xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") diff --git a/gae_proxy/local/gae_handler.py b/gae_proxy/local/gae_handler.py index ba2b2abf64..a8187941da 100644 --- a/gae_proxy/local/gae_handler.py +++ b/gae_proxy/local/gae_handler.py @@ -17,7 +17,9 @@ import urlparse import threading -from proxy import xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") from connect_manager import https_manager from appids_manager import appid_manager diff --git a/gae_proxy/local/google_ip.py b/gae_proxy/local/google_ip.py index 7ae35f8d1c..c0cf409ee2 100644 --- a/gae_proxy/local/google_ip.py +++ b/gae_proxy/local/google_ip.py @@ -10,14 +10,30 @@ current_path = os.path.dirname(os.path.abspath(__file__)) +import check_local_network import check_ip import google_ip_range -from proxy import xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") + from config import config import connect_control from scan_ip_log import scan_ip_log +###################################### +# about ip connect time and handshake time +# handshake time is double of connect time in common case. +# after connect and handshaked, http get time is like connect time +# +# connect time is zero if you use socks proxy. +# +# +# most case, connect time is 300ms - 600ms. +# good case is 60ms +# bad case is 1300ms and more. + class IpManager(): def __init__(self): @@ -64,7 +80,7 @@ def reset(self): self.load_config() self.load_ip() - #if check_ip.network_stat == "OK" and not config.USE_IPV6: + #if check_local_network.network_stat == "OK" and not config.USE_IPV6: # self.start_scan_all_exist_ip() self.search_more_google_ip() @@ -340,7 +356,7 @@ def update_ip(self, ip, handshake_time): return time_now = time.time() - check_ip.network_stat = "OK" + check_local_network.network_stat = "OK" check_ip.last_check_time = time_now check_ip.continue_fail_count = 0 @@ -398,15 +414,15 @@ def report_connect_fail(self, ip, force_remove=False): self.ip_dict[ip]['links'] -= 1 # ignore if system network is disconnected. - if check_ip.network_stat == "Fail": + if check_local_network.network_stat == "Fail": xlog.debug("report_connect_fail network fail") return - check_ip.continue_fail_count += 1 - if check_ip.continue_fail_count > 10: - check_ip.network_stat = "unknown" - xlog.debug("report_connect_fail continue_fail_count:%d", check_ip.continue_fail_count) - check_ip.triger_check_network() + check_local_network.continue_fail_count += 1 + if check_local_network.continue_fail_count > 10: + check_local_network.network_stat = "unknown" + xlog.debug("report_connect_fail continue_fail_count:%d", check_local_network.continue_fail_count) + check_local_network.triger_check_network() return fail_time = self.ip_dict[ip]["fail_time"] @@ -420,7 +436,7 @@ def report_connect_fail(self, ip, force_remove=False): self.append_ip_history(ip, "fail") self.ip_dict[ip]["fail_time"] = time_now - check_ip.triger_check_network() + check_local_network.triger_check_network() self.to_check_ip_queue.put((ip, time_now + 10)) xlog.debug("report_connect_fail:%s", ip) @@ -461,7 +477,7 @@ def check_ip_process(self): if time_wait > 0: time.sleep(time_wait) - if check_ip.network_stat == "Fail": + if check_local_network.network_stat == "Fail": try: if self.ip_dict[ip]['fail_times']: self.ip_dict[ip]['fail_times'] = 0 diff --git a/gae_proxy/local/google_ip_range.py b/gae_proxy/local/google_ip_range.py index 3a41b5051f..15db8a9d54 100644 --- a/gae_proxy/local/google_ip_range.py +++ b/gae_proxy/local/google_ip_range.py @@ -2,13 +2,13 @@ # -*- coding: utf-8 -*- # Based on checkgoogleip by import random -import bisect import time import os -import shutil import ip_utils from config import config -from proxy import xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") random.seed(time.time()* 1000000) @@ -104,7 +104,7 @@ def get_ip(self): return ip = ip_range[0] + id_2 - add_last_byte = ip % 255 + add_last_byte = ip % 256 if add_last_byte == 0 or add_last_byte == 255: continue diff --git a/gae_proxy/local/openssl_wrap.py b/gae_proxy/local/openssl_wrap.py index 77ec9180e1..d1abc67d2e 100644 --- a/gae_proxy/local/openssl_wrap.py +++ b/gae_proxy/local/openssl_wrap.py @@ -10,7 +10,9 @@ import OpenSSL SSLError = OpenSSL.SSL.WantReadError -from proxy import xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") ssl_version = '' @@ -19,16 +21,16 @@ class SSLConnection(object): def __init__(self, context, sock, ip=None, on_close=None): self._context = context - self.sock = sock + self._sock = sock self.ip = ip self._connection = OpenSSL.SSL.Connection(context, sock) self._makefile_refs = 0 self.on_close = on_close def __del__(self): - if self.sock: - socket.socket.close(self.sock) - self.sock = None + if self._sock: + socket.socket.close(self._sock) + self._sock = None if self.on_close: self.on_close(self.ip) @@ -37,8 +39,8 @@ def __getattr__(self, attr): return getattr(self._connection, attr) def __iowait(self, io_func, *args, **kwargs): - timeout = self.sock.gettimeout() or 0.1 - fd = self.sock.fileno() + timeout = self._sock.gettimeout() or 0.1 + fd = self._sock.fileno() time_start = time.time() while True: try: @@ -61,7 +63,7 @@ def __iowait(self, io_func, *args, **kwargs): break def accept(self): - sock, addr = self.sock.accept() + sock, addr = self._sock.accept() client = OpenSSL.SSL.Connection(sock._context, sock) return client, addr @@ -110,9 +112,9 @@ def write(self, buf, flags=0): def close(self): if self._makefile_refs < 1: self._connection = None - if self.sock: - socket.socket.close(self.sock) - self.sock = None + if self._sock: + socket.socket.close(self._sock) + self._sock = None if self.on_close: self.on_close(self.ip) else: diff --git a/gae_proxy/local/pac_server.py b/gae_proxy/local/pac_server.py index f15df6ed9f..8670beaa33 100644 --- a/gae_proxy/local/pac_server.py +++ b/gae_proxy/local/pac_server.py @@ -11,7 +11,10 @@ import simple_http_server -from proxy import xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") + from config import config default_pacfile = os.path.join(os.path.dirname(os.path.abspath(__file__)), config.PAC_FILE) diff --git a/gae_proxy/local/proxy.py b/gae_proxy/local/proxy.py index 5ce3d57bc4..696f16973e 100644 --- a/gae_proxy/local/proxy.py +++ b/gae_proxy/local/proxy.py @@ -84,12 +84,13 @@ def create_data_path(): from config import config -from xlog import Logger + +from xlog import getLogger +xlog = getLogger("gae_proxy") +xlog.set_buffer(500) if config.log_file: log_file = os.path.join(data_gae_proxy_path, "local.log") -else: - log_file = None -xlog = Logger(buffer_size=500, file_name=log_file) + xlog.set_file(log_file) from cert_util import CertUtil import pac_server diff --git a/gae_proxy/local/proxy_handler.py b/gae_proxy/local/proxy_handler.py index f3ab91b71e..fd6f500c45 100644 --- a/gae_proxy/local/proxy_handler.py +++ b/gae_proxy/local/proxy_handler.py @@ -11,7 +11,8 @@ NetWorkIOError = (socket.error, ssl.SSLError, OpenSSL.SSL.Error, OSError) -from proxy import xlog +from xlog import getLogger +xlog = getLogger("gae_proxy") import simple_http_client import simple_http_server from cert_util import CertUtil diff --git a/gae_proxy/local/scan_ip_log.py b/gae_proxy/local/scan_ip_log.py index 9985a29451..12142415d0 100644 --- a/gae_proxy/local/scan_ip_log.py +++ b/gae_proxy/local/scan_ip_log.py @@ -3,7 +3,9 @@ import shutil import time -from proxy import xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") from config import config class Scan_ip_log(): diff --git a/gae_proxy/local/test_appid.py b/gae_proxy/local/test_appid.py index 6cec1d4357..bafbc421a7 100644 --- a/gae_proxy/local/test_appid.py +++ b/gae_proxy/local/test_appid.py @@ -1,5 +1,7 @@ import httplib -from proxy import xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") from connect_manager import https_manager diff --git a/gae_proxy/local/web_control.py b/gae_proxy/local/web_control.py index 2942a2a008..96076836ea 100644 --- a/gae_proxy/local/web_control.py +++ b/gae_proxy/local/web_control.py @@ -17,7 +17,9 @@ import time -from proxy import xlog + +from xlog import getLogger +xlog = getLogger("gae_proxy") from config import config from appids_manager import appid_manager from google_ip import google_ip @@ -27,6 +29,7 @@ import ConfigParser import connect_control import ip_utils +import check_local_network import check_ip import cert_util import simple_http_server @@ -421,7 +424,7 @@ def req_status_handler(self): "out_of_quota_appids": "|".join(appid_manager.out_of_quota_appids), "not_exist_appids": "|".join(appid_manager.not_exist_appids), - "network_state": check_ip.network_stat, + "network_state": check_local_network.network_stat, "ip_num": len(google_ip.gws_ip_list), "good_ip_num": good_ip_num, "connected_link_new": len(https_manager.new_conn_pool.pool), @@ -472,7 +475,7 @@ def req_config_handler(self): use_ipv6 = int(self.postvars['use_ipv6'][0]) if user_config.user_special.use_ipv6 != use_ipv6: if use_ipv6: - if not check_ip.check_ipv6(): + if not check_local_network.check_ipv6(): xlog.warn("Enable Ipv6 but check failed.") return self.send_response('text/html', '{"res":"fail", "reason":"IPv6 fail"}') diff --git a/launcher/web_ui/config.html b/launcher/web_ui/config.html index 63f44a62fa..8c89adbeb3 100644 --- a/launcher/web_ui/config.html +++ b/launcher/web_ui/config.html @@ -517,10 +517,10 @@ } var data = rst[v]; if ( data.status === 'downloading'){ - var progress = +data.downloaded / +data.size; + var progress = (+data.downloaded / +data.size) * 100 ; - window.loading.setProgress(parseInt(progress * 100)); - window.updating_button.html('{{ _( "Downloading ..." ) }}' + parseInt(progress * 100) + '%'); + window.loading.setProgress(progress); + window.updating_button.html('{{ _( "Downloading ..." ) }}' + parseInt(progress) + '%'); }else if (data.status === "finished"){ window.updating_button.html( '{{ _( "Download completed" ) }}'); window.updating_button.prop('disabled', true); diff --git a/python27/1.0/lib/noarch/xlog.py b/python27/1.0/lib/noarch/xlog.py index 56602199eb..88bfc9d015 100644 --- a/python27/1.0/lib/noarch/xlog.py +++ b/python27/1.0/lib/noarch/xlog.py @@ -29,9 +29,12 @@ def __init__(self, buffer_size=0, file_name=None, roll_num=1): self.min_level = NOTSET self.log_fd = None self.set_color() + self.roll_num = roll_num if file_name: - self.roll_num = roll_num - self.log_to_file(file_name) + self.set_file(file_name) + + def set_buffer(self, buffer_size): + self.buffer_size = buffer_size def setLevel(self, level): if level == "DEBUG": @@ -73,7 +76,7 @@ def set_color(self): self.set_console_color = lambda color: sys.stderr.write(color) - def log_to_file(self, file_name): + def set_file(self, file_name): self.log_filename = file_name if os.path.isfile(file_name) and os.path.getsize(file_name) > 1024 * 1024: self.roll_log() @@ -209,3 +212,22 @@ def unicode_line(self, line): print("line can't decode:%s" % line) print("Except stack:%s" % traceback.format_exc()) return "" + + +loggerDict = {} + + +def getLogger(name=None, buffer_size=0, file_name=None, roll_num=1): + global loggerDict + + if not isinstance(name, basestring): + raise TypeError('A logger name must be string or Unicode') + if isinstance(name, unicode): + name = name.encode('utf-8') + + if name in loggerDict: + return loggerDict[name] + else: + logger_instance = Logger(buffer_size, file_name, roll_num) + loggerDict[name] = logger_instance + return logger_instance \ No newline at end of file