serialize status
This commit is contained in:
parent
6a4eeb807b
commit
28d1efaebd
@ -20,7 +20,7 @@ Todos
|
||||
- [ ] documentations
|
||||
|
||||
2020/07/25 Sat
|
||||
- [ ] frontend ui design
|
||||
- [ ] configuration defination
|
||||
- [ ] frontend ui design: ajax
|
||||
- [x] configuration defination
|
||||
- [ ] configuration save and load
|
||||
- [ ] charts: mpld3
|
||||
41
o_manager.py
41
o_manager.py
@ -35,18 +35,13 @@ class OManager:
|
||||
else:
|
||||
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}")
|
||||
if not os.path.isdir(folder_path):
|
||||
os.makedirs(folder_path)
|
||||
if not name:
|
||||
name = f"openvpn-{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 = {
|
||||
"folder_path": folder_path,
|
||||
"script_template_fp": "script.sh.template",
|
||||
@ -62,6 +57,32 @@ class OManager:
|
||||
self.new_idx += 1
|
||||
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):
|
||||
lb_cfg_fp = os.path.join(
|
||||
self.base_folder, f"go-socks-lb-{load_balance_mode}.yml")
|
||||
@ -247,11 +268,3 @@ class OManager:
|
||||
if stderr:
|
||||
print(f'[stderr]\n{stderr.decode()}')
|
||||
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:
|
||||
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):
|
||||
self.script_fp = os.path.join(self.folder_path, "script.sh")
|
||||
|
||||
|
||||
8
test.py
8
test.py
@ -44,6 +44,11 @@ class MainHandler(tornado.web.RequestHandler):
|
||||
self.write(buf)
|
||||
|
||||
|
||||
class StateHandler(tornado.web.RequestHandler):
|
||||
def get(self):
|
||||
self.write(json.dumps(om.serialize()))
|
||||
|
||||
|
||||
class ConfigsHandeler(tornado.web.RequestHandler):
|
||||
def get(self):
|
||||
cfgs = json.loads(open("profiles.json", "r").read())
|
||||
@ -60,7 +65,7 @@ class CreateInstantceHandler(tornado.web.RequestHandler):
|
||||
idx = int(idx)
|
||||
cfgs = json.loads(open("profiles.json", "r").read())
|
||||
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"])
|
||||
self.write("create sucess!")
|
||||
else:
|
||||
@ -108,6 +113,7 @@ class ClearCacheInstatnceHandler(tornado.web.RequestHandler):
|
||||
def make_app():
|
||||
return tornado.web.Application([
|
||||
(r"/", MainHandler),
|
||||
(r"/state", StateHandler),
|
||||
(r"/select", ConfigsHandeler),
|
||||
(r"/create", CreateInstantceHandler),
|
||||
(r"/start", StartInstatnceHandler),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user