Prettify JSON output.

This commit is contained in:
Adam Ierymenko 2015-04-16 12:06:40 -07:00
parent 4e5d2d2c72
commit 12b4646ce8
2 changed files with 159 additions and 106 deletions

View file

@ -60,6 +60,7 @@ static std::string _jsonEscape(const char *s)
return buf; return buf;
} }
static std::string _jsonEscape(const std::string &s) { return _jsonEscape(s.c_str()); } static std::string _jsonEscape(const std::string &s) { return _jsonEscape(s.c_str()); }
static std::string _jsonEnumerate(const ZT1_MulticastGroup *mg,unsigned int count) static std::string _jsonEnumerate(const ZT1_MulticastGroup *mg,unsigned int count)
{ {
std::string buf; std::string buf;
@ -81,6 +82,7 @@ static std::string _jsonEnumerate(const ZT1_MulticastGroup *mg,unsigned int coun
buf.push_back(']'); buf.push_back(']');
return buf; return buf;
} }
static std::string _jsonEnumerate(const struct sockaddr_storage *ss,unsigned int count) static std::string _jsonEnumerate(const struct sockaddr_storage *ss,unsigned int count)
{ {
std::string buf; std::string buf;
@ -95,30 +97,18 @@ static std::string _jsonEnumerate(const struct sockaddr_storage *ss,unsigned int
buf.push_back(']'); buf.push_back(']');
return buf; return buf;
} }
static std::string _jsonEnumerate(const ZT1_PeerPhysicalPath *pp,unsigned int count)
static void _jsonAppend(unsigned int depth,std::string &buf,const ZT1_VirtualNetworkConfig *nc,const std::string &portDeviceName)
{ {
char tmp[1024]; char json[4096];
std::string buf; char prefix[32];
buf.push_back('[');
for(unsigned int i=0;i<count;++i) { if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
if (i > 0) return;
buf.push_back(','); for(unsigned int i=0;i<depth;++i)
buf.append("{\"address\":\""); prefix[i] = '\t';
buf.append(_jsonEscape(reinterpret_cast<const InetAddress *>(&(pp[i].address))->toString())); prefix[depth] = '\0';
Utils::snprintf(tmp,sizeof(tmp),"\",\"lastSend\":%llu,\"lastReceive\":%llu,\"fixed\":%s,\"active\":%s,\"preferred\":%s}",
pp[i].lastSend,
pp[i].lastReceive,
(pp[i].fixed == 0) ? "false" : "true",
(pp[i].active == 0) ? "false" : "true",
(pp[i].preferred == 0) ? "false" : "true");
buf.append(tmp);
}
buf.push_back(']');
return buf;
}
static void _jsonAppend(std::string &buf,const ZT1_VirtualNetworkConfig *nc,const std::string &portDeviceName)
{
char json[65536];
const char *nstatus = "",*ntype = ""; const char *nstatus = "",*ntype = "";
switch(nc->status) { switch(nc->status) {
case ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break; case ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
@ -132,71 +122,122 @@ static void _jsonAppend(std::string &buf,const ZT1_VirtualNetworkConfig *nc,cons
case ZT1_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break; case ZT1_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
case ZT1_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break; case ZT1_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
} }
Utils::snprintf(json,sizeof(json), Utils::snprintf(json,sizeof(json),
"{" "%s{\n"
"\"nwid\": \"%.16llx\"," "%s\t\"nwid\": \"%.16llx\",\n"
"\"mac\": \"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\"," "%s\t\"mac\": \"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\",\n"
"\"name\": \"%s\"," "%s\t\"name\": \"%s\",\n"
"\"status\": \"%s\"," "%s\t\"status\": \"%s\",\n"
"\"type\": \"%s\"," "%s\t\"type\": \"%s\",\n"
"\"mtu\": %u," "%s\t\"mtu\": %u,\n"
"\"dhcp\": %s," "%s\t\"dhcp\": %s,\n"
"\"bridge\": %s," "%s\t\"bridge\": %s,\n"
"\"broadcastEnabled\": %s," "%s\t\"broadcastEnabled\": %s,\n"
"\"portError\": %d," "%s\t\"portError\": %d,\n"
"\"netconfRevision\": %lu," "%s\t\"netconfRevision\": %lu,\n"
"\"multicastSubscriptions\": %s," "%s\t\"multicastSubscriptions\": %s,\n"
"\"assignedAddresses\": %s," "%s\t\"assignedAddresses\": %s,\n"
"\"portDeviceName\": \"%s\"" "%s\t\"portDeviceName\": \"%s\"\n"
"}", "%s}",
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,nc->nwid,
_jsonEscape(nc->name).c_str(), 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),
nstatus, prefix,_jsonEscape(nc->name).c_str(),
ntype, prefix,nstatus,
nc->mtu, prefix,ntype,
(nc->dhcp == 0) ? "false" : "true", prefix,nc->mtu,
(nc->bridge == 0) ? "false" : "true", prefix,(nc->dhcp == 0) ? "false" : "true",
(nc->broadcastEnabled == 0) ? "false" : "true", prefix,(nc->bridge == 0) ? "false" : "true",
nc->portError, prefix,(nc->broadcastEnabled == 0) ? "false" : "true",
nc->netconfRevision, prefix,nc->portError,
_jsonEnumerate(nc->multicastSubscriptions,nc->multicastSubscriptionCount).c_str(), prefix,nc->netconfRevision,
_jsonEnumerate(nc->assignedAddresses,nc->assignedAddressCount).c_str(), prefix,_jsonEnumerate(nc->multicastSubscriptions,nc->multicastSubscriptionCount).c_str(),
_jsonEscape(portDeviceName).c_str()); prefix,_jsonEnumerate(nc->assignedAddresses,nc->assignedAddressCount).c_str(),
prefix,_jsonEscape(portDeviceName).c_str(),
prefix);
buf.append(json); buf.append(json);
} }
static void _jsonAppend(std::string &buf,const ZT1_Peer *peer)
static std::string _jsonEnumerate(unsigned int depth,const ZT1_PeerPhysicalPath *pp,unsigned int count)
{ {
char json[65536]; char json[1024];
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"
"%s\t\"fixed\": %s,\n"
"%s\t\"active\": %s,\n"
"%s\t\"preferred\": %s\n"
"%s}",
prefix,_jsonEscape(reinterpret_cast<const InetAddress *>(&(pp[i].address))->toString()).c_str(),
prefix,pp[i].lastSend,
prefix,pp[i].lastReceive,
prefix,(pp[i].fixed == 0) ? "false" : "true",
prefix,(pp[i].active == 0) ? "false" : "true",
prefix,(pp[i].preferred == 0) ? "false" : "true",
prefix);
buf.append(json);
}
return buf;
}
static void _jsonAppend(unsigned int depth,std::string &buf,const ZT1_Peer *peer)
{
char json[1024];
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';
const char *prole = ""; const char *prole = "";
switch(peer->role) { switch(peer->role) {
case ZT1_PEER_ROLE_LEAF: prole = "LEAF"; break; case ZT1_PEER_ROLE_LEAF: prole = "LEAF"; break;
case ZT1_PEER_ROLE_HUB: prole = "HUB"; break; case ZT1_PEER_ROLE_HUB: prole = "HUB"; break;
case ZT1_PEER_ROLE_SUPERNODE: prole = "SUPERNODE"; break; case ZT1_PEER_ROLE_SUPERNODE: prole = "SUPERNODE"; break;
} }
Utils::snprintf(json,sizeof(json), Utils::snprintf(json,sizeof(json),
"{" "%s{\n"
"\"address\": \"%.10llx\"," "%s\t\"address\": \"%.10llx\",\n"
"\"lastUnicastFrame\": %llu," "%s\t\"lastUnicastFrame\": %llu,\n"
"\"lastMulticastFrame\": %llu," "%s\t\"lastMulticastFrame\": %llu,\n"
"\"versionMajor\": %d," "%s\t\"versionMajor\": %d,\n"
"\"versionMinor\": %d," "%s\t\"versionMinor\": %d,\n"
"\"versionRev\": %d," "%s\t\"versionRev\": %d,\n"
"\"version\": \"%d.%d.%d\"," "%s\t\"version\": \"%d.%d.%d\",\n"
"\"latency\": %u," "%s\t\"latency\": %u,\n"
"\"role\": \"%s\"," "%s\t\"role\": \"%s\",\n"
"\"paths\": %s" "%s\t\"paths\": [%s]\n"
"}", "%s}",
peer->address, prefix,
peer->lastUnicastFrame, prefix,peer->address,
peer->lastMulticastFrame, prefix,peer->lastUnicastFrame,
peer->versionMajor, prefix,peer->lastMulticastFrame,
peer->versionMinor, prefix,peer->versionMajor,
peer->versionRev, prefix,peer->versionMinor,
peer->versionMajor,peer->versionMinor,peer->versionRev, prefix,peer->versionRev,
peer->latency, prefix,peer->versionMajor,peer->versionMinor,peer->versionRev,
prole, prefix,peer->latency,
_jsonEnumerate(peer->paths,peer->pathCount).c_str()); prefix,prole,
prefix,_jsonEnumerate(depth+1,peer->paths,peer->pathCount).c_str(),
prefix);
buf.append(json); buf.append(json);
} }
@ -219,7 +260,7 @@ unsigned int ControlPlane::handleRequest(
std::string &responseBody, std::string &responseBody,
std::string &responseContentType) std::string &responseContentType)
{ {
char json[65536]; char json[1024];
unsigned int scode = 404; unsigned int scode = 404;
std::vector<std::string> ps(Utils::split(path.c_str(),"/","","")); std::vector<std::string> ps(Utils::split(path.c_str(),"/","",""));
std::map<std::string,std::string> urlArgs; std::map<std::string,std::string> urlArgs;
@ -252,9 +293,9 @@ unsigned int ControlPlane::handleRequest(
{ {
Mutex::Lock _l(_authTokens_m); Mutex::Lock _l(_authTokens_m);
std::map<std::string,std::string>::const_iterator ah(headers.find("x-zt1-auth")); std::map<std::string,std::string>::const_iterator ah(headers.find("x-zt1-auth"));
if ((ah != headers.end())&&(_authTokens.count(ah->second) > 0)) if ((ah != headers.end())&&(_authTokens.count(ah->second) > 0)) {
isAuth = true; isAuth = true;
else { } else {
ah = urlArgs.find("auth"); ah = urlArgs.find("auth");
if ((ah != urlArgs.end())&&(_authTokens.count(ah->second) > 0)) if ((ah != urlArgs.end())&&(_authTokens.count(ah->second) > 0))
isAuth = true; isAuth = true;
@ -269,8 +310,12 @@ unsigned int ControlPlane::handleRequest(
ext = ps[0].substr(dotIdx); ext = ps[0].substr(dotIdx);
if ((ps.size() == 1)&&(ext.length() >= 2)&&(ext[0] == '.')) { if ((ps.size() == 1)&&(ext.length() >= 2)&&(ext[0] == '.')) {
/* 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. */
#ifdef ZT_BUILD_IN_WEB_UI #ifdef ZT_BUILD_IN_WEB_UI
// .anything == static page -- also the only thing you can get without isAuth == true
if (ext == ".html") if (ext == ".html")
responseContentType = "text/html"; responseContentType = "text/html";
else if (ext == ".js") else if (ext == ".js")
@ -295,21 +340,24 @@ unsigned int ControlPlane::handleRequest(
responseBody = "<html><body>Hello World!</body></html>"; responseBody = "<html><body>Hello World!</body></html>";
scode = 200; scode = 200;
#endif // ZT_BUILD_IN_WEB_UI #endif // ZT_BUILD_IN_WEB_UI
} else if (isAuth) { } else if (isAuth) {
/* Things that require authentication -- a.k.a. everything but static web app pages. */
if (ps[0] == "status") { if (ps[0] == "status") {
responseContentType = "application/json"; responseContentType = "application/json";
ZT1_NodeStatus status; ZT1_NodeStatus status;
_node->status(&status); _node->status(&status);
Utils::snprintf(json,sizeof(json), Utils::snprintf(json,sizeof(json),
"{" "{\n"
"\"address\":\"%.10llx\"," "\t\"address\":\"%.10llx\",\n"
"\"publicIdentity\":\"%s\"," "\t\"publicIdentity\":\"%s\",\n"
"\"online\":%s," "\t\"online\":%s,\n"
"\"versionMajor\":%d," "\t\"versionMajor\":%d,\n"
"\"versionMinor\":%d," "\t\"versionMinor\":%d,\n"
"\"versionRev\":%d," "\t\"versionRev\":%d,\n"
"\"version\":\"%d.%d.%d\"" "\t\"version\":\"%d.%d.%d\"\n"
"}", "}\n",
status.address, status.address,
status.publicIdentity, status.publicIdentity,
(status.online) ? "true" : "false", (status.online) ? "true" : "false",
@ -329,13 +377,13 @@ unsigned int ControlPlane::handleRequest(
if (ps.size() == 1) { if (ps.size() == 1) {
// Return [array] of all networks // Return [array] of all networks
responseContentType = "application/json"; responseContentType = "application/json";
responseBody = "["; responseBody = "[\n";
for(unsigned long i=0;i<nws->networkCount;++i) { for(unsigned long i=0;i<nws->networkCount;++i) {
if (i > 0) if (i > 0)
responseBody.push_back(','); responseBody.append(",");
_jsonAppend(responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid)); _jsonAppend(1,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
} }
responseBody.push_back(']'); responseBody.append("\n]\n");
scode = 200; scode = 200;
} else if (ps.size() == 2) { } else if (ps.size() == 2) {
// Return a single network by ID or 404 if not found // Return a single network by ID or 404 if not found
@ -343,7 +391,8 @@ unsigned int ControlPlane::handleRequest(
for(unsigned long i=0;i<nws->networkCount;++i) { for(unsigned long i=0;i<nws->networkCount;++i) {
if (nws->networks[i].nwid == wantnw) { if (nws->networks[i].nwid == wantnw) {
responseContentType = "application/json"; responseContentType = "application/json";
_jsonAppend(responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid)); _jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
responseBody.push_back('\n');
scode = 200; scode = 200;
break; break;
} }
@ -357,13 +406,13 @@ unsigned int ControlPlane::handleRequest(
if (ps.size() == 1) { if (ps.size() == 1) {
// Return [array] of all peers // Return [array] of all peers
responseContentType = "application/json"; responseContentType = "application/json";
responseBody = "["; responseBody = "[\n";
for(unsigned long i=0;i<pl->peerCount;++i) { for(unsigned long i=0;i<pl->peerCount;++i) {
if (i > 0) if (i > 0)
responseBody.push_back(','); responseBody.append(",\n");
_jsonAppend(responseBody,&(pl->peers[i])); _jsonAppend(1,responseBody,&(pl->peers[i]));
} }
responseBody.push_back(']'); responseBody.append("\n]\n");
scode = 200; scode = 200;
} else if (ps.size() == 2) { } else if (ps.size() == 2) {
// Return a single peer by ID or 404 if not found // Return a single peer by ID or 404 if not found
@ -371,7 +420,8 @@ unsigned int ControlPlane::handleRequest(
for(unsigned long i=0;i<pl->peerCount;++i) { for(unsigned long i=0;i<pl->peerCount;++i) {
if (pl->peers[i].address == wantp) { if (pl->peers[i].address == wantp) {
responseContentType = "application/json"; responseContentType = "application/json";
_jsonAppend(responseBody,&(pl->peers[i])); _jsonAppend(0,responseBody,&(pl->peers[i]));
responseBody.push_back('\n');
scode = 200; scode = 200;
break; break;
} }
@ -399,7 +449,8 @@ unsigned int ControlPlane::handleRequest(
for(unsigned long i=0;i<nws->networkCount;++i) { for(unsigned long i=0;i<nws->networkCount;++i) {
if (nws->networks[i].nwid == wantnw) { if (nws->networks[i].nwid == wantnw) {
responseContentType = "application/json"; responseContentType = "application/json";
_jsonAppend(responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid)); _jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
responseBody.push_back('\n');
scode = 200; scode = 200;
break; break;
} }
@ -438,16 +489,20 @@ unsigned int ControlPlane::handleRequest(
} else scode = 500; } else scode = 500;
} }
} // else 404 } // else 404
} else scode = 401; // isAuth = false
} else {
scode = 401; // isAuth = false
}
} else { } else {
scode = 400; scode = 400;
responseBody = "Method not supported."; responseBody = "Method not supported.";
} }
// Wrap result in jsonp function call if the user included a jsonp= url argument // Wrap result in jsonp function call if the user included a jsonp= url argument.
// Also double-check isAuth since it feels like the right thing to do.
std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp")); std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp"));
if ((jsonp != urlArgs.end())&&(responseContentType == "application/json")) { if ((isAuth)&&(jsonp != urlArgs.end())&&(responseContentType == "application/json")) {
if (responseBody.length() > 0) if (responseBody.length() > 0)
responseBody = jsonp->second + "(" + responseBody + ");"; responseBody = jsonp->second + "(" + responseBody + ");";
else responseBody = jsonp->second + "(null);"; else responseBody = jsonp->second + "(null);";

View file

@ -131,7 +131,6 @@ class OneServiceImpl : public OneService
public: public:
OneServiceImpl(const char *hp,unsigned int port,NetworkController *master,const char *overrideRootTopology) : OneServiceImpl(const char *hp,unsigned int port,NetworkController *master,const char *overrideRootTopology) :
_homePath((hp) ? hp : "."), _homePath((hp) ? hp : "."),
_port(port),
_phy(this,true), _phy(this,true),
_master(master), _master(master),
_overrideRootTopology((overrideRootTopology) ? overrideRootTopology : ""), _overrideRootTopology((overrideRootTopology) ? overrideRootTopology : ""),
@ -637,7 +636,6 @@ private:
} }
const std::string _homePath; const std::string _homePath;
unsigned int _port;
Phy<OneServiceImpl *> _phy; Phy<OneServiceImpl *> _phy;
NetworkController *_master; NetworkController *_master;
std::string _overrideRootTopology; std::string _overrideRootTopology;