From 1f1fa3711af6747fcc63e0ca1ef814a831e11b59 Mon Sep 17 00:00:00 2001 From: TCH Date: Sat, 17 Feb 2024 19:45:39 +0000 Subject: [PATCH] no posts + everything behind auth --- api/main.py | 102 +++++++---------------------------------- api/modules/ap.py | 48 ------------------- api/modules/restart.py | 7 --- 3 files changed, 17 insertions(+), 140 deletions(-) delete mode 100644 api/modules/restart.py diff --git a/api/main.py b/api/main.py index a3153f0c..f5981860 100644 --- a/api/main.py +++ b/api/main.py @@ -14,18 +14,9 @@ import modules.firewall as firewall import modules.networking as networking import modules.openvpn as openvpn import modules.wireguard as wireguard -import modules.restart as restart tags_metadata = [ - { - "name": "system", - "description": "All information regarding the underlying system." - }, - { - "name": "accesspoint/hostpost", - "description": "Get and change all information regarding the hotspot" - } ] app = FastAPI( title="API for Raspap", @@ -37,7 +28,7 @@ app = FastAPI( } ) -@app.get("/system", tags=["system"]) +@app.get("/system", tags=["system"], api_key: APIKey = Depends(auth.get_api_key) async def get_system(): return{ 'hostname': system.hostname(), @@ -54,7 +45,7 @@ async def get_system(): 'rpiRevision': system.rpiRevision() } -@app.get("/ap", tags=["accesspoint/hostpost"]) +@app.get("/ap", tags=["accesspoint/hostpost"], api_key: APIKey = Depends(auth.get_api_key) async def get_ap(): return{ 'driver': ap.driver(), @@ -75,66 +66,14 @@ async def get_ap(): 'ignore_broadcast_ssid': ap.ignore_broadcast_ssid() } -@app.post("/ap", tags=["accesspoint/hostpost"]) -async def post_ap(driver=None, - ctrl_interface=None, - ctrl_interface_group=None, - auth_algs=None, - wpa_key_mgmt=None, - beacon_int=None, - ssid=None, - channel=None, - hw_mode=None, - ieee80211n=None, - wpa_passphrase=None, - interface=None, - wpa=None, - wpa_pairwise=None, - country_code=None, - ignore_broadcast_ssid=None, - api_key: APIKey = Depends(auth.get_api_key)): - if driver != None: - ap.set_driver(driver) - if ctrl_interface != None: - ap.set_ctrl_interface(ctrl_interface) - if ctrl_interface_group !=None: - ap.set_ctrl_interface_group(ctrl_interface_group) - if auth_algs != None: - ap.set_auth_algs(auth_algs) - if wpa_key_mgmt != None: - ap.set_wpa_key_mgmt(wpa_key_mgmt) - if beacon_int != None: - ap.set_beacon_int(beacon_int) - if ssid != None: - ap.set_ssid(ssid) - if channel != None: - ap.set_channel(channel) - if hw_mode != None: - ap.set_hw_mode(hw_mode) - if ieee80211n != None: - ap.set_ieee80211n(ieee80211n) - if wpa_passphrase != None: - ap.set_wpa_passphrase(wpa_passphrase) - if interface != None: - ap.set_interface(interface) - if wpa != None: - ap.set_wpa(wpa) - if wpa_pairwise != None: - ap.set_wpa_pairwise(wpa_pairwise) - if country_code != None: - ap.set_country_code(country_code) - if ignore_broadcast_ssid != None: - ap.set_ignore_broadcast_ssid(ignore_broadcast_ssid) - - -@app.get("/clients/{wireless_interface}", tags=["Clients"]) +@app.get("/clients/{wireless_interface}", tags=["Clients"], api_key: APIKey = Depends(auth.get_api_key) async def get_clients(wireless_interface): return{ 'active_clients_amount': client.get_active_clients_amount(wireless_interface), 'active_clients': json.loads(client.get_active_clients(wireless_interface)) } -@app.get("/dhcp", tags=["DHCP"]) +@app.get("/dhcp", tags=["DHCP"], api_key: APIKey = Depends(auth.get_api_key) async def get_dhcp(): return{ 'range_start': dhcp.range_start(), @@ -145,29 +84,30 @@ async def get_dhcp(): 'range_nameservers': dhcp.range_nameservers() } -@app.get("/dns/domains", tags=["DNS"]) +@app.get("/dns/domains", tags=["DNS"], api_key: APIKey = Depends(auth.get_api_key) async def get_domains(): return{ 'domains': json.loads(dns.adblockdomains()) } -@app.get("/dns/hostnames", tags=["DNS"]) + +@app.get("/dns/hostnames", tags=["DNS"], api_key: APIKey = Depends(auth.get_api_key) async def get_hostnames(): return{ 'hostnames': json.loads(dns.adblockhostnames()) } -@app.get("/dns/upstream", tags=["DNS"]) +@app.get("/dns/upstream", tags=["DNS"], api_key: APIKey = Depends(auth.get_api_key) async def get_upstream(): return{ 'upstream_nameserver': dns.upstream_nameserver() } -@app.get("/dns/logs", tags=["DNS"]) +@app.get("/dns/logs", tags=["DNS"], api_key: APIKey = Depends(auth.get_api_key) async def get_dnsmasq_logs(): return(dns.dnsmasq_logs()) -@app.get("/ddns", tags=["DDNS"]) +@app.get("/ddns", tags=["DDNS"], api_key: APIKey = Depends(auth.get_api_key) async def get_ddns(): return{ 'use': ddns.use(), @@ -179,18 +119,18 @@ async def get_ddns(): 'domain': ddns.domain() } -@app.get("/firewall", tags=["Firewall"]) +@app.get("/firewall", tags=["Firewall"], api_key: APIKey = Depends(auth.get_api_key) async def get_firewall(): return json.loads(firewall.firewall_rules()) -@app.get("/networking", tags=["Networking"]) +@app.get("/networking", tags=["Networking"], api_key: APIKey = Depends(auth.get_api_key) async def get_networking(): return{ 'interfaces': json.loads(networking.interfaces()), 'throughput': json.loads(networking.throughput()) } -@app.get("/openvpn", tags=["OpenVPN"]) +@app.get("/openvpn", tags=["OpenVPN"], api_key: APIKey = Depends(auth.get_api_key) async def get_openvpn(): return{ 'client_configs': openvpn.client_configs(), @@ -200,13 +140,13 @@ async def get_openvpn(): 'client_login_active': openvpn.client_login_active() } -@app.get("/openvpn/{config}", tags=["OpenVPN"]) +@app.get("/openvpn/{config}", tags=["OpenVPN"], api_key: APIKey = Depends(auth.get_api_key) async def client_config_list(config): return{ 'client_config': openvpn.client_config_list(config) } -@app.get("/wireguard", tags=["WireGuard"]) +@app.get("/wireguard", tags=["WireGuard"], api_key: APIKey = Depends(auth.get_api_key) async def get_wireguard(): return{ 'client_configs': wireguard.configs(), @@ -214,16 +154,8 @@ async def get_wireguard(): 'client_config_active': wireguard.client_config_active() } -@app.get("/wireguard/{config}", tags=["WireGuard"]) +@app.get("/wireguard/{config}", tags=["WireGuard"], api_key: APIKey = Depends(auth.get_api_key) async def client_config_list(config): return{ 'client_config': wireguard.client_config_list(config) -} - -@app.post("/restart/webgui") -async def restart_webgui(): - restart.webgui() - -@app.post("/restart/adblock") -async def restart_adblock(): - restart.adblock() \ No newline at end of file +} \ No newline at end of file diff --git a/api/modules/ap.py b/api/modules/ap.py index 2d6fd8e3..25dffdad 100644 --- a/api/modules/ap.py +++ b/api/modules/ap.py @@ -4,99 +4,51 @@ import json def driver(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep driver= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_driver(driver): - return subprocess.run(f"sudo sed -i 's/^driver=.*/driver={driver}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def ctrl_interface(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep ctrl_interface= | cut -d'=' -f2 | head -1", shell=True, capture_output=True, text=True).stdout.strip() -def set_ctrl_interface(ctrl_interface): - return subprocess.run(f"sudo sed -i 's/^ctrl_interface=.*/ctrl_interface={ctrl_interface}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def ctrl_interface_group(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep ctrl_interface_group= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_ctrl_interface_group(ctrl_interface_group): - return subprocess.run(f"sudo sed -i 's/^ctrl_interface_group=.*/ctrl_interface_group={ctrl_interface_group}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def auth_algs(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep auth_algs= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_auth_algs(auth_algs): - return subprocess.run(f"sudo sed -i 's/^auth_algs=.*/auth_algs={auth_algs}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def wpa_key_mgmt(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep wpa_key_mgmt= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_wpa_key_mgmt(wpa_key_mgmt): - return subprocess.run(f"sudo sed -i 's/^wpa_key_mgmt=.*/wpa_key_mgmt={wpa_key_mgmt}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def beacon_int(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep beacon_int= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_beacon_int(beacon_int): - return subprocess.run(f"sudo sed -i 's/^beacon_int=.*/beacon_int={beacon_int}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def ssid(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep ssid= | cut -d'=' -f2 | head -1", shell=True, capture_output=True, text=True).stdout.strip() -def set_ssid(ssid): - return subprocess.run(f"sudo sed -i 's/^ssid=.*/ssid={ssid}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def channel(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep channel= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_channel(channel): - return subprocess.run(f"sudo sed -i 's/^channel=.*/channel={channel}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def hw_mode(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep hw_mode= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_hw_mode(hw_mode): - return subprocess.run(f"sudo sed -i 's/^hw_mode=.*/hw_mode={hw_mode}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def ieee80211n(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep ieee80211n= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_ieee80211n(ieee80211n): - return subprocess.run(f"sudo sed -i 's/^ieee80211n=.*/ieee80211n={ieee80211n}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def wpa_passphrase(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep wpa_passphrase= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_wpa_passphrase(wpa_passphrase): - return subprocess.run(f"sudo sed -i 's/^wpa_passphrase=.*/wpa_passphrase={wpa_passphrase}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def interface(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep interface= | cut -d'=' -f2 | head -1", shell=True, capture_output=True, text=True).stdout.strip() -def set_interface(interface): - return subprocess.run(f"sudo sed -i 's/^interface=.*/interface={interface}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def wpa(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep wpa= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_wpa(wpa): - return subprocess.run(f"sudo sed -i 's/^wpa=.*/wpa={wpa}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def wpa_pairwise(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep wpa_pairwise= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_wpa_pairwise(wpa_pairwise): - return subprocess.run(f"sudo sed -i 's/^wpa_pairwise=.*/wpa_pairwise={wpa_pairwise}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def country_code(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep country_code= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_country_code(country_code): - return subprocess.run(f"sudo sed -i 's/^country_code=.*/country_code={country_code}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def ignore_broadcast_ssid(): return subprocess.run("cat /etc/hostapd/hostapd.conf | grep ignore_broadcast_ssid= | cut -d'=' -f2", shell=True, capture_output=True, text=True).stdout.strip() -def set_ignore_broadcast_ssid(ignore_broadcast_ssid): - return subprocess.run(f"sudo sed -i 's/^ignore_broadcast_ssid=.*/ignore_broadcast_ssid={ignore_broadcast_ssid}/' /etc/hostapd/hostapd.conf", shell=True, capture_output=True, text=True).stdout.strip() - def logging(): log_output = subprocess.run(f"cat /tmp/hostapd.log", shell=True, capture_output=True, text=True).stdout.strip() logs = {} diff --git a/api/modules/restart.py b/api/modules/restart.py deleted file mode 100644 index 18eb1b57..00000000 --- a/api/modules/restart.py +++ /dev/null @@ -1,7 +0,0 @@ -import subprocess - -def webgui(): - return subprocess.run("sudo /etc/raspap/lighttpd/configport.sh --restart", shell=True, capture_output=True, text=True).stdout.strip() - -def adblock(): - return subprocess.run("sudo /bin/systemctl restart dnsmasq.service", shell=True, capture_output=True, text=True).stdout.strip() \ No newline at end of file