浏览代码

Add ZeroTier Module (#4)

* Add ZeroTier Module

the src for the ZeroTier Module which allows the wifi pineapple to connect to a Zerotier-one network

* Add ZeroTier Module

Allows Wifi Pineapple to connect to a ZeroTier-one network

* Add files via upload

add requested changes and fix interface name issue

* Add files via upload

* Add files via upload

removed bash in front of dependencies.sh

* Add files via upload

remove unneeded lines

* Delete module.php

wrong folder

* Add files via upload

remove unneeded lines

* Add files via upload

removed unneeded lines
m5kro 2 年之前
父节点
当前提交
108e9ce91b

二进制
modules/build/ZeroTier.tar.gz


+ 176 - 0
modules/src/ZeroTier/api/module.php

@@ -0,0 +1,176 @@
+<?php namespace pineapple;
+// 2023 - m5kro
+
+class ZeroTier extends Module
+{
+    public function route()
+    {
+        switch ($this->request->action) {
+
+            case "getDependenciesStatus":
+                $this->getDependenciesStatus();
+                break;
+
+            case "managerDependencies":
+                $this->managerDependencies();
+                break;
+
+            case "getDependenciesInstallStatus":
+                $this->getDependenciesInstallStatus();
+                break;
+            
+            case "getZeroTierStatus":
+                $this->getZeroTierStatus();
+                break;
+
+            case "zerotierSwitch":
+                $this->zerotierSwitch();
+                break;
+
+            case "zerotierBootSwitch":
+                $this->zerotierBootSwitch();
+                break;
+
+            case "zerotierSetID":
+                $this->zerotierSetID();
+                break;
+
+            case "zerotierGetID":
+                $this->zerotierGetID();
+                break;
+
+            case "zerotierNewIdentity":
+                $this->zerotierNewIdentity();
+                break;
+
+            case "zerotierGetIdentity":
+                $this->zerotierGetIdentity();
+                break;
+        }
+    }
+
+    // Thanks to xchwarze (DSR) for most of the dependencies code
+    protected function getDependenciesStatus()
+    {
+        $response = [
+            "installed" => false,
+            "install" => "Install",
+            "installLabel" => "success",
+            "processing" => false,
+        ];
+
+        if (file_exists("/tmp/zerotier.progress")) {
+            $response["install"] = "Installing...";
+            $response["installLabel"] = "warning";
+            $response["processing"] = true;
+        
+        } else if ($this->checkDependencyInstalled()) {
+            $response["install"] = "Remove";
+            $response["installLabel"] = "danger";
+            $response["installed"] = true;
+        }
+
+        $this->response = $response;
+    }
+
+    protected function checkDependencyInstalled()
+    {      
+        if ($this->checkDependency("zerotier-cli")) {
+            return true; 
+        }
+
+        return false;
+    }
+
+    protected function managerDependencies()
+    {
+        $action = $this->checkDependencyInstalled() ? "remove" : "install";
+        $command = "/pineapple/modules/ZeroTier/scripts/dependencies.sh";
+        $this->execBackground("{$command} {$action} {$this->request->where}");
+        $this->response = ["success" => true];
+    }
+
+    protected function getDependenciesInstallStatus()
+    {
+        $this->response = ["success" => !file_exists("/tmp/zerotier.progress")];
+    }
+
+    protected function getZeroTierStatus()
+    {
+        $response = [
+            "running" => "Start",
+            "runningLabel" => "success",
+            "boot" => "Disabled",
+            "bootLabel" => "danger",
+            "ip" => ""
+        ];
+
+        if ($this->checkRunning("zerotier-one")) {
+            $response["running"] = "Stop";
+            $response["runningLabel"] = "danger";
+            $interface = exec("/usr/bin/zerotier-cli get {$this->uciGet("zerotier.openwrt_network.join")} portDeviceName");
+            $response["ip"] = exec("/sbin/ifconfig {$interface} | /bin/grep 'inet addr' | /usr/bin/cut -d: -f2 | /usr/bin/awk '{print $1}'");
+            $this->execBackground("rm /tmp/zerotier.process");
+        } else if (file_exists("/tmp/zerotier.process")) {
+            $response["running"] = "Starting...";
+            $response["runningLabel"] = "warning";
+        } else {
+            $response["running"] = "Start";
+            $response["runningLabel"] = "success";
+        }
+
+        if (file_exists("/pineapple/modules/ZeroTier/zerotier.boot")) {
+            $response["boot"] = "Enabled";
+            $response["bootLabel"] = "success";
+        }
+
+        $this->response = $response;
+    }
+
+    protected function zerotierSwitch()
+    {
+        if ($this->checkRunning("zerotier-one")) {
+            $this->execBackground("/etc/init.d/zerotier stop && rm /tmp/zerotier.process");
+        } else {
+            $this->execBackground("touch /tmp/zerotier.process && /etc/init.d/zerotier start");
+        }
+
+    }
+
+    protected function zerotierBootSwitch()
+    {
+        if (file_exists("/pineapple/modules/ZeroTier/zerotier.boot")) {
+            $this->execBackground("/etc/init.d/zerotier disable && rm /pineapple/modules/ZeroTier/zerotier.boot");
+        } else {
+            $this->execBackground("/etc/init.d/zerotier enable && touch /pineapple/modules/ZeroTier/zerotier.boot");
+        }
+    }
+
+    protected function zerotierSetID()
+    {
+        if($this->request->ID === ""){
+            $this->uciSet("zerotier.openwrt_network.join", null);
+        } else {
+            $this->uciSet("zerotier.openwrt_network.join", null);
+            $this->uciAddList("zerotier.openwrt_network.join", $this->request->ID);
+        }
+        $this->response = ["confirm" => "Success"];
+    }
+
+    protected function zerotierGetID()
+    {
+        $this->response = ["ID" => $this->uciGet("zerotier.openwrt_network.join")];
+    }
+
+    protected function zerotierNewIdentity()
+    {
+        $this->uciSet("zerotier.openwrt_network.secret", null);
+        $this->execBackground("rm -rf /var/lib/zerotier-one && /etc/init.d/zerotier restart && /etc/init.d/zerotier stop");
+    }
+
+    protected function zerotierGetIdentity()
+    {
+        $this->response = ["identity" => $this->uciGet("zerotier.openwrt_network.secret")];
+    }
+
+}

+ 157 - 0
modules/src/ZeroTier/js/module.js

@@ -0,0 +1,157 @@
+// 2023 - m5kro
+
+// Thanks to xchwarze (DSR) for most of the dependencies code
+registerController('ZeroTierDependencies', ['$api', '$scope', '$rootScope', '$interval', '$timeout', function($api, $scope, $rootScope, $interval, $timeout) {
+    $scope.install = "Loading...";
+    $scope.installLabel = "";
+    $scope.processing = false;
+    $rootScope.zerotier_installedDependencies = false;
+    $rootScope.rebootWhenDone = false;
+
+    $scope.refreshStatus = function () {
+        $rootScope.zerotier_installedDependencies = false;
+
+        $api.request({
+            module: "ZeroTier",
+            action: "getDependenciesStatus"
+        }, function (response) {
+            $rootScope.zerotier_installedDependencies = response.installed;
+            $scope.processing = response.processing;
+            $scope.install = response.install;
+            $scope.installLabel = response.installLabel;
+
+            if ($scope.processing) {
+                $scope.getDependenciesInstallStatus();
+            }
+        });
+    };
+
+    $scope.getDependenciesInstallStatus = function () {
+        var dependenciesInstallStatusInterval = $interval(function () {
+            $api.request({
+                module: "ZeroTier",
+                action: "getDependenciesInstallStatus"
+            }, function (response) {
+                if (response.success === true) {
+                    $scope.processing = false;
+                    $scope.refreshStatus();
+                    $interval.cancel(dependenciesInstallStatusInterval);
+                }
+            });
+        }, 2000);
+    };
+
+    $scope.managerDependencies = function (dest) {
+        $scope.install = $rootScope.zerotier_installedDependencies ? "Removing..." : "Installing...";
+        $api.request({
+            module: "ZeroTier",
+            action: "managerDependencies",
+            where: dest
+        }, function (response) {
+            if (response.success === true) {
+                $scope.installLabel = "warning";
+                $scope.processing = true;
+                if (dest != "remove"){
+                    $rootScope.rebootWhenDone = true;
+                }
+                $scope.getDependenciesInstallStatus();
+            }
+        });
+    };
+
+    $scope.rebootNow = function () {
+        alert("Rebooting!");
+        $api.request({
+            module: "Configuration",
+            action: "rebootPineapple"
+        });
+    };
+
+    $scope.refreshStatus();
+
+}]);
+
+
+// Start Stop Zerotier
+registerController('ZeroTierController', ['$api', '$scope', '$rootScope', '$interval', '$timeout', function($api, $scope, $rootScope, $interval, $timeout) {
+
+    $scope.running = "Loading...";
+    $scope.runningLabel = "";
+
+    $scope.boot = "Loading...";
+    $scope.bootLabel = "";
+
+    $scope.identity = "";
+    $scope.ip = "";
+    $scope.setIDStatus = "";
+
+    $scope.getIdentity = function () {
+        $api.request({
+            module: "ZeroTier",
+            action: "zerotierGetIdentity"
+        }, function (response) {
+            $scope.identity = response.identity.slice(0,10);
+        });
+    };
+
+    $scope.refreshZeroTierStatus = function () {
+        var dependenciesInstallStatusInterval = $interval(function () {
+            $api.request({
+                module: "ZeroTier",
+                action: "getZeroTierStatus"
+            }, function (response) {
+                $scope.running = response.running;
+                $scope.runningLabel = response.runningLabel;
+                $scope.boot = response.boot;
+                $scope.bootLabel = response.bootLabel;
+                $scope.ip = response.ip;
+                $scope.setIDStatus = ""
+                $scope.getIdentity();
+            });
+        }, 2000);
+    };
+
+    $scope.zerotierSetID = function () {
+        $api.request({
+            module: "ZeroTier",
+            action: "zerotierSetID",
+            ID: $scope.ID
+        }, function (response) {
+            $scope.setIDStatus = response.confirm;
+        });
+    };
+
+    $scope.zerotierGetID = function () {
+        $api.request({
+            module: "ZeroTier",
+            action: "zerotierGetID",
+        }, function (response) {
+            $scope.ID = response.ID;
+        });
+    };
+
+    $scope.ID = $scope.zerotierGetID();
+
+    $scope.zerotierSwitch = function () {
+        $api.request({
+            module: "ZeroTier",
+            action: "zerotierSwitch"
+        });
+    };
+
+    $scope.zerotierNewIdentity = function () {
+        $api.request({
+            module: "ZeroTier",
+            action: "zerotierNewIdentity"
+        });
+    };
+
+    $scope.zerotierBootSwitch = function () {
+        $api.request({
+            module: "ZeroTier",
+            action: "zerotierBootSwitch"
+        });
+    };
+
+    $scope.refreshZeroTierStatus();
+}]);

+ 145 - 0
modules/src/ZeroTier/module.html

@@ -0,0 +1,145 @@
+<div class="panel panel-default">
+	<div class="panel-heading">
+		<h4 class="panel-title pull-left">ZeroTier</h4>
+		<span class="pull-right">v1.1</span>
+		<div class="clearfix"></div>
+	</div>
+</div>
+
+<div class="row">
+	<div class="col-md-4" ng-controller="ZeroTierDependencies">
+		<div class="panel panel-default">
+			<div class="panel-heading">
+				<h3 class="panel-title">Dependencies</h3>
+			</div>
+
+			<div class="panel-body">
+				<table style="width:100%">
+					<tr>
+                        <td style="padding-bottom: .5em;" class="text-muted">Dependencies</td>
+						<td ng-hide="$root.zerotier_installedDependencies" style="text-align:right;padding-bottom: .5em;">
+							<button type="button" style="width: 90px;" class="btn btn-{{installLabel}} btn-xs" data-toggle="modal" data-target="#dependenciesInstallModal" ng-disabled="processing">
+								{{install}}
+							</button>
+						</td>
+						<td ng-show="$root.zerotier_installedDependencies" style="text-align:right;padding-bottom: .5em;">
+							<button type="button" style="width: 90px;" class="btn btn-{{installLabel}} btn-xs" data-toggle="modal" data-target="#dependenciesRemoveModal" ng-disabled="processing">
+								{{install}}
+							</button>
+						</td>
+                    </tr>
+                </table>
+				<table style="width:100%">
+					<tr ng-show="$root.rebootWhenDone&&$root.zerotier_installedDependencies">
+						<td style="padding-bottom: .5em;" class="text-muted">Please reboot after installing dependencies!</td>
+						<td style="text-align:right;padding-bottom: .5em;">
+							<button type="button" style="width: 90px;" class="btn btn-warning btn-xs" data-toggle="modal" ng-click="rebootNow()">
+								Reboot
+							</button>
+						</td>
+					</tr>
+				</table>
+			</div>
+
+			<div class="modal fade" id="dependenciesInstallModal" tabindex="-1" role="dialog" aria-labelledby="dependenciesModalLabel">
+				<div class="modal-dialog" role="document">
+				  <div class="modal-content">
+					<div class="modal-header">
+					  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
+					  <h4 class="modal-title" id="dependenciesInstallModalLabel">Install dependencies</h4>
+					</div>
+					<div class="modal-body">
+					  All required dependencies have to be installed first. Kmod-tun will always be installed in root due to compatability issues. This may take a few minutes.<br /><br />
+					  Please wait, do not leave or refresh this page. Once the install is complete, this page will refresh automatically. 
+					  Please reboot after dependencies are installed. This is only required once.
+					</div>
+					<div class="modal-footer">
+					  <button type="button" class="btn btn-info" ng-click="managerDependencies('internal')" data-dismiss="modal">Internal</button>
+					  <button type="button" class="btn btn-info" ng-click="managerDependencies('sd')" data-dismiss="modal">SD Card</button>
+					</div>
+				  </div>
+				</div>
+			  </div>
+
+			  <div class="modal fade" id="dependenciesRemoveModal" tabindex="-1" role="dialog" aria-labelledby="dependenciesModalLabel">
+				<div class="modal-dialog" role="document">
+				  <div class="modal-content">
+					<div class="modal-header">
+					  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
+					  <h4 class="modal-title" id="dependenciesRemoveModalLabel">Remove dependencies</h4>
+					</div>
+					<div class="modal-body">
+					  All required dependencies will be removed. This may take a few minutes.<br /><br />
+					  Please wait, do not leave or refresh this page. Once the remove is complete, this page will refresh automatically.
+					</div>
+					<div class="modal-footer">
+					  <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
+					  <button type="button" class="btn btn-info" ng-click="managerDependencies('remove')" data-dismiss="modal">Confirm</button>
+					</div>
+				  </div>
+				</div>
+			  </div>
+
+		</div>
+	</div>
+</div>
+
+<div class="row">
+	<div class="col-md-4" ng-controller="ZeroTierController">
+		<div class="panel panel-default" ng-show="$root.zerotier_installedDependencies">
+			<div class="panel-heading">
+				<h3 class="panel-title">Controls</h3>
+			</div>
+
+			<div class="panel-body">
+				<table style="width:100%">
+					<td style="padding-bottom: .5em;" class="text-muted">Start ZeroTier Service</td>
+					<td style="text-align:right;padding-bottom: .5em;">
+						<button type="button" style="width: 90px;" class="btn btn-{{runningLabel}} btn-xs" data-toggle="modal" ng-click="zerotierSwitch()">
+							{{running}}
+						</button>
+					</td>
+				</table>
+
+				<table style="width:100%">
+					<td style="padding-bottom: .5em;" class="text-muted">Start ZeroTier On Boot</td>
+					<td style="text-align:right;padding-bottom: .5em;">
+						<button type="button" style="width: 90px;" class="btn btn-{{bootLabel}} btn-xs" data-toggle="modal" ng-click="zerotierBootSwitch()">
+							{{boot}}
+						</button>
+					</td>
+				</table>
+
+				<table style="width:100%">
+					<td style="padding-bottom: .5em;" class="text-muted">New Identity (Warning: may take a while)</td>
+					<td style="text-align:right;padding-bottom: .5em;">
+						<button type="button" style="width: 90px;" class="btn btn-warning btn-xs" data-toggle="modal" ng-click="zerotierNewIdentity()">
+							Reset
+						</button>
+					</td>
+				</table>
+				
+				<table>
+					<td style="padding-bottom: .5em;" class="text">Identity: {{identity}}</td>
+				</table>
+
+				<table>
+					<td style="padding-bottom: .5em;" class="text">IP: {{ip}}</td>
+				</table>
+
+				<div class="form-group">
+					<div class="input-group">
+						<span class="input-group-addon input-sm">Network ID</span>
+						<input type="text" class="form-control input-sm" ng-model="ID" placeholder="Network ID">
+					</div>
+				</div>
+				<td style="text-align:right;padding-bottom: .5em;">
+					<button type="button" style="width: 90px;" class="btn btn-success btn-xs" data-toggle="modal" ng-click="zerotierSetID()">
+						Set ID
+					</button>
+				</td>  
+				<td style="padding-bottom: .5em;" class="text-muted">{{setIDStatus}}</td>
+			</div>
+		</div>
+	</div>
+</div>

+ 6 - 0
modules/src/ZeroTier/module.info

@@ -0,0 +1,6 @@
+{
+    "title": "ZeroTier",
+    "description": "Connect to a ZeroTier-one network.",
+    "version": "1.1",
+    "author": "m5kro"
+}

+ 48 - 0
modules/src/ZeroTier/scripts/dependencies.sh

@@ -0,0 +1,48 @@
+#!/bin/sh
+# 2023 - m5kro
+
+[[ -f /tmp/zerotier.progress ]] && {
+  exit 0
+}
+
+touch /tmp/zerotier.progress
+
+if [ "$1" = "install" ]; then
+  opkg update
+  if [ "$2" = "internal" ]; then
+     opkg install zerotier
+
+# kmod-tun installed at root due to /dev/net/tun missing issues
+  elif [ "$2" = "sd" ]; then
+     opkg install kmod-tun
+     opkg -d sd install zerotier
+     cp /sd/etc/config/zerotier /etc/config/
+     ln -s /sd/etc/init.d/zerotier /etc/init.d/
+     ln -s /sd/usr/bin/zerotier-one /usr/bin/
+     ln -s /sd/usr/bin/zerotier-cli /usr/bin/
+     ln -s /sd/usr/lib/libminiupnpc.so.17 /lib/libminiupnpc.so.17
+     ln -s /sd/usr/lib/libnatpmp.so.1 /lib/libnatpmp.so.1
+
+  fi
+
+  uci set zerotier.openwrt_network=zerotier
+  uci add_list zerotier.openwrt_network.join=''
+  uci set zerotier.openwrt_network.enabled='1'
+  uci commit zerotier
+  /etc/init.d/zerotier start
+  /etc/init.d/zerotier stop
+  /etc/init.d/zerotier disable
+
+elif [ "$1" = "remove" ]; then
+    opkg remove zerotier
+    opkg remove kmod-tun
+    opkg remove libmnl
+    opkg remove ip-tiny
+    opkg remove libminiupnpc
+    opkg remove libnatpmp
+    rm -rf /etc/config/zerotier
+    rm -rf /sd/etc/config/zerotier
+    rm -rf /var/lib/zerotier-one
+fi
+
+rm /tmp/zerotier.progress