Ver código fonte

Add some proxy debugging, and auto-resolve and periodically re-resolve TCP fallback tunnel hostname.

Adam Ierymenko 10 anos atrás
pai
commit
0f7dcb3ef2
3 arquivos alterados com 39 adições e 5 exclusões
  1. 1 0
      osdep/Phy.hpp
  2. 23 5
      service/OneService.cpp
  3. 15 0
      tcp-proxy/tcp-proxy.cpp

+ 1 - 0
osdep/Phy.hpp

@@ -33,6 +33,7 @@
 #include <string.h>
 
 #include <list>
+#include <stdexcept>
 
 #if defined(_WIN32) || defined(_WIN64)
 

+ 23 - 5
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<TcpConnection *>(*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

+ 15 - 0
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<const InetAddress *>(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<len;++i)
 						c.tcpWriteBuf[c.tcpWritePtr++] = ((const char *)data)[i];
 				}
+			} else {
+#ifdef ZT_TCP_PROXY_TRACE
+				printf("UDP [%u] %s >> (unknown, discarded)\n",len,reinterpret_cast<const InetAddress *>(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<const InetAddress *>(from)->toString().c_str(),(unsigned long long)&c);
+#endif
 	}
 
 	void phyOnTcpClose(PhySocket *sock,void **uptr)