Skip to content

Commit

Permalink
fix bugs, rename start script
Browse files Browse the repository at this point in the history
  • Loading branch information
xxnet committed Apr 28, 2016
1 parent f23314d commit bbfd632
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 46 deletions.
12 changes: 10 additions & 2 deletions code/default/gae_proxy/local/check_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ def test_gae_ip2(ip, appid="xxnet-1", use_openssl=True):
return False

if not hasattr(ssl_sock._connection, "protos"):
#xlog.warn("ip:%s not support http/2", ip)
if __name__ == "__main__":
xlog.warn("ip:%s not support http/2", ip)

try:
if not check_goagent(ssl_sock, appid):
return False
Expand All @@ -230,7 +232,13 @@ def test_gae_ip2(ip, appid="xxnet-1", use_openssl=True):
#xlog.exception("gae %r", e)
xlog.debug("ip:%s http/1.1:%r", ip, e )
return False
response = conn.get_response()
try:
response = conn.get_response()
except Exception as e:
if __name__ == "__main__":
xlog.exception("http2 get response fail:%r", e)
return False

xlog.debug("ip:%s http/2", ip)

if response.status == 404:
Expand Down
8 changes: 6 additions & 2 deletions code/default/gae_proxy/local/connect_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,11 @@ def connect_process(self):
ssl_sock = self._create_ssl_connection( (ip_str, port) )
if ssl_sock:
ssl_sock.last_use_time = time.time()
self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock))

if self.new_conn_pool.qsize() >= self.connection_pool_max_num and self.ssl_timeout_cb:
self.ssl_timeout_cb(ssl_sock)
else:
self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock))
finally:
self.thread_num_lock.acquire()
self.thread_num -= 1
Expand Down Expand Up @@ -375,7 +379,7 @@ def connect_thread(self, sleep_time=0):
ssl_sock = self._create_ssl_connection( (ip_str, port) )
if ssl_sock:
ssl_sock.last_use_time = time.time()
if self.new_conn_pool.qsize() > self.connection_pool_max_num and self.ssl_timeout_cb:
if self.new_conn_pool.qsize() >= self.connection_pool_max_num and self.ssl_timeout_cb:
self.ssl_timeout_cb(ssl_sock)
else:
self.new_conn_pool.put((ssl_sock.handshake_time, ssl_sock))
Expand Down
10 changes: 6 additions & 4 deletions code/default/gae_proxy/local/http1.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ def work_loop(self):
last_ssl_active_time = self.ssl_sock.create_time
last_request_time = time.time()
while connect_control.keep_running and self.keep_running:
time_to_ping = min(0, 50 - (time.time() - last_ssl_active_time))
time_to_ping = min(0, 55 - (time.time() - last_ssl_active_time))
try:
task = self.task_queue.get(True, timeout=time_to_ping)
if not task:
# None task to exit
return
except:
if time.time() - last_request_time > 4 * 60:
self.close("idle 4 mins")
if time.time() - last_request_time > 2 * 60:
self.close("idle 2 mins")
return

last_ssl_active_time = time.time()
if not self.head_request():
self.close("keep alive")
google_ip.report_connect_fail(self.ssl_sock.ip, force_remove=True)
# now many gvs don't support gae
self.close("keep alive, maybe not support")
return
else:
continue
Expand Down
5 changes: 4 additions & 1 deletion code/default/gae_proxy/local/http2_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ def send_loop(self):
except socket.error as e:
if e.errno not in (errno.EPIPE, errno.ECONNRESET):
xlog.warn("%s http2 send fail:%r", self.ip, e)
self.close("send fail")
else:
xlog.exceptiong("send error:%r", e)

self.close("send fail:%r", e)

def recv_loop(self):
while connect_control.keep_running and self.keep_running:
Expand Down
20 changes: 20 additions & 0 deletions code/default/gae_proxy/local/http_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,39 @@ def on_ssl_created_cb(self, ssl_sock):
self.workers.append(worker)
return worker

def create_worker_thread(self):
ssl_sock = https_manager.get_ssl_connection()
if not ssl_sock:
xlog.warn("create_worker_thread get ssl_sock fail")

self.on_ssl_created_cb(ssl_sock)

