Touched up ztproxy a little

This commit is contained in:
Joseph Henry
2017-09-29 15:31:44 -07:00
parent af7a164f30
commit 7c303b69fd
2 changed files with 63 additions and 110 deletions

View File

@@ -24,27 +24,16 @@
* of your own application. * of your own application.
*/ */
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h> #include <string.h>
#include <netinet/in.h>
#include <netdb.h> #include <netdb.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string> #include <string>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <queue>
#include <iostream>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <fstream>
#include <map> #include <map>
#include "RingBuffer.hpp" #include "RingBuffer.hpp"
@@ -55,7 +44,8 @@ namespace ZeroTier {
typedef void PhySocket; typedef void PhySocket;
ZTProxy::ZTProxy(int proxy_listen_port, std::string nwid, std::string path, std::string internal_addr, int internal_port) ZTProxy::ZTProxy(int proxy_listen_port, std::string nwid, std::string path, std::string internal_addr,
int internal_port)
: :
_enabled(true), _enabled(true),
_run(true), _run(true),
@@ -74,7 +64,7 @@ namespace ZeroTier {
struct sockaddr_in in4; struct sockaddr_in in4;
memset(&in4,0,sizeof(in4)); memset(&in4,0,sizeof(in4));
in4.sin_family = AF_INET; in4.sin_family = AF_INET;
in4.sin_addr.s_addr = Utils::hton((uint32_t)(0x7f000001)); // right now we just listen for TCP @127.0.0.1 in4.sin_addr.s_addr = Utils::hton((uint32_t)(0x7f000001)); // listen for TCP @127.0.0.1
in4.sin_port = Utils::hton((uint16_t)proxy_listen_port); in4.sin_port = Utils::hton((uint16_t)proxy_listen_port);
_tcpListenSocket = _phy.tcpListen((const struct sockaddr *)&in4,this); _tcpListenSocket = _phy.tcpListen((const struct sockaddr *)&in4,this);
// IPv6 // IPv6
@@ -86,11 +76,12 @@ namespace ZeroTier {
in6.sin6_port = Utils::hton((uint16_t)proxy_listen_port); in6.sin6_port = Utils::hton((uint16_t)proxy_listen_port);
_tcpListenSocket6 = _phy.tcpListen((const struct sockaddr *)&in6,this); _tcpListenSocket6 = _phy.tcpListen((const struct sockaddr *)&in6,this);
if(!_tcpListenSocket) if (!_tcpListenSocket) {
DEBUG_ERROR("Error binding on port %d for IPv4 HTTP listen socket", proxy_listen_port); DEBUG_ERROR("Error binding on port %d for IPv4 HTTP listen socket", proxy_listen_port);
if(!_tcpListenSocket6) }
if (!_tcpListenSocket6) {
DEBUG_ERROR("Error binding on port %d for IPv6 HTTP listen socket", proxy_listen_port); DEBUG_ERROR("Error binding on port %d for IPv6 HTTP listen socket", proxy_listen_port);
}
_thread = Thread::start(this); _thread = Thread::start(this);
} }
@@ -107,13 +98,11 @@ namespace ZeroTier {
throw() throw()
{ {
TcpConnection *conn = NULL; TcpConnection *conn = NULL;
uint32_t msecs = 1; uint32_t msecs = 1;
struct timeval tv; struct timeval tv;
tv.tv_sec = msecs / 1000; tv.tv_sec = msecs / 1000;
tv.tv_usec = (msecs % 1000) * 1000; tv.tv_usec = (msecs % 1000) * 1000;
int ret = 0; int ret = 0;
// Main I/O loop // Main I/O loop
// Moves data between client application socket and libzt VirtualSocket // Moves data between client application socket and libzt VirtualSocket
while(_run) { while(_run) {
@@ -131,26 +120,26 @@ namespace ZeroTier {
nfds = clist[i]->zfd > nfds ? clist[i]->zfd : nfds; nfds = clist[i]->zfd > nfds ? clist[i]->zfd : nfds;
} }
ret = zts_select(nfds + 1, &read_set, &write_set, NULL, &tv); ret = zts_select(nfds + 1, &read_set, &write_set, NULL, &tv);
if (ret > 0) { if (ret > 0) {
for (int fd_i=0; fd_i<nfds+1; fd_i++) { for (int fd_i=0; fd_i<nfds+1; fd_i++) { // I/O needs to be handled on at least one fd
// RX, Handle data incoming from libzt // RX, Handle data incoming from libzt
if (FD_ISSET(fd_i, &read_set)) { if (FD_ISSET(fd_i, &read_set)) {
int wr = 0, rd = 0; int wr = 0, rd = 0;
conn = zmap[fd_i]; conn = zmap[fd_i];
if (conn == NULL) { if (conn == NULL) {
DEBUG_ERROR("invalid conn=%p", conn); DEBUG_ERROR("invalid conn");
exit(0); exit(0);
} }
// read data from libzt and place it on ring buffer // read data from libzt and place it on ring buffer
conn->rx_m.lock(); conn->rx_m.lock();
if (conn->RXbuf->count() > 0) { if (conn->RXbuf->count() > 0) {
DEBUG_INFO("libzt has incoming data on fd=%d, we will receive it via conn=%p, sock=%p", conn->zfd, conn, conn->client_sock); //DEBUG_INFO("libzt has incoming data on fd=%d, RXing via conn=%p, sock=%p",
// conn->zfd, conn, conn->client_sock);
} }
if ((rd = zts_read(conn->zfd, conn->RXbuf->get_buf(),ZT_MAX_MTU)) < 0) { if ((rd = zts_read(conn->zfd, conn->RXbuf->get_buf(),ZT_MAX_MTU)) < 0) {
DEBUG_ERROR("there was an error while reading data from libzt, err=%d", rd); DEBUG_ERROR("error while reading data from libzt, err=%d", rd);
} }
else { else {
//DEBUG_INFO("LIBZT -> RXBUFFER = %d bytes", rd); //DEBUG_INFO("LIBZT -> RXBUFFER = %d bytes", rd);
@@ -158,7 +147,7 @@ namespace ZeroTier {
} }
// attempt to write data to client from buffer // attempt to write data to client from buffer
if ((wr = _phy.streamSend(conn->client_sock, conn->RXbuf->get_buf(), conn->RXbuf->count())) < 0) { if ((wr = _phy.streamSend(conn->client_sock, conn->RXbuf->get_buf(), conn->RXbuf->count())) < 0) {
DEBUG_ERROR("there was an error while writing the data from the RXbuf to the client PhySocket, err=%d", wr); DEBUG_ERROR("error while writing the data from the RXbuf to the client PhySocket, err=%d", wr);
} }
else { else {
//DEBUG_INFO("RXBUFFER -> CLIENT = %d bytes", wr); //DEBUG_INFO("RXBUFFER -> CLIENT = %d bytes", wr);
@@ -169,21 +158,19 @@ namespace ZeroTier {
// TX, Handle data outgoing from client to libzt // TX, Handle data outgoing from client to libzt
if (FD_ISSET(fd_i, &write_set)) { if (FD_ISSET(fd_i, &write_set)) {
int rd, wr = 0; int wr = 0;
conn = zmap[fd_i]; conn = zmap[fd_i];
if (conn == NULL) { if (conn == NULL) {
DEBUG_ERROR("invalid conn=%p", conn); DEBUG_ERROR("invalid conn");
exit(0); exit(0);
} }
// read data from client and place it on ring buffer // read data from client and place it on ring buffer
//
conn->tx_m.lock(); conn->tx_m.lock();
if (conn->TXbuf->count() > 0) { if (conn->TXbuf->count() > 0) {
DEBUG_INFO("client has outgoing data of len=%d on fd=%d, we will send it via conn=%p, sock=%p", conn->TXbuf->count(), conn->zfd, conn, conn->client_sock); // DEBUG_INFO("client has outgoing data of len=%d on fd=%d, TXing via conn=%p, sock=%p",
wr = zts_write(conn->zfd, conn->TXbuf->get_buf(), conn->TXbuf->count()); // conn->TXbuf->count(), conn->zfd, conn, conn->client_sock);
if (wr < 0) { if ((wr = zts_write(conn->zfd, conn->TXbuf->get_buf(), conn->TXbuf->count())) < 0) {
DEBUG_ERROR("there was an error while sending the data over libzt, err=%d", wr); DEBUG_ERROR("error while sending the data over libzt, err=%d", wr);
} }
else { else {
//DEBUG_INFO("TXBUFFER -> LIBZT = %d bytes", wr); //DEBUG_INFO("TXBUFFER -> LIBZT = %d bytes", wr);
@@ -191,31 +178,27 @@ namespace ZeroTier {
} }
} }
conn->tx_m.unlock(); conn->tx_m.unlock();
} }
} }
} }
conn_m.unlock(); conn_m.unlock();
} }
} }
void ZTProxy::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) void ZTProxy::phyOnTcpData(PhySocket *sock, void **uptr, void *data, unsigned long len)
{ {
int wr = 0, zfd = -1, err = 0; int wr = 0, zfd = -1, err = 0;
DEBUG_INFO("sock=%p, len=%lu", sock, len); DEBUG_EXTRA("sock=%p, len=%lu", sock, len);
unsigned char *buf = (unsigned char*)data;
std::string host = _internal_addr; std::string host = _internal_addr;
// Get the TcpConnection object // Get the TcpConnection object
TcpConnection *conn = cmap[sock]; TcpConnection *conn = cmap[sock];
if(conn == NULL) { if (conn == NULL) {
DEBUG_ERROR("no connection object"); DEBUG_ERROR("invalid conn");
return; exit(0);
} }
if (conn->zfd < 0) { // no connection yet
if(conn->zfd < 0) { // no connection yet DEBUG_INFO("establishing proxy connection...");
DEBUG_INFO("no connection yet, proxying..."); if (host != "")
if(host != "")
{ {
uint16_t dest_port, ipv; uint16_t dest_port, ipv;
dest_port = _internal_port; dest_port = _internal_port;
@@ -223,7 +206,7 @@ namespace ZeroTier {
ipv = host.find(":") != std::string::npos ? 6 : 4; ipv = host.find(":") != std::string::npos ? 6 : 4;
if(ipv == 4) { if (ipv == 4) {
// Connect to proxied host via libzt // Connect to proxied host via libzt
DEBUG_INFO("attempting to proxy [0.0.0.0:%d -> %s:%d]", _proxy_listen_port, host.c_str(), dest_port); DEBUG_INFO("attempting to proxy [0.0.0.0:%d -> %s:%d]", _proxy_listen_port, host.c_str(), dest_port);
struct sockaddr_in in4; struct sockaddr_in in4;
@@ -234,7 +217,7 @@ namespace ZeroTier {
zfd = zts_socket(AF_INET, SOCK_STREAM, 0); zfd = zts_socket(AF_INET, SOCK_STREAM, 0);
err = zts_connect(zfd, (const struct sockaddr *)&in4, sizeof(in4)); err = zts_connect(zfd, (const struct sockaddr *)&in4, sizeof(in4));
} }
if(ipv == 6) { if (ipv == 6) {
// Connect to proxied host via libzt // Connect to proxied host via libzt
//DEBUG_INFO("attempting to proxy [0.0.0.0:%d -> %s:%d]", _proxy_listen_port, host.c_str(), dest_port); //DEBUG_INFO("attempting to proxy [0.0.0.0:%d -> %s:%d]", _proxy_listen_port, host.c_str(), dest_port);
struct sockaddr_in6 in6; struct sockaddr_in6 in6;
@@ -251,7 +234,6 @@ namespace ZeroTier {
// now release TX buffer contents we previously saved, since we can't connect // now release TX buffer contents we previously saved, since we can't connect
DEBUG_ERROR("error while connecting to remote host (zfd=%d, err=%d)", zfd, err); DEBUG_ERROR("error while connecting to remote host (zfd=%d, err=%d)", zfd, err);
conn->tx_m.lock(); conn->tx_m.lock();
DEBUG_INFO("resetting TX buffer");
conn->TXbuf->reset(); conn->TXbuf->reset();
conn->tx_m.unlock(); conn->tx_m.unlock();
return; return;
@@ -260,7 +242,6 @@ namespace ZeroTier {
DEBUG_INFO("successfully connected to remote host"); DEBUG_INFO("successfully connected to remote host");
} }
} }
conn_m.lock(); conn_m.lock();
// on success, add connection entry to map, set physock for later // on success, add connection entry to map, set physock for later
clist.push_back(conn); clist.push_back(conn);
@@ -276,43 +257,13 @@ namespace ZeroTier {
DEBUG_ERROR("there was an error while writing data from client to tx buffer, err=%d", wr); DEBUG_ERROR("there was an error while writing data from client to tx buffer, err=%d", wr);
} }
else { else {
DEBUG_INFO("CLIENT -> TXBUFFER = %d bytes", wr); // DEBUG_INFO("CLIENT -> TXBUFFER = %d bytes", wr);
} }
conn->tx_m.unlock(); conn->tx_m.unlock();
} }
void ZTProxy::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len) void ZTProxy::phyOnTcpAccept(PhySocket *sockL, PhySocket *sockN, void **uptrL, void **uptrN,
{ const struct sockaddr *from)
// Not used, connections are handled via user space network stack and VirtualTap
DEBUG_INFO("not used. exiting...");
exit(0);
}
void ZTProxy::phyOnTcpWritable(PhySocket *sock,void **uptr)
{
// Not used, connections are handled via user space network stack and VirtualTap
DEBUG_INFO();
//exit(0);
}
void ZTProxy::phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable)
{
// Not used, connections are handled via user space network stack and VirtualTap
DEBUG_INFO("sock=%p", sock);
//exit(0);
}
void ZTProxy::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success)
{
// Not used, connections are handled via user space network stack and VirtualTap
DEBUG_INFO("sock=%p", sock);
//exit(0);
}
void ZTProxy::phyOnUnixClose(PhySocket *sock,void **uptr)
{
// Not used, connections are handled via user space network stack and VirtualTap
DEBUG_INFO("sock=%p", sock);
//exit(0);
}
void ZTProxy::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from)
{ {
DEBUG_INFO("sockL=%p, sockN=%p", sockL, sockN); DEBUG_INFO("sockL=%p, sockN=%p", sockL, sockN);
TcpConnection *conn = new TcpConnection(); TcpConnection *conn = new TcpConnection();
@@ -320,32 +271,12 @@ namespace ZeroTier {
cmap[sockN]=conn; cmap[sockN]=conn;
} }
void ZTProxy::phyOnUnixData(PhySocket *sock,void **uptr,void *data,ssize_t len) void ZTProxy::phyOnTcpClose(PhySocket *sock, void **uptr)
{
DEBUG_INFO("sock=%p, len=%lu", sock, len);
unsigned char *buf = (unsigned char*)data;
TcpConnection *conn = cmap[sock];
if(conn == NULL) {
DEBUG_ERROR("no connection object");
return;
}
else // If connection to host already established, just forward the data in the correct direction
{
}
}
void ZTProxy::phyOnUnixWritable(PhySocket *sock,void **uptr,bool lwip_invoked)
{
DEBUG_INFO("sock=%p", sock);
exit(0);
}
void ZTProxy::phyOnTcpClose(PhySocket *sock,void **uptr)
{ {
DEBUG_INFO("sock=%p", sock); DEBUG_INFO("sock=%p", sock);
conn_m.lock(); conn_m.lock();
TcpConnection *conn = cmap[sock]; TcpConnection *conn = cmap[sock];
if(conn) { if (conn) {
conn->client_sock=NULL; conn->client_sock=NULL;
cmap.erase(sock); cmap.erase(sock);
for (int i=0; i<clist.size(); i++) { for (int i=0; i<clist.size(); i++) {
@@ -361,11 +292,34 @@ namespace ZeroTier {
close(_phy.getDescriptor(sock)); close(_phy.getDescriptor(sock));
conn_m.unlock(); conn_m.unlock();
} }
void ZTProxy::phyOnDatagram(PhySocket *sock, void **uptr, const struct sockaddr *localAddr,
const struct sockaddr *from, void *data, unsigned long len) {
DEBUG_INFO();
}
void ZTProxy::phyOnTcpWritable(PhySocket *sock, void **uptr) {
DEBUG_INFO();
}
void ZTProxy::phyOnFileDescriptorActivity(PhySocket *sock, void **uptr, bool readable, bool writable) {
DEBUG_INFO("sock=%p", sock);
}
void ZTProxy::phyOnTcpConnect(PhySocket *sock, void **uptr, bool success) {
DEBUG_INFO("sock=%p", sock);
}
void ZTProxy::phyOnUnixClose(PhySocket *sock, void **uptr) {
DEBUG_INFO("sock=%p", sock);
}
void ZTProxy::phyOnUnixData(PhySocket *sock,void **uptr,void *data,ssize_t len) {
DEBUG_INFO("sock=%p, len=%lu", sock, len);
}
void ZTProxy::phyOnUnixWritable(PhySocket *sock, void **uptr, bool lwip_invoked) {
DEBUG_INFO("sock=%p", sock);
}
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if(argc != 6) { if (argc != 6) {
printf("\nZeroTier TCP Proxy Service\n"); printf("\nZeroTier TCP Proxy Service\n");
printf("ztproxy [config_file_path] [local_listen_port] [nwid] [zt_host_addr] [zt_resource_port]\n"); printf("ztproxy [config_file_path] [local_listen_port] [nwid] [zt_host_addr] [zt_resource_port]\n");
exit(0); exit(0);
@@ -378,7 +332,7 @@ int main(int argc, char **argv)
ZeroTier::ZTProxy *proxy = new ZeroTier::ZTProxy(proxy_listen_port, nwid, path, internal_addr, internal_port); ZeroTier::ZTProxy *proxy = new ZeroTier::ZTProxy(proxy_listen_port, nwid, path, internal_addr, internal_port);
if(proxy) { if (proxy) {
printf("\nZTProxy started. Listening on %d\n", proxy_listen_port); printf("\nZTProxy started. Listening on %d\n", proxy_listen_port);
printf("Traffic will be proxied to and from %s:%d on network %s\n", internal_addr.c_str(), internal_port, nwid.c_str()); printf("Traffic will be proxied to and from %s:%d on network %s\n", internal_addr.c_str(), internal_port, nwid.c_str());
printf("Proxy Node config files and key stored in: %s/\n\n", path.c_str()); printf("Proxy Node config files and key stored in: %s/\n\n", path.c_str());

View File

@@ -56,16 +56,15 @@ namespace ZeroTier {
Mutex tx_m, rx_m; Mutex tx_m, rx_m;
TcpConnection() { TcpConnection() {
printf("TcpConnection()\n");
zfd = -1; zfd = -1;
TXbuf = new RingBuffer<unsigned char>(BUF_SZ); TXbuf = new RingBuffer<unsigned char>(BUF_SZ);
RXbuf = new RingBuffer<unsigned char>(BUF_SZ); RXbuf = new RingBuffer<unsigned char>(BUF_SZ);
} }
~TcpConnection() { ~TcpConnection() {
printf("~TcpConnection()\n");
delete TXbuf; delete TXbuf;
delete RXbuf; delete RXbuf;
client_sock = NULL;
TXbuf = NULL; TXbuf = NULL;
RXbuf = NULL; RXbuf = NULL;
} }
@@ -106,7 +105,7 @@ namespace ZeroTier {
Mutex conn_m; Mutex conn_m;
fd_set read_set, write_set; fd_set read_set, write_set;
int nfds = 0; int nfds;
int _proxy_listen_port; int _proxy_listen_port;
int _internal_port; int _internal_port;