Commit ee26cbf2 authored by Joel Martin's avatar Joel Martin

Argument processing fixes. Misc proxy fixes.

parent 6ee61a4c
wsproxy: wsproxy.o websocket.o wsproxy: wsproxy.o websocket.o
$(CC) $^ -l ssl -l resolv -o $@ $(CC) $^ -l ssl -l resolv -o $@
#websocket.o: websocket.c websocket.o wsproxy.o: websocket.h
# $(CC) -c $^ -o $@
#
#wsproxy.o: wsproxy.c
# $(CC) -c $^ -o $@
clean: clean:
rm -f wsproxy wsproxy.o websocket.o rm -f wsproxy wsproxy.o websocket.o
......
...@@ -296,12 +296,13 @@ ws_ctx_t *do_handshake(int sock) { ...@@ -296,12 +296,13 @@ ws_ctx_t *do_handshake(int sock) {
return NULL; return NULL;
} else if (bcmp(handshake, "\x16", 1) == 0) { } else if (bcmp(handshake, "\x16", 1) == 0) {
// SSL // SSL
if (! settings.cert) { return NULL; }
ws_ctx = ws_socket_ssl(sock, settings.cert); ws_ctx = ws_socket_ssl(sock, settings.cert);
if (! ws_ctx) { return NULL; } if (! ws_ctx) { return NULL; }
scheme = "wss"; scheme = "wss";
printf(" using SSL socket\n"); printf(" using SSL socket\n");
} else if (settings.ssl_only) { } else if (settings.ssl_only) {
printf("Non-SSL connection disallowed"); printf("Non-SSL connection disallowed\n");
close(sock); close(sock);
return NULL; return NULL;
} else { } else {
...@@ -401,10 +402,6 @@ void start_server() { ...@@ -401,10 +402,6 @@ void start_server() {
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
ws_ctx_t *ws_ctx; ws_ctx_t *ws_ctx;
if (settings.daemon) {
daemonize();
}
/* Initialize buffers */ /* Initialize buffers */
bufsize = 65536; bufsize = 65536;
if (! (tbuf = malloc(bufsize)) ) if (! (tbuf = malloc(bufsize)) )
...@@ -416,6 +413,10 @@ void start_server() { ...@@ -416,6 +413,10 @@ void start_server() {
if (! (cbuf_tmp = malloc(bufsize)) ) if (! (cbuf_tmp = malloc(bufsize)) )
{ fatal("malloc()"); } { fatal("malloc()"); }
if (settings.daemon) {
daemonize();
}
lsock = socket(AF_INET, SOCK_STREAM, 0); lsock = socket(AF_INET, SOCK_STREAM, 0);
if (lsock < 0) { error("ERROR creating listener socket"); } if (lsock < 0) { error("ERROR creating listener socket"); }
bzero((char *) &serv_addr, sizeof(serv_addr)); bzero((char *) &serv_addr, sizeof(serv_addr));
......
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <unistd.h>
typedef struct { typedef struct {
int sockfd; int sockfd;
...@@ -13,8 +12,8 @@ typedef struct { ...@@ -13,8 +12,8 @@ typedef struct {
void (*handler)(ws_ctx_t*); void (*handler)(ws_ctx_t*);
int ssl_only; int ssl_only;
int daemon; int daemon;
char record[1024]; char *record;
char cert[1024]; char *cert;
} settings_t; } settings_t;
typedef struct { typedef struct {
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <getopt.h> #include <getopt.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
...@@ -198,8 +199,8 @@ void proxy_handler(ws_ctx_t *ws_ctx) { ...@@ -198,8 +199,8 @@ void proxy_handler(ws_ctx_t *ws_ctx) {
int tsock = 0; int tsock = 0;
struct sockaddr_in taddr; struct sockaddr_in taddr;
if (settings.record) { if (settings.record && settings.record[0] != '\0') {
recordfd = open(settings.record, O_WRONLY | O_CREAT | O_TRUNC, recordfd = open(settings.record, O_WRONLY | O_CREAT | O_APPEND,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
} }
...@@ -238,7 +239,7 @@ void proxy_handler(ws_ctx_t *ws_ctx) { ...@@ -238,7 +239,7 @@ void proxy_handler(ws_ctx_t *ws_ctx) {
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int listen_port, c, option_index = 0; int listen_port, fd, c, option_index = 0;
static int ssl_only = 0, foreground = 0; static int ssl_only = 0, foreground = 0;
char *found; char *found;
static struct option long_options[] = { static struct option long_options[] = {
...@@ -250,8 +251,8 @@ int main(int argc, char *argv[]) ...@@ -250,8 +251,8 @@ int main(int argc, char *argv[])
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
settings.record[0] = '\0'; settings.record = NULL;
strcpy(settings.cert, "self.pem"); settings.cert = realpath("self.pem", NULL);
while (1) { while (1) {
c = getopt_long (argc, argv, "fr:c:", c = getopt_long (argc, argv, "fr:c:",
...@@ -269,10 +270,18 @@ int main(int argc, char *argv[]) ...@@ -269,10 +270,18 @@ int main(int argc, char *argv[])
foreground = 1; foreground = 1;
break; break;
case 'r': case 'r':
memcpy(settings.record, optarg, sizeof(settings.record)); if ((fd = open(optarg, O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < -1) {
fatal("Could not access %s\n", optarg);
}
close(fd);
settings.record = realpath(optarg, NULL);
break; break;
case 'c': case 'c':
memcpy(settings.cert, optarg, sizeof(settings.cert)); settings.cert = realpath(optarg, NULL);
if (! settings.cert) {
fatal("No cert file at %s\n", optarg);
}
break; break;
default: default:
usage(); usage();
...@@ -314,17 +323,6 @@ int main(int argc, char *argv[]) ...@@ -314,17 +323,6 @@ int main(int argc, char *argv[])
usage(); usage();
} }
/* Initialize buffers */
bufsize = 65536;
if (! (tbuf = malloc(bufsize)) )
{ fatal("malloc()"); }
if (! (cbuf = malloc(bufsize)) )
{ fatal("malloc()"); }
if (! (tbuf_tmp = malloc(bufsize)) )
{ fatal("malloc()"); }
if (! (cbuf_tmp = malloc(bufsize)) )
{ fatal("malloc()"); }
settings.handler = proxy_handler; settings.handler = proxy_handler;
start_server(); start_server();
......
...@@ -101,7 +101,7 @@ def proxy_handler(client): ...@@ -101,7 +101,7 @@ def proxy_handler(client):
if settings['record']: if settings['record']:
print "Opening record file: %s" % settings['record'] print "Opening record file: %s" % settings['record']
rec = open(settings['record'], 'w') rec = open(settings['record'], 'a')
print "Connecting to: %s:%s" % (target_host, target_port) print "Connecting to: %s:%s" % (target_host, target_port)
tsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
...@@ -152,5 +152,6 @@ if __name__ == '__main__': ...@@ -152,5 +152,6 @@ if __name__ == '__main__':
settings['cert'] = os.path.abspath(options.cert) settings['cert'] = os.path.abspath(options.cert)
settings['ssl_only'] = options.ssl_only settings['ssl_only'] = options.ssl_only
settings['daemon'] = options.daemon settings['daemon'] = options.daemon
settings['record'] = os.path.abspath(options.record) if options.record:
settings['record'] = os.path.abspath(options.record)
start_server() start_server()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment