ControlPlane.cpp 18 KB

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