mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-05 03:53:44 +02:00
JSON control plane, almost done...
This commit is contained in:
parent
ff0eff4b7c
commit
b888e033c0
8 changed files with 413 additions and 26 deletions
|
@ -328,7 +328,12 @@ enum ZT1_VirtualNetworkStatus
|
||||||
/**
|
/**
|
||||||
* Initialization of network failed or other internal error
|
* Initialization of network failed or other internal error
|
||||||
*/
|
*/
|
||||||
ZT1_NETWORK_STATUS_PORT_ERROR = 4
|
ZT1_NETWORK_STATUS_PORT_ERROR = 4,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ZeroTier One version too old
|
||||||
|
*/
|
||||||
|
ZT1_NETWORK_STATUS_CLIENT_TOO_OLD = 5
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -853,6 +858,14 @@ enum ZT1_ResultCode ZT1_Node_multicastSubscribe(ZT1_Node *node,uint64_t nwid,uin
|
||||||
*/
|
*/
|
||||||
enum ZT1_ResultCode ZT1_Node_multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi = 0);
|
enum ZT1_ResultCode ZT1_Node_multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi = 0);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get this node's 40-bit ZeroTier address
|
||||||
|
*
|
||||||
|
* @param node Node instance
|
||||||
|
* @return ZeroTier address (least significant 40 bits of 64-bit int)
|
||||||
|
*/
|
||||||
|
uint64_t ZT1_Node_address(ZT1_Node *node);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the status of this node
|
* Get the status of this node
|
||||||
*
|
*
|
||||||
|
|
|
@ -111,7 +111,7 @@ public:
|
||||||
inline std::string toString() const
|
inline std::string toString() const
|
||||||
{
|
{
|
||||||
char buf[64];
|
char buf[64];
|
||||||
Utils::snprintf(buf,sizeof(buf),"%.2x%.2x%.2x%.2x%.2x%.2x/%.4x",(unsigned int)_mac[0],(unsigned int)_mac[1],(unsigned int)_mac[2],(unsigned int)_mac[3],(unsigned int)_mac[4],(unsigned int)_mac[5],(unsigned int)_adi);
|
Utils::snprintf(buf,sizeof(buf),"%.2x%.2x%.2x%.2x%.2x%.2x/%.8lx",(unsigned int)_mac[0],(unsigned int)_mac[1],(unsigned int)_mac[2],(unsigned int)_mac[3],(unsigned int)_mac[4],(unsigned int)_mac[5],(unsigned long)_adi);
|
||||||
return std::string(buf);
|
return std::string(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -317,7 +317,12 @@ ZT1_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,
|
||||||
return ZT1_RESULT_OK;
|
return ZT1_RESULT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::status(ZT1_NodeStatus *status)
|
uint64_t Node::address() const
|
||||||
|
{
|
||||||
|
return RR->identity.address().toInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Node::status(ZT1_NodeStatus *status) const
|
||||||
{
|
{
|
||||||
status->address = RR->identity.address().toInt();
|
status->address = RR->identity.address().toInt();
|
||||||
status->publicIdentity = RR->publicIdentityStr.c_str();
|
status->publicIdentity = RR->publicIdentityStr.c_str();
|
||||||
|
@ -325,7 +330,7 @@ void Node::status(ZT1_NodeStatus *status)
|
||||||
status->online = _online ? 1 : 0;
|
status->online = _online ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZT1_PeerList *Node::peers()
|
ZT1_PeerList *Node::peers() const
|
||||||
{
|
{
|
||||||
std::map< Address,SharedPtr<Peer> > peers(RR->topology->allPeers());
|
std::map< Address,SharedPtr<Peer> > peers(RR->topology->allPeers());
|
||||||
|
|
||||||
|
@ -365,7 +370,7 @@ ZT1_PeerList *Node::peers()
|
||||||
return pl;
|
return pl;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid)
|
ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
|
||||||
{
|
{
|
||||||
Mutex::Lock _l(_networks_m);
|
Mutex::Lock _l(_networks_m);
|
||||||
std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
|
std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
|
||||||
|
@ -377,7 +382,7 @@ ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid)
|
||||||
return (ZT1_VirtualNetworkConfig *)0;
|
return (ZT1_VirtualNetworkConfig *)0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZT1_VirtualNetworkList *Node::networks()
|
ZT1_VirtualNetworkList *Node::networks() const
|
||||||
{
|
{
|
||||||
Mutex::Lock _l(_networks_m);
|
Mutex::Lock _l(_networks_m);
|
||||||
|
|
||||||
|
@ -601,6 +606,11 @@ enum ZT1_ResultCode ZT1_Node_multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,u
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64_t ZT1_Node_address(ZT1_Node *node)
|
||||||
|
{
|
||||||
|
return reinterpret_cast<ZeroTier::Node *>(node)->address();
|
||||||
|
}
|
||||||
|
|
||||||
void ZT1_Node_status(ZT1_Node *node,ZT1_NodeStatus *status)
|
void ZT1_Node_status(ZT1_Node *node,ZT1_NodeStatus *status)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -99,10 +99,11 @@ public:
|
||||||
ZT1_ResultCode leave(uint64_t nwid);
|
ZT1_ResultCode leave(uint64_t nwid);
|
||||||
ZT1_ResultCode multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
|
ZT1_ResultCode multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
|
||||||
ZT1_ResultCode multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
|
ZT1_ResultCode multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
|
||||||
void status(ZT1_NodeStatus *status);
|
uint64_t address(ZT1_Node *node) const;
|
||||||
ZT1_PeerList *peers();
|
void status(ZT1_NodeStatus *status) const;
|
||||||
ZT1_VirtualNetworkConfig *networkConfig(uint64_t nwid);
|
ZT1_PeerList *peers() const;
|
||||||
ZT1_VirtualNetworkList *networks();
|
ZT1_VirtualNetworkConfig *networkConfig(uint64_t nwid) const;
|
||||||
|
ZT1_VirtualNetworkList *networks() const;
|
||||||
void freeQueryResult(void *qr);
|
void freeQueryResult(void *qr);
|
||||||
void setNetconfMaster(void *networkConfigMasterInstance);
|
void setNetconfMaster(void *networkConfigMasterInstance);
|
||||||
|
|
||||||
|
|
|
@ -24,4 +24,5 @@ OBJS=\
|
||||||
node/Utils.o \
|
node/Utils.o \
|
||||||
osdep/HttpClient.o \
|
osdep/HttpClient.o \
|
||||||
osdep/OSUtils.o \
|
osdep/OSUtils.o \
|
||||||
|
service/ControlPlane.o \
|
||||||
service/One.o
|
service/One.o
|
||||||
|
|
274
service/ControlPlane.cpp
Normal file
274
service/ControlPlane.cpp
Normal file
|
@ -0,0 +1,274 @@
|
||||||
|
/*
|
||||||
|
* ZeroTier One - Network Virtualization Everywhere
|
||||||
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*
|
||||||
|
* --
|
||||||
|
*
|
||||||
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||||
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||||
|
*
|
||||||
|
* If you would like to embed ZeroTier into a commercial application or
|
||||||
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||||
|
* LLC. Start here: http://www.zerotier.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "ControlPlane.hpp"
|
||||||
|
|
||||||
|
#include "../version.h"
|
||||||
|
#include "../include/ZeroTierOne.h"
|
||||||
|
|
||||||
|
#include "../ext/http-parser/http_parser.h"
|
||||||
|
|
||||||
|
#include "../node/InetAddress.hpp"
|
||||||
|
#include "../node/Node.hpp"
|
||||||
|
#include "../node/Utils.hpp"
|
||||||
|
|
||||||
|
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;
|
||||||
|
default: buf.push_back(*s); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
static std::string _jsonEscape(const std::string &s) { return _jsonEscape(s.c_str()); }
|
||||||
|
static std::string _jsonEnumerate(const ZT1_MulticastGroup *mg,unsigned int count)
|
||||||
|
{
|
||||||
|
std::string buf;
|
||||||
|
char tmp[128];
|
||||||
|
buf.push_back('[');
|
||||||
|
for(unsigned int i=0;i<count;++i) {
|
||||||
|
if (i > 0)
|
||||||
|
buf.push_back(',');
|
||||||
|
Utils::snprintf(tmp,sizeof(tmp),"\"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\\/%.8lx\"",
|
||||||
|
(unsigned int)((mg->mac >> 40) & 0xff),
|
||||||
|
(unsigned int)((mg->mac >> 32) & 0xff),
|
||||||
|
(unsigned int)((mg->mac >> 24) & 0xff),
|
||||||
|
(unsigned int)((mg->mac >> 16) & 0xff),
|
||||||
|
(unsigned int)((mg->mac >> 8) & 0xff),
|
||||||
|
(unsigned int)(mg->mac & 0xff),
|
||||||
|
mg->adi);
|
||||||
|
buf.append(tmp);
|
||||||
|
}
|
||||||
|
buf.push_back(']');
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
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('"');
|
||||||
|
buf.append(_jsonEscape(reinterpret_cast<const InetAddress *>(ss)->toString()));
|
||||||
|
buf.push_back('"');
|
||||||
|
}
|
||||||
|
buf.push_back(']');
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
static void _jsonAppend(std::string &buf,const ZT1_VirtualNetworkConfig *nc)
|
||||||
|
{
|
||||||
|
char json[65536];
|
||||||
|
const char *nstatus = "",*ntype = "";
|
||||||
|
switch(nc->status) {
|
||||||
|
case ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
|
||||||
|
case ZT1_NETWORK_STATUS_OK: nstatus = "OK"; break;
|
||||||
|
case ZT1_NETWORK_STATUS_ACCESS_DENIED: nstatus = "ACCESS_DENIED"; break;
|
||||||
|
case ZT1_NETWORK_STATUS_NOT_FOUND: nstatus = "NOT_FOUND"; break;
|
||||||
|
case ZT1_NETWORK_STATUS_PORT_ERROR: nstatus = "PORT_ERROR"; break;
|
||||||
|
case ZT1_NETWORK_STATUS_CLIENT_TOO_OLD: nstatus = "CLIENT_TOO_OLD"; break;
|
||||||
|
}
|
||||||
|
switch(nc->type) {
|
||||||
|
case ZT1_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
|
||||||
|
case ZT1_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
|
||||||
|
}
|
||||||
|
Utils::snprintf(json,sizeof(json),
|
||||||
|
"{"
|
||||||
|
"\"nwid\": \"%.16llx\","
|
||||||
|
"\"mac\": \"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\","
|
||||||
|
"\"name\": %s,"
|
||||||
|
"\"status\": \"%s\","
|
||||||
|
"\"type\": \"%s\","
|
||||||
|
"\"mtu\": %u,"
|
||||||
|
"\"dhcp\": %s,"
|
||||||
|
"\"bridge\": %s,"
|
||||||
|
"\"broadcastEnabled\": %s,"
|
||||||
|
"\"portError\": %d,"
|
||||||
|
"\"netconfRevision\": %lu,"
|
||||||
|
"\"multicastSubscriptions\": %s,"
|
||||||
|
"\"assignedAddresses\": %s"
|
||||||
|
"}",
|
||||||
|
nc->nwid,
|
||||||
|
(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),
|
||||||
|
_jsonEscape(nc->name).c_str(),
|
||||||
|
nstatus,
|
||||||
|
ntype,
|
||||||
|
nc->mtu,
|
||||||
|
(nc->dhcp == 0) ? "false" : "true",
|
||||||
|
(nc->bridge == 0) ? "false" : "true",
|
||||||
|
(nc->broadcastEnabled == 0) ? "false" : "true",
|
||||||
|
nc->portError,
|
||||||
|
nc->netconfRevision,
|
||||||
|
_jsonEnumerate(nc->multicastSubscriptions,nc->multicastSubscriptionCount).c_str(),
|
||||||
|
_jsonEnumerate(nc->assignedAddresses,nc->assignedAddressCount).c_str());
|
||||||
|
buf.append(json);
|
||||||
|
}
|
||||||
|
static void _jsonAppend(std::string &buf,const ZT1_Peer *peer)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ControlPlane::ControlPlane(Node *n,const std::set<std::string> atoks) :
|
||||||
|
_node(n),
|
||||||
|
_authTokens(atoks)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ControlPlane::~ControlPlane()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int ControlPlane::handleRequest(
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
char json[65536];
|
||||||
|
unsigned int scode = 404;
|
||||||
|
std::vector<std::string> ps(Utils::split(path.c_str(),"/","",""));
|
||||||
|
std::map<std::string,std::string> urlArgs;
|
||||||
|
|
||||||
|
/* 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 {
|
||||||
|
ps.push_back(std::string("index"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (httpMethod == HTTP_GET) {
|
||||||
|
if (ps[0] == "index") {
|
||||||
|
responseContentType = "text/html";
|
||||||
|
responseBody = "<html><body>Hello World!</body></html>";
|
||||||
|
} else if (ps[0] == "status") {
|
||||||
|
responseContentType = "application/json";
|
||||||
|
ZT1_NodeStatus status;
|
||||||
|
_node->status(&status);
|
||||||
|
Utils::snprintf(json,sizeof(json),
|
||||||
|
"{"
|
||||||
|
"\"address\":\"%.10llx\","
|
||||||
|
"\"publicIdentity\":\"%s\","
|
||||||
|
"\"online\":%s,"
|
||||||
|
"\"versionMajor\":%d,"
|
||||||
|
"\"versionMinor\":%d,"
|
||||||
|
"\"versionRevision\":%d,"
|
||||||
|
"\"version\":\"%d.%d.%d\""
|
||||||
|
"}",
|
||||||
|
status.address,
|
||||||
|
status.publicIdentity,
|
||||||
|
(status.online) ? "true" : "false",
|
||||||
|
ZEROTIER_ONE_VERSION_MAJOR,
|
||||||
|
ZEROTIER_ONE_VERSION_MINOR,
|
||||||
|
ZEROTIER_ONE_VERSION_REVISION,
|
||||||
|
ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION);
|
||||||
|
responseBody = json;
|
||||||
|
} else if (ps[0] == "config") {
|
||||||
|
responseContentType = "application/json";
|
||||||
|
responseBody = "{}"; // TODO
|
||||||
|
} else if (ps[0] == "network") {
|
||||||
|
ZT1_VirtualNetworkList *nws = _node->networks();
|
||||||
|
if (nws) {
|
||||||
|
if (ps.size() == 1) {
|
||||||
|
// Return [array] of all networks
|
||||||
|
responseContentType = "application/json";
|
||||||
|
responseBody = "[";
|
||||||
|
for(unsigned long i=0;i<nws->networkCount;++i) {
|
||||||
|
if (i > 0)
|
||||||
|
responseBody.push_back(',');
|
||||||
|
_jsonAppend(responseBody,&(nws->networks[i]));
|
||||||
|
}
|
||||||
|
responseBody.push_back(']');
|
||||||
|
} 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());
|
||||||
|
bool got = false;
|
||||||
|
for(unsigned long i=0;i<nws->networkCount;++i) {
|
||||||
|
if (nws->networks[i].nwid == wantnw) {
|
||||||
|
got = true;
|
||||||
|
responseContentType = "application/json";
|
||||||
|
_jsonAppend(responseBody,&(nws->networks[i]));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!got)
|
||||||
|
scode = 404;
|
||||||
|
} else {
|
||||||
|
// Not sure what they want here, 404
|
||||||
|
scode = 404;
|
||||||
|
}
|
||||||
|
_node->freeQueryResult((void *)nws);
|
||||||
|
} else scode = 500;
|
||||||
|
} else if (ps[0] == "peer") {
|
||||||
|
}
|
||||||
|
} else if ((httpMethod == HTTP_POST)||(httpMethod == HTTP_PUT)) { // PUT is weird in REST but we'll take it anyway
|
||||||
|
} else if (httpMethod == HTTP_DELETE) {
|
||||||
|
} else {
|
||||||
|
scode = 400;
|
||||||
|
responseBody = "Method not supported for resource ";
|
||||||
|
responseBody.append(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp"));
|
||||||
|
if (jsonp != urlArgs.end()) {
|
||||||
|
if (responseBody.length() > 0)
|
||||||
|
responseBody = jsonp->second + "(" + responseBody + ");";
|
||||||
|
else responseBody = jsonp->second + "(null);";
|
||||||
|
responseContentType = "application/javascript";
|
||||||
|
}
|
||||||
|
|
||||||
|
return scode;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ZeroTier
|
76
service/ControlPlane.hpp
Normal file
76
service/ControlPlane.hpp
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* ZeroTier One - Network Virtualization Everywhere
|
||||||
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*
|
||||||
|
* --
|
||||||
|
*
|
||||||
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||||
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||||
|
*
|
||||||
|
* If you would like to embed ZeroTier into a commercial application or
|
||||||
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||||
|
* LLC. Start here: http://www.zerotier.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ZT_ONE_CONTROLPLANE_HPP
|
||||||
|
#define ZT_ONE_CONTROLPLANE_HPP
|
||||||
|
|
||||||
|
#include "../include/ZeroTierOne.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
namespace ZeroTier {
|
||||||
|
|
||||||
|
class Node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP control plane and static web server
|
||||||
|
*/
|
||||||
|
class ControlPlane
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ControlPlane(Node *n,const std::set<std::string> atoks);
|
||||||
|
~ControlPlane();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle HTTP request
|
||||||
|
*
|
||||||
|
* @param httpMethod HTTP method (as defined in ext/http-parser/http_parser.h)
|
||||||
|
* @param path Request path
|
||||||
|
* @param headers Request headers
|
||||||
|
* @param body Request body
|
||||||
|
* @param responseBody Result parameter: fill with response data
|
||||||
|
* @param responseContentType Result parameter: fill with content type
|
||||||
|
* @return HTTP response code
|
||||||
|
*/
|
||||||
|
unsigned int handleRequest(
|
||||||
|
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);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Node *const _node;
|
||||||
|
std::set<std::string> _authTokens;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace ZeroTier
|
||||||
|
|
||||||
|
#endif
|
|
@ -49,6 +49,7 @@
|
||||||
#include "../osdep/OSUtils.hpp"
|
#include "../osdep/OSUtils.hpp"
|
||||||
|
|
||||||
#include "One.hpp"
|
#include "One.hpp"
|
||||||
|
#include "ControlPlane.hpp"
|
||||||
|
|
||||||
// Sanity limits for HTTP
|
// Sanity limits for HTTP
|
||||||
#define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 8)
|
#define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 8)
|
||||||
|
@ -119,6 +120,7 @@ public:
|
||||||
_master(master),
|
_master(master),
|
||||||
_overrideRootTopology((overrideRootTopology) ? overrideRootTopology : ""),
|
_overrideRootTopology((overrideRootTopology) ? overrideRootTopology : ""),
|
||||||
_node((Node *)0),
|
_node((Node *)0),
|
||||||
|
_controlPlane((ControlPlane *)0),
|
||||||
_nextBackgroundTaskDeadline(0),
|
_nextBackgroundTaskDeadline(0),
|
||||||
_termReason(ONE_STILL_RUNNING),
|
_termReason(ONE_STILL_RUNNING),
|
||||||
_run(true)
|
_run(true)
|
||||||
|
@ -186,6 +188,8 @@ public:
|
||||||
if (_master)
|
if (_master)
|
||||||
_node->setNetconfMaster((void *)_master);
|
_node->setNetconfMaster((void *)_master);
|
||||||
|
|
||||||
|
_controlPlane = new ControlPlane(_node);
|
||||||
|
|
||||||
_nextBackgroundTaskDeadline = 0;
|
_nextBackgroundTaskDeadline = 0;
|
||||||
for(;;) {
|
for(;;) {
|
||||||
_run_m.lock();
|
_run_m.lock();
|
||||||
|
@ -225,6 +229,8 @@ public:
|
||||||
_phy.close(_httpConnections.begin()->first);
|
_phy.close(_httpConnections.begin()->first);
|
||||||
} catch ( ... ) {}
|
} catch ( ... ) {}
|
||||||
|
|
||||||
|
delete _controlPlane;
|
||||||
|
_controlPlane = (ControlPlane *)0;
|
||||||
delete _node;
|
delete _node;
|
||||||
_node = (Node *)0;
|
_node = (Node *)0;
|
||||||
|
|
||||||
|
@ -443,31 +449,36 @@ public:
|
||||||
inline void onHttpRequestToServer(HttpConnection *htc)
|
inline void onHttpRequestToServer(HttpConnection *htc)
|
||||||
{
|
{
|
||||||
char tmpn[256];
|
char tmpn[256];
|
||||||
|
|
||||||
/*
|
|
||||||
printf("HTTP request:\n");
|
|
||||||
printf(" url: %s\n",htc->url.c_str());
|
|
||||||
printf(" status: %s\n",htc->status.c_str());
|
|
||||||
printf(" headers:\n");
|
|
||||||
for(std::map<std::string,std::string>::iterator h(htc->headers.begin());h!=htc->headers.end();++h)
|
|
||||||
printf(" %s: %s\n",h->first.c_str(),h->second.c_str());
|
|
||||||
printf(" body:\n----\n%s\n----\n",htc->body.c_str());
|
|
||||||
*/
|
|
||||||
|
|
||||||
std::string data;
|
std::string data;
|
||||||
std::string contentType;
|
std::string contentType("text/plain"); // default if not changed in handleRequest()
|
||||||
unsigned int scode = 404;
|
unsigned int scode = 404;
|
||||||
|
|
||||||
if ((htc->from == InetAddress::LO4)||(htc->from == InetAddress::LO6)) {
|
if ((htc->from == InetAddress::LO4)||(htc->from == InetAddress::LO6)) {
|
||||||
//scode = _controlPlane.handleRequest(htc->parser.method,htc->url,htc->headers,htc->body,data,contentType);
|
try {
|
||||||
|
if (_controlPlane)
|
||||||
|
scode = _controlPlane->handleRequest(htc->parser.method,htc->url,htc->headers,htc->body,data,contentType);
|
||||||
|
} catch ( ... ) {
|
||||||
|
scode = 500;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
data = "Forbidden.";
|
|
||||||
contentType = "text/plain";
|
|
||||||
scode = 403;
|
scode = 403;
|
||||||
htc->shouldKeepAlive = false;
|
htc->shouldKeepAlive = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Utils::snprintf(tmpn,sizeof(tmpn),"HTTP/1.1 %.3u %s\r\nServer: ZeroTier One\r\nCache-Control: no-cache\r\nPragma: no-cache\r\n",scode,((scode == 200) ? "OK" : ((scode == 404) ? "Not Found" : "Error")));
|
const char *scodestr;
|
||||||
|
switch(scode) {
|
||||||
|
case 200: scodestr = "OK";
|
||||||
|
case 400: scodestr = "Bad Request";
|
||||||
|
case 401: scodestr = "Unauthorized";
|
||||||
|
case 403: scodestr = "Forbidden";
|
||||||
|
case 404: scodestr = "Not Found";
|
||||||
|
case 500: scodestr = "Internal Server Error";
|
||||||
|
case 501: scodestr = "Not Implemented";
|
||||||
|
case 503: scodestr = "Service Unavailable";
|
||||||
|
default: scodestr = "Error";
|
||||||
|
}
|
||||||
|
|
||||||
|
Utils::snprintf(tmpn,sizeof(tmpn),"HTTP/1.1 %.3u %s\r\nCache-Control: no-cache\r\nPragma: no-cache\r\n",scode,scodestr);
|
||||||
htc->body.assign(tmpn);
|
htc->body.assign(tmpn);
|
||||||
htc->body.append("Content-Type: ");
|
htc->body.append("Content-Type: ");
|
||||||
htc->body.append(contentType);
|
htc->body.append(contentType);
|
||||||
|
@ -517,6 +528,7 @@ private:
|
||||||
PhySocket *_v6UdpSocket;
|
PhySocket *_v6UdpSocket;
|
||||||
PhySocket *_v4TcpListenSocket;
|
PhySocket *_v4TcpListenSocket;
|
||||||
PhySocket *_v6TcpListenSocket;
|
PhySocket *_v6TcpListenSocket;
|
||||||
|
ControlPlane *_controlPlane;
|
||||||
uint64_t _nextBackgroundTaskDeadline;
|
uint64_t _nextBackgroundTaskDeadline;
|
||||||
|
|
||||||
std::map< PhySocket *,HttpConnection > _httpConnections; // no mutex for this since it's done in the main loop thread only
|
std::map< PhySocket *,HttpConnection > _httpConnections; // no mutex for this since it's done in the main loop thread only
|
||||||
|
|
Loading…
Add table
Reference in a new issue