mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-08-20 13:24:09 -07:00
More control/ refactoring
This commit is contained in:
parent
e22fae2397
commit
edff13dcae
5 changed files with 84 additions and 96 deletions
|
@ -51,43 +51,20 @@ static void _CBipcResultHandler(void *arg,IpcConnection *ipcc,IpcConnection::Eve
|
|||
}
|
||||
}
|
||||
|
||||
NodeControlClient::NodeControlClient(const char *hp,void (*resultHandler)(void *,const char *),void *arg,const char *authToken)
|
||||
NodeControlClient::NodeControlClient(const char *ep,const char *authToken,void (*resultHandler)(void *,const char *),void *arg)
|
||||
throw() :
|
||||
_impl((void *)new _NodeControlClientImpl)
|
||||
{
|
||||
_NodeControlClientImpl *impl = (_NodeControlClientImpl *)_impl;
|
||||
impl->ipcc = (IpcConnection *)0;
|
||||
|
||||
if (!hp)
|
||||
hp = ZT_DEFAULTS.defaultHomePath.c_str();
|
||||
|
||||
std::string at;
|
||||
if (authToken)
|
||||
at = authToken;
|
||||
else if (!Utils::readFile(authTokenDefaultSystemPath(),at)) {
|
||||
if (!Utils::readFile(authTokenDefaultUserPath(),at)) {
|
||||
impl->err = "no authentication token specified and authtoken.secret not readable";
|
||||
return;
|
||||
}
|
||||
impl->resultHandler = resultHandler;
|
||||
impl->arg = arg;
|
||||
try {
|
||||
impl->ipcc = new IpcConnection(ep,&_CBipcResultHandler,_impl);
|
||||
impl->ipcc->printf("auth %s"ZT_EOL_S,authToken);
|
||||
} catch ( ... ) {
|
||||
impl->ipcc = (IpcConnection *)0;
|
||||
impl->err = "failure connecting to running ZeroTier One service";
|
||||
}
|
||||
|
||||
std::string myid;
|
||||
if (Utils::readFile((std::string(hp) + ZT_PATH_SEPARATOR_S + "identity.public").c_str(),myid)) {
|
||||
std::string myaddr(myid.substr(0,myid.find(':')));
|
||||
if (myaddr.length() != 10)
|
||||
impl->err = "invalid address extracted from identity.public";
|
||||
else {
|
||||
try {
|
||||
impl->resultHandler = resultHandler;
|
||||
impl->arg = arg;
|
||||
impl->ipcc = new IpcConnection((std::string(ZT_IPC_ENDPOINT_BASE) + myaddr).c_str(),&_CBipcResultHandler,_impl);
|
||||
impl->ipcc->printf("auth %s"ZT_EOL_S,at.c_str());
|
||||
} catch ( ... ) {
|
||||
impl->ipcc = (IpcConnection *)0;
|
||||
impl->err = "failure connecting to running ZeroTier One service";
|
||||
}
|
||||
}
|
||||
} else impl->err = "unable to read identity.public";
|
||||
}
|
||||
|
||||
NodeControlClient::~NodeControlClient()
|
||||
|
@ -153,17 +130,25 @@ const char *NodeControlClient::authTokenDefaultUserPath()
|
|||
return dlp.c_str();
|
||||
}
|
||||
|
||||
const char *NodeControlClient::authTokenDefaultSystemPath()
|
||||
std::string NodeControlClient::getAuthToken(const char *path,bool generateIfNotFound)
|
||||
{
|
||||
static std::string dsp;
|
||||
static Mutex dsp_m;
|
||||
unsigned char randbuf[24];
|
||||
std::string token;
|
||||
|
||||
Mutex::Lock _l(dsp_m);
|
||||
if (Utils::readFile(path,token))
|
||||
return Utils::trim(token);
|
||||
else token = "";
|
||||
|
||||
if (!dsp.length())
|
||||
dsp = (ZT_DEFAULTS.defaultHomePath + ZT_PATH_SEPARATOR_S"authtoken.secret");
|
||||
if (generateIfNotFound) {
|
||||
Utils::getSecureRandom(randbuf,sizeof(randbuf));
|
||||
for(unsigned int i=0;i<sizeof(randbuf);++i)
|
||||
token.push_back(("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")[(unsigned int)randbuf[i] % 62]);
|
||||
if (!Utils::writeFile(path,token))
|
||||
return std::string();
|
||||
Utils::lockDownFile(path,false);
|
||||
}
|
||||
|
||||
return dsp.c_str();
|
||||
return token;
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
|
@ -31,14 +31,16 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
#define ZT_IPC_ENDPOINT_BASE "\\\\.\\pipe\\ZeroTierOne-"
|
||||
#else
|
||||
#define ZT_IPC_ENDPOINT_BASE "/tmp/.ZeroTierOne-"
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
/**
|
||||
* Client for controlling a local ZeroTier One node
|
||||
*
|
||||
* Windows note: be sure you call WSAStartup() before using this,
|
||||
* otherwise it will be unable to open a local UDP socket to
|
||||
* communicate with the service.
|
||||
*/
|
||||
class NodeControlClient
|
||||
{
|
||||
|
@ -48,12 +50,11 @@ public:
|
|||
*
|
||||
* Initialization may fail. Call error() to check.
|
||||
*
|
||||
* @param hp Home path of ZeroTier One instance or NULL for default system home path
|
||||
* @param ep Endpoint to connect to (OS-dependent)
|
||||
* @param resultHandler Function to call when commands provide results
|
||||
* @param arg First argument to result handler
|
||||
* @param authToken Authentication token or NULL (default) to read from authtoken.secret in home path
|
||||
*/
|
||||
NodeControlClient(const char *hp,void (*resultHandler)(void *,const char *),void *arg,const char *authToken = (const char *)0)
|
||||
NodeControlClient(const char *ep,const char *authToken,void (*resultHandler)(void *,const char *),void *arg)
|
||||
throw();
|
||||
|
||||
~NodeControlClient();
|
||||
|
@ -89,14 +90,18 @@ public:
|
|||
static inline std::vector<std::string> splitLine(const std::string &line) { return splitLine(line.c_str()); }
|
||||
|
||||
/**
|
||||
* @return Default path for current user's authtoken.secret
|
||||
* @return Default path for current user's authtoken.secret or ~/.zeroTierOneAuthToken (location is platform-dependent)
|
||||
*/
|
||||
static const char *authTokenDefaultUserPath();
|
||||
|
||||
/**
|
||||
* @return Default path to system authtoken.secret
|
||||
* Load (or generate) the authentication token
|
||||
*
|
||||
* @param path Full path to authtoken.secret
|
||||
* @param generateIfNotFound If true, generate and save if not found or readable (requires appropriate privileges, returns empty on failure)
|
||||
* @return Authentication token or empty string on failure
|
||||
*/
|
||||
static const char *authTokenDefaultSystemPath();
|
||||
static std::string getAuthToken(const char *path,bool generateIfNotFound);
|
||||
|
||||
private:
|
||||
// NodeControlClient is not copyable
|
||||
|
|
|
@ -29,7 +29,9 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../node/Constants.hpp"
|
||||
#include "NodeControlService.hpp"
|
||||
#include "NodeControlClient.hpp"
|
||||
#include "../node/Node.hpp"
|
||||
#include "../node/Utils.hpp"
|
||||
|
||||
|
@ -114,7 +116,7 @@ void NodeControlService::_doCommand(IpcConnection *ipcc,const char *commandLine)
|
|||
ipcc->printf("200 help terminate [<reason>]"ZT_EOL_S);
|
||||
ipcc->printf("200 help updatecheck"ZT_EOL_S);
|
||||
} else if (cmd[0] == "auth") {
|
||||
if ((cmd.size() > 1)&&(_authToken == cmd[1])) {
|
||||
if ((cmd.size() > 1)&&(_authToken.length() > 0)&&(_authToken == cmd[1])) {
|
||||
Mutex::Lock _l(_connections_m);
|
||||
_connections[ipcc] = true;
|
||||
ipcc->printf("200 auth OK"ZT_EOL_S);
|
||||
|
@ -226,25 +228,4 @@ void NodeControlService::_doCommand(IpcConnection *ipcc,const char *commandLine)
|
|||
ipcc->printf("."ZT_EOL_S); // blank line ends response
|
||||
}
|
||||
|
||||
std::string NodeControlService::readOrCreateAuthtoken(const char *path,bool generateIfNotFound)
|
||||
{
|
||||
unsigned char randbuf[24];
|
||||
std::string token;
|
||||
|
||||
if (Utils::readFile(path,token))
|
||||
return token;
|
||||
else token = "";
|
||||
|
||||
if (generateIfNotFound) {
|
||||
Utils::getSecureRandom(randbuf,sizeof(randbuf));
|
||||
for(unsigned int i=0;i<sizeof(randbuf);++i)
|
||||
token.push_back(("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")[(unsigned int)randbuf[i] % 62]);
|
||||
if (!Utils::writeFile(path,token))
|
||||
return std::string();
|
||||
Utils::lockDownFile(path,false);
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
|
@ -38,12 +38,6 @@
|
|||
#include "../node/NonCopyable.hpp"
|
||||
#include "../node/Thread.hpp"
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
#define ZT_IPC_ENDPOINT_BASE "\\\\.\\pipe\\ZeroTierOne-"
|
||||
#else
|
||||
#define ZT_IPC_ENDPOINT_BASE "/tmp/.ZeroTierOne-"
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class Node;
|
||||
|
@ -65,19 +59,11 @@ public:
|
|||
|
||||
~NodeControlService();
|
||||
|
||||
// Background thread waits for node to initialize, then creates IpcListener
|
||||
// Background thread waits for node to initialize, then creates IpcListener and
|
||||
// terminates. It also terminates on delete if it hasn't bootstrapped yet.
|
||||
void threadMain()
|
||||
throw();
|
||||
|
||||
/**
|
||||
* Load (or generate) the authentication token
|
||||
*
|
||||
* @param path Full path to authtoken.secret
|
||||
* @param generateIfNotFound If true, generate and save if not found or readable
|
||||
* @return Authentication token or empty string on failure
|
||||
*/
|
||||
static std::string readOrCreateAuthtoken(const char *path,bool generateIfNotFound);
|
||||
|
||||
private:
|
||||
static void _CBcommandHandler(void *arg,IpcConnection *ipcc,IpcConnection::EventType event,const char *commandLine);
|
||||
void _doCommand(IpcConnection *ipcc,const char *commandLine);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue