2015-04-14 01:12:45 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2016-01-12 22:04:55 +00:00
|
|
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
2015-04-14 01:12:45 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ControlPlane.hpp"
|
2015-04-15 21:44:30 +00:00
|
|
|
#include "OneService.hpp"
|
2015-04-14 01:12:45 +00:00
|
|
|
|
|
|
|
#include "../version.h"
|
|
|
|
#include "../include/ZeroTierOne.h"
|
|
|
|
|
2016-06-08 19:49:07 +00:00
|
|
|
#ifdef ZT_USE_SYSTEM_HTTP_PARSER
|
|
|
|
#include <http_parser.h>
|
|
|
|
#else
|
2015-04-14 01:12:45 +00:00
|
|
|
#include "../ext/http-parser/http_parser.h"
|
2016-06-08 19:49:07 +00:00
|
|
|
#endif
|
2015-04-14 01:12:45 +00:00
|
|
|
|
2016-08-24 20:37:57 +00:00
|
|
|
#include "../ext/json/json.hpp"
|
2016-06-21 23:52:49 +00:00
|
|
|
|
2016-08-17 17:42:32 +00:00
|
|
|
#include "../controller/EmbeddedNetworkController.hpp"
|
2015-05-16 23:09:28 +00:00
|
|
|
|
2015-04-14 01:12:45 +00:00
|
|
|
#include "../node/InetAddress.hpp"
|
|
|
|
#include "../node/Node.hpp"
|
|
|
|
#include "../node/Utils.hpp"
|
2015-05-06 03:53:30 +00:00
|
|
|
#include "../osdep/OSUtils.hpp"
|
2015-04-14 20:56:28 +00:00
|
|
|
|
2015-04-14 01:12:45 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
static std::string _jsonEscape(const char *s)
|
|
|
|
{
|
|
|
|
std::string buf;
|
|
|
|
for(const char *p=s;(*p);++p) {
|
|
|
|
switch(*p) {
|
|
|
|
case '\t': buf.append("\\t"); break;
|
|
|
|
case '\b': buf.append("\\b"); break;
|
|
|
|
case '\r': buf.append("\\r"); break;
|
|
|
|
case '\n': buf.append("\\n"); break;
|
|
|
|
case '\f': buf.append("\\f"); break;
|
|
|
|
case '"': buf.append("\\\""); break;
|
|
|
|
case '\\': buf.append("\\\\"); break;
|
|
|
|
case '/': buf.append("\\/"); break;
|
2015-04-14 01:43:33 +00:00
|
|
|
default: buf.push_back(*p); break;
|
2015-04-14 01:12:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
static std::string _jsonEscape(const std::string &s) { return _jsonEscape(s.c_str()); }
|
2015-04-16 19:06:40 +00:00
|
|
|
|
2015-04-14 01:12:45 +00:00
|
|
|
static std::string _jsonEnumerate(const struct sockaddr_storage *ss,unsigned int count)
|
|
|
|
{
|
|
|
|
std::string buf;
|
|
|
|
buf.push_back('[');
|
|
|
|
for(unsigned int i=0;i<count;++i) {
|
|
|
|
if (i > 0)
|
|
|
|
buf.push_back(',');
|
|
|
|
buf.push_back('"');
|
2015-04-30 22:16:21 +00:00
|
|
|
buf.append(_jsonEscape(reinterpret_cast<const InetAddress *>(&(ss[i]))->toString()));
|
2015-04-14 01:12:45 +00:00
|
|
|
buf.push_back('"');
|
|
|
|
}
|
|
|
|
buf.push_back(']');
|
|
|
|
return buf;
|
|
|
|
}
|
2016-06-17 00:40:32 +00:00
|
|
|
static std::string _jsonEnumerate(const ZT_VirtualNetworkRoute *routes,unsigned int count)
|
|
|
|
{
|
|
|
|
std::string buf;
|
|
|
|
buf.push_back('[');
|
|
|
|
for(unsigned int i=0;i<count;++i) {
|
|
|
|
if (i > 0)
|
|
|
|
buf.push_back(',');
|
|
|
|
buf.append("{\"target\":\"");
|
|
|
|
buf.append(_jsonEscape(reinterpret_cast<const InetAddress *>(&(routes[i].target))->toString()));
|
|
|
|
buf.append("\",\"via\":");
|
|
|
|
if (routes[i].via.ss_family == routes[i].target.ss_family) {
|
|
|
|
buf.push_back('"');
|
|
|
|
buf.append(_jsonEscape(reinterpret_cast<const InetAddress *>(&(routes[i].via))->toIpString()));
|
|
|
|
buf.append("\",");
|
|
|
|
} else buf.append("null,");
|
|
|
|
char tmp[1024];
|
|
|
|
Utils::snprintf(tmp,sizeof(tmp),"\"flags\":%u,\"metric\":%u}",(unsigned int)routes[i].flags,(unsigned int)routes[i].metric);
|
|
|
|
buf.append(tmp);
|
|
|
|
}
|
|
|
|
buf.push_back(']');
|
|
|
|
return buf;
|
|
|
|
}
|
2015-04-16 19:06:40 +00:00
|
|
|
|
2016-06-21 23:52:49 +00:00
|
|
|
static void _jsonAppend(unsigned int depth,std::string &buf,const ZT_VirtualNetworkConfig *nc,const std::string &portDeviceName,const OneService::NetworkSettings &localSettings)
|
2015-04-14 01:12:45 +00:00
|
|
|
{
|
2015-04-16 19:06:40 +00:00
|
|
|
char json[4096];
|
|
|
|
char prefix[32];
|
|
|
|
|
|
|
|
if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
|
|
|
|
return;
|
|
|
|
for(unsigned int i=0;i<depth;++i)
|
|
|
|
prefix[i] = '\t';
|
|
|
|
prefix[depth] = '\0';
|
|
|
|
|
2015-04-14 01:12:45 +00:00
|
|
|
const char *nstatus = "",*ntype = "";
|
|
|
|
switch(nc->status) {
|
2015-09-24 23:21:36 +00:00
|
|
|
case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
|
|
|
|
case ZT_NETWORK_STATUS_OK: nstatus = "OK"; break;
|
|
|
|
case ZT_NETWORK_STATUS_ACCESS_DENIED: nstatus = "ACCESS_DENIED"; break;
|
|
|
|
case ZT_NETWORK_STATUS_NOT_FOUND: nstatus = "NOT_FOUND"; break;
|
|
|
|
case ZT_NETWORK_STATUS_PORT_ERROR: nstatus = "PORT_ERROR"; break;
|
|
|
|
case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: nstatus = "CLIENT_TOO_OLD"; break;
|
2015-04-14 01:12:45 +00:00
|
|
|
}
|
|
|
|
switch(nc->type) {
|
2015-09-24 23:21:36 +00:00
|
|
|
case ZT_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
|
|
|
|
case ZT_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
|
2015-04-14 01:12:45 +00:00
|
|
|
}
|
2015-04-16 19:06:40 +00:00
|
|
|
|
2015-04-14 01:12:45 +00:00
|
|
|
Utils::snprintf(json,sizeof(json),
|
2015-04-16 19:06:40 +00:00
|
|
|
"%s{\n"
|
|
|
|
"%s\t\"nwid\": \"%.16llx\",\n"
|
|
|
|
"%s\t\"mac\": \"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\",\n"
|
|
|
|
"%s\t\"name\": \"%s\",\n"
|
|
|
|
"%s\t\"status\": \"%s\",\n"
|
|
|
|
"%s\t\"type\": \"%s\",\n"
|
|
|
|
"%s\t\"mtu\": %u,\n"
|
|
|
|
"%s\t\"dhcp\": %s,\n"
|
|
|
|
"%s\t\"bridge\": %s,\n"
|
|
|
|
"%s\t\"broadcastEnabled\": %s,\n"
|
|
|
|
"%s\t\"portError\": %d,\n"
|
|
|
|
"%s\t\"netconfRevision\": %lu,\n"
|
|
|
|
"%s\t\"assignedAddresses\": %s,\n"
|
2016-06-17 00:40:32 +00:00
|
|
|
"%s\t\"routes\": %s,\n"
|
2016-06-21 23:52:49 +00:00
|
|
|
"%s\t\"portDeviceName\": \"%s\",\n"
|
|
|
|
"%s\t\"allowManaged\": %s,\n"
|
|
|
|
"%s\t\"allowGlobal\": %s,\n"
|
|
|
|
"%s\t\"allowDefault\": %s\n"
|
2015-04-16 19:06:40 +00:00
|
|
|
"%s}",
|
|
|
|
prefix,
|
|
|
|
prefix,nc->nwid,
|
|
|
|
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),
|
|
|
|
prefix,_jsonEscape(nc->name).c_str(),
|
|
|
|
prefix,nstatus,
|
|
|
|
prefix,ntype,
|
|
|
|
prefix,nc->mtu,
|
|
|
|
prefix,(nc->dhcp == 0) ? "false" : "true",
|
|
|
|
prefix,(nc->bridge == 0) ? "false" : "true",
|
|
|
|
prefix,(nc->broadcastEnabled == 0) ? "false" : "true",
|
|
|
|
prefix,nc->portError,
|
|
|
|
prefix,nc->netconfRevision,
|
|
|
|
prefix,_jsonEnumerate(nc->assignedAddresses,nc->assignedAddressCount).c_str(),
|
2016-06-17 00:40:32 +00:00
|
|
|
prefix,_jsonEnumerate(nc->routes,nc->routeCount).c_str(),
|
2015-04-16 19:06:40 +00:00
|
|
|
prefix,_jsonEscape(portDeviceName).c_str(),
|
2016-06-21 23:52:49 +00:00
|
|
|
prefix,(localSettings.allowManaged) ? "true" : "false",
|
|
|
|
prefix,(localSettings.allowGlobal) ? "true" : "false",
|
|
|
|
prefix,(localSettings.allowDefault) ? "true" : "false",
|
2015-04-16 19:06:40 +00:00
|
|
|
prefix);
|
2015-04-14 01:12:45 +00:00
|
|
|
buf.append(json);
|
|
|
|
}
|
2015-04-16 19:06:40 +00:00
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
static std::string _jsonEnumerate(unsigned int depth,const ZT_PeerPhysicalPath *pp,unsigned int count)
|
2015-04-14 01:12:45 +00:00
|
|
|
{
|
2016-09-05 22:47:22 +00:00
|
|
|
char json[2048];
|
2015-04-16 19:06:40 +00:00
|
|
|
char prefix[32];
|
|
|
|
|
|
|
|
if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
|
|
|
|
return std::string();
|
|
|
|
for(unsigned int i=0;i<depth;++i)
|
|
|
|
prefix[i] = '\t';
|
|
|
|
prefix[depth] = '\0';
|
|
|
|
|
|
|
|
std::string buf;
|
|
|
|
for(unsigned int i=0;i<count;++i) {
|
|
|
|
if (i > 0)
|
|
|
|
buf.push_back(',');
|
|
|
|
Utils::snprintf(json,sizeof(json),
|
|
|
|
"{\n"
|
|
|
|
"%s\t\"address\": \"%s\",\n"
|
|
|
|
"%s\t\"lastSend\": %llu,\n"
|
|
|
|
"%s\t\"lastReceive\": %llu,\n"
|
2016-09-07 18:13:17 +00:00
|
|
|
"%s\t\"active\": %s,\n"
|
|
|
|
"%s\t\"expired\": %s,\n"
|
2016-07-12 18:30:22 +00:00
|
|
|
"%s\t\"preferred\": %s,\n"
|
2016-07-12 20:58:15 +00:00
|
|
|
"%s\t\"trustedPathId\": %llu\n"
|
2015-04-16 19:06:40 +00:00
|
|
|
"%s}",
|
|
|
|
prefix,_jsonEscape(reinterpret_cast<const InetAddress *>(&(pp[i].address))->toString()).c_str(),
|
|
|
|
prefix,pp[i].lastSend,
|
|
|
|
prefix,pp[i].lastReceive,
|
2016-09-07 18:13:17 +00:00
|
|
|
prefix,(pp[i].expired != 0) ? "false" : "true",
|
|
|
|
prefix,(pp[i].expired == 0) ? "false" : "true",
|
2015-04-16 19:06:40 +00:00
|
|
|
prefix,(pp[i].preferred == 0) ? "false" : "true",
|
2016-07-12 18:30:22 +00:00
|
|
|
prefix,pp[i].trustedPathId,
|
2015-04-16 19:06:40 +00:00
|
|
|
prefix);
|
|
|
|
buf.append(json);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
static void _jsonAppend(unsigned int depth,std::string &buf,const ZT_Peer *peer)
|
2015-04-16 19:06:40 +00:00
|
|
|
{
|
2016-09-05 22:47:22 +00:00
|
|
|
char json[2048];
|
2015-04-16 19:06:40 +00:00
|
|
|
char prefix[32];
|
|
|
|
|
|
|
|
if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
|
|
|
|
return;
|
|
|
|
for(unsigned int i=0;i<depth;++i)
|
|
|
|
prefix[i] = '\t';
|
|
|
|
prefix[depth] = '\0';
|
|
|
|
|
2015-04-14 01:43:33 +00:00
|
|
|
const char *prole = "";
|
|
|
|
switch(peer->role) {
|
2015-09-24 23:21:36 +00:00
|
|
|
case ZT_PEER_ROLE_LEAF: prole = "LEAF"; break;
|
2016-09-13 21:27:18 +00:00
|
|
|
case ZT_PEER_ROLE_UPSTREAM: prole = "UPSTREAM"; break;
|
2015-09-24 23:21:36 +00:00
|
|
|
case ZT_PEER_ROLE_ROOT: prole = "ROOT"; break;
|
2015-04-14 01:43:33 +00:00
|
|
|
}
|
2015-04-16 19:06:40 +00:00
|
|
|
|
2015-04-14 01:43:33 +00:00
|
|
|
Utils::snprintf(json,sizeof(json),
|
2015-04-16 19:06:40 +00:00
|
|
|
"%s{\n"
|
|
|
|
"%s\t\"address\": \"%.10llx\",\n"
|
|
|
|
"%s\t\"lastUnicastFrame\": %llu,\n"
|
|
|
|
"%s\t\"lastMulticastFrame\": %llu,\n"
|
|
|
|
"%s\t\"versionMajor\": %d,\n"
|
|
|
|
"%s\t\"versionMinor\": %d,\n"
|
|
|
|
"%s\t\"versionRev\": %d,\n"
|
|
|
|
"%s\t\"version\": \"%d.%d.%d\",\n"
|
|
|
|
"%s\t\"latency\": %u,\n"
|
|
|
|
"%s\t\"role\": \"%s\",\n"
|
|
|
|
"%s\t\"paths\": [%s]\n"
|
|
|
|
"%s}",
|
|
|
|
prefix,
|
|
|
|
prefix,peer->address,
|
|
|
|
prefix,peer->lastUnicastFrame,
|
|
|
|
prefix,peer->lastMulticastFrame,
|
|
|
|
prefix,peer->versionMajor,
|
|
|
|
prefix,peer->versionMinor,
|
|
|
|
prefix,peer->versionRev,
|
|
|
|
prefix,peer->versionMajor,peer->versionMinor,peer->versionRev,
|
|
|
|
prefix,peer->latency,
|
|
|
|
prefix,prole,
|
|
|
|
prefix,_jsonEnumerate(depth+1,peer->paths,peer->pathCount).c_str(),
|
|
|
|
prefix);
|
2015-04-14 01:43:33 +00:00
|
|
|
buf.append(json);
|
2015-04-14 01:12:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 03:53:30 +00:00
|
|
|
ControlPlane::ControlPlane(OneService *svc,Node *n,const char *uiStaticPath) :
|
2015-04-14 22:55:03 +00:00
|
|
|
_svc(svc),
|
2015-05-06 03:53:30 +00:00
|
|
|
_node(n),
|
2016-08-17 17:42:32 +00:00
|
|
|
_controller((EmbeddedNetworkController *)0),
|
2015-05-06 03:53:30 +00:00
|
|
|
_uiStaticPath((uiStaticPath) ? uiStaticPath : "")
|
2015-04-14 01:12:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlPlane::~ControlPlane()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ControlPlane::handleRequest(
|
2015-04-14 20:56:28 +00:00
|
|
|
const InetAddress &fromAddress,
|
2015-04-14 01:12:45 +00:00
|
|
|
unsigned int httpMethod,
|
|
|
|
const std::string &path,
|
|
|
|
const std::map<std::string,std::string> &headers,
|
|
|
|
const std::string &body,
|
|
|
|
std::string &responseBody,
|
|
|
|
std::string &responseContentType)
|
|
|
|
{
|
2015-10-26 23:09:56 +00:00
|
|
|
char json[8194];
|
2015-04-14 01:12:45 +00:00
|
|
|
unsigned int scode = 404;
|
|
|
|
std::vector<std::string> ps(Utils::split(path.c_str(),"/","",""));
|
|
|
|
std::map<std::string,std::string> urlArgs;
|
2015-04-21 23:41:35 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2015-04-14 01:12:45 +00:00
|
|
|
|
2015-04-14 20:56:28 +00:00
|
|
|
if (!((fromAddress.ipsEqual(InetAddress::LO4))||(fromAddress.ipsEqual(InetAddress::LO6))))
|
|
|
|
return 403; // Forbidden: we only allow access from localhost right now
|
|
|
|
|
2015-04-14 01:12:45 +00:00
|
|
|
/* Note: this is kind of restricted in what it'll take. It does not support
|
|
|
|
* URL encoding, and /'s in URL args will screw it up. But the only URL args
|
|
|
|
* it really uses in ?jsonp=funcionName, and otherwise it just takes simple
|
|
|
|
* paths to simply-named resources. */
|
|
|
|
if (ps.size() > 0) {
|
|
|
|
std::size_t qpos = ps[ps.size() - 1].find('?');
|
|
|
|
if (qpos != std::string::npos) {
|
|
|
|
std::string args(ps[ps.size() - 1].substr(qpos + 1));
|
|
|
|
ps[ps.size() - 1] = ps[ps.size() - 1].substr(0,qpos);
|
|
|
|
std::vector<std::string> asplit(Utils::split(args.c_str(),"&","",""));
|
|
|
|
for(std::vector<std::string>::iterator a(asplit.begin());a!=asplit.end();++a) {
|
|
|
|
std::size_t eqpos = a->find('=');
|
|
|
|
if (eqpos == std::string::npos)
|
|
|
|
urlArgs[*a] = "";
|
|
|
|
else urlArgs[a->substr(0,eqpos)] = a->substr(eqpos + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-14 20:56:28 +00:00
|
|
|
ps.push_back(std::string("index.html"));
|
2015-04-14 01:12:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 02:14:12 +00:00
|
|
|
bool isAuth = false;
|
|
|
|
{
|
|
|
|
std::map<std::string,std::string>::const_iterator ah(headers.find("x-zt1-auth"));
|
2015-04-16 19:06:40 +00:00
|
|
|
if ((ah != headers.end())&&(_authTokens.count(ah->second) > 0)) {
|
2015-04-16 02:14:12 +00:00
|
|
|
isAuth = true;
|
2015-04-16 19:06:40 +00:00
|
|
|
} else {
|
2015-04-16 02:14:12 +00:00
|
|
|
ah = urlArgs.find("auth");
|
|
|
|
if ((ah != urlArgs.end())&&(_authTokens.count(ah->second) > 0))
|
|
|
|
isAuth = true;
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 20:56:28 +00:00
|
|
|
|
2015-04-14 01:12:45 +00:00
|
|
|
if (httpMethod == HTTP_GET) {
|
2015-04-14 20:56:28 +00:00
|
|
|
|
|
|
|
std::string ext;
|
|
|
|
std::size_t dotIdx = ps[0].find_last_of('.');
|
|
|
|
if (dotIdx != std::string::npos)
|
|
|
|
ext = ps[0].substr(dotIdx);
|
|
|
|
|
|
|
|
if ((ps.size() == 1)&&(ext.length() >= 2)&&(ext[0] == '.')) {
|
2015-04-16 19:06:40 +00:00
|
|
|
/* Static web pages can be served without authentication to enable a simple web
|
|
|
|
* UI. This is still only allowed from approved IP addresses. Anything with a
|
|
|
|
* dot in the first path element (e.g. foo.html) is considered a static page,
|
|
|
|
* as nothing in the API is so named. */
|
|
|
|
|
2015-05-06 03:53:30 +00:00
|
|
|
if (_uiStaticPath.length() > 0) {
|
|
|
|
if (ext == ".html")
|
|
|
|
responseContentType = "text/html";
|
|
|
|
else if (ext == ".js")
|
|
|
|
responseContentType = "application/javascript";
|
|
|
|
else if (ext == ".jsx")
|
|
|
|
responseContentType = "text/jsx";
|
|
|
|
else if (ext == ".json")
|
|
|
|
responseContentType = "application/json";
|
|
|
|
else if (ext == ".css")
|
|
|
|
responseContentType = "text/css";
|
|
|
|
else if (ext == ".png")
|
|
|
|
responseContentType = "image/png";
|
|
|
|
else if (ext == ".jpg")
|
|
|
|
responseContentType = "image/jpeg";
|
|
|
|
else if (ext == ".gif")
|
|
|
|
responseContentType = "image/gif";
|
|
|
|
else if (ext == ".txt")
|
|
|
|
responseContentType = "text/plain";
|
|
|
|
else if (ext == ".xml")
|
|
|
|
responseContentType = "text/xml";
|
|
|
|
else if (ext == ".svg")
|
|
|
|
responseContentType = "image/svg+xml";
|
|
|
|
else responseContentType = "application/octet-stream";
|
|
|
|
scode = OSUtils::readFile((_uiStaticPath + ZT_PATH_SEPARATOR_S + ps[0]).c_str(),responseBody) ? 200 : 404;
|
|
|
|
} else {
|
|
|
|
scode = 404;
|
|
|
|
}
|
2015-04-16 19:06:40 +00:00
|
|
|
|
2015-04-14 20:56:28 +00:00
|
|
|
} else if (isAuth) {
|
2015-04-16 19:06:40 +00:00
|
|
|
/* Things that require authentication -- a.k.a. everything but static web app pages. */
|
|
|
|
|
2015-04-14 20:56:28 +00:00
|
|
|
if (ps[0] == "status") {
|
|
|
|
responseContentType = "application/json";
|
2015-10-26 20:06:10 +00:00
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_NodeStatus status;
|
2015-04-14 20:56:28 +00:00
|
|
|
_node->status(&status);
|
2015-10-26 20:06:10 +00:00
|
|
|
|
|
|
|
std::string clusterJson;
|
|
|
|
#ifdef ZT_ENABLE_CLUSTER
|
|
|
|
{
|
|
|
|
ZT_ClusterStatus cs;
|
|
|
|
_node->clusterStatus(&cs);
|
|
|
|
|
2015-10-26 20:07:00 +00:00
|
|
|
if (cs.clusterSize >= 1) {
|
2015-10-26 23:09:56 +00:00
|
|
|
char t[1024];
|
|
|
|
Utils::snprintf(t,sizeof(t),"{\n\t\t\"myId\": %u,\n\t\t\"clusterSize\": %u,\n\t\t\"members\": [",cs.myId,cs.clusterSize);
|
2015-10-26 20:06:10 +00:00
|
|
|
clusterJson.append(t);
|
2015-10-26 20:07:00 +00:00
|
|
|
for(unsigned int i=0;i<cs.clusterSize;++i) {
|
2015-10-26 23:09:56 +00:00
|
|
|
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}",
|
|
|
|
((i == 0) ? "\n" : ",\n"),
|
2015-10-26 20:07:00 +00:00
|
|
|
cs.members[i].id,
|
|
|
|
cs.members[i].msSinceLastHeartbeat,
|
|
|
|
(cs.members[i].alive != 0) ? "true" : "false",
|
|
|
|
cs.members[i].x,
|
|
|
|
cs.members[i].y,
|
|
|
|
cs.members[i].z,
|
|
|
|
cs.members[i].load,
|
2015-10-26 23:09:56 +00:00
|
|
|
cs.members[i].peers);
|
2015-10-26 20:07:00 +00:00
|
|
|
clusterJson.append(t);
|
|
|
|
}
|
|
|
|
clusterJson.append(" ]\n\t\t}");
|
2015-10-26 20:06:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-14 20:56:28 +00:00
|
|
|
Utils::snprintf(json,sizeof(json),
|
2015-04-16 19:06:40 +00:00
|
|
|
"{\n"
|
2015-05-22 21:52:23 +00:00
|
|
|
"\t\"address\": \"%.10llx\",\n"
|
|
|
|
"\t\"publicIdentity\": \"%s\",\n"
|
2015-10-13 19:33:15 +00:00
|
|
|
"\t\"worldId\": %llu,\n"
|
|
|
|
"\t\"worldTimestamp\": %llu,\n"
|
2015-05-22 21:52:23 +00:00
|
|
|
"\t\"online\": %s,\n"
|
|
|
|
"\t\"tcpFallbackActive\": %s,\n"
|
|
|
|
"\t\"versionMajor\": %d,\n"
|
|
|
|
"\t\"versionMinor\": %d,\n"
|
|
|
|
"\t\"versionRev\": %d,\n"
|
|
|
|
"\t\"version\": \"%d.%d.%d\",\n"
|
2015-10-26 20:06:10 +00:00
|
|
|
"\t\"clock\": %llu,\n"
|
|
|
|
"\t\"cluster\": %s\n"
|
2015-04-16 19:06:40 +00:00
|
|
|
"}\n",
|
2015-04-14 20:56:28 +00:00
|
|
|
status.address,
|
|
|
|
status.publicIdentity,
|
2015-10-13 19:33:15 +00:00
|
|
|
status.worldId,
|
|
|
|
status.worldTimestamp,
|
2015-04-14 20:56:28 +00:00
|
|
|
(status.online) ? "true" : "false",
|
2015-05-22 21:52:23 +00:00
|
|
|
(_svc->tcpFallbackActive()) ? "true" : "false",
|
2015-04-14 20:56:28 +00:00
|
|
|
ZEROTIER_ONE_VERSION_MAJOR,
|
|
|
|
ZEROTIER_ONE_VERSION_MINOR,
|
|
|
|
ZEROTIER_ONE_VERSION_REVISION,
|
2015-05-15 22:20:12 +00:00
|
|
|
ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION,
|
2015-10-26 20:06:10 +00:00
|
|
|
(unsigned long long)OSUtils::now(),
|
|
|
|
((clusterJson.length() > 0) ? clusterJson.c_str() : "null"));
|
2015-04-14 20:56:28 +00:00
|
|
|
responseBody = json;
|
|
|
|
scode = 200;
|
|
|
|
} else if (ps[0] == "config") {
|
|
|
|
responseContentType = "application/json";
|
|
|
|
responseBody = "{}"; // TODO
|
|
|
|
scode = 200;
|
|
|
|
} else if (ps[0] == "network") {
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkList *nws = _node->networks();
|
2015-04-16 02:01:38 +00:00
|
|
|
if (nws) {
|
|
|
|
if (ps.size() == 1) {
|
|
|
|
// Return [array] of all networks
|
|
|
|
responseContentType = "application/json";
|
2015-04-16 19:06:40 +00:00
|
|
|
responseBody = "[\n";
|
2015-04-16 02:01:38 +00:00
|
|
|
for(unsigned long i=0;i<nws->networkCount;++i) {
|
|
|
|
if (i > 0)
|
2015-04-16 19:06:40 +00:00
|
|
|
responseBody.append(",");
|
2016-06-21 23:52:49 +00:00
|
|
|
OneService::NetworkSettings localSettings;
|
|
|
|
_svc->getNetworkSettings(nws->networks[i].nwid,localSettings);
|
|
|
|
_jsonAppend(1,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid),localSettings);
|
2015-04-16 02:01:38 +00:00
|
|
|
}
|
2015-04-16 19:06:40 +00:00
|
|
|
responseBody.append("\n]\n");
|
2015-04-16 02:01:38 +00:00
|
|
|
scode = 200;
|
|
|
|
} else if (ps.size() == 2) {
|
|
|
|
// Return a single network by ID or 404 if not found
|
|
|
|
uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
|
|
|
|
for(unsigned long i=0;i<nws->networkCount;++i) {
|
|
|
|
if (nws->networks[i].nwid == wantnw) {
|
|
|
|
responseContentType = "application/json";
|
2016-06-21 23:52:49 +00:00
|
|
|
OneService::NetworkSettings localSettings;
|
|
|
|
_svc->getNetworkSettings(nws->networks[i].nwid,localSettings);
|
|
|
|
_jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid),localSettings);
|
2015-04-16 19:06:40 +00:00
|
|
|
responseBody.push_back('\n');
|
2015-04-16 02:01:38 +00:00
|
|
|
scode = 200;
|
|
|
|
break;
|
2015-04-14 20:56:28 +00:00
|
|
|
}
|
2015-04-16 02:01:38 +00:00
|
|
|
}
|
|
|
|
} // else 404
|
|
|
|
_node->freeQueryResult((void *)nws);
|
|
|
|
} else scode = 500;
|
2015-04-14 20:56:28 +00:00
|
|
|
} else if (ps[0] == "peer") {
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_PeerList *pl = _node->peers();
|
2015-04-14 20:56:28 +00:00
|
|
|
if (pl) {
|
|
|
|
if (ps.size() == 1) {
|
|
|
|
// Return [array] of all peers
|
|
|
|
responseContentType = "application/json";
|
2015-04-16 19:06:40 +00:00
|
|
|
responseBody = "[\n";
|
2015-04-14 20:56:28 +00:00
|
|
|
for(unsigned long i=0;i<pl->peerCount;++i) {
|
|
|
|
if (i > 0)
|
2015-04-16 19:06:40 +00:00
|
|
|
responseBody.append(",\n");
|
|
|
|
_jsonAppend(1,responseBody,&(pl->peers[i]));
|
2015-04-14 01:43:33 +00:00
|
|
|
}
|
2015-04-16 19:06:40 +00:00
|
|
|
responseBody.append("\n]\n");
|
2015-04-14 20:56:28 +00:00
|
|
|
scode = 200;
|
|
|
|
} else if (ps.size() == 2) {
|
|
|
|
// Return a single peer by ID or 404 if not found
|
|
|
|
uint64_t wantp = Utils::hexStrToU64(ps[1].c_str());
|
|
|
|
for(unsigned long i=0;i<pl->peerCount;++i) {
|
|
|
|
if (pl->peers[i].address == wantp) {
|
|
|
|
responseContentType = "application/json";
|
2015-04-16 19:06:40 +00:00
|
|
|
_jsonAppend(0,responseBody,&(pl->peers[i]));
|
|
|
|
responseBody.push_back('\n');
|
2015-04-14 20:56:28 +00:00
|
|
|
scode = 200;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // else 404
|
|
|
|
_node->freeQueryResult((void *)pl);
|
|
|
|
} else scode = 500;
|
2015-05-16 20:42:53 +00:00
|
|
|
} else if (ps[0] == "newIdentity") {
|
|
|
|
// Return a newly generated ZeroTier identity -- this is primarily for debugging
|
|
|
|
// and testing to make it easy for automated test scripts to generate test IDs.
|
|
|
|
Identity newid;
|
|
|
|
newid.generate();
|
|
|
|
responseBody = newid.toString(true);
|
|
|
|
responseContentType = "text/plain";
|
|
|
|
scode = 200;
|
2015-04-21 23:41:35 +00:00
|
|
|
} else {
|
2015-05-17 22:51:07 +00:00
|
|
|
if (_controller)
|
2015-06-13 01:14:06 +00:00
|
|
|
scode = _controller->handleControlPlaneHttpGET(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
|
2015-04-21 23:41:35 +00:00
|
|
|
else scode = 404;
|
|
|
|
}
|
|
|
|
|
2015-04-14 20:56:28 +00:00
|
|
|
} else scode = 401; // isAuth == false
|
|
|
|
|
|
|
|
} else if ((httpMethod == HTTP_POST)||(httpMethod == HTTP_PUT)) {
|
|
|
|
|
|
|
|
if (isAuth) {
|
2015-04-21 23:41:35 +00:00
|
|
|
|
2015-04-14 20:56:28 +00:00
|
|
|
if (ps[0] == "config") {
|
|
|
|
// TODO
|
|
|
|
} else if (ps[0] == "network") {
|
2015-04-16 23:12:10 +00:00
|
|
|
if (ps.size() == 2) {
|
|
|
|
uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
|
2016-01-12 19:04:35 +00:00
|
|
|
_node->join(wantnw,(void *)0); // does nothing if we are a member
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkList *nws = _node->networks();
|
2015-04-16 23:12:10 +00:00
|
|
|
if (nws) {
|
|
|
|
for(unsigned long i=0;i<nws->networkCount;++i) {
|
|
|
|
if (nws->networks[i].nwid == wantnw) {
|
2016-06-21 23:52:49 +00:00
|
|
|
OneService::NetworkSettings localSettings;
|
|
|
|
_svc->getNetworkSettings(nws->networks[i].nwid,localSettings);
|
|
|
|
|
2016-08-24 20:37:57 +00:00
|
|
|
try {
|
|
|
|
nlohmann::json j(nlohmann::json::parse(body));
|
|
|
|
if (j.is_object()) {
|
|
|
|
auto allowManaged = j["allowManaged"];
|
|
|
|
if (allowManaged.is_boolean()) localSettings.allowManaged = (bool)allowManaged;
|
|
|
|
auto allowGlobal = j["allowGlobal"];
|
|
|
|
if (allowGlobal.is_boolean()) localSettings.allowGlobal = (bool)allowGlobal;
|
|
|
|
auto allowDefault = j["allowDefault"];
|
|
|
|
if (allowDefault.is_boolean()) localSettings.allowDefault = (bool)allowDefault;
|
2016-06-21 23:52:49 +00:00
|
|
|
}
|
2016-08-24 20:37:57 +00:00
|
|
|
} catch ( ... ) {
|
|
|
|
// discard invalid JSON
|
2016-06-21 23:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_svc->setNetworkSettings(nws->networks[i].nwid,localSettings);
|
|
|
|
|
2015-04-16 23:12:10 +00:00
|
|
|
responseContentType = "application/json";
|
2016-06-21 23:52:49 +00:00
|
|
|
_jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid),localSettings);
|
2015-04-16 23:12:10 +00:00
|
|
|
responseBody.push_back('\n');
|
|
|
|
scode = 200;
|
|
|
|
break;
|
2015-04-14 20:56:28 +00:00
|
|
|
}
|
2015-04-16 23:12:10 +00:00
|
|
|
}
|
|
|
|
_node->freeQueryResult((void *)nws);
|
|
|
|
} else scode = 500;
|
2015-04-14 20:56:28 +00:00
|
|
|
}
|
2015-04-21 23:41:35 +00:00
|
|
|
} else {
|
2015-05-17 22:51:07 +00:00
|
|
|
if (_controller)
|
2015-06-13 01:14:06 +00:00
|
|
|
scode = _controller->handleControlPlaneHttpPOST(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
|
2015-04-21 23:41:35 +00:00
|
|
|
else scode = 404;
|
|
|
|
}
|
|
|
|
|
2015-04-14 20:56:28 +00:00
|
|
|
} else scode = 401; // isAuth == false
|
|
|
|
|
2015-04-14 01:12:45 +00:00
|
|
|
} else if (httpMethod == HTTP_DELETE) {
|
2015-04-14 20:56:28 +00:00
|
|
|
|
|
|
|
if (isAuth) {
|
2015-04-21 23:41:35 +00:00
|
|
|
|
2015-04-14 20:56:28 +00:00
|
|
|
if (ps[0] == "config") {
|
|
|
|
// TODO
|
|
|
|
} else if (ps[0] == "network") {
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkList *nws = _node->networks();
|
2015-04-16 23:12:10 +00:00
|
|
|
if (nws) {
|
|
|
|
if (ps.size() == 2) {
|
|
|
|
uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
|
|
|
|
for(unsigned long i=0;i<nws->networkCount;++i) {
|
|
|
|
if (nws->networks[i].nwid == wantnw) {
|
2016-01-12 19:04:35 +00:00
|
|
|
_node->leave(wantnw,(void **)0);
|
2015-04-16 23:12:10 +00:00
|
|
|
responseBody = "true";
|
|
|
|
responseContentType = "application/json";
|
|
|
|
scode = 200;
|
|
|
|
break;
|
2015-04-14 20:56:28 +00:00
|
|
|
}
|
2015-04-16 23:12:10 +00:00
|
|
|
}
|
|
|
|
} // else 404
|
|
|
|
_node->freeQueryResult((void *)nws);
|
|
|
|
} else scode = 500;
|
2015-04-21 23:41:35 +00:00
|
|
|
} else {
|
2015-05-17 22:51:07 +00:00
|
|
|
if (_controller)
|
2015-06-11 09:49:13 +00:00
|
|
|
scode = _controller->handleControlPlaneHttpDELETE(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
|
2015-04-21 23:41:35 +00:00
|
|
|
else scode = 404;
|
|
|
|
}
|
2015-04-16 19:06:40 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
scode = 401; // isAuth = false
|
|
|
|
}
|
2015-04-14 20:56:28 +00:00
|
|
|
|
2015-04-14 01:12:45 +00:00
|
|
|
} else {
|
|
|
|
scode = 400;
|
2015-04-14 20:56:28 +00:00
|
|
|
responseBody = "Method not supported.";
|
2015-04-14 01:12:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 19:06:40 +00:00
|
|
|
// Wrap result in jsonp function call if the user included a jsonp= url argument.
|
2015-04-21 23:41:35 +00:00
|
|
|
// Also double-check isAuth since forbidding this without auth feels safer.
|
2015-04-14 01:12:45 +00:00
|
|
|
std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp"));
|
2015-04-16 19:06:40 +00:00
|
|
|
if ((isAuth)&&(jsonp != urlArgs.end())&&(responseContentType == "application/json")) {
|
2015-04-14 01:12:45 +00:00
|
|
|
if (responseBody.length() > 0)
|
|
|
|
responseBody = jsonp->second + "(" + responseBody + ");";
|
|
|
|
else responseBody = jsonp->second + "(null);";
|
|
|
|
responseContentType = "application/javascript";
|
|
|
|
}
|
|
|
|
|
|
|
|
return scode;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|