module.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php namespace pineapple;
  2. require_once('AccessPoint.php');
  3. require_once('ClientMode.php');
  4. require_once('Interfaces.php');
  5. class Networking extends SystemModule
  6. {
  7. public function route()
  8. {
  9. switch ($this->request->action) {
  10. case 'getRoutingTable':
  11. $this->getRoutingTable();
  12. break;
  13. case 'restartDNS':
  14. $this->restartDNS();
  15. break;
  16. case 'updateRoute':
  17. $this->updateRoute();
  18. break;
  19. case 'getAdvancedData':
  20. $this->getAdvancedData();
  21. break;
  22. case 'setHostname':
  23. $this->setHostname();
  24. break;
  25. case 'resetWirelessConfig':
  26. $this->resetWirelessConfig();
  27. break;
  28. case 'getInterfaceList':
  29. $this->getInterfaceList();
  30. break;
  31. case 'saveAPConfig':
  32. $this->saveAPConfig();
  33. break;
  34. case 'getAPConfig':
  35. $this->getAPConfig();
  36. break;
  37. case 'getMacData':
  38. $this->getMacData();
  39. break;
  40. case 'setMac':
  41. $this->setMac(false);
  42. break;
  43. case 'setRandomMac':
  44. $this->setMac(true);
  45. break;
  46. case 'resetMac':
  47. $this->resetMac();
  48. break;
  49. case 'scanForNetworks':
  50. $this->scanForNetworks();
  51. break;
  52. case 'getClientInterfaces':
  53. $this->getClientInterfaces();
  54. break;
  55. case 'connectToAP':
  56. $this->connectToAP();
  57. break;
  58. case 'checkConnection':
  59. $this->checkConnection();
  60. break;
  61. case 'disconnect':
  62. $this->disconnect();
  63. break;
  64. case 'genOUIProcess':
  65. $this->genOUIProcess();
  66. break;
  67. case 'getFirewallConfig':
  68. $this->getFirewallConfig();
  69. break;
  70. case 'setFirewallConfig':
  71. $this->setFirewallConfig();
  72. break;
  73. case 'saveWirelessConfig':
  74. $this->saveWirelessConfig();
  75. break;
  76. case 'getInfoData':
  77. $this->getInfoData();
  78. break;
  79. case 'interfaceActions':
  80. $this->interfaceActions();
  81. break;
  82. }
  83. }
  84. private function getRoutingTable()
  85. {
  86. exec('ifconfig | grep encap:Ethernet | awk "{print \$1}"', $routeInterfaces);
  87. exec('route', $routingTable);
  88. $routingTable = implode("\n", $routingTable);
  89. $this->response = ['routeTable' => $routingTable, 'routeInterfaces' => $routeInterfaces];
  90. }
  91. private function restartDNS()
  92. {
  93. $this->execBackground('/etc/init.d/dnsmasq restart');
  94. $this->response = ["success" => true];
  95. }
  96. private function updateRoute()
  97. {
  98. $routeInterface = escapeshellarg($this->request->routeInterface);
  99. $routeIP = escapeshellarg($this->request->routeIP);
  100. exec("route del default");
  101. exec("route add default gw {$routeIP} {$routeInterface}");
  102. $this->response = ["success" => true];
  103. }
  104. private function getAdvancedData()
  105. {
  106. $this->response = [
  107. "hostname" => gethostname(),
  108. "wireless" => file_get_contents('/etc/config/wireless')
  109. ];
  110. }
  111. private function setHostname()
  112. {
  113. exec("uci set system.@system[0].hostname=" . escapeshellarg($this->request->hostname));
  114. exec("uci commit system");
  115. exec("echo $(uci get system.@system[0].hostname) > /proc/sys/kernel/hostname");
  116. $this->response = ["success" => true];
  117. }
  118. private function resetWirelessConfig()
  119. {
  120. $interfaceHelper = new \helper\Interfaces();
  121. $this->response = $interfaceHelper->resetWirelessConfig();
  122. }
  123. private function getInterfaceList()
  124. {
  125. $interfaceHelper = new \helper\Interfaces();
  126. $this->response = $interfaceHelper->getInterfaceList();
  127. }
  128. private function saveAPConfig()
  129. {
  130. $accessPointHelper = new \helper\AccessPoint();
  131. $config = $this->request->apConfig;
  132. if (empty($config->openSSID) || empty($config->managementSSID)) {
  133. $this->error = "Error: SSIDs must be at least one character.";
  134. return;
  135. }
  136. if (strlen($config->managementKey) < 8 && $config->disableManagementAP == false) {
  137. $this->error = "Error: WPA2 Passwords must be at least 8 characters long.";
  138. return;
  139. }
  140. $this->response = $accessPointHelper->saveAPConfig($config);
  141. }
  142. private function getAPConfig()
  143. {
  144. $accessPointHelper = new \helper\AccessPoint();
  145. $this->response = $accessPointHelper->getAPConfig();
  146. }
  147. private function getMacData()
  148. {
  149. $interfaceHelper = new \helper\Interfaces();
  150. $this->response = $interfaceHelper->getMacData();
  151. }
  152. private function setMac($force_random)
  153. {
  154. $interfaceHelper = new \helper\Interfaces();
  155. $this->response = $interfaceHelper->setMac($force_random, $this->request->interface, $this->request->mac, $this->request->forceReload);
  156. }
  157. private function resetMac()
  158. {
  159. $interfaceHelper = new \helper\Interfaces();
  160. $this->response = $interfaceHelper->resetMac($this->request->interface);
  161. }
  162. private function checkConnection()
  163. {
  164. $clientModeHelper = new \helper\ClientMode();
  165. $this->response = $clientModeHelper->checkConnection();
  166. }
  167. private function disconnect()
  168. {
  169. $interfaceHelper = new \helper\Interfaces();
  170. $clientModeHelper = new \helper\ClientMode();
  171. $interface = $this->request->interface;
  172. $uciID = $interfaceHelper->getUciID($interface);
  173. $radioID = $interfaceHelper->getRadioID($interface);
  174. $this->response = $clientModeHelper->disconnect($uciID, $radioID);
  175. }
  176. private function connectToAP()
  177. {
  178. $interfaceHelper = new \helper\Interfaces();
  179. $clientModeHelper = new \helper\ClientMode();
  180. $accessPointHelper = new \helper\AccessPoint();
  181. $interface = $this->request->interface;
  182. $uciID = $interfaceHelper->getUciID($interface);
  183. $radioID = $interfaceHelper->getRadioID($interface);
  184. $apConfig = $accessPointHelper->getAPConfig(false);
  185. if ($radioID === false) {
  186. exec('wifi config > /etc/config/wireless && wifi');
  187. $accessPointHelper->saveAPConfig($apConfig);
  188. $radioID = $interfaceHelper->getRadioID($interface);
  189. }
  190. $this->response = $clientModeHelper->connectToAP($uciID, $this->request->ap, $this->request->key, $radioID);
  191. }
  192. private function scanForNetworks()
  193. {
  194. $interfaceHelper = new \helper\Interfaces();
  195. $clientModeHelper = new \helper\ClientMode();
  196. $interface = $this->request->interface;
  197. $uciID = $interfaceHelper->getUciID($interface);
  198. $radioID = $interfaceHelper->getRadioID($interface);
  199. $this->response = $clientModeHelper->scanForNetworks($interface, $uciID, $radioID);
  200. }
  201. private function getClientInterfaces()
  202. {
  203. $interfaceHelper = new \helper\Interfaces();
  204. $this->response = $interfaceHelper->getClientInterfaces();
  205. }
  206. private function getOUI()
  207. {
  208. $data = file_get_contents(self::REMOTE_URL . "/oui/oui.txt");
  209. if ($data !== null) {
  210. $this->response = ["ouiText" => implode("\n", $data)];
  211. } else {
  212. $this->error = "Failed to download OUI file from " . self::REMOTE_NAME;
  213. }
  214. }
  215. private function getFirewallConfig()
  216. {
  217. $this->response = [
  218. "allowWANSSH" => $this->uciGet("firewall.allowssh.enabled"),
  219. "allowWANUI" => $this->uciGet("firewall.allowui.enabled")
  220. ];
  221. }
  222. private function setFirewallConfig()
  223. {
  224. $wan = $this->request->WANSSHAccess ? 1 : 0;
  225. $ui = $this->request->WANUIAccess ? 1 : 0;
  226. $this->uciSet("firewall.allowssh.enabled", $wan);
  227. $this->uciSet("firewall.allowui.enabled", $ui);
  228. $this->uciSet("firewall.allowws.enabled", $ui);
  229. $this->execBackground('/etc/init.d/firewall restart');
  230. $this->response = ["success" => true];
  231. }
  232. private function saveWirelessConfig()
  233. {
  234. if (isset($this->request->wireless)) {
  235. file_put_contents('/etc/config/wireless', $this->request->wireless);
  236. $this->execBackground('wifi');
  237. $this->response = ["success" => true];
  238. }
  239. }
  240. private function getInfoData()
  241. {
  242. switch ((int)$this->request->type) {
  243. case 2:
  244. $command = 'iw dev';
  245. break;
  246. case 3:
  247. $command = 'airmon-ng';
  248. break;
  249. default:
  250. $command = 'ifconfig -a';
  251. }
  252. exec($command, $info);
  253. $this->response = implode("\n", $info);
  254. }
  255. private function interfaceActions()
  256. {
  257. $interface = escapeshellarg($this->request->interface);
  258. switch ((int)$this->request->type) {
  259. case 2:
  260. $command = "ifconfig {$interface} down";
  261. break;
  262. case 3:
  263. $command = "airmon-ng start {$interface}";
  264. break;
  265. case 4:
  266. $command = "airmon-ng stop {$interface}";
  267. break;
  268. default:
  269. $command = "ifconfig {$interface} up";
  270. }
  271. exec($command, $info);
  272. $this->response = implode("\n", $info);
  273. }
  274. }