|
|
@@ -1,6 +1,7 @@
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
import os
|
|
|
+import sys
|
|
|
realp = os.path.realpath(os.path.dirname(__file__))
|
|
|
|
|
|
def main():
|
|
|
@@ -8,42 +9,110 @@ def main():
|
|
|
args = p.parse_args()
|
|
|
if "func" in args:
|
|
|
return args.func(args)
|
|
|
- else:
|
|
|
- p.print_help()
|
|
|
+ p.print_help()
|
|
|
|
|
|
def parser():
|
|
|
from argparse import ArgumentParser
|
|
|
p = ArgumentParser()
|
|
|
sub = p.add_subparsers()
|
|
|
- p_serve = sub.add_parser("serve")
|
|
|
- p_serve.set_defaults(func=com_serve)
|
|
|
+ p_server = sub.add_parser("server")
|
|
|
+ p_server.set_defaults(func=com_server)
|
|
|
p_install = sub.add_parser("install")
|
|
|
p_install.set_defaults(func=com_install)
|
|
|
p_install.add_argument("-i", "--install_dir", default="/opt/eu.tsimnet.git/dasimmet/bootstrap_captive")
|
|
|
+ p_install.add_argument("-r", "--remove_installer", action="store_true")
|
|
|
+ p_install.add_argument("-e", "--enable", action="store_true")
|
|
|
+ p_enable = sub.add_parser("enable")
|
|
|
+ p_enable.set_defaults(func=com_enable)
|
|
|
+ p_enable = sub.add_parser("disable")
|
|
|
+ p_enable.set_defaults(func=com_disable)
|
|
|
return p
|
|
|
|
|
|
def com_install(args):
|
|
|
- import os
|
|
|
if not "UNSHARXZ_INSTALLER" in os.environ:
|
|
|
- print("Installer not found in Environment")
|
|
|
- installer = os.environ["UNSHARXZ_INSTALLER"]
|
|
|
- print("Installer: ", installer)
|
|
|
+ print("hostapt_captive is already installed")
|
|
|
+ if args.enable:
|
|
|
+ com_enable(args)
|
|
|
+ return
|
|
|
COM="""
|
|
|
set -e
|
|
|
set -x
|
|
|
pwd
|
|
|
-mkdir -p "{1}"
|
|
|
-printf "#!/usr/bin/env sh\n\ncd {1} && ./main.py \\$@" > "/usr/local/bin/bootstrap_captive"
|
|
|
-cp -aT "." "{1}"
|
|
|
+
|
|
|
+# dpkg -l hostapd dnsmasq &> /dev/null || dpkg -i -R blob/
|
|
|
+
|
|
|
+sed -i 's/^source-directory.*$/source \/etc\/network\/interfaces.d\/*/' /etc/network/interfaces
|
|
|
+touch /etc/network/interfaces.d/wlan0.conf
|
|
|
+systemctl unmask hostapd.service
|
|
|
+systemctl disable hostapd.service dnsmasq.service
|
|
|
+rm -f /etc/hostapd/hostapd.conf
|
|
|
+ln -s "{0}/hostapd.conf" /etc/hostapd/hostapd.conf
|
|
|
+mv /etc/dnsmasq.conf /etc/dnsmasq.orig || true
|
|
|
+ln -s "{0}/dnsmasq.conf" /etc/dnsmasq.conf
|
|
|
+mkdir -p "{0}"
|
|
|
+rm -rf "{0}"
|
|
|
+cp -a "." "{0}"
|
|
|
+printf "#!/usr/bin/env sh\n\ncd {0} && ./main.py \\$@" > "/usr/local/bin/bootstrap_captive"
|
|
|
chmod +x "/usr/local/bin/bootstrap_captive"
|
|
|
+rm -f /lib/systemd/system/bootstrap_captive@.service
|
|
|
+ln -s "{0}/bootstrap_captive@.service" /lib/systemd/system/
|
|
|
+systemctl daemon-reload
|
|
|
|
|
|
-""".format(installer,args.install_dir)
|
|
|
+""".format(args.install_dir)
|
|
|
from subprocess import run
|
|
|
run(COM, shell=True, check=True)
|
|
|
+ if args.remove_installer:
|
|
|
+ inst_file = os.environ["UNSHARXZ_INSTALLER"]
|
|
|
+ os.unlink(inst_file)
|
|
|
+ if args.enable:
|
|
|
+ com_enable(args)
|
|
|
+
|
|
|
+def wifi_settings(on):
|
|
|
+ return """
|
|
|
+# HOSTAPD_AUTOCONFIG
|
|
|
+{0}auto wlan0
|
|
|
+{0}iface wlan0 inet static
|
|
|
+{0}address 192.168.5.1
|
|
|
+{0}netmask 255.255.255.0
|
|
|
+# HOSTAPD_AUTOCONFIG
|
|
|
+""".format("" if on else "# ")
|
|
|
+
|
|
|
+def set_wifi(on=False):
|
|
|
+ with open("/etc/network/interfaces.d/wlan0.conf", "w") as fd:
|
|
|
+ fd.write(wifi_settings(on))
|
|
|
|
|
|
-def com_serve(args):
|
|
|
- COM=["python3", "-m", "http.server", "--cgi", "8080"]
|
|
|
+def com_enable(args):
|
|
|
from subprocess import run
|
|
|
+ set_wifi(True)
|
|
|
+ COM="""
|
|
|
+set -x
|
|
|
+systemctl stop networking
|
|
|
+systemctl disable --now dhcpcd.service
|
|
|
+wpa_cli terminate
|
|
|
+systemctl start networking
|
|
|
+systemctl enable --now hostapd.service bootstrap_captive@server.service dnsmasq.service
|
|
|
+iptables -t nat -A PREROUTING -d 0/0 -p tcp --dport 80 -j DNAT --to-destination 192.168.5.1:80
|
|
|
+iptables-save > iptables.save
|
|
|
+
|
|
|
+"""
|
|
|
+ run(COM, shell=True)
|
|
|
+
|
|
|
+def com_disable(args):
|
|
|
+ from subprocess import run
|
|
|
+ set_wifi(False)
|
|
|
+ COM="""
|
|
|
+iptables -F -t nat
|
|
|
+iptables-save > iptables.save
|
|
|
+systemctl disable --now bootstrap_captive@server.service hostapd.service dnsmasq.service
|
|
|
+systemctl enable --now dhcpcd.service
|
|
|
+"""
|
|
|
+ run(COM, shell=True)
|
|
|
+
|
|
|
+def com_server(args):
|
|
|
+ from subprocess import run
|
|
|
+
|
|
|
+ run("cat iptables.save|iptables-restore", shell=True)
|
|
|
+ COM=["python3", "-m", "http.server", "--cgi", "80"]
|
|
|
run(COM, cwd="http", check=True)
|
|
|
|
|
|
if __name__=="__main__":
|