proxychains-ng/src/daemon/udpserver.c

66 lines
1.7 KiB
C
Raw Normal View History

experimental new feature: proxy_dns_daemon since many users complain about issues with modern, ultracomplex clusterfuck software such as chromium, nodejs, etc, i've reconsidered one of my original ideas how to implement remote dns lookup support. instead of having a background thread serving requests via a pipe, the user manually starts a background daemon process before running proxychains, and the two processes then communicate via UDP. this requires much less hacks (like hooking of close() to prevent pipes from getting closed) and doesn't need to call any async-signal unsafe code like malloc(). this means it should be much more compatible than the previous method, however it's not as practical and slightly slower. it's recommended that the proxychains4-daemon runs on localhost, and if you use proxychains-ng a lot you might want to set ip up as a service that starts on boot. a single proxychains4-daemon should theoretically be able to serve many parallel proxychains4 instances, but this has not yet been tested so far. it's also possible to run the daemon on other computers, even over internet, but currently there is no error-checking/ timeout code at all; that means the UDP connection needs to be very stable. the library code used for the daemon sources are from my projects libulz[0] and htab[1], and the server code is loosely based on microsocks[2]. their licenses are all compatible with the GPL. if not otherwise mentioned, they're released for this purpose under the standard proxychains-ng license (see COPYING). [0]: https://github.com/rofl0r/libulz [1]: https://github.com/rofl0r/htab [2]: https://github.com/rofl0r/microsocks
2020-09-23 21:00:29 +00:00
#include "udpserver.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int resolve(const char *host, unsigned short port, struct addrinfo** addr) {
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_flags = AI_PASSIVE,
};
char port_buf[8];
snprintf(port_buf, sizeof port_buf, "%u", port);
return getaddrinfo(host, port_buf, &hints, addr);
}
int resolve_sa(const char *host, unsigned short port, union sockaddr_union *res) {
struct addrinfo *ainfo = 0;
int ret;
SOCKADDR_UNION_AF(res) = AF_UNSPEC;
if((ret = resolve(host, port, &ainfo))) return ret;
memcpy(res, ainfo->ai_addr, ainfo->ai_addrlen);
freeaddrinfo(ainfo);
return 0;
}
int bindtoip(int fd, union sockaddr_union *bindaddr) {
socklen_t sz = SOCKADDR_UNION_LENGTH(bindaddr);
if(sz)
return bind(fd, (struct sockaddr*) bindaddr, sz);
return 0;
}
int server_waitclient(struct server *server, struct client* client, void* buf, size_t *buflen) {
socklen_t clen = sizeof client->addr;
ssize_t ret = recvfrom(server->fd, buf, *buflen, 0, (void*)&client->addr, &clen);
if(ret >= 0) {
*buflen = ret;
return 0;
}
return ret;
}
int server_setup(struct server *server, const char* listenip, unsigned short port) {
struct addrinfo *ainfo = 0;
if(resolve(listenip, port, &ainfo)) return -1;
struct addrinfo* p;
int listenfd = -1;
for(p = ainfo; p; p = p->ai_next) {
if((listenfd = socket(p->ai_family, SOCK_DGRAM, 0)) < 0)
continue;
int yes = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if(bind(listenfd, p->ai_addr, p->ai_addrlen) < 0) {
close(listenfd);
listenfd = -1;
continue;
}
break;
}
freeaddrinfo(ainfo);
if(listenfd < 0) return -2;
server->fd = listenfd;
return 0;
}