remove the need of openvpn_api
This commit is contained in:
parent
ccd10794f6
commit
db3943aa6e
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
|||||||
|
config/
|
||||||
|
|
||||||
# virtual env
|
# virtual env
|
||||||
env/
|
env/
|
||||||
|
|
||||||
|
|||||||
@ -14,6 +14,13 @@ Todos
|
|||||||
- [x] improve log reading efficiency, potentially using pytailer
|
- [x] improve log reading efficiency, potentially using pytailer
|
||||||
- [x] remove instance or change configuration
|
- [x] remove instance or change configuration
|
||||||
- [ ] improve ui using template, move to ajax
|
- [ ] improve ui using template, move to ajax
|
||||||
|
- [ ] save and load configurations
|
||||||
- [ ] parameterize inline constants
|
- [ ] parameterize inline constants
|
||||||
- [ ] refractor code
|
- [ ] refractor code
|
||||||
- [ ] documentations
|
- [ ] documentations
|
||||||
|
|
||||||
|
2020/07/25 Sat
|
||||||
|
- [ ] frontend ui design
|
||||||
|
- [ ] configuration defination
|
||||||
|
- [ ] configuration save and load
|
||||||
|
- [ ] charts: mpld3
|
||||||
127
olb_web.py
Normal file
127
olb_web.py
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
import tornado.ioloop
|
||||||
|
import tornado.web
|
||||||
|
import json
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
from o_manager import OManager
|
||||||
|
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
|
ap = argparse.ArgumentParser()
|
||||||
|
ap.add_argument("-i", "--interface", required=False,
|
||||||
|
help="interface (default: eth0)", default="eth0")
|
||||||
|
ap.add_argument("-s", "--session_folder", required=True,
|
||||||
|
help="path to the session folder")
|
||||||
|
args = vars(ap.parse_args())
|
||||||
|
session_folder = args["session_folder"]
|
||||||
|
interface = args["interface"]
|
||||||
|
om = OManager(session_folder, interface)
|
||||||
|
|
||||||
|
|
||||||
|
class MainHandler(tornado.web.RequestHandler):
|
||||||
|
def get(self):
|
||||||
|
buf = "<html>\n<head><meta http-equiv=\"refresh\" content=\"5\"></head>\n"
|
||||||
|
buf += "<body>\n"
|
||||||
|
buf += f"ops len: {len(om.instances)}, lb pids: {om.pids} <br/>\n"
|
||||||
|
if len(om.instances):
|
||||||
|
for i in om.instances:
|
||||||
|
op = i["op"]
|
||||||
|
idx = i["idx"]
|
||||||
|
weight = i["weight"]
|
||||||
|
buf += f"op #{idx}, name: {op.name} <br/>\n"
|
||||||
|
buf += f"status: {op.status}, weight: {weight} <br/>\n"
|
||||||
|
buf += f"op pids: {op.pids}<br/>\n"
|
||||||
|
buf += "log: <br/>\n"
|
||||||
|
buf += "<pre>\n"
|
||||||
|
buf += "\n".join(op.get_log(lines=10))
|
||||||
|
buf += f"\nSpeed: {op.get_latest_speed()}\n"
|
||||||
|
buf += "\nio_stat\n"
|
||||||
|
buf += "\n".join(op.get_io_stat(lines=5))
|
||||||
|
buf += "\nping_stat\n"
|
||||||
|
buf += "\n".join(op.get_ping_stat(lines=5))
|
||||||
|
buf += "\n</pre>\n"
|
||||||
|
buf += "</body>\n</html>"
|
||||||
|
self.write(buf)
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigsHandeler(tornado.web.RequestHandler):
|
||||||
|
def get(self):
|
||||||
|
cfgs = json.loads(open("profiles.json", "r").read())
|
||||||
|
buf = "Select one configuration to create. <br/>\n"
|
||||||
|
for idx, cfg in enumerate(cfgs):
|
||||||
|
buf += f"<a href='/create?i={idx}'>{cfg['name']}</a><br/>\n"
|
||||||
|
self.write(buf)
|
||||||
|
|
||||||
|
|
||||||
|
class CreateInstantceHandler(tornado.web.RequestHandler):
|
||||||
|
def get(self):
|
||||||
|
idx = self.get_query_argument("i", None)
|
||||||
|
if idx != None:
|
||||||
|
idx = int(idx)
|
||||||
|
cfgs = json.loads(open("profiles.json", "r").read())
|
||||||
|
cfg = cfgs[idx]
|
||||||
|
om.new_op(cfg["cfg_fp"], cfg["name"],
|
||||||
|
additional_cfg=cfg["additional_cfg"])
|
||||||
|
self.write("create sucess!")
|
||||||
|
else:
|
||||||
|
self.write("need idx")
|
||||||
|
|
||||||
|
|
||||||
|
class StartInstatnceHandler(tornado.web.RequestHandler):
|
||||||
|
def get(self):
|
||||||
|
idx = self.get_query_argument("i", None)
|
||||||
|
if idx != None:
|
||||||
|
om.start_op(int(idx))
|
||||||
|
self.write("start sucess!")
|
||||||
|
else:
|
||||||
|
om.start_all()
|
||||||
|
self.write("start all")
|
||||||
|
|
||||||
|
|
||||||
|
class StopInstatnceHandler(tornado.web.RequestHandler):
|
||||||
|
def get(self):
|
||||||
|
idx = self.get_query_argument("i", None)
|
||||||
|
if idx != None:
|
||||||
|
om.stop_op(int(idx))
|
||||||
|
self.write("stop sucess!")
|
||||||
|
else:
|
||||||
|
om.stop_all()
|
||||||
|
self.write("stop all")
|
||||||
|
|
||||||
|
|
||||||
|
class RemoveInstatnceHandler(tornado.web.RequestHandler):
|
||||||
|
def get(self):
|
||||||
|
idx = self.get_query_argument("i", None)
|
||||||
|
if idx != None:
|
||||||
|
om.remove_op(int(idx))
|
||||||
|
self.write("remove sucess!")
|
||||||
|
else:
|
||||||
|
om.remove_all_op()
|
||||||
|
self.write("remove all")
|
||||||
|
|
||||||
|
|
||||||
|
class ClearCacheInstatnceHandler(tornado.web.RequestHandler):
|
||||||
|
def get(self):
|
||||||
|
om.clear_cache()
|
||||||
|
|
||||||
|
|
||||||
|
def make_app():
|
||||||
|
return tornado.web.Application([
|
||||||
|
(r"/", MainHandler),
|
||||||
|
(r"/select", ConfigsHandeler),
|
||||||
|
(r"/create", CreateInstantceHandler),
|
||||||
|
(r"/start", StartInstatnceHandler),
|
||||||
|
(r"/stop", StopInstatnceHandler),
|
||||||
|
(r"/remove", RemoveInstatnceHandler),
|
||||||
|
(r"/clear_cache", ClearCacheInstatnceHandler),
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app = make_app()
|
||||||
|
app.listen(8000)
|
||||||
|
proc = subprocess.Popen([os.path.join(dir_path, "iptable_docker.sh"), interface],
|
||||||
|
stdout=subprocess.PIPE)
|
||||||
|
outs, errs = proc.communicate()
|
||||||
|
print(outs.decode())
|
||||||
|
tornado.ioloop.IOLoop.current().start()
|
||||||
11
openvpn.py
11
openvpn.py
@ -5,7 +5,7 @@ import asyncio
|
|||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import openvpn_api
|
# import openvpn_api
|
||||||
import signal
|
import signal
|
||||||
import psutil
|
import psutil
|
||||||
import subprocess
|
import subprocess
|
||||||
@ -55,7 +55,7 @@ class Openvpn:
|
|||||||
self.exit_future = None
|
self.exit_future = None
|
||||||
self.additional_cfg = additional_cfg
|
self.additional_cfg = additional_cfg
|
||||||
self.run_task = []
|
self.run_task = []
|
||||||
self.openvpn_api = None
|
# self.openvpn_api = None
|
||||||
self.name = name
|
self.name = name
|
||||||
self.pid_fp = None
|
self.pid_fp = None
|
||||||
# TODO: update paths function
|
# TODO: update paths function
|
||||||
@ -68,6 +68,9 @@ class Openvpn:
|
|||||||
else:
|
else:
|
||||||
self.loop = asyncio.get_event_loop()
|
self.loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
|
def load_cfg(self):
|
||||||
|
pass
|
||||||
|
|
||||||
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")
|
||||||
|
|
||||||
@ -112,7 +115,7 @@ class Openvpn:
|
|||||||
cfg = {
|
cfg = {
|
||||||
"dev": self.interface,
|
"dev": self.interface,
|
||||||
"dev-type": "tun", # TODO: add code to read dev-type
|
"dev-type": "tun", # TODO: add code to read dev-type
|
||||||
"management": f"localhost {self.management_port}",
|
# "management": f"localhost {self.management_port}", # disable management since we are not using it now
|
||||||
"log-append": self.log_fp
|
"log-append": self.log_fp
|
||||||
}
|
}
|
||||||
cfg.update(self.additional_cfg)
|
cfg.update(self.additional_cfg)
|
||||||
@ -267,7 +270,7 @@ class Openvpn:
|
|||||||
stderr=asyncio.subprocess.PIPE)
|
stderr=asyncio.subprocess.PIPE)
|
||||||
print("openpvn started")
|
print("openpvn started")
|
||||||
self.proc = proc
|
self.proc = proc
|
||||||
for i in range(3):
|
for _ in range(3):
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
got_pid = False
|
got_pid = False
|
||||||
try:
|
try:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user