Skip to content

Commit

Permalink
Merge pull request XX-net#1085 from aOJzQT/feature-allow_remote_webcon
Browse files Browse the repository at this point in the history
fix XX-net#1068 && add "allow remote webcon" in setting page
  • Loading branch information
xxnet committed Sep 21, 2015
2 parents 5fb0e5b + 252a72e commit c74fffe
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 16 deletions.
3 changes: 3 additions & 0 deletions launcher/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ def recheck_module_path():

if get(["modules", "launcher", "control_port"], 0) == 0:
set(["modules", "launcher", "control_port"], 8085)
set(["modules", "launcher", "allow_remote_connect"], 0)

#if get(["modules", "gae_proxy", "control_port"], 0) == 0:
# set(["modules", "gae_proxy", "control_port"], 8084)

if get(["modules", "php_proxy", "control_port"], 0) == 0:
set(["modules", "php_proxy", "control_port"], 8083)

Expand Down
5 changes: 3 additions & 2 deletions launcher/gtk_tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import launcher_log

import pygtk

import config
if __name__ == "__main__":
import os, sys
current_path = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -77,7 +77,8 @@ def notify_general(self, msg="msg", title="Title", buttons={}, timeout=3600):
return True

def show_control_web(self, widget=None, data=None):
webbrowser.open_new("http://127.0.0.1:8085/")
host_port = config.get(["modules", "launcher", "control_port"], 8085)
webbrowser.open_new("http://127.0.0.1:%s/" % host_port)

def on_restart_gae_proxy(self, widget=None, data=None):
module_init.stop("gae_proxy")
Expand Down
4 changes: 3 additions & 1 deletion launcher/mac_tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import sys
import config

current_path = os.path.dirname(os.path.abspath(__file__))

Expand Down Expand Up @@ -80,7 +81,8 @@ def windowWillClose_(self, notification):
NSApp.terminate_(self)

def config_(self, notification):
webbrowser.open_new("http://127.0.0.1:8085/")
host_port = config.get(["modules", "launcher", "control_port"], 8085)
webbrowser.open_new("http://127.0.0.1:%s/" % host_port)

#Note: the function name for action can include '_'
# limited by Mac cocoa
Expand Down
4 changes: 3 additions & 1 deletion launcher/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ def http_request(url, method="GET"):
return False

for i in range(20):
if http_request("http://127.0.0.1:8085/quit") == False:
host_port = config.get(["modules", "launcher", "control_port"], 8085)
req_url = "http://127.0.0.1:{port}/quit".format(port=host_port)
if http_request(req_url) == False:
return True
time.sleep(1)
return False
Expand Down
3 changes: 2 additions & 1 deletion launcher/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def main():


if has_desktop and config.get(["modules", "launcher", "popup_webui"], 1) == 1:
webbrowser.open("http://127.0.0.1:8085/")
host_port = config.get(["modules", "launcher", "control_port"], 8085)
webbrowser.open("http://127.0.0.1:%s/" % host_port)

update.start()

Expand Down
58 changes: 48 additions & 10 deletions launcher/web_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def load_module_menus(self):
menu_path = os.path.join(root_path, module, "web_ui", "menu.yaml")
if not os.path.isfile(menu_path):
continue

module_menu = yaml.load(file(menu_path, 'r'))
module_menus[module] = module_menu

Expand Down Expand Up @@ -255,9 +255,10 @@ def req_config_handler(self):
elif check_update == 1:
check_update = "long-term-stable"

data = '{ "check_update": "%s", "popup_webui": %d, "show_systray": %d, "auto_start": %d, "php_enable": %d, "gae_proxy_enable": %d }' %\
data = '{ "check_update": "%s", "popup_webui": %d, "allow_remote_connect": %d, "show_systray": %d, "auto_start": %d, "php_enable": %d, "gae_proxy_enable": %d }' %\
(check_update
, config.get(["modules", "launcher", "popup_webui"], 1)
, config.get(["modules", "launcher", "allow_remote_connect"], 0)
, config.get(["modules", "launcher", "show_systray"], 1)
, config.get(["modules", "launcher", "auto_start"], 0)
, config.get(["modules", "php_proxy", "auto_start"], 0)
Expand All @@ -273,7 +274,7 @@ def req_config_handler(self):

data = '{"res":"success"}'

elif 'popup_webui' in reqs :
elif 'popup_webui' in reqs:
popup_webui = int(reqs['popup_webui'][0])
if popup_webui != 0 and popup_webui != 1:
data = '{"res":"fail, popup_webui:%s"}' % popup_webui
Expand All @@ -282,7 +283,24 @@ def req_config_handler(self):
config.save()

data = '{"res":"success"}'
elif 'show_systray' in reqs :

elif 'allow_remote_connect' in reqs:
allow_remote_connect = int(reqs['allow_remote_connect'][0])
if allow_remote_connect != 0 and allow_remote_connect != 1:
data = '{"res":"fail, allow_remote_connect:%s"}' % allow_remote_connect
else:
config.set(["modules", "launcher", "allow_remote_connect"], allow_remote_connect)
config.save()

data = '{"res":"success"}'

launcher_log.debug("restart web control.")
stop()
time.sleep(1)
start()
launcher_log.debug("launcher web control restarted.")

elif 'show_systray' in reqs:
show_systray = int(reqs['show_systray'][0])
if show_systray != 0 and show_systray != 1:
data = '{"res":"fail, show_systray:%s"}' % show_systray
Expand All @@ -291,7 +309,8 @@ def req_config_handler(self):
config.save()

data = '{"res":"success"}'
elif 'auto_start' in reqs :

elif 'auto_start' in reqs:
auto_start = int(reqs['auto_start'][0])
if auto_start != 0 and auto_start != 1:
data = '{"res":"fail, auto_start:%s"}' % auto_start
Expand Down Expand Up @@ -387,10 +406,20 @@ def req_init_module_handler(self):
def start():
global process, server
# should use config.yaml to bing ip
server = LocalServer(("127.0.0.1", 8085), Http_Handler)
allow_remote = config.get(["modules", "launcher", "allow_remote_connect"], 0)
host_port = config.get(["modules", "launcher", "control_port"], 8085)

if allow_remote:
host_addr = "0.0.0.0"
else:
host_addr = "127.0.0.1"

launcher_log.info("begin to start web control")
server = LocalServer((host_addr, host_port), Http_Handler)
process = threading.Thread(target=server.serve_forever)
process.setDaemon(True)
process.start()
launcher_log.info("launcher web control started.")

def stop():
global process, server
Expand All @@ -416,24 +445,33 @@ def http_request(url, method="GET"):
return False

def confirm_xxnet_exit():
# suppose xxnet is running, try to close it
"""suppose xxnet is running, try to close it
"""
is_xxnet_exit = False
launcher_log.debug("start confirm_xxnet_exit")

for i in range(30):
# gae_proxy(default port:8087)
if http_request("http://127.0.0.1:8087/quit") == False:
launcher_log.debug("good, xxnet:8087 cleared!")
is_xxnet_exit = True
break
else:
launcher_log.debug("<%d>: try to terminate xxnet:8087" % i)
time.sleep(1)


for i in range(30):
if http_request("http://127.0.0.1:8085/quit") == False:
launcher_log.debug("good, xxnet:8085 clear!")
# web_control(default port:8085)
host_port = config.get(["modules", "launcher", "control_port"], 8085)
req_url = "http://127.0.0.1:{port}/quit".format(port=host_port)
if http_request(req_url) == False:
launcher_log.debug("good, xxnet:%s clear!" % host_port)
is_xxnet_exit = True
break
else:
launcher_log.debug("<%d>: try to terminate xxnet:8085" % i)
launcher_log.debug("<%d>: try to terminate xxnet:%s" % (i, host_port))
time.sleep(1)
launcher_log.debug("finished confirm_xxnet_exit")
return is_xxnet_exit
Expand Down
20 changes: 20 additions & 0 deletions launcher/web_ui/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<input id="popup-webui" type="checkbox" data-toggle="switch" />
</div> <!-- .span8 -->
</div> <!-- .row-fluid -->
<div class="row-fluid">
<div class="span4">允许远程连接Web控制端<a href="https://github.com/XX-net/XX-Net/wiki/AllowRemoteConnectToWebControl" target="_blank">(帮助)</a></div> <!-- .span4 -->
<div class="span8">
<input id="allow-remote-connect" type="checkbox" data-toggle="switch" />
</div> <!-- .span8 -->
</div> <!-- .row-fluid -->
<div class="row-fluid">
<div class="span4">显示托盘图标(需要重启XX-Net)</div> <!-- .span4 -->
<div class="span8">
Expand Down Expand Up @@ -136,6 +142,12 @@

$( "#popup-webui").prop('checked', true);
}
if ( result['allow_remote_connect'] != 0 ) {
$( "#allow-remote-connect").parent().removeClass('switch-off');
$( "#allow-remote-connect").parent().addClass('switch-on');

$( "#allow-remote-connect").prop('checked', true);
}
if ( result['show_systray'] != 0 ) {
$( "#show-systray").parent().removeClass('switch-off');
$( "#show-systray").parent().addClass('switch-on');
Expand Down Expand Up @@ -211,6 +223,14 @@
return setConfig(key, value);
});

$('#allow-remote-connect').change(function() {
var isChecked = $(this).is(':checked'),
key = 'allow_remote_connect',
value = isChecked ? 1 : 0;

return setConfig(key, value);
});

$('#gae_proxy-enable').change(function() {
var isChecked = $(this).is(':checked'),
key = 'gae_proxy_enable',
Expand Down
4 changes: 3 additions & 1 deletion launcher/win_tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import module_init
import update
import launcher_log
import config


class Win_tray():
Expand Down Expand Up @@ -99,7 +100,8 @@ def on_disable_proxy(self, widget=None, data=None):
win32_proxy_manager.disable_proxy()

def show_control_web(self, widget=None, data=None):
webbrowser.open("http://127.0.0.1:8085/")
host_port = config.get(["modules", "launcher", "control_port"], 8085)
webbrowser.open("http://127.0.0.1:%s/" % host_port)
ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)

def on_quit(self, widget, data=None):
Expand Down

0 comments on commit c74fffe

Please sign in to comment.