def create_more_worker(self):
threading.Thread(target=self.create_worker_thread).start()

def get_worker(self):
best_rtt = 9999
best_worker = None
idle_num = 0
for worker in self.workers:
if not worker.accept_task:
continue

if worker.version == "1.1":
idle_num += 1
else:
if len(worker.streams) == 0:
idle_num += 1

rtt = worker.get_rtt_rate()

if rtt < best_rtt:
best_rtt = rtt
best_worker = worker

if idle_num == 0:
self.create_more_worker()

if best_worker:
return best_worker

Expand Down
40 changes: 25 additions & 15 deletions code/default/gae_proxy/local/openssl_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __getattr__(self, attr):
if attr not in ('_context', '_sock', '_connection', '_makefile_refs'):
return getattr(self._connection, attr)

def __iowait(self, io_func, *args, **kwargs):
def __iowait2(self, io_func, *args, **kwargs):
timeout = self._sock.gettimeout() or 0.1
fd = self._sock.fileno()
time_start = time.time()
Expand All @@ -78,6 +78,30 @@ def __iowait(self, io_func, *args, **kwargs):
#xlog.exception("e:%r", e)
raise e

def __iowait(self, io_func, *args, **kwargs):
timeout = self._sock.gettimeout() or 0.1
fd = self._sock.fileno()
time_start = time.time()
while True:
try:
return io_func(*args, **kwargs)
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantX509LookupError):
sys.exc_clear()
_, _, errors = select.select([fd], [], [fd], timeout)
if errors:
break
time_now = time.time()
if time_now - time_start > timeout:
break
except OpenSSL.SSL.WantWriteError:
sys.exc_clear()
_, _, errors = select.select([], [fd], [fd], timeout)
if errors:
break
time_now = time.time()
if time_now - time_start > timeout:
break

def accept(self):
sock, addr = self._sock.accept()
client = OpenSSL.SSL.Connection(sock._context, sock)
Expand Down Expand Up @@ -221,20 +245,6 @@ def context_builder(ca_certs=None, cipher_suites=None):
#
if not cipher_suites:
cipher_suites = ('ALL:!RC4-SHA:!ECDHE-RSA-RC4-SHA:!ECDHE-RSA-AES128-GCM-SHA256:!AES128-GCM-SHA256',)
"""
cipher_suites = (
"DHE-RSA-AES256-SHA256",
"DHE-RSA-AES256-SHA",
"DHE-RSA-AES128-SHA",
"ECDHE-ECDSA-AES256-SHA",
"ECDHE-ECDSA-AES128-SHA",
"ECDHE-RSA-AES128-SHA",
"ECDHE-ECDSA-RC4-SHA",
"AES256-SHA",
"AES128-SHA",
"RC4-MD5",
"DES-CBC3-SHA"
)"""

protocol_version = getattr(OpenSSL.SSL, '%s_METHOD' % ssl_version)
ssl_context = OpenSSL.SSL.Context(protocol_version)
Expand Down
4 changes: 2 additions & 2 deletions code/default/gae_proxy/local/proxy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ record_ip_history = 0

[connect_manager]
https_max_connect_thread = 20
https_new_connect_num = 10
https_new_connect_num = 1

;0 means don't keep new ssl link
https_connection_pool_min = 0

; if exceed max, put ssl to worker.
https_connection_pool_max = 5
https_connection_pool_max = 3

; keep connection pool until no active request timeout
keep_active_timeout = 600
Expand Down
9 changes: 4 additions & 5 deletions code/default/launcher/autorun.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

current_path = os.path.dirname(os.path.abspath(__file__))
root_path = os.path.abspath( os.path.join(current_path, os.pardir))
top_path = os.path.abspath( os.path.join(root_path, os.pardir, os.pardir))

if sys.platform == 'win32':
import _winreg
Expand Down Expand Up @@ -44,8 +45,7 @@ def remove(name):
_winreg.DeleteValue(key, name)
_winreg.CloseKey(key)

run_cmd = "\"" + os.path.abspath( os.path.join(root_path, "python27", "1.0", "pythonw.exe")) + "\" \"" +\
os.path.abspath( os.path.join(root_path, "launcher", "start.py")) + "\""
run_cmd = "\"" + os.path.join(top_path, "start.vbs") + "\""
elif sys.platform.startswith('linux'):
_xdg_config_home = os.environ.get("XDG_CONFIG_HOME", "~/.config")
home_config_path = os.path.expanduser(_xdg_config_home)
Expand Down Expand Up @@ -86,7 +86,7 @@ def remove(name):
if(exists(name)):
os.unlink(getfilename(name))

run_cmd = os.path.abspath( os.path.join(root_path, os.pardir, os.pardir, "xxnet"))
run_cmd = os.path.join(top_path, "start")
elif sys.platform == 'darwin':
plist_template = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Expand All @@ -100,7 +100,6 @@ def remove(name):
<key>ProgramArguments</key>
<array>
<string>/usr/bin/python2.7</string>
<string>%s</string>
</array>
Expand All @@ -114,7 +113,7 @@ def remove(name):
</dict>
</plist>"""

run_cmd = os.path.abspath( os.path.join(root_path, "launcher", "start.py"))
run_cmd = os.path.join(top_path, "start")
from os.path import expanduser
home = expanduser("~")
launch_path = os.path.join(home, "Library/LaunchAgents")
Expand Down
4 changes: 2 additions & 2 deletions code/default/launcher/create_shortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
function CreateShortcut()
{
wsh = new ActiveXObject('WScript.Shell');
target_path = '"' + wsh.CurrentDirectory + '\\..\\python27\\1.0\\pythonw.exe"';
target_path = '"' + wsh.CurrentDirectory + '\\..\\..\\..\\start.vbs"';
icon_path = wsh.CurrentDirectory + '\\web_ui\\favicon.ico';


link = wsh.CreateShortcut(wsh.SpecialFolders("Desktop") + '\\XX-Net.lnk');
link.TargetPath = target_path;
link.Arguments = '"' + wsh.CurrentDirectory + '\\start.py"';
link.Arguments = '"'"';
link.WindowStyle = 7;
link.IconLocation = icon_path;
link.Description = 'XX-Net';
Expand Down
2 changes: 1 addition & 1 deletion code/default/launcher/post_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def run(last_run_version):
if sys.platform != 'win32' and filename == 'xxnet':
st = os.stat(filepath)
os.chmod(filepath, st.st_mode | stat.S_IEXEC)
if not filename.startswith('.') and filename not in ['README.md', 'xxnet', 'xxnet.bat', 'xxnet.vbs']:
if not filename.startswith('.') and filename not in ['README.md', 'start', 'start.bat', 'start.vbs']:
os.remove(filepath)
else:
if not filename.startswith('.') and filename not in ['code', 'data']:
Expand Down
4 changes: 2 additions & 2 deletions code/default/launcher/update_from_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ def restart_xxnet(version):
start_script = os.path.join(top_path, "code", version, "launcher", "start.py")

subprocess.Popen([sys.executable, start_script])
time.sleep(10)
os._exit(0)
time.sleep(20)
#os._exit(0)


def update_version(version):
Expand Down
14 changes: 8 additions & 6 deletions code/default/python27/1.0/lib/noarch/hyper/common/bufsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,20 @@ def send(self, buf, flush=True):
self.send_buffer.append(buf)

if len(self.send_buffer) > 1300:
data = self.send_buffer.get_string()
self.send_buffer.reset()
return self._sck.send(data)
self.flush()

def flush(self):
if len(self.send_buffer):
data = self.send_buffer.get_string()
logger.debug("buffer socket flush:%d", len(data))
self.send_buffer.reset()
sended = self._sck.send(data)
if sended != len(data):
raise Exception("send fail")

data_len = len(data)
start = 0
while start < data_len:
send_size = min(data_len - start, 65535)
sended = self._sck.send(data[start:start+send_size])
start += sended

@property
def _remaining_capacity(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def __init__(self, stream_id, pad_length=0, **kwargs):

self.pad_length = pad_length


def serialize_padding_data(self):
if 'PADDED' in self.flags:
return struct.pack('!B', self.pad_length)
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions start.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SET PYTHONPATH=
"%~dp0%start.vbs" console

File renamed without changes.
3 changes: 0 additions & 3 deletions xxnet.bat

This file was deleted.

0 comments on commit bbfd632

Please sign in to comment.