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