Implemented SOCKS proxy port file: networks.d/nwid.port

This commit is contained in:
Joseph Henry
2016-07-18 01:42:18 -07:00
parent 2393cd6367
commit 9df8a57dd0
17 changed files with 263 additions and 99 deletions

View File

@@ -135,8 +135,8 @@ NetconEthernetTap::NetconEthernetTap(
// Start SOCKS5 Proxy server
// For use when traditional syscall hooking isn't available (ex. some APIs on iOS and Android)
#if defined(USE_SOCKS_PROXY) || defined(__ANDROID__)
StartProxy(sockPath);
#if defined(USE_SOCKS_PROXY)
StartProxy(sockPath, _homePath.c_str(), _nwid);
#endif
Utils::snprintf(lwipPath,sizeof(lwipPath),"%s%sliblwip.so",homePath,ZT_PATH_SEPARATOR_S);
@@ -1222,6 +1222,7 @@ void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, Conne
d[1] = (ip >> 8) & 0xFF;
d[2] = (ip >> 16) & 0xFF;
d[3] = (ip >> 24) & 0xFF;
dwr(MSG_DEBUG," handleConnect(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], port);
dwr(MSG_DEBUG," handleConnect(): pcb->state = %x\n", conn->TCP_pcb->state);
if(conn->TCP_pcb->state != CLOSED) {

View File

@@ -461,7 +461,7 @@ namespace ZeroTier {
int proxyListenSocket;
PhySocket *proxyListenPhySocket;
void StartProxy(const char *nwid);
void StartProxy(const char *sockpath, const char *homepath, uint64_t nwid);
void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable);

View File

@@ -29,10 +29,13 @@
#include "SDK_EthernetTap.hpp"
#include "Phy.hpp"
#include "Utils.hpp"
#include "OSUtils.hpp"
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sstream>
#define SOCKS_OPEN 0
#define SOCKS_CONNECT_INIT 1
@@ -59,7 +62,7 @@
namespace ZeroTier
{
void NetconEthernetTap::StartProxy(const char *nwid)
void NetconEthernetTap::StartProxy(const char *sockpath, const char *homepath, uint64_t nwid)
{
#if defined (__ANDROID__)
LOGV("StartProxy()\n");
@@ -67,9 +70,31 @@ namespace ZeroTier
printf("StartProxy()\n");
#endif
unsigned int randp = 0;
Utils::getSecureRandom(&randp,sizeof(randp));
proxyListenPort = 1000 + (randp % 1000);
// Look for a port file for this network's proxy server instance
char portFile[4096];
Utils::snprintf(portFile,sizeof(portFile),"%s/networks.d/%.16llx.port",homepath,nwid);
std::string portStr;
printf("Proxy(): Reading port from: %s\n", portFile);
if(ZeroTier::OSUtils::fileExists(portFile,true))
{
if(ZeroTier::OSUtils::readFile(portFile, portStr)) {
proxyListenPort = atoi(portStr.c_str());
}
}
else
{
unsigned int randp = 0;
Utils::getSecureRandom(&randp,sizeof(randp));
proxyListenPort = 1000 + (randp % 1000);
printf("Proxy(): No port specified in networks.d/%.16llx.port, randomly picking port\n", nwid);
std::stringstream ss;
ss << proxyListenPort;
portStr = ss.str();
if(!ZeroTier::OSUtils::writeFile(portFile, portStr)) {
LOGV("unable to write proxy port file: %s\n", portFile);
}
}
struct sockaddr_in in4;
memset(&in4,0,sizeof(in4));
in4.sin_family = AF_INET;
@@ -77,7 +102,7 @@ namespace ZeroTier
in4.sin_port = Utils::hton((uint16_t)proxyListenPort);
proxyListenPhySocket = _phy.tcpListen((const struct sockaddr*)&in4,(void *)this);
sockstate = SOCKS_OPEN;
printf("SOCKS5 proxy server address for <%s> is: <0.0.0.0:%d> (sock=%p)\n", nwid, proxyListenPort, (void*)&proxyListenPhySocket);
printf("SOCKS5 proxy server address for <%.16llx> is: <0.0.0.0:%d> (sock=%p)\n", nwid, proxyListenPort, (void*)&proxyListenPhySocket);
}
void ExtractAddress(int addr_type, unsigned char *buf, struct sockaddr_in * addr)
@@ -384,28 +409,5 @@ namespace ZeroTier
void NetconEthernetTap::phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable)
{
printf("phyOnFileDescriptorActivity(): sock=%p\n", (void*&)sock);
/*
if(readable)
{
//ProxyConn *conn = (ProxyConn*)*uptr;
//if(!conn){
// printf("\t!conn");
// return;
//}
//char buf[50];
//memset(buf, 0, sizeof(buf));
//printf("Activity(R)->socket() = %d\n", _phy.getDescriptor(sock));
//printf("Activity(W)->socket() = %d\n", conn->fd);
//int n_read = read(_phy.getDescriptor(sock), buf, sizeof(buf));
//printf(" read = %d\n", n_read);
//int n_sent = write(conn->fd, buf, n_read);
//printf("buf = %s\n", buf);
//printf(" sent = %d\n", n_sent);
}
if(writable)
{
printf(" writable\n");
}
*/
}
}

View File

@@ -61,6 +61,9 @@ std::string givenHomeDir; // What the user/application provides as a suggestion
std::string homeDir; // The resultant platform-specific dir we *must* use internally
std::string netDir;
bool rpcEnabled;
std::string rpcNWID;
#ifdef __cplusplus
extern "C" {
#endif
@@ -92,10 +95,6 @@ void zt_init_rpc(const char * path, const char * nwid);
if(!ZeroTier::OSUtils::writeFile(confFile.c_str(), "")) {
LOGV("unable to write network conf file: %s\n", confFile.c_str());
}
LOGV("zt1Service = %x\n", (void*)zt1Service);
//zt1Service->join(nwid);
LOGV("started up\n");
// This provides the shim API with the RPC information
zt_init_rpc(homeDir.c_str(), nwid);
}
@@ -155,10 +154,10 @@ void zt_init_rpc(const char * path, const char * nwid);
pthread_create(&intercept_thread, NULL, startOneService, (void *)(intercept_thread_id));
}
void init_service_and_rpc(int key, const char * path, const char * nwid)
{
void init_service_and_rpc(int key, const char * path, const char * nwid) {
rpcEnabled = true;
rpcNWID = nwid;
init_service(key, path);
zt_init_rpc(path, nwid);
}
/*
@@ -233,6 +232,7 @@ void zt_init_rpc(const char * path, const char * nwid);
#if defined(__APPLE__) && !defined(__IOS__)
homeDir = givenHomeDir;
localHomeDir = givenHomeDir; // Used for RPC and *can* differ from homeDir on some platforms
#endif
LOGV("homeDir = %s", givenHomeDir.c_str());
@@ -265,8 +265,6 @@ void zt_init_rpc(const char * path, const char * nwid);
}
}
#if defined(__IOS__)
// Go to the app's data directory so we can shorten the sun_path we bind to
int MAX_DIR_SZ = 256;
@@ -280,6 +278,11 @@ void zt_init_rpc(const char * path, const char * nwid);
//chdir(current_dir); // Return to previous current working directory (at the request of Unity3D)
//Debug(homeDir.c_str());
// Initialize RPC
if(rpcEnabled) {
zt_init_rpc(localHomeDir.c_str(), rpcNWID.c_str());
}
// Generate random port for new service instance
unsigned int randp = 0;
ZeroTier::Utils::getSecureRandom(&randp,sizeof(randp));

View File

@@ -109,7 +109,6 @@ int (*realclose)(CLOSE_SIG);
strcat(fullpath, "/nc_");
strcat(fullpath, nwid);
api_netpath = fullpath;
//api_netpath = "/data/data/com.example.joseph.example_app/files/zerotier/nc_565799d8f65063e5";
}
#else
// Get path/nwid from environment variables
@@ -371,8 +370,7 @@ int (*realclose)(CLOSE_SIG);
#endif
#endif
/* -1 is passed since we we're generating the new socket in this call */
printf("path = %s\n", api_netpath);
LOGV("path = %s\n", api_netpath);
printf("api_netpath = %s\n", api_netpath);
int err = rpc_send_command(api_netpath, RPC_SOCKET, -1, &rpc_st, sizeof(struct socket_st));
//LOGV("socket() = %d\n", err);
dwr(MSG_DEBUG," socket() = %d\n", err);
@@ -388,7 +386,6 @@ int (*realclose)(CLOSE_SIG);
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_ztjniConnect(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port) {
struct sockaddr_in addr;
char *str;
// = env->GetStringUTFChars(addrstr, NULL);
(*env)->ReleaseStringUTFChars(env, addrstr, str);
addr.sin_addr.s_addr = inet_addr(str);
addr.sin_family = AF_INET;