ControlPlane.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "ControlPlane.hpp"
  19. #include "OneService.hpp"
  20. #include "../version.h"
  21. #include "../include/ZeroTierOne.h"
  22. #ifdef ZT_USE_SYSTEM_HTTP_PARSER
  23. #include <http_parser.h>
  24. #else
  25. #include "../ext/http-parser/http_parser.h"
  26. #endif
  27. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  28. #include "../controller/SqliteNetworkController.hpp"
  29. #endif
  30. #include "../node/InetAddress.hpp"
  31. #include "../node/Node.hpp"
  32. #include "../node/Utils.hpp"
  33. #include "../osdep/OSUtils.hpp"
  34. namespace ZeroTier {
  35. static std::string _jsonEscape(const char *s)
  36. {
  37. std::string buf;
  38. for(const char *p=s;(*p);++p) {
  39. switch(*p) {
  40. case '\t': buf.append("\\t"); break;
  41. case '\b': buf.append("\\b"); break;
  42. case '\r': buf.append("\\r"); break;
  43. case '\n': buf.append("\\n"); break;
  44. case '\f': buf.append("\\f"); break;
  45. case '"': buf.append("\\\""); break;
  46. case '\\': buf.append("\\\\"); break;
  47. case '/': buf.append("\\/"); break;
  48. default: buf.push_back(*p); break;
  49. }
  50. }
  51. return buf;
  52. }
  53. static std::string _jsonEscape(const std::string &s) { return _jsonEscape(s.c_str()); }
  54. static std::string _jsonEnumerate(const ZT_MulticastGroup *mg,unsigned int count)
  55. {
  56. std::string buf;
  57. char tmp[128];
  58. buf.push_back('[');
  59. for(unsigned int i=0;i<count;++i) {
  60. if (i > 0)
  61. buf.push_back(',');
  62. Utils::snprintf(tmp,sizeof(tmp),"\"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\\/%.8lx\"",
  63. (unsigned int)((mg[i].mac >> 40) & 0xff),
  64. (unsigned int)((mg[i].mac >> 32) & 0xff),
  65. (unsigned int)((mg[i].mac >> 24) & 0xff),
  66. (unsigned int)((mg[i].mac >> 16) & 0xff),
  67. (unsigned int)((mg[i].mac >> 8) & 0xff),
  68. (unsigned int)(mg[i].mac & 0xff),
  69. (unsigned long)(mg[i].adi));
  70. buf.append(tmp);
  71. }
  72. buf.push_back(']');
  73. return buf;
  74. }
  75. static std::string _jsonEnumerate(const struct sockaddr_storage *ss,unsigned int count)
  76. {
  77. std::string buf;
  78. buf.push_back('[');
  79. for(unsigned int i=0;i<count;++i) {
  80. if (i > 0)
  81. buf.push_back(',');
  82. buf.push_back('"');
  83. buf.append(_jsonEscape(reinterpret_cast<const InetAddress *>(&(ss[i]))->toString()));
  84. buf.push_back('"');
  85. }
  86. buf.push_back(']');
  87. return buf;
  88. }
  89. static void _jsonAppend(unsigned int depth,std::string &buf,const ZT_VirtualNetworkConfig *nc,const std::string &portDeviceName)
  90. {
  91. char json[4096];
  92. char prefix[32];
  93. if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
  94. return;
  95. for(unsigned int i=0;i<depth;++i)
  96. prefix[i] = '\t';
  97. prefix[depth] = '\0';
  98. const char *nstatus = "",*ntype = "";
  99. switch(nc->status) {
  100. case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
  101. case ZT_NETWORK_STATUS_OK: nstatus = "OK"; break;
  102. case ZT_NETWORK_STATUS_ACCESS_DENIED: nstatus = "ACCESS_DENIED"; break;
  103. case ZT_NETWORK_STATUS_NOT_FOUND: nstatus = "NOT_FOUND"; break;
  104. case ZT_NETWORK_STATUS_PORT_ERROR: nstatus = "PORT_ERROR"; break;
  105. case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: nstatus = "CLIENT_TOO_OLD"; break;
  106. }
  107. switch(nc->type) {
  108. case ZT_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
  109. case ZT_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
  110. }
  111. Utils::snprintf(json,sizeof(json),
  112. "%s{\n"
  113. "%s\t\"nwid\": \"%.16llx\",\n"
  114. "%s\t\"mac\": \"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\",\n"
  115. "%s\t\"name\": \"%s\",\n"
  116. "%s\t\"status\": \"%s\",\n"
  117. "%s\t\"type\": \"%s\",\n"
  118. "%s\t\"mtu\": %u,\n"
  119. "%s\t\"dhcp\": %s,\n"
  120. "%s\t\"bridge\": %s,\n"
  121. "%s\t\"broadcastEnabled\": %s,\n"
  122. "%s\t\"portError\": %d,\n"
  123. "%s\t\"netconfRevision\": %lu,\n"
  124. "%s\t\"multicastSubscriptions\": %s,\n"
  125. "%s\t\"assignedAddresses\": %s,\n"
  126. "%s\t\"portDeviceName\": \"%s\"\n"
  127. "%s}",
  128. prefix,
  129. prefix,nc->nwid,
  130. prefix,(unsigned int)((nc->mac >> 40) & 0xff),(unsigned int)((nc->mac >> 32) & 0xff),(unsigned int)((nc->mac >> 24) & 0xff),(unsigned int)((nc->mac >> 16) & 0xff),(unsigned int)((nc->mac >> 8) & 0xff),(unsigned int)(nc->mac & 0xff),
  131. prefix,_jsonEscape(nc->name).c_str(),
  132. prefix,nstatus,
  133. prefix,ntype,
  134. prefix,nc->mtu,
  135. prefix,(nc->dhcp == 0) ? "false" : "true",
  136. prefix,(nc->bridge == 0) ? "false" : "true",
  137. prefix,(nc->broadcastEnabled == 0) ? "false" : "true",
  138. prefix,nc->portError,
  139. prefix,nc->netconfRevision,
  140. prefix,_jsonEnumerate(nc->multicastSubscriptions,nc->multicastSubscriptionCount).c_str(),
  141. prefix,_jsonEnumerate(nc->assignedAddresses,nc->assignedAddressCount).c_str(),
  142. prefix,_jsonEscape(portDeviceName).c_str(),
  143. prefix);
  144. buf.append(json);
  145. }
  146. static std::string _jsonEnumerate(unsigned int depth,const ZT_PeerPhysicalPath *pp,unsigned int count)
  147. {
  148. char json[1024];
  149. char prefix[32];
  150. if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
  151. return std::string();
  152. for(unsigned int i=0;i<depth;++i)
  153. prefix[i] = '\t';
  154. prefix[depth] = '\0';
  155. std::string buf;
  156. for(unsigned int i=0;i<count;++i) {
  157. if (i > 0)
  158. buf.push_back(',');
  159. Utils::snprintf(json,sizeof(json),
  160. "{\n"
  161. "%s\t\"address\": \"%s\",\n"
  162. "%s\t\"lastSend\": %llu,\n"
  163. "%s\t\"lastReceive\": %llu,\n"
  164. "%s\t\"active\": %s,\n"
  165. "%s\t\"preferred\": %s\n"
  166. "%s}",
  167. prefix,_jsonEscape(reinterpret_cast<const InetAddress *>(&(pp[i].address))->toString()).c_str(),
  168. prefix,pp[i].lastSend,
  169. prefix,pp[i].lastReceive,
  170. prefix,(pp[i].active == 0) ? "false" : "true",
  171. prefix,(pp[i].preferred == 0) ? "false" : "true",
  172. prefix);
  173. buf.append(json);
  174. }
  175. return buf;
  176. }
  177. static void _jsonAppend(unsigned int depth,std::string &buf,const ZT_Peer *peer)
  178. {
  179. char json[1024];
  180. char prefix[32];
  181. if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
  182. return;
  183. for(unsigned int i=0;i<depth;++i)
  184. prefix[i] = '\t';
  185. prefix[depth] = '\0';
  186. const char *prole = "";
  187. switch(peer->role) {
  188. case ZT_PEER_ROLE_LEAF: prole = "LEAF"; break;
  189. case ZT_PEER_ROLE_RELAY: prole = "RELAY"; break;
  190. case ZT_PEER_ROLE_ROOT: prole = "ROOT"; break;
  191. }
  192. Utils::snprintf(json,sizeof(json),
  193. "%s{\n"
  194. "%s\t\"address\": \"%.10llx\",\n"
  195. "%s\t\"lastUnicastFrame\": %llu,\n"
  196. "%s\t\"lastMulticastFrame\": %llu,\n"
  197. "%s\t\"versionMajor\": %d,\n"
  198. "%s\t\"versionMinor\": %d,\n"
  199. "%s\t\"versionRev\": %d,\n"
  200. "%s\t\"version\": \"%d.%d.%d\",\n"
  201. "%s\t\"latency\": %u,\n"
  202. "%s\t\"role\": \"%s\",\n"
  203. "%s\t\"paths\": [%s]\n"
  204. "%s}",
  205. prefix,
  206. prefix,peer->address,
  207. prefix,peer->lastUnicastFrame,
  208. prefix,peer->lastMulticastFrame,
  209. prefix,peer->versionMajor,
  210. prefix,peer->versionMinor,
  211. prefix,peer->versionRev,
  212. prefix,peer->versionMajor,peer->versionMinor,peer->versionRev,
  213. prefix,peer->latency,
  214. prefix,prole,
  215. prefix,_jsonEnumerate(depth+1,peer->paths,peer->pathCount).c_str(),
  216. prefix);
  217. buf.append(json);
  218. }
  219. ControlPlane::ControlPlane(OneService *svc,Node *n,const char *uiStaticPath) :
  220. _svc(svc),
  221. _node(n),
  222. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  223. _controller((SqliteNetworkController *)0),
  224. #endif
  225. _uiStaticPath((uiStaticPath) ? uiStaticPath : "")
  226. {
  227. }
  228. ControlPlane::~ControlPlane()
  229. {
  230. }
  231. unsigned int ControlPlane::handleRequest(
  232. const InetAddress &fromAddress,
  233. unsigned int httpMethod,
  234. const std::string &path,
  235. const std::map<std::string,std::string> &headers,
  236. const std::string &body,
  237. std::string &responseBody,
  238. std::string &responseContentType)
  239. {
  240. char json[8194];
  241. unsigned int scode = 404;
  242. std::vector<std::string> ps(Utils::split(path.c_str(),"/","",""));
  243. std::map<std::string,std::string> urlArgs;
  244. Mutex::Lock _l(_lock);
  245. if (!((fromAddress.ipsEqual(InetAddress::LO4))||(fromAddress.ipsEqual(InetAddress::LO6))))
  246. return 403; // Forbidden: we only allow access from localhost right now
  247. /* Note: this is kind of restricted in what it'll take. It does not support
  248. * URL encoding, and /'s in URL args will screw it up. But the only URL args
  249. * it really uses in ?jsonp=funcionName, and otherwise it just takes simple
  250. * paths to simply-named resources. */
  251. if (ps.size() > 0) {
  252. std::size_t qpos = ps[ps.size() - 1].find('?');
  253. if (qpos != std::string::npos) {
  254. std::string args(ps[ps.size() - 1].substr(qpos + 1));
  255. ps[ps.size() - 1] = ps[ps.size() - 1].substr(0,qpos);
  256. std::vector<std::string> asplit(Utils::split(args.c_str(),"&","",""));
  257. for(std::vector<std::string>::iterator a(asplit.begin());a!=asplit.end();++a) {
  258. std::size_t eqpos = a->find('=');
  259. if (eqpos == std::string::npos)
  260. urlArgs[*a] = "";
  261. else urlArgs[a->substr(0,eqpos)] = a->substr(eqpos + 1);
  262. }
  263. }
  264. } else {
  265. ps.push_back(std::string("index.html"));
  266. }
  267. bool isAuth = false;
  268. {
  269. std::map<std::string,std::string>::const_iterator ah(headers.find("x-zt1-auth"));
  270. if ((ah != headers.end())&&(_authTokens.count(ah->second) > 0)) {
  271. isAuth = true;
  272. } else {
  273. ah = urlArgs.find("auth");
  274. if ((ah != urlArgs.end())&&(_authTokens.count(ah->second) > 0))
  275. isAuth = true;
  276. }
  277. }
  278. if (httpMethod == HTTP_GET) {
  279. std::string ext;
  280. std::size_t dotIdx = ps[0].find_last_of('.');
  281. if (dotIdx != std::string::npos)
  282. ext = ps[0].substr(dotIdx);
  283. if ((ps.size() == 1)&&(ext.length() >= 2)&&(ext[0] == '.')) {
  284. /* Static web pages can be served without authentication to enable a simple web
  285. * UI. This is still only allowed from approved IP addresses. Anything with a
  286. * dot in the first path element (e.g. foo.html) is considered a static page,
  287. * as nothing in the API is so named. */
  288. if (_uiStaticPath.length() > 0) {
  289. if (ext == ".html")
  290. responseContentType = "text/html";
  291. else if (ext == ".js")
  292. responseContentType = "application/javascript";
  293. else if (ext == ".jsx")
  294. responseContentType = "text/jsx";
  295. else if (ext == ".json")
  296. responseContentType = "application/json";
  297. else if (ext == ".css")
  298. responseContentType = "text/css";
  299. else if (ext == ".png")
  300. responseContentType = "image/png";
  301. else if (ext == ".jpg")
  302. responseContentType = "image/jpeg";
  303. else if (ext == ".gif")
  304. responseContentType = "image/gif";
  305. else if (ext == ".txt")
  306. responseContentType = "text/plain";
  307. else if (ext == ".xml")
  308. responseContentType = "text/xml";
  309. else if (ext == ".svg")
  310. responseContentType = "image/svg+xml";
  311. else responseContentType = "application/octet-stream";
  312. scode = OSUtils::readFile((_uiStaticPath + ZT_PATH_SEPARATOR_S + ps[0]).c_str(),responseBody) ? 200 : 404;
  313. } else {
  314. scode = 404;
  315. }
  316. } else if (isAuth) {
  317. /* Things that require authentication -- a.k.a. everything but static web app pages. */
  318. if (ps[0] == "status") {
  319. responseContentType = "application/json";
  320. ZT_NodeStatus status;
  321. _node->status(&status);
  322. std::string clusterJson;
  323. #ifdef ZT_ENABLE_CLUSTER
  324. {
  325. ZT_ClusterStatus cs;
  326. _node->clusterStatus(&cs);
  327. if (cs.clusterSize >= 1) {
  328. char t[1024];
  329. Utils::snprintf(t,sizeof(t),"{\n\t\t\"myId\": %u,\n\t\t\"clusterSize\": %u,\n\t\t\"members\": [",cs.myId,cs.clusterSize);
  330. clusterJson.append(t);
  331. for(unsigned int i=0;i<cs.clusterSize;++i) {
  332. Utils::snprintf(t,sizeof(t),"%s\t\t\t{\n\t\t\t\t\"id\": %u,\n\t\t\t\t\"msSinceLastHeartbeat\": %u,\n\t\t\t\t\"alive\": %s,\n\t\t\t\t\"x\": %d,\n\t\t\t\t\"y\": %d,\n\t\t\t\t\"z\": %d,\n\t\t\t\t\"load\": %llu,\n\t\t\t\t\"peers\": %llu\n\t\t\t}",
  333. ((i == 0) ? "\n" : ",\n"),
  334. cs.members[i].id,
  335. cs.members[i].msSinceLastHeartbeat,
  336. (cs.members[i].alive != 0) ? "true" : "false",
  337. cs.members[i].x,
  338. cs.members[i].y,
  339. cs.members[i].z,
  340. cs.members[i].load,
  341. cs.members[i].peers);
  342. clusterJson.append(t);
  343. }
  344. clusterJson.append(" ]\n\t\t}");
  345. }
  346. }
  347. #endif
  348. Utils::snprintf(json,sizeof(json),
  349. "{\n"
  350. "\t\"address\": \"%.10llx\",\n"
  351. "\t\"publicIdentity\": \"%s\",\n"
  352. "\t\"worldId\": %llu,\n"
  353. "\t\"worldTimestamp\": %llu,\n"
  354. "\t\"online\": %s,\n"
  355. "\t\"tcpFallbackActive\": %s,\n"
  356. "\t\"versionMajor\": %d,\n"
  357. "\t\"versionMinor\": %d,\n"
  358. "\t\"versionRev\": %d,\n"
  359. "\t\"version\": \"%d.%d.%d\",\n"
  360. "\t\"clock\": %llu,\n"
  361. "\t\"cluster\": %s\n"
  362. "}\n",
  363. status.address,
  364. status.publicIdentity,
  365. status.worldId,
  366. status.worldTimestamp,
  367. (status.online) ? "true" : "false",
  368. (_svc->tcpFallbackActive()) ? "true" : "false",
  369. ZEROTIER_ONE_VERSION_MAJOR,
  370. ZEROTIER_ONE_VERSION_MINOR,
  371. ZEROTIER_ONE_VERSION_REVISION,
  372. ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION,
  373. (unsigned long long)OSUtils::now(),
  374. ((clusterJson.length() > 0) ? clusterJson.c_str() : "null"));
  375. responseBody = json;
  376. scode = 200;
  377. } else if (ps[0] == "config") {
  378. responseContentType = "application/json";
  379. responseBody = "{}"; // TODO
  380. scode = 200;
  381. } else if (ps[0] == "network") {
  382. ZT_VirtualNetworkList *nws = _node->networks();
  383. if (nws) {
  384. if (ps.size() == 1) {
  385. // Return [array] of all networks
  386. responseContentType = "application/json";
  387. responseBody = "[\n";
  388. for(unsigned long i=0;i<nws->networkCount;++i) {
  389. if (i > 0)
  390. responseBody.append(",");
  391. _jsonAppend(1,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
  392. }
  393. responseBody.append("\n]\n");
  394. scode = 200;
  395. } else if (ps.size() == 2) {
  396. // Return a single network by ID or 404 if not found
  397. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  398. for(unsigned long i=0;i<nws->networkCount;++i) {
  399. if (nws->networks[i].nwid == wantnw) {
  400. responseContentType = "application/json";
  401. _jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
  402. responseBody.push_back('\n');
  403. scode = 200;
  404. break;
  405. }
  406. }
  407. } // else 404
  408. _node->freeQueryResult((void *)nws);
  409. } else scode = 500;
  410. } else if (ps[0] == "peer") {
  411. ZT_PeerList *pl = _node->peers();
  412. if (pl) {
  413. if (ps.size() == 1) {
  414. // Return [array] of all peers
  415. responseContentType = "application/json";
  416. responseBody = "[\n";
  417. for(unsigned long i=0;i<pl->peerCount;++i) {
  418. if (i > 0)
  419. responseBody.append(",\n");
  420. _jsonAppend(1,responseBody,&(pl->peers[i]));
  421. }
  422. responseBody.append("\n]\n");
  423. scode = 200;
  424. } else if (ps.size() == 2) {
  425. // Return a single peer by ID or 404 if not found
  426. uint64_t wantp = Utils::hexStrToU64(ps[1].c_str());
  427. for(unsigned long i=0;i<pl->peerCount;++i) {
  428. if (pl->peers[i].address == wantp) {
  429. responseContentType = "application/json";
  430. _jsonAppend(0,responseBody,&(pl->peers[i]));
  431. responseBody.push_back('\n');
  432. scode = 200;
  433. break;
  434. }
  435. }
  436. } // else 404
  437. _node->freeQueryResult((void *)pl);
  438. } else scode = 500;
  439. } else if (ps[0] == "newIdentity") {
  440. // Return a newly generated ZeroTier identity -- this is primarily for debugging
  441. // and testing to make it easy for automated test scripts to generate test IDs.
  442. Identity newid;
  443. newid.generate();
  444. responseBody = newid.toString(true);
  445. responseContentType = "text/plain";
  446. scode = 200;
  447. } else {
  448. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  449. if (_controller)
  450. scode = _controller->handleControlPlaneHttpGET(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  451. else scode = 404;
  452. #else
  453. scode = 404;
  454. #endif
  455. }
  456. } else scode = 401; // isAuth == false
  457. } else if ((httpMethod == HTTP_POST)||(httpMethod == HTTP_PUT)) {
  458. if (isAuth) {
  459. if (ps[0] == "config") {
  460. // TODO
  461. } else if (ps[0] == "network") {
  462. if (ps.size() == 2) {
  463. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  464. _node->join(wantnw,(void *)0); // does nothing if we are a member
  465. ZT_VirtualNetworkList *nws = _node->networks();
  466. if (nws) {
  467. for(unsigned long i=0;i<nws->networkCount;++i) {
  468. if (nws->networks[i].nwid == wantnw) {
  469. responseContentType = "application/json";
  470. _jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
  471. responseBody.push_back('\n');
  472. scode = 200;
  473. break;
  474. }
  475. }
  476. _node->freeQueryResult((void *)nws);
  477. } else scode = 500;
  478. }
  479. } else {
  480. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  481. if (_controller)
  482. scode = _controller->handleControlPlaneHttpPOST(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  483. else scode = 404;
  484. #else
  485. scode = 404;
  486. #endif
  487. }
  488. } else scode = 401; // isAuth == false
  489. } else if (httpMethod == HTTP_DELETE) {
  490. if (isAuth) {
  491. if (ps[0] == "config") {
  492. // TODO
  493. } else if (ps[0] == "network") {
  494. ZT_VirtualNetworkList *nws = _node->networks();
  495. if (nws) {
  496. if (ps.size() == 2) {
  497. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  498. for(unsigned long i=0;i<nws->networkCount;++i) {
  499. if (nws->networks[i].nwid == wantnw) {
  500. _node->leave(wantnw,(void **)0);
  501. responseBody = "true";
  502. responseContentType = "application/json";
  503. scode = 200;
  504. break;
  505. }
  506. }
  507. } // else 404
  508. _node->freeQueryResult((void *)nws);
  509. } else scode = 500;
  510. } else {
  511. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  512. if (_controller)
  513. scode = _controller->handleControlPlaneHttpDELETE(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  514. else scode = 404;
  515. #else
  516. scode = 404;
  517. #endif
  518. }
  519. } else {
  520. scode = 401; // isAuth = false
  521. }
  522. } else {
  523. scode = 400;
  524. responseBody = "Method not supported.";
  525. }
  526. // Wrap result in jsonp function call if the user included a jsonp= url argument.
  527. // Also double-check isAuth since forbidding this without auth feels safer.
  528. std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp"));
  529. if ((isAuth)&&(jsonp != urlArgs.end())&&(responseContentType == "application/json")) {
  530. if (responseBody.length() > 0)
  531. responseBody = jsonp->second + "(" + responseBody + ");";
  532. else responseBody = jsonp->second + "(null);";
  533. responseContentType = "application/javascript";
  534. }
  535. return scode;
  536. }
  537. } // namespace ZeroTier