From 0f7dcb3ef20f408df96307e19442cce2a1f5c6b5 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 21 May 2015 15:22:41 -0700 Subject: [PATCH] Add some proxy debugging, and auto-resolve and periodically re-resolve TCP fallback tunnel hostname. --- osdep/Phy.hpp | 1 + service/OneService.cpp | 28 +++++++++++++++++++++++----- tcp-proxy/tcp-proxy.cpp | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/osdep/Phy.hpp b/osdep/Phy.hpp index 5cebe1692..23fd2ee24 100644 --- a/osdep/Phy.hpp +++ b/osdep/Phy.hpp @@ -33,6 +33,7 @@ #include #include +#include #if defined(_WIN32) || defined(_WIN64) diff --git a/service/OneService.cpp b/service/OneService.cpp index 790f691b9..aef3f557a 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -53,6 +53,7 @@ #include "../osdep/Thread.hpp" #include "../osdep/OSUtils.hpp" #include "../osdep/Http.hpp" +#include "../osdep/BackgroundResolver.hpp" #include "OneService.hpp" #include "ControlPlane.hpp" @@ -102,6 +103,12 @@ namespace ZeroTier { typedef BSDEthernetTap EthernetTap; } // Path under ZT1 home for controller database if controller is enabled #define ZT1_CONTROLLER_DB_PATH "controller.db" +// TCP fallback relay host +#define ZT1_TCP_FALLBACK_RELAY "tcp-fallback.zerotier.com" + +// Frequency at which we re-resolve the TCP fallback relay +#define ZT1_TCP_FALLBACK_RERESOLVE_DELAY 86400000 + namespace ZeroTier { namespace { @@ -365,6 +372,7 @@ class OneServiceImpl : public OneService public: OneServiceImpl(const char *hp,unsigned int port,const char *overrideRootTopology) : _homePath((hp) ? hp : "."), + _tcpFallbackResolver(ZT1_TCP_FALLBACK_RELAY), #ifdef ZT_ENABLE_NETWORK_CONTROLLER _controller((_homePath + ZT_PATH_SEPARATOR_S + ZT1_CONTROLLER_DB_PATH).c_str()), #endif @@ -467,6 +475,7 @@ public: _nextBackgroundTaskDeadline = 0; uint64_t lastTapMulticastGroupCheck = 0; + uint64_t lastTcpFallbackResolve = 0; #ifdef ZT_AUTO_UPDATE uint64_t lastSoftwareUpdateCheck = 0; #endif // ZT_AUTO_UPDATE @@ -494,6 +503,11 @@ public: } #endif // ZT_AUTO_UPDATE + if ((now - lastTcpFallbackResolve) >= ZT1_TCP_FALLBACK_RERESOLVE_DELAY) { + lastTcpFallbackResolve = now; + _tcpFallbackResolver.resolveNow(); + } + if ((now - lastTapMulticastGroupCheck) >= ZT_TAP_CHECK_MULTICAST_INTERVAL) { lastTapMulticastGroupCheck = now; Mutex::Lock _l(_taps_m); @@ -595,7 +609,8 @@ public: if (!success) return; - // Outgoing connections are right now only tunnel connections + // Outgoing TCP connections are always TCP fallback tunnel connections. + TcpConnection *tc = &(_tcpConections[sock]); tc->type = TcpConnection::TCP_TUNNEL_OUTGOING; tc->shouldKeepAlive = true; // unused @@ -623,7 +638,8 @@ public: inline void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) { - // Incoming connections are TCP HTTP requests + // Incoming TCP connections are HTTP JSON API requests. + TcpConnection *tc = &(_tcpConections[sockN]); tc->type = TcpConnection::TCP_HTTP_INCOMING; tc->shouldKeepAlive = true; @@ -653,6 +669,7 @@ public: { TcpConnection *tc = reinterpret_cast(*uptr); switch(tc->type) { + case TcpConnection::TCP_HTTP_INCOMING: case TcpConnection::TCP_HTTP_OUTGOING: http_parser_execute(&(tc->parser),&HTTP_PARSER_SETTINGS,(const char *)data,len); @@ -661,6 +678,7 @@ public: return; } break; + case TcpConnection::TCP_TUNNEL_OUTGOING: tc->body.append((const char *)data,len); if (tc->body.length() > 65535) { @@ -727,12 +745,14 @@ public: return; } } + if (tc->body.length() > (mlen + 5)) tc->body = tc->body.substr(mlen + 5); else tc->body = ""; } } break; + } } @@ -829,9 +849,6 @@ public: this->terminate(); } break; - case ZT1_EVENT_SAW_MORE_RECENT_VERSION: { - } break; - case ZT1_EVENT_TRACE: { if (metaData) { ::fprintf(stderr,"%s"ZT_EOL_S,(const char *)metaData); @@ -994,6 +1011,7 @@ private: } const std::string _homePath; + BackgroundResolver _tcpFallbackResolver; #ifdef ZT_ENABLE_NETWORK_CONTROLLER SqliteNetworkController _controller; #endif diff --git a/tcp-proxy/tcp-proxy.cpp b/tcp-proxy/tcp-proxy.cpp index 9e3f5d079..e30981cc9 100644 --- a/tcp-proxy/tcp-proxy.cpp +++ b/tcp-proxy/tcp-proxy.cpp @@ -45,6 +45,9 @@ #define ZT_TCP_PROXY_UDP_POOL_START_PORT 10000 #define ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS 300 +// Uncomment to print tracing output to stdout +#define ZT_TCP_PROXY_TRACE + using namespace ZeroTier; /* @@ -135,6 +138,10 @@ struct TcpProxyService if (rm != reverseMappings.end()) { Client &c = *(rm->second); +#ifdef ZT_TCP_PROXY_TRACE + printf("UDP [%u] %s >> %.16llx\n",len,reinterpret_cast(from)->toString().c_str(),(unsigned long long)&c); +#endif + unsigned long mlen = len; if (c.newVersion) mlen += 7; // new clients get IP info @@ -161,6 +168,10 @@ struct TcpProxyService for(unsigned long i=0;i> (unknown, discarded)\n",len,reinterpret_cast(from)->toString().c_str()); +#endif } } } @@ -180,6 +191,10 @@ struct TcpProxyService c.lastActivity = time((time_t *)0); c.newVersion = false; *uptrN = (void *)&c; + +#ifdef ZT_TCP_PROXY_TRACE + printf("TCP connect from %s -> %.16llx\n",reinterpret_cast(from)->toString().c_str(),(unsigned long long)&c); +#endif } void phyOnTcpClose(PhySocket *sock,void **uptr)