serialize status
This commit is contained in:
parent
6a4eeb807b
commit
28d1efaebd
@ -20,7 +20,7 @@ Todos
|
|||||||
- [ ] documentations
|
- [ ] documentations
|
||||||
|
|
||||||
2020/07/25 Sat
|
2020/07/25 Sat
|
||||||
- [ ] frontend ui design
|
- [ ] frontend ui design: ajax
|
||||||
- [ ] configuration defination
|
- [x] configuration defination
|
||||||
- [ ] configuration save and load
|
- [ ] configuration save and load
|
||||||
- [ ] charts: mpld3
|
- [ ] charts: mpld3
|
||||||
41
o_manager.py
41
o_manager.py
@ -35,18 +35,13 @@ class OManager:
|
|||||||
else:
|
else:
|
||||||
self.loop = asyncio.get_event_loop()
|
self.loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
def new_op(self, cfg_fp, name=None, additional_cfg={}):
|
def new_op(self, ovpn_config):
|
||||||
folder_path = os.path.join(self.base_folder, f"session{self.new_idx}")
|
folder_path = os.path.join(self.base_folder, f"session{self.new_idx}")
|
||||||
if not os.path.isdir(folder_path):
|
if not os.path.isdir(folder_path):
|
||||||
os.makedirs(folder_path)
|
os.makedirs(folder_path)
|
||||||
if not name:
|
if not name:
|
||||||
name = f"openvpn-{self.new_idx}"
|
name = f"openvpn-{self.new_idx}"
|
||||||
os.system(f"groupadd vpn{self.new_idx}")
|
os.system(f"groupadd vpn{self.new_idx}")
|
||||||
ovpn_config = {
|
|
||||||
"cfg_fp": cfg_fp,
|
|
||||||
"name": name,
|
|
||||||
"additional_cfg": additional_cfg,
|
|
||||||
}
|
|
||||||
env_config = {
|
env_config = {
|
||||||
"folder_path": folder_path,
|
"folder_path": folder_path,
|
||||||
"script_template_fp": "script.sh.template",
|
"script_template_fp": "script.sh.template",
|
||||||
@ -62,6 +57,32 @@ class OManager:
|
|||||||
self.new_idx += 1
|
self.new_idx += 1
|
||||||
return op
|
return op
|
||||||
|
|
||||||
|
def new_op_old(self, cfg_fp, name=None, additional_cfg={}):
|
||||||
|
ovpn_config = {
|
||||||
|
"cfg_fp": cfg_fp,
|
||||||
|
"name": name,
|
||||||
|
"additional_cfg": additional_cfg,
|
||||||
|
}
|
||||||
|
return self.new_op(ovpn_config)
|
||||||
|
|
||||||
|
def serialize(self):
|
||||||
|
instances = [
|
||||||
|
{
|
||||||
|
"idx": i["idx"],
|
||||||
|
"name": i["op"].name,
|
||||||
|
"weight": i["weight"],
|
||||||
|
"pids": i["op"].pids,
|
||||||
|
"log": i["op"].get_log(lines=10),
|
||||||
|
"io_stat": i["op"].get_io_stat(lines=5),
|
||||||
|
"ping_stat": i["op"].get_ping_stat(lines=5)
|
||||||
|
} for i in self.instances
|
||||||
|
]
|
||||||
|
state = {
|
||||||
|
"instances": instances,
|
||||||
|
"lb_pids": self.pids
|
||||||
|
}
|
||||||
|
return state
|
||||||
|
|
||||||
def generate_lb_cfg(self, load_balance_mode):
|
def generate_lb_cfg(self, load_balance_mode):
|
||||||
lb_cfg_fp = os.path.join(
|
lb_cfg_fp = os.path.join(
|
||||||
self.base_folder, f"go-socks-lb-{load_balance_mode}.yml")
|
self.base_folder, f"go-socks-lb-{load_balance_mode}.yml")
|
||||||
@ -247,11 +268,3 @@ class OManager:
|
|||||||
if stderr:
|
if stderr:
|
||||||
print(f'[stderr]\n{stderr.decode()}')
|
print(f'[stderr]\n{stderr.decode()}')
|
||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
folder_fp = "/home/mantao/Desktop/t/"
|
|
||||||
cfg_fp = "/home/mantao/Desktop/t/TCP_Files/UK2-TCP.ovpn"
|
|
||||||
om = OManager(folder_fp, "eth0")
|
|
||||||
om.new_op(cfg_fp, "op1", {
|
|
||||||
"auth-user-pass": "/home/mantao/Desktop/t/fast.txt"})
|
|
||||||
|
|||||||
12
openvpn.py
12
openvpn.py
@ -96,6 +96,18 @@ class Openvpn:
|
|||||||
if enabled:
|
if enabled:
|
||||||
self.start()
|
self.start()
|
||||||
|
|
||||||
|
def export_cfg(self):
|
||||||
|
ovpn_config = {
|
||||||
|
"cfg_fp": self.cfg_fp,
|
||||||
|
"name": self.name,
|
||||||
|
"additional_cfg": self.additional_cfg,
|
||||||
|
"enabled": self.status is RUNNING,
|
||||||
|
"ping_timeout": self.ping_timeout,
|
||||||
|
"n_up": self.n_up,
|
||||||
|
"n_down": self.n_down
|
||||||
|
}
|
||||||
|
return ovpn_config
|
||||||
|
|
||||||
def generate_script(self):
|
def generate_script(self):
|
||||||
self.script_fp = os.path.join(self.folder_path, "script.sh")
|
self.script_fp = os.path.join(self.folder_path, "script.sh")
|
||||||
|
|
||||||
|
|||||||
14
test.py
14
test.py
@ -9,7 +9,7 @@ dir_path = os.path.dirname(os.path.realpath(__file__))
|
|||||||
|
|
||||||
ap = argparse.ArgumentParser()
|
ap = argparse.ArgumentParser()
|
||||||
ap.add_argument("-i", "--interface", required=False,
|
ap.add_argument("-i", "--interface", required=False,
|
||||||
help="interface (default: eth0)", default="eth0")
|
help="interface (default: eth0)", default="eth0")
|
||||||
ap.add_argument("-s", "--session_folder", required=True,
|
ap.add_argument("-s", "--session_folder", required=True,
|
||||||
help="path to the session folder")
|
help="path to the session folder")
|
||||||
args = vars(ap.parse_args())
|
args = vars(ap.parse_args())
|
||||||
@ -44,6 +44,11 @@ class MainHandler(tornado.web.RequestHandler):
|
|||||||
self.write(buf)
|
self.write(buf)
|
||||||
|
|
||||||
|
|
||||||
|
class StateHandler(tornado.web.RequestHandler):
|
||||||
|
def get(self):
|
||||||
|
self.write(json.dumps(om.serialize()))
|
||||||
|
|
||||||
|
|
||||||
class ConfigsHandeler(tornado.web.RequestHandler):
|
class ConfigsHandeler(tornado.web.RequestHandler):
|
||||||
def get(self):
|
def get(self):
|
||||||
cfgs = json.loads(open("profiles.json", "r").read())
|
cfgs = json.loads(open("profiles.json", "r").read())
|
||||||
@ -60,8 +65,8 @@ class CreateInstantceHandler(tornado.web.RequestHandler):
|
|||||||
idx = int(idx)
|
idx = int(idx)
|
||||||
cfgs = json.loads(open("profiles.json", "r").read())
|
cfgs = json.loads(open("profiles.json", "r").read())
|
||||||
cfg = cfgs[idx]
|
cfg = cfgs[idx]
|
||||||
om.new_op(cfg["cfg_fp"], cfg["name"],
|
om.new_op_old(cfg["cfg_fp"], cfg["name"],
|
||||||
additional_cfg=cfg["additional_cfg"])
|
additional_cfg=cfg["additional_cfg"])
|
||||||
self.write("create sucess!")
|
self.write("create sucess!")
|
||||||
else:
|
else:
|
||||||
self.write("need idx")
|
self.write("need idx")
|
||||||
@ -108,6 +113,7 @@ class ClearCacheInstatnceHandler(tornado.web.RequestHandler):
|
|||||||
def make_app():
|
def make_app():
|
||||||
return tornado.web.Application([
|
return tornado.web.Application([
|
||||||
(r"/", MainHandler),
|
(r"/", MainHandler),
|
||||||
|
(r"/state", StateHandler),
|
||||||
(r"/select", ConfigsHandeler),
|
(r"/select", ConfigsHandeler),
|
||||||
(r"/create", CreateInstantceHandler),
|
(r"/create", CreateInstantceHandler),
|
||||||
(r"/start", StartInstatnceHandler),
|
(r"/start", StartInstatnceHandler),
|
||||||
@ -121,7 +127,7 @@ if __name__ == "__main__":
|
|||||||
app = make_app()
|
app = make_app()
|
||||||
app.listen(8000)
|
app.listen(8000)
|
||||||
proc = subprocess.Popen([os.path.join(dir_path, "iptable_docker.sh"), interface],
|
proc = subprocess.Popen([os.path.join(dir_path, "iptable_docker.sh"), interface],
|
||||||
stdout=subprocess.PIPE)
|
stdout=subprocess.PIPE)
|
||||||
outs, errs = proc.communicate()
|
outs, errs = proc.communicate()
|
||||||
print(outs.decode())
|
print(outs.decode())
|
||||||
tornado.ioloop.IOLoop.current().start()
|
tornado.ioloop.IOLoop.current().start()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user