|
| 1 | +/* |
| 2 | + * ZeroTier One - Network Virtualization Everywhere |
| 3 | + * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +// HACK! Will eventually use epoll() or something in Phy<> instead of select(). |
| 20 | +// Also be sure to change ulimit -n and fs.file-max in /etc/sysctl.conf on relays. |
| 21 | +#if defined(__linux__) || defined(__LINUX__) || defined(__LINUX) || defined(LINUX) |
| 22 | +#include <linux/posix_types.h> |
| 23 | +#include <bits/types.h> |
| 24 | +#undef __FD_SETSIZE |
| 25 | +#define __FD_SETSIZE 1048576 |
| 26 | +#undef FD_SETSIZE |
| 27 | +#define FD_SETSIZE 1048576 |
| 28 | +#endif |
| 29 | + |
| 30 | +#include <stdio.h> |
| 31 | +#include <stdlib.h> |
| 32 | +#include <string.h> |
| 33 | +#include <time.h> |
| 34 | +#include <stdint.h> |
| 35 | +#include <unistd.h> |
| 36 | +#include <signal.h> |
| 37 | + |
| 38 | +#include <map> |
| 39 | +#include <set> |
| 40 | +#include <string> |
| 41 | +#include <algorithm> |
| 42 | +#include <vector> |
| 43 | + |
| 44 | +#include "../osdep/Phy.hpp" |
| 45 | + |
| 46 | +#define ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS 300 |
| 47 | +#define ZT_TCP_PROXY_TCP_PORT 443 |
| 48 | + |
| 49 | +using namespace ZeroTier; |
| 50 | + |
| 51 | +/* |
| 52 | + * ZeroTier TCP Proxy Server |
| 53 | + * |
| 54 | + * This implements a simple packet encapsulation that is designed to look like |
| 55 | + * a TLS connection. It's not a TLS connection, but it sends TLS format record |
| 56 | + * headers. It could be extended in the future to implement a fake TLS |
| 57 | + * handshake. |
| 58 | + * |
| 59 | + * At the moment, each packet is just made to look like TLS application data: |
| 60 | + * <[1] TLS content type> - currently 0x17 for "application data" |
| 61 | + * <[1] TLS major version> - currently 0x03 for TLS 1.2 |
| 62 | + * <[1] TLS minor version> - currently 0x03 for TLS 1.2 |
| 63 | + * <[2] payload length> - 16-bit length of payload in bytes |
| 64 | + * <[...] payload> - Message payload |
| 65 | + * |
| 66 | + * TCP is inherently inefficient for encapsulating Ethernet, since TCP and TCP |
| 67 | + * like protocols over TCP lead to double-ACKs. So this transport is only used |
| 68 | + * to enable access when UDP or other datagram protocols are not available. |
| 69 | + * |
| 70 | + * Clients send a greeting, which is a four-byte message that contains: |
| 71 | + * <[1] ZeroTier major version> |
| 72 | + * <[1] minor version> |
| 73 | + * <[2] revision> |
| 74 | + * |
| 75 | + * If a client has sent a greeting, it uses the new version of this protocol |
| 76 | + * in which every encapsulated ZT packet is prepended by an IP address where |
| 77 | + * it should be forwarded (or where it came from for replies). This causes |
| 78 | + * this proxy to act as a remote UDP socket similar to a socks proxy, which |
| 79 | + * will allow us to move this function off the rootservers and onto dedicated |
| 80 | + * proxy nodes. |
| 81 | + * |
| 82 | + * Older ZT clients that do not send this message get their packets relayed |
| 83 | + * to/from 127.0.0.1:9993, which will allow them to talk to and relay via |
| 84 | + * the ZT node on the same machine as the proxy. We'll only support this for |
| 85 | + * as long as such nodes appear to be in the wild. |
| 86 | + */ |
| 87 | + |
| 88 | +struct TcpProxyService; |
| 89 | +struct TcpProxyService |
| 90 | +{ |
| 91 | + Phy<TcpProxyService *> *phy; |
| 92 | + int udpPortCounter; |
| 93 | + struct Client |
| 94 | + { |
| 95 | + char tcpReadBuf[131072]; |
| 96 | + char tcpWriteBuf[131072]; |
| 97 | + unsigned long tcpWritePtr; |
| 98 | + unsigned long tcpReadPtr; |
| 99 | + PhySocket *tcp; |
| 100 | + PhySocket *udp; |
| 101 | + time_t lastActivity; |
| 102 | + bool newVersion; |
| 103 | + }; |
| 104 | + std::map< PhySocket *,Client > clients; |
| 105 | + |
| 106 | + PhySocket *getUnusedUdp(void *uptr) |
| 107 | + { |
| 108 | + for(int i=0;i<65535;++i) { |
| 109 | + ++udpPortCounter; |
| 110 | + if (udpPortCounter > 0xfffe) |
| 111 | + udpPortCounter = 1024; |
| 112 | + struct sockaddr_in laddr; |
| 113 | + memset(&laddr,0,sizeof(struct sockaddr_in)); |
| 114 | + laddr.sin_family = AF_INET; |
| 115 | + laddr.sin_port = htons((uint16_t)udpPortCounter); |
| 116 | + PhySocket *udp = phy->udpBind(reinterpret_cast<struct sockaddr *>(&laddr),uptr); |
| 117 | + if (udp) |
| 118 | + return udp; |
| 119 | + } |
| 120 | + return (PhySocket *)0; |
| 121 | + } |
| 122 | + |
| 123 | + void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len) |
| 124 | + { |
| 125 | + if (!*uptr) |
| 126 | + return; |
| 127 | + if ((from->sa_family == AF_INET)&&(len >= 16)&&(len < 2048)) { |
| 128 | + Client &c = *((Client *)*uptr); |
| 129 | + c.lastActivity = time((time_t *)0); |
| 130 | + |
| 131 | + unsigned long mlen = len; |
| 132 | + if (c.newVersion) |
| 133 | + mlen += 7; // new clients get IP info |
| 134 | + |
| 135 | + if ((c.tcpWritePtr + 5 + mlen) <= sizeof(c.tcpWriteBuf)) { |
| 136 | + if (!c.tcpWritePtr) |
| 137 | + phy->setNotifyWritable(c.tcp,true); |
| 138 | + |
| 139 | + c.tcpWriteBuf[c.tcpWritePtr++] = 0x17; // look like TLS data |
| 140 | + c.tcpWriteBuf[c.tcpWritePtr++] = 0x03; // look like TLS 1.2 |
| 141 | + c.tcpWriteBuf[c.tcpWritePtr++] = 0x03; // look like TLS 1.2 |
| 142 | + |
| 143 | + c.tcpWriteBuf[c.tcpWritePtr++] = (char)((mlen >> 8) & 0xff); |
| 144 | + c.tcpWriteBuf[c.tcpWritePtr++] = (char)(mlen & 0xff); |
| 145 | + |
| 146 | + if (c.newVersion) { |
| 147 | + c.tcpWriteBuf[c.tcpWritePtr++] = (char)4; // IPv4 |
| 148 | + *((uint32_t *)(c.tcpWriteBuf + c.tcpWritePtr)) = ((const struct sockaddr_in *)from)->sin_addr.s_addr; |
| 149 | + c.tcpWritePtr += 4; |
| 150 | + *((uint16_t *)(c.tcpWriteBuf + c.tcpWritePtr)) = ((const struct sockaddr_in *)from)->sin_port; |
| 151 | + c.tcpWritePtr += 2; |
| 152 | + } |
| 153 | + |
| 154 | + for(unsigned long i=0;i<len;++i) |
| 155 | + c.tcpWriteBuf[c.tcpWritePtr++] = ((const char *)data)[i]; |
| 156 | + } |
| 157 | + |
| 158 | + printf("<< UDP %s:%d -> %.16llx\n",inet_ntoa(reinterpret_cast<const struct sockaddr_in *>(from)->sin_addr),(int)ntohs(reinterpret_cast<const struct sockaddr_in *>(from)->sin_port),(unsigned long long)&c); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) |
| 163 | + { |
| 164 | + // unused, we don't initiate outbound connections |
| 165 | + } |
| 166 | + |
| 167 | + void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) |
| 168 | + { |
| 169 | + Client &c = clients[sockN]; |
| 170 | + PhySocket *udp = getUnusedUdp((void *)&c); |
| 171 | + if (!udp) { |
| 172 | + phy->close(sockN); |
| 173 | + clients.erase(sockN); |
| 174 | + printf("** TCP rejected, no more UDP ports to assign\n"); |
| 175 | + return; |
| 176 | + } |
| 177 | + c.tcpWritePtr = 0; |
| 178 | + c.tcpReadPtr = 0; |
| 179 | + c.tcp = sockN; |
| 180 | + c.udp = udp; |
| 181 | + c.lastActivity = time((time_t *)0); |
| 182 | + c.newVersion = false; |
| 183 | + *uptrN = (void *)&c; |
| 184 | + printf("<< TCP from %s -> %.16llx\n",inet_ntoa(reinterpret_cast<const struct sockaddr_in *>(from)->sin_addr),(unsigned long long)&c); |
| 185 | + } |
| 186 | + |
| 187 | + void phyOnTcpClose(PhySocket *sock,void **uptr) |
| 188 | + { |
| 189 | + if (!*uptr) |
| 190 | + return; |
| 191 | + Client &c = *((Client *)*uptr); |
| 192 | + phy->close(c.udp); |
| 193 | + clients.erase(sock); |
| 194 | + printf("** TCP %.16llx closed\n",(unsigned long long)*uptr); |
| 195 | + } |
| 196 | + |
| 197 | + void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) |
| 198 | + { |
| 199 | + Client &c = *((Client *)*uptr); |
| 200 | + c.lastActivity = time((time_t *)0); |
| 201 | + |
| 202 | + for(unsigned long i=0;i<len;++i) { |
| 203 | + if (c.tcpReadPtr >= sizeof(c.tcpReadBuf)) { |
| 204 | + phy->close(sock); |
| 205 | + return; |
| 206 | + } |
| 207 | + c.tcpReadBuf[c.tcpReadPtr++] = ((const char *)data)[i]; |
| 208 | + |
| 209 | + if (c.tcpReadPtr >= 5) { |
| 210 | + unsigned long mlen = ( ((((unsigned long)c.tcpReadBuf[3]) & 0xff) << 8) | (((unsigned long)c.tcpReadBuf[4]) & 0xff) ); |
| 211 | + if (c.tcpReadPtr >= (mlen + 5)) { |
| 212 | + if (mlen == 4) { |
| 213 | + // Right now just sending this means the client is 'new enough' for the IP header |
| 214 | + c.newVersion = true; |
| 215 | + printf("<< TCP %.16llx HELLO\n",(unsigned long long)*uptr); |
| 216 | + } else if (mlen >= 7) { |
| 217 | + char *payload = c.tcpReadBuf + 5; |
| 218 | + unsigned long payloadLen = mlen; |
| 219 | + |
| 220 | + struct sockaddr_in dest; |
| 221 | + memset(&dest,0,sizeof(dest)); |
| 222 | + if (c.newVersion) { |
| 223 | + if (*payload == (char)4) { |
| 224 | + // New clients tell us where their packets go. |
| 225 | + ++payload; |
| 226 | + dest.sin_family = AF_INET; |
| 227 | + dest.sin_addr.s_addr = *((uint32_t *)payload); |
| 228 | + payload += 4; |
| 229 | + dest.sin_port = *((uint16_t *)payload); // will be in network byte order already |
| 230 | + payload += 2; |
| 231 | + payloadLen -= 7; |
| 232 | + } |
| 233 | + } else { |
| 234 | + // For old clients we will just proxy everything to a local ZT instance. The |
| 235 | + // fact that this will come from 127.0.0.1 will in turn prevent that instance |
| 236 | + // from doing unite() with us. It'll just forward. There will not be many of |
| 237 | + // these. |
| 238 | + dest.sin_family = AF_INET; |
| 239 | + dest.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1 |
| 240 | + dest.sin_port = htons(9993); |
| 241 | + } |
| 242 | + |
| 243 | + // Note: we do not relay to privileged ports... just an abuse prevention rule. |
| 244 | + if ((ntohs(dest.sin_port) > 1024)&&(payloadLen >= 16)) { |
| 245 | + phy->udpSend(c.udp,(const struct sockaddr *)&dest,payload,payloadLen); |
| 246 | + printf(">> TCP %.16llx to %s:%d\n",(unsigned long long)*uptr,inet_ntoa(dest.sin_addr),(int)ntohs(dest.sin_port)); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + memmove(c.tcpReadBuf,c.tcpReadBuf + (mlen + 5),c.tcpReadPtr -= (mlen + 5)); |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + void phyOnTcpWritable(PhySocket *sock,void **uptr) |
| 257 | + { |
| 258 | + Client &c = *((Client *)*uptr); |
| 259 | + if (c.tcpWritePtr) { |
| 260 | + long n = phy->streamSend(sock,c.tcpWriteBuf,c.tcpWritePtr); |
| 261 | + if (n > 0) { |
| 262 | + memmove(c.tcpWriteBuf,c.tcpWriteBuf + n,c.tcpWritePtr -= (unsigned long)n); |
| 263 | + if (!c.tcpWritePtr) |
| 264 | + phy->setNotifyWritable(sock,false); |
| 265 | + } |
| 266 | + } else phy->setNotifyWritable(sock,false); |
| 267 | + } |
| 268 | + |
| 269 | + void doHousekeeping() |
| 270 | + { |
| 271 | + std::vector<PhySocket *> toClose; |
| 272 | + time_t now = time((time_t *)0); |
| 273 | + for(std::map< PhySocket *,Client >::iterator c(clients.begin());c!=clients.end();++c) { |
| 274 | + if ((now - c->second.lastActivity) >= ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS) { |
| 275 | + toClose.push_back(c->first); |
| 276 | + toClose.push_back(c->second.udp); |
| 277 | + } |
| 278 | + } |
| 279 | + for(std::vector<PhySocket *>::iterator s(toClose.begin());s!=toClose.end();++s) |
| 280 | + phy->close(*s); |
| 281 | + } |
| 282 | +}; |
| 283 | + |
| 284 | +int main(int argc,char **argv) |
| 285 | +{ |
| 286 | + signal(SIGPIPE,SIG_IGN); |
| 287 | + signal(SIGHUP,SIG_IGN); |
| 288 | + srand(time((time_t *)0)); |
| 289 | + |
| 290 | + TcpProxyService svc; |
| 291 | + Phy<TcpProxyService *> phy(&svc,false,true); |
| 292 | + svc.phy = &phy; |
| 293 | + svc.udpPortCounter = 1023; |
| 294 | + |
| 295 | + { |
| 296 | + struct sockaddr_in laddr; |
| 297 | + memset(&laddr,0,sizeof(laddr)); |
| 298 | + laddr.sin_family = AF_INET; |
| 299 | + laddr.sin_port = htons(ZT_TCP_PROXY_TCP_PORT); |
| 300 | + if (!phy.tcpListen((const struct sockaddr *)&laddr)) { |
| 301 | + fprintf(stderr,"%s: fatal error: unable to bind TCP port %d\n",argv[0],ZT_TCP_PROXY_TCP_PORT); |
| 302 | + return 1; |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + time_t lastDidHousekeeping = time((time_t *)0); |
| 307 | + for(;;) { |
| 308 | + phy.poll(120000); |
| 309 | + time_t now = time((time_t *)0); |
| 310 | + if ((now - lastDidHousekeeping) > 120) { |
| 311 | + lastDidHousekeeping = now; |
| 312 | + svc.doHousekeeping(); |
| 313 | + } |
| 314 | + } |
| 315 | + |
| 316 | + return 0; |
| 317 | +} |
0 commit comments