Browse Source

first kind of working version

Tobias Simetsreiter 4 years ago
parent
commit
2ecc5e5cc8
7 changed files with 111 additions and 22 deletions
  1. 10 1
      Makefile
  2. 2 2
      sharxz.sh
  3. 3 3
      src/bootstrap_captive@.service
  4. 1 0
      src/dnsmasq.conf
  5. 83 14
      src/main.py
  6. 8 0
      src/wpa_supplicant.conf
  7. 4 2
      unsharxz.sh

+ 10 - 1
Makefile

@@ -3,6 +3,7 @@
 .PHONY: build
 build: build/bootstrap_captive_bundle.sh
 
+rebuild: clean build
 
 build/bootstrap_captive_bundle.sh: src src/blob/hostapd.deb
 	mkdir -p build
@@ -10,9 +11,17 @@ build/bootstrap_captive_bundle.sh: src src/blob/hostapd.deb
 	mv ${@}.tmp ${@}
 
 HOSTAPD_URL="http://ftp.de.debian.org/debian/pool/main/w/wpa/hostapd_2.7+git20190128+0c1e29f-6+deb10u2_armhf.deb"
+DNSMASQ_URL="http://ftp.de.debian.org/debian/pool/main/d/dnsmasq/dnsmasq-base_2.80-1_armhf.deb"
+DBASE_URL="http://ftp.de.debian.org/debian/pool/main/d/dnsmasq/dnsmasq-base_2.80-1_armhf.deb"
+DNSDATA_URL="http://ftp.de.debian.org/debian/pool/main/d/dns-root-data/dns-root-data_2019031302_all.deb"
+#	 dns-root-data dnsmasq dnsmasq-base
 src/blob/hostapd.deb:
 	mkdir -p src/blob
-	curl -L -o "${@}" "${HOSTAPD_URL}"
+	curl -L -o "src/blob/hostapd.deb" "${HOSTAPD_URL}"
+	curl -L -o "src/blob/dnsmasq.deb" "${DNSMASQ_URL}"
+	curl -L -o "src/blob/dns-root-data.deb" "${DNSDATA_URL}"
+	curl -L -o "src/blob/dnsmasq-base.deb" "${DBASE_URL}"
 
 clean:
 	rm -rf build
+	rm -rf src/blob

+ 2 - 2
sharxz.sh

@@ -6,8 +6,8 @@ set -x
 INDIR=$1
 OUTFILE=$2
 
-dd if=/dev/zero of=$OUTFILE bs=512 count=1
+dd if=/dev/zero of=$OUTFILE bs=1024 count=1
 dd of=$OUTFILE if=./unsharxz.sh conv=notrunc
-tar -cC $INDIR .| xz -c -T 0 >> $OUTFILE
+tar -cC $INDIR .| xz -c -T 0 | base64 -w 0 >> $OUTFILE
 chmod +x $OUTFILE
 

+ 3 - 3
src/bootstrap_captive.service → src/bootstrap_captive@.service

@@ -1,5 +1,5 @@
 [Unit]
-Description=A Captive Portal for Raspberry Setup
+Description=A Captive Portal for Raspberry Setup. Argument: %I
 After=network.target
 StartLimitIntervalSec=0
 
@@ -7,8 +7,8 @@ StartLimitIntervalSec=0
 Type=simple
 Restart=always
 RestartSec=1
-User=centos
-ExecStart=/usr/local/bin/bootstrap_portal serve
+User=root
+ExecStart=/usr/local/bin/bootstrap_captive %i
 
 [Install]
 WantedBy=multi-user.target

+ 1 - 0
src/dnsmasq.conf

@@ -5,3 +5,4 @@ server=8.8.8.8
 domain-needed
 bogus-priv
 dhcp-range=192.168.5.100,192.168.5.200,24h
+address=/#/192.168.5.1

+ 83 - 14
src/main.py

@@ -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__":

+ 8 - 0
src/wpa_supplicant.conf

@@ -0,0 +1,8 @@
+ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
+update_config=1
+country=DE
+
+network={
+ ssid=""
+ psk=""
+}

+ 4 - 2
unsharxz.sh

@@ -5,10 +5,12 @@ set +x
 
 TMPDIR=$(mktemp -d)
 
-dd if=$0 bs=512 skip=1 2>/dev/null |xz -d -T 0 |tar -xC $TMPDIR
+dd if=$0 bs=1024 skip=1 2>/dev/null | base64 -d |xz -d -T 0 |tar -xC $TMPDIR
 
-export UNSHARXZ_INSTALLER=$(realpath $0)
+export UNSHARXZ_INSTALLER="$(realpath $0)"
+export UNSHARXZ_ORIG_DIR="$(pwd)"
 cd $TMPDIR
+set +e
 ./main.py $@
 EXITCODE=$?
 rm -r $TMPDIR