mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-05 20:13:44 +02:00
zerotier-cli suspiciously working... that didn't hurt *too* badly.
This commit is contained in:
parent
c4c5fa63d1
commit
1f5700191f
3 changed files with 89 additions and 41 deletions
60
one.cpp
60
one.cpp
|
@ -408,6 +408,59 @@ static int cli(int argc,char **argv)
|
||||||
printf("%s",cliFixJsonCRs(responseBody).c_str());
|
printf("%s",cliFixJsonCRs(responseBody).c_str());
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
|
printf("200 listnetworks <nwid> <name> <mac> <status> <type> <dev> <ZT assigned ips>"ZT_EOL_S);
|
||||||
|
json_value *j = json_parse(responseBody.c_str(),responseBody.length());
|
||||||
|
if (j) {
|
||||||
|
if (j->type == json_array) {
|
||||||
|
for(unsigned int p=0;p<j->u.array.length;++p) {
|
||||||
|
json_value *jn = j->u.array.values[p];
|
||||||
|
if (jn->type == json_object) {
|
||||||
|
const char *nwid = (const char *)0;
|
||||||
|
const char *name = "";
|
||||||
|
const char *mac = (const char *)0;
|
||||||
|
const char *status = (const char *)0;
|
||||||
|
const char *type = (const char *)0;
|
||||||
|
const char *portDeviceName = "";
|
||||||
|
std::string ips;
|
||||||
|
for(unsigned int k=0;k<jn->u.object.length;++k) {
|
||||||
|
if ((!strcmp(jn->u.object.values[k].name,"nwid"))&&(jn->u.object.values[k].value->type == json_string))
|
||||||
|
nwid = jn->u.object.values[k].value->u.string.ptr;
|
||||||
|
else if ((!strcmp(jn->u.object.values[k].name,"name"))&&(jn->u.object.values[k].value->type == json_string))
|
||||||
|
name = jn->u.object.values[k].value->u.string.ptr;
|
||||||
|
else if ((!strcmp(jn->u.object.values[k].name,"mac"))&&(jn->u.object.values[k].value->type == json_string))
|
||||||
|
mac = jn->u.object.values[k].value->u.string.ptr;
|
||||||
|
else if ((!strcmp(jn->u.object.values[k].name,"status"))&&(jn->u.object.values[k].value->type == json_string))
|
||||||
|
status = jn->u.object.values[k].value->u.string.ptr;
|
||||||
|
else if ((!strcmp(jn->u.object.values[k].name,"type"))&&(jn->u.object.values[k].value->type == json_string))
|
||||||
|
type = jn->u.object.values[k].value->u.string.ptr;
|
||||||
|
else if ((!strcmp(jn->u.object.values[k].name,"portDeviceName"))&&(jn->u.object.values[k].value->type == json_string))
|
||||||
|
portDeviceName = jn->u.object.values[k].value->u.string.ptr;
|
||||||
|
else if ((!strcmp(jn->u.object.values[k].name,"assignedAddresses"))&&(jn->u.object.values[k].value->type == json_array)) {
|
||||||
|
for(unsigned int a=0;a<jn->u.object.values[k].value->u.array.length;++a) {
|
||||||
|
json_value *aa = jn->u.object.values[k].value->u.array.values[a];
|
||||||
|
if (aa->type == json_string) {
|
||||||
|
if (ips.length())
|
||||||
|
ips.push_back(',');
|
||||||
|
ips.append(aa->u.string.ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((nwid)&&(mac)&&(status)&&(type)) {
|
||||||
|
printf("200 listnetworks %s %s %s %s %s %s %s"ZT_EOL_S,
|
||||||
|
nwid,
|
||||||
|
(((name)&&(name[0])) ? name : "-"),
|
||||||
|
mac,
|
||||||
|
status,
|
||||||
|
type,
|
||||||
|
(((portDeviceName)&&(portDeviceName[0])) ? portDeviceName : "-"),
|
||||||
|
((ips.length() > 0) ? ips.c_str() : "-"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
json_value_free(j);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("%u %s %s"ZT_EOL_S,scode,command.c_str(),responseBody.c_str());
|
printf("%u %s %s"ZT_EOL_S,scode,command.c_str(),responseBody.c_str());
|
||||||
|
@ -418,20 +471,23 @@ static int cli(int argc,char **argv)
|
||||||
cliPrintHelp(argv[0],stderr);
|
cliPrintHelp(argv[0],stderr);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
requestHeaders["Content-Type"] = "application/json";
|
||||||
|
requestHeaders["Content-Length"] = "2";
|
||||||
unsigned int scode = Http::POST(
|
unsigned int scode = Http::POST(
|
||||||
1024 * 1024 * 16,
|
1024 * 1024 * 16,
|
||||||
60000,
|
60000,
|
||||||
(const struct sockaddr *)&addr,
|
(const struct sockaddr *)&addr,
|
||||||
(std::string("/network/") + arg1).c_str(),
|
(std::string("/network/") + arg1).c_str(),
|
||||||
requestHeaders,
|
requestHeaders,
|
||||||
"",
|
"{}",
|
||||||
0,
|
2,
|
||||||
responseHeaders,
|
responseHeaders,
|
||||||
responseBody);
|
responseBody);
|
||||||
if (scode == 200) {
|
if (scode == 200) {
|
||||||
if (json) {
|
if (json) {
|
||||||
printf("%s",cliFixJsonCRs(responseBody).c_str());
|
printf("%s",cliFixJsonCRs(responseBody).c_str());
|
||||||
} else {
|
} else {
|
||||||
|
printf("200 join OK"ZT_EOL_S);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -209,7 +209,7 @@ unsigned int Http::_do(
|
||||||
handler.lastActivity = OSUtils::now();
|
handler.lastActivity = OSUtils::now();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
handler.writeSize = Utils::snprintf(handler.writeBuf,sizeof(handler.writeBuf),"GET %s HTTP/1.1\r\n",path);
|
handler.writeSize = Utils::snprintf(handler.writeBuf,sizeof(handler.writeBuf),"%s %s HTTP/1.1\r\n",method,path);
|
||||||
for(std::map<std::string,std::string>::const_iterator h(requestHeaders.begin());h!=requestHeaders.end();++h)
|
for(std::map<std::string,std::string>::const_iterator h(requestHeaders.begin());h!=requestHeaders.end();++h)
|
||||||
handler.writeSize += Utils::snprintf(handler.writeBuf + handler.writeSize,sizeof(handler.writeBuf) - handler.writeSize,"%s: %s\r\n",h->first.c_str(),h->second.c_str());
|
handler.writeSize += Utils::snprintf(handler.writeBuf + handler.writeSize,sizeof(handler.writeBuf) - handler.writeSize,"%s: %s\r\n",h->first.c_str(),h->second.c_str());
|
||||||
handler.writeSize += Utils::snprintf(handler.writeBuf + handler.writeSize,sizeof(handler.writeBuf) - handler.writeSize,"\r\n");
|
handler.writeSize += Utils::snprintf(handler.writeBuf + handler.writeSize,sizeof(handler.writeBuf) - handler.writeSize,"\r\n");
|
||||||
|
|
|
@ -438,26 +438,22 @@ unsigned int ControlPlane::handleRequest(
|
||||||
if (ps[0] == "config") {
|
if (ps[0] == "config") {
|
||||||
// TODO
|
// TODO
|
||||||
} else if (ps[0] == "network") {
|
} else if (ps[0] == "network") {
|
||||||
if ((ps.size() > 1)&&(ps[1] == "controller")) {
|
if (ps.size() == 2) {
|
||||||
// TODO
|
uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
|
||||||
} else {
|
_node->join(wantnw); // does nothing if we are a member
|
||||||
if (ps.size() == 2) {
|
ZT1_VirtualNetworkList *nws = _node->networks();
|
||||||
uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
|
if (nws) {
|
||||||
_node->join(wantnw); // does nothing if we are a member
|
for(unsigned long i=0;i<nws->networkCount;++i) {
|
||||||
ZT1_VirtualNetworkList *nws = _node->networks();
|
if (nws->networks[i].nwid == wantnw) {
|
||||||
if (nws) {
|
responseContentType = "application/json";
|
||||||
for(unsigned long i=0;i<nws->networkCount;++i) {
|
_jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
|
||||||
if (nws->networks[i].nwid == wantnw) {
|
responseBody.push_back('\n');
|
||||||
responseContentType = "application/json";
|
scode = 200;
|
||||||
_jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
|
break;
|
||||||
responseBody.push_back('\n');
|
|
||||||
scode = 200;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_node->freeQueryResult((void *)nws);
|
}
|
||||||
} else scode = 500;
|
_node->freeQueryResult((void *)nws);
|
||||||
} // else 404
|
} else scode = 500;
|
||||||
}
|
}
|
||||||
} // else 404
|
} // else 404
|
||||||
} else scode = 401; // isAuth == false
|
} else scode = 401; // isAuth == false
|
||||||
|
@ -468,26 +464,22 @@ unsigned int ControlPlane::handleRequest(
|
||||||
if (ps[0] == "config") {
|
if (ps[0] == "config") {
|
||||||
// TODO
|
// TODO
|
||||||
} else if (ps[0] == "network") {
|
} else if (ps[0] == "network") {
|
||||||
if ((ps.size() > 1)&&(ps[1] == "controller")) {
|
ZT1_VirtualNetworkList *nws = _node->networks();
|
||||||
// TODO
|
if (nws) {
|
||||||
} else {
|
if (ps.size() == 2) {
|
||||||
ZT1_VirtualNetworkList *nws = _node->networks();
|
uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
|
||||||
if (nws) {
|
for(unsigned long i=0;i<nws->networkCount;++i) {
|
||||||
if (ps.size() == 2) {
|
if (nws->networks[i].nwid == wantnw) {
|
||||||
uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
|
_node->leave(wantnw);
|
||||||
for(unsigned long i=0;i<nws->networkCount;++i) {
|
responseBody = "true";
|
||||||
if (nws->networks[i].nwid == wantnw) {
|
responseContentType = "application/json";
|
||||||
_node->leave(wantnw);
|
scode = 200;
|
||||||
responseBody = "true";
|
break;
|
||||||
responseContentType = "application/json";
|
|
||||||
scode = 200;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} // else 404
|
}
|
||||||
_node->freeQueryResult((void *)nws);
|
} // else 404
|
||||||
} else scode = 500;
|
_node->freeQueryResult((void *)nws);
|
||||||
}
|
} else scode = 500;
|
||||||
} // else 404
|
} // else 404
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue