Commit f2898eab authored by Joel Martin's avatar Joel Martin

Add listen address to proxy (C and python).

This allows forwarding from an external port to the same port on
localhost (loopback). I.e.

./utils/wsproxy `hostname -f`:5901 localhost:5901
parent f755ca96
...@@ -60,9 +60,9 @@ Usage ...@@ -60,9 +60,9 @@ Usage
* run the python proxy: * run the python proxy:
`./utils/wsproxy.py [listen_port] [vnc_host] [vnc_port]` `./utils/wsproxy.py source_port target_addr:target_port
`./utils/wsproxy.py 8787 localhost 5901` `./utils/wsproxy.py 8787 localhost:5901`
* run the mini python web server to serve the directory: * run the mini python web server to serve the directory:
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/ssl.h> #include <openssl/ssl.h>
...@@ -326,9 +327,11 @@ ws_ctx_t *do_handshake(int sock) { ...@@ -326,9 +327,11 @@ ws_ctx_t *do_handshake(int sock) {
} }
void start_server(int listen_port, void start_server(int listen_port,
void (*handler)(ws_ctx_t*)) { void (*handler)(ws_ctx_t*),
int lsock, csock, clilen, sopt = 1; char *listen_host) {
int lsock, csock, clilen, sopt = 1, i;
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
struct hostent *lhost;
ws_ctx_t *ws_ctx; ws_ctx_t *ws_ctx;
/* Initialize buffers */ /* Initialize buffers */
...@@ -346,17 +349,37 @@ void start_server(int listen_port, ...@@ -346,17 +349,37 @@ void start_server(int listen_port,
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));
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(listen_port); serv_addr.sin_port = htons(listen_port);
/* Resolve listen address */
if ((listen_host == NULL) || (listen_host[0] == '\0')) {
serv_addr.sin_addr.s_addr = INADDR_ANY;
} else {
lhost = gethostbyname(listen_host);
if (lhost == NULL) {
fatal("Could not resolve self address");
}
bcopy((char *) lhost->h_addr,
(char *) &serv_addr.sin_addr.s_addr,
lhost->h_length);
for (i=0; i < strlen(lhost->h_addr); i++) {
printf("%d: %d\n", i, lhost->h_addr[i]);
}
}
setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, (char *)&sopt, sizeof(sopt)); setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, (char *)&sopt, sizeof(sopt));
if (bind(lsock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { if (bind(lsock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
error("ERROR on binding listener socket"); fatal("ERROR on binding listener socket");
} }
listen(lsock,100); listen(lsock,100);
while (1) { while (1) {
clilen = sizeof(cli_addr); clilen = sizeof(cli_addr);
if (listen_host) {
printf("waiting for connection on %s:%d\n", listen_host, listen_port);
} else {
printf("waiting for connection on port %d\n", listen_port); printf("waiting for connection on port %d\n", listen_port);
}
csock = accept(lsock, csock = accept(lsock,
(struct sockaddr *) &cli_addr, (struct sockaddr *) &cli_addr,
&clilen); &clilen);
......
...@@ -101,10 +101,10 @@ def do_handshake(sock): ...@@ -101,10 +101,10 @@ def do_handshake(sock):
retsock.send(server_handshake % (origin, scheme, host, path)) retsock.send(server_handshake % (origin, scheme, host, path))
return retsock return retsock
def start_server(listen_port, handler): def start_server(listen_port, handler, listen_host=''):
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
lsock.bind(('', listen_port)) lsock.bind((listen_host, listen_port))
lsock.listen(100) lsock.listen(100)
while True: while True:
try: try:
......
...@@ -28,7 +28,7 @@ Traffic Legend:\n\ ...@@ -28,7 +28,7 @@ Traffic Legend:\n\
"; ";
void usage() { void usage() {
fprintf(stderr,"Usage: <listen_port> <target_host> <target_port>\n"); fprintf(stderr,"Usage: [--record FILE] [source_addr:]source_port target_addr:target_port\n");
exit(1); exit(1);
} }
...@@ -239,19 +239,43 @@ void proxy_handler(ws_ctx_t *ws_ctx) { ...@@ -239,19 +239,43 @@ void proxy_handler(ws_ctx_t *ws_ctx) {
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int listen_port, idx=1; int listen_port, idx = 1;
char *listen_host;
if (strcmp(argv[idx], "--record") == 0) { if (argc < 2) {
usage();
}
if (strncmp(argv[idx], "--record", 8) == 0) {
idx++; idx++;
record_filename = argv[idx++]; record_filename = argv[idx++];
} }
if ((argc-idx) != 3) { usage(); } if ((argc-idx) != 2) {
listen_port = strtol(argv[idx++], NULL, 10); usage();
if (errno != 0) { usage(); } }
target_host = argv[idx++];
target_port = strtol(argv[idx++], NULL, 10); if (strstr(argv[idx], ":")) {
if (errno != 0) { usage(); } listen_host = strtok(argv[idx], ":");
listen_port = strtol(strtok(NULL, ":"), NULL, 10);
} else {
listen_host = NULL;
listen_port = strtol(argv[idx], NULL, 10);
}
idx++;
if ((errno != 0) || (listen_port == 0)) {
usage();
}
if (strstr(argv[idx], ":")) {
target_host = strtok(argv[idx], ":");
target_port = strtol(strtok(NULL, ":"), NULL, 10);
} else {
usage();
}
if ((errno != 0) || (target_port == 0)) {
usage();
}
/* Initialize buffers */ /* Initialize buffers */
bufsize = 65536; bufsize = 65536;
...@@ -264,7 +288,7 @@ int main(int argc, char *argv[]) ...@@ -264,7 +288,7 @@ int main(int argc, char *argv[])
if (! (cbuf_tmp = malloc(bufsize)) ) if (! (cbuf_tmp = malloc(bufsize)) )
{ fatal("malloc()"); } { fatal("malloc()"); }
start_server(listen_port, &proxy_handler); start_server(listen_port, &proxy_handler, listen_host);
free(tbuf); free(tbuf);
free(cbuf); free(cbuf);
......
...@@ -117,18 +117,27 @@ def proxy_handler(client): ...@@ -117,18 +117,27 @@ def proxy_handler(client):
raise raise
if __name__ == '__main__': if __name__ == '__main__':
parser = optparse.OptionParser() usage = "%prog [--record FILE]"
usage += " [source_addr:]source_port target_addr:target_port"
parser = optparse.OptionParser(usage=usage)
parser.add_option("--record", dest="record", parser.add_option("--record", dest="record",
help="record session to a file", metavar="FILE") help="record session to a file", metavar="FILE")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if len(args) > 3: parser.error("Too many arguments") if len(args) > 2: parser.error("Too many arguments")
if len(args) < 3: parser.error("Too few arguments") if len(args) < 2: parser.error("Too few arguments")
try: listen_port = int(args[0]) if args[0].count(':') > 0:
listen_host,listen_port = args[0].split(':')
else:
listen_host = ''
listen_port = args[0]
if args[1].count(':') > 0:
target_host,target_port = args[1].split(':')
else:
parser.error("Error parsing target")
try: listen_port = int(listen_port)
except: parser.error("Error parsing listen port") except: parser.error("Error parsing listen port")
try: target_host = args[1] try: target_port = int(target_port)
except: parser.error("Error parsing target host")
try: target_port = int(args[2])
except: parser.error("Error parsing target port") except: parser.error("Error parsing target port")
start_server(listen_port, proxy_handler) start_server(listen_port, proxy_handler, listen_host=listen_host)
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