tlsgate

TLS reverse proxy
git clone git://git.akobets.xyz/tlsgate
Log | Files | Refs | README | LICENSE

commit 718f03264c9d34885caaf5bc09df358d1f818387
parent cdaa9c71fa4af2608b9b56299c7e76d8945ae636
Author: Artem Kobets <artem@akobets.xyz>
Date:   Sat, 19 Sep 2020 22:32:45 +0300

add logging

Diffstat:
Mmain.c | 30++++++++++++++++++++++++------
Msock.c | 24+++++++++++++++++++++++-
Msock.h | 3+++
3 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/main.c b/main.c @@ -5,9 +5,10 @@ #include <stdlib.h> #include <string.h> #include <sys/wait.h> +#include <time.h> #include <unistd.h> -#include <sys/socket.h> +#include <netinet/in.h> #include <tls.h> #include "sock.h" @@ -16,7 +17,7 @@ // milliseconds #define REQUEST_TIMEOUT 30000 -static void serve(struct tls *ctx, int cfd); +static void serve(struct tls *ctx, int cfd, struct sockaddr_storage *addr); static void cleanup(void); static void sigchld(int unused); static void sigcleanup(int sig); @@ -36,7 +37,7 @@ static char *server_port = NULL; static char *server_udsfile = NULL; void -serve(struct tls *ctx, int cfd) +serve(struct tls *ctx, int cfd, struct sockaddr_storage *addr) { struct tls *cctx = NULL; int sfd = -1; @@ -46,6 +47,20 @@ serve(struct tls *ctx, int cfd) char buf[BUFSIZ], *bufp; ssize_t nread, nwritten; + time_t t; + char addr_str[INET6_ADDRSTRLEN /* > INET_ADDRSTRLEN */]; + char tstmp[21]; + + // log + t = time(NULL); + if (strftime(tstmp, sizeof(tstmp), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t)) == 0) { + warnx("strftime: Exceeded buffer capacity"); + goto cleanup; + } + if (sock_get_addr_str(addr, addr_str, sizeof(addr_str))) + goto cleanup; + printf("%s %s\n", tstmp, addr_str); + // connect to server sfd = server_udsfile ? sock_server_uds(server_udsfile) @@ -201,7 +216,7 @@ sigchld(int unused) } static void -cleanup() +cleanup(void) { if (proxy_udsfile != NULL) sock_remove_uds(proxy_udsfile); @@ -254,6 +269,8 @@ main(int argc, char **argv) struct tls_config *config; int fd, cfd; struct sigaction act; + struct sockaddr_storage addr; + socklen_t addr_len; argv0 = argv[0]; @@ -359,8 +376,9 @@ main(int argc, char **argv) act.sa_flags = 0; sigaction(SIGCHLD, &act, NULL); + addr_len = sizeof(addr); while (1) { - if ((cfd = accept(fd, NULL, NULL)) == -1) { + if ((cfd = accept(fd, (struct sockaddr *)&addr, &addr_len)) == -1) { // can be interrupted with SIGCHLD if (errno != EINTR) warn("accept"); @@ -369,7 +387,7 @@ main(int argc, char **argv) switch (fork()) { case 0: - serve(ctx, cfd); + serve(ctx, cfd, &addr); exit(0); case -1: warn("fork"); diff --git a/sock.c b/sock.c @@ -4,7 +4,6 @@ #include <sys/time.h> #include <unistd.h> -#include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <netdb.h> @@ -166,3 +165,26 @@ sock_set_nonblock(int fd) return 0; } + +int +sock_get_addr_str(const struct sockaddr_storage *addr, char *str, size_t len) { + switch (addr->ss_family) { + case AF_INET: + if (!inet_ntop(AF_INET, &(((struct sockaddr_in *)addr)->sin_addr), str, len)) { + warn("inet_ntop:"); + return -1; + } + break; + case AF_INET6: + if (!inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)addr)->sin6_addr), str, len)) { + warn("inet_ntop:"); + return -1; + } + break; + default: + snprintf(str, len, "uds"); + break; + } + + return 0; +} diff --git a/sock.h b/sock.h @@ -1,6 +1,9 @@ +#include <sys/socket.h> + int sock_proxy_ips(const char *host, const char *port); int sock_proxy_uds(const char *file); void sock_remove_uds(const char *file); int sock_server_ips(const char *host, const char *port); int sock_server_uds(const char *file); int sock_set_nonblock(int fd); +int sock_get_addr_str(const struct sockaddr_storage *addr, char *str, size_t len);