2010-06-04 23:10:06 +01:00
|
|
|
/*
|
|
|
|
* A WebSocket to TCP socket proxy with support for "wss://" encryption.
|
2010-07-17 18:05:58 +01:00
|
|
|
* Copyright 2010 Joel Martin
|
|
|
|
* Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
|
2010-06-04 23:10:06 +01:00
|
|
|
*
|
|
|
|
* You can make a cert/key with openssl using:
|
|
|
|
* openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem
|
|
|
|
* as taken from http://docs.python.org/dev/library/ssl.html#certificates
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2010-06-17 23:05:33 +01:00
|
|
|
#include <limits.h>
|
2010-06-16 19:58:00 +01:00
|
|
|
#include <getopt.h>
|
2010-06-04 23:10:06 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "websocket.h"
|
|
|
|
|
|
|
|
char traffic_legend[] = "\n\
|
|
|
|
Traffic Legend:\n\
|
|
|
|
} - Client receive\n\
|
|
|
|
}. - Client receive partial\n\
|
|
|
|
{ - Target receive\n\
|
|
|
|
\n\
|
|
|
|
> - Target send\n\
|
|
|
|
>. - Target send partial\n\
|
|
|
|
< - Client send\n\
|
|
|
|
<. - Client send partial\n\
|
|
|
|
";
|
|
|
|
|
2010-06-17 23:50:15 +01:00
|
|
|
char USAGE[] = "Usage: [options] " \
|
|
|
|
"[source_addr:]source_port target_addr:target_port\n\n" \
|
2010-11-06 15:55:09 +00:00
|
|
|
" --verbose|-v verbose messages and per frame traffic\n" \
|
wsproxy, wstelnet: wrap command, WS telnet client.
wswrapper:
Getting the wswrapper.c LD_PRELOAD model working has turned out to
involve too many dark corners of the glibc/POSIX file descriptor
space. I realized that 95% of what I want can be accomplished by
adding a "wrap command" mode to wsproxy.
The code is still there for now, but consider it experimental at
best. Minor fix to dup2 and add dup and dup3 logging.
wsproxy Wrap Command:
In wsproxy wrap command mode, a command line is specified instead
of a target address and port. wsproxy then uses a much simpler
LD_PRELOAD library, rebind.so, to move intercept any bind() system
calls made by the program. If the bind() call is for the wsproxy
listen port number then the real bind() system call is issued for
an alternate (free high) port on loopback/localhost. wsproxy then
forwards from the listen address/port to the moved port.
The --wrap-mode argument takes three options that determine the
behavior of wsproxy when the wrapped command returns an exit code
(exit or daemonizing): ignore, exit, respawn.
For example, this runs vncserver on turns port 5901 into
a WebSockets port (rebind.so must be built first):
./utils/wsproxy.py --wrap-mode=ignore 5901 -- vncserver :1
The vncserver command backgrounds itself so the wrap mode is set
to "ignore" so that wsproxy keeps running even after it receives
an exit code from vncserver.
wstelnet:
To demonstrate the wrap command mode, I added WebSockets telnet
client.
For example, this runs telnetd (krb5-telnetd) on turns port 2023
into a WebSockets port (using "respawn" mode since telnetd exits
after each connection closes):
sudo ./utils/wsproxy.py --wrap-mode=respawn 2023 -- telnetd -debug 2023
Then the utils/wstelnet.html page can be used to connect to the
telnetd server on port 2023. The telnet client includes VT100.js
(from http://code.google.com/p/sshconsole) which handles the
terminal emulation and rendering.
rebind:
The rebind LD_PRELOAD library is used by wsproxy in wrap command
mode to intercept bind() system calls and move the port to
a different port on loopback/localhost. The rebind.so library can
be built by running make in the utils directory.
The rebind library can be used separately from wsproxy by setting
the REBIND_OLD_PORT and REBIND_NEW_PORT environment variables
prior to executing a command. For example:
export export REBIND_PORT_OLD="23"
export export REBIND_PORT_NEW="65023"
LD_PRELOAD=./rebind.so telnetd -debug 23
Alternately, the rebind script does the same thing:
rebind 23 65023 telnetd -debug 23
Other changes/notes:
- wsproxy no longer daemonizes by default. Remove -f/--foreground
option and add -D/--deamon option.
- When wsproxy is used to wrap a command in "respawn" mode, the
command will not be respawn more often than 3 times within 10
seconds.
- Move getKeysym routine out of Canvas object so that it can be called
directly.
2011-01-12 19:15:11 +00:00
|
|
|
" --daemon|-D become a daemon (background process)\n" \
|
2010-11-06 15:55:09 +00:00
|
|
|
" --cert CERT SSL certificate file\n" \
|
|
|
|
" --key KEY SSL key file (if separate from cert)\n" \
|
|
|
|
" --ssl-only disallow non-encrypted connections";
|
2010-06-16 19:58:00 +01:00
|
|
|
|
2010-06-17 23:24:54 +01:00
|
|
|
#define usage(fmt, args...) \
|
|
|
|
fprintf(stderr, "%s\n\n", USAGE); \
|
|
|
|
fprintf(stderr, fmt , ## args); \
|
2010-06-04 23:10:06 +01:00
|
|
|
exit(1);
|
|
|
|
|
2010-06-17 22:06:18 +01:00
|
|
|
char target_host[256];
|
2010-06-04 23:10:06 +01:00
|
|
|
int target_port;
|
2010-06-07 18:44:02 +01:00
|
|
|
|
2010-09-10 19:05:48 +01:00
|
|
|
extern pipe_error;
|
2010-06-17 22:06:18 +01:00
|
|
|
extern settings_t settings;
|
2010-06-07 18:44:02 +01:00
|
|
|
extern char *tbuf, *cbuf, *tbuf_tmp, *cbuf_tmp;
|
|
|
|
extern unsigned int bufsize, dbufsize;
|
2010-06-04 23:10:06 +01:00
|
|
|
|
|
|
|
void do_proxy(ws_ctx_t *ws_ctx, int target) {
|
|
|
|
fd_set rlist, wlist, elist;
|
|
|
|
struct timeval tv;
|
2010-06-07 18:44:02 +01:00
|
|
|
int i, maxfd, client = ws_ctx->sockfd;
|
2010-06-04 23:10:06 +01:00
|
|
|
unsigned int tstart, tend, cstart, cend, ret;
|
|
|
|
ssize_t len, bytes;
|
|
|
|
|
|
|
|
tstart = tend = cstart = cend = 0;
|
|
|
|
maxfd = client > target ? client+1 : target+1;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
FD_ZERO(&rlist);
|
|
|
|
FD_ZERO(&wlist);
|
|
|
|
FD_ZERO(&elist);
|
|
|
|
|
|
|
|
FD_SET(client, &elist);
|
|
|
|
FD_SET(target, &elist);
|
|
|
|
|
|
|
|
if (tend == tstart) {
|
|
|
|
// Nothing queued for target, so read from client
|
|
|
|
FD_SET(client, &rlist);
|
|
|
|
} else {
|
|
|
|
// Data queued for target, so write to it
|
|
|
|
FD_SET(target, &wlist);
|
|
|
|
}
|
|
|
|
if (cend == cstart) {
|
|
|
|
// Nothing queued for client, so read from target
|
|
|
|
FD_SET(target, &rlist);
|
|
|
|
} else {
|
|
|
|
// Data queued for client, so write to it
|
|
|
|
FD_SET(client, &wlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = select(maxfd, &rlist, &wlist, &elist, &tv);
|
2010-09-10 19:05:48 +01:00
|
|
|
if (pipe_error) { break; }
|
2010-06-04 23:10:06 +01:00
|
|
|
|
|
|
|
if (FD_ISSET(target, &elist)) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("target exception\n");
|
2010-06-04 23:10:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (FD_ISSET(client, &elist)) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("client exception\n");
|
2010-06-04 23:10:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == -1) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("select(): %s\n", strerror(errno));
|
2010-06-04 23:10:06 +01:00
|
|
|
break;
|
|
|
|
} else if (ret == 0) {
|
2010-09-10 19:05:48 +01:00
|
|
|
//handler_emsg("select timeout\n");
|
2010-06-04 23:10:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(target, &wlist)) {
|
|
|
|
len = tend-tstart;
|
|
|
|
bytes = send(target, tbuf + tstart, len, 0);
|
2010-09-10 19:05:48 +01:00
|
|
|
if (pipe_error) { break; }
|
2010-06-04 23:10:06 +01:00
|
|
|
if (bytes < 0) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("target connection error: %s\n",
|
|
|
|
strerror(errno));
|
2010-06-04 23:10:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
tstart += bytes;
|
|
|
|
if (tstart >= tend) {
|
|
|
|
tstart = tend = 0;
|
|
|
|
traffic(">");
|
|
|
|
} else {
|
|
|
|
traffic(">.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(client, &wlist)) {
|
|
|
|
len = cend-cstart;
|
|
|
|
bytes = ws_send(ws_ctx, cbuf + cstart, len);
|
2010-09-10 19:05:48 +01:00
|
|
|
if (pipe_error) { break; }
|
2010-06-04 23:10:06 +01:00
|
|
|
if (len < 3) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("len: %d, bytes: %d: %d\n", len, bytes, *(cbuf + cstart));
|
2010-06-04 23:10:06 +01:00
|
|
|
}
|
|
|
|
cstart += bytes;
|
|
|
|
if (cstart >= cend) {
|
|
|
|
cstart = cend = 0;
|
|
|
|
traffic("<");
|
|
|
|
} else {
|
|
|
|
traffic("<.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(target, &rlist)) {
|
|
|
|
bytes = recv(target, cbuf_tmp, dbufsize , 0);
|
2010-09-10 19:05:48 +01:00
|
|
|
if (pipe_error) { break; }
|
2010-06-04 23:10:06 +01:00
|
|
|
if (bytes <= 0) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("target closed connection\n");
|
2010-06-04 23:10:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
cstart = 0;
|
2010-06-07 18:44:02 +01:00
|
|
|
cend = encode(cbuf_tmp, bytes, cbuf, bufsize);
|
|
|
|
/*
|
|
|
|
printf("encoded: ");
|
2010-06-07 19:47:44 +01:00
|
|
|
for (i=0; i< cend; i++) {
|
|
|
|
printf("%u,", (unsigned char) *(cbuf+i));
|
2010-06-07 18:44:02 +01:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
*/
|
|
|
|
if (cend < 0) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("encoding error\n");
|
2010-06-04 23:10:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
traffic("{");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(client, &rlist)) {
|
|
|
|
bytes = ws_recv(ws_ctx, tbuf_tmp, bufsize-1);
|
2010-09-10 19:05:48 +01:00
|
|
|
if (pipe_error) { break; }
|
2010-06-04 23:10:06 +01:00
|
|
|
if (bytes <= 0) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("client closed connection\n");
|
2010-06-04 23:10:06 +01:00
|
|
|
break;
|
2010-08-03 19:21:00 +01:00
|
|
|
} else if ((bytes == 2) &&
|
|
|
|
(tbuf_tmp[0] == '\xff') &&
|
|
|
|
(tbuf_tmp[1] == '\x00')) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("client sent orderly close frame\n");
|
2010-08-03 19:21:00 +01:00
|
|
|
break;
|
2010-06-04 23:10:06 +01:00
|
|
|
}
|
2010-06-07 19:47:44 +01:00
|
|
|
/*
|
|
|
|
printf("before decode: ");
|
|
|
|
for (i=0; i< bytes; i++) {
|
|
|
|
printf("%u,", (unsigned char) *(tbuf_tmp+i));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
*/
|
2010-06-07 18:44:02 +01:00
|
|
|
len = decode(tbuf_tmp, bytes, tbuf, bufsize-1);
|
|
|
|
/*
|
|
|
|
printf("decoded: ");
|
2010-06-07 19:47:44 +01:00
|
|
|
for (i=0; i< len; i++) {
|
|
|
|
printf("%u,", (unsigned char) *(tbuf+i));
|
2010-06-07 18:44:02 +01:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
*/
|
2010-06-04 23:10:06 +01:00
|
|
|
if (len < 0) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("decoding error\n");
|
2010-06-04 23:10:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
traffic("}");
|
|
|
|
tstart = 0;
|
|
|
|
tend = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void proxy_handler(ws_ctx_t *ws_ctx) {
|
|
|
|
int tsock = 0;
|
|
|
|
struct sockaddr_in taddr;
|
|
|
|
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_msg("connecting to: %s:%d\n", target_host, target_port);
|
2010-06-04 23:10:06 +01:00
|
|
|
|
|
|
|
tsock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (tsock < 0) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("Could not create target socket: %s\n",
|
|
|
|
strerror(errno));
|
2010-06-04 23:10:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bzero((char *) &taddr, sizeof(taddr));
|
|
|
|
taddr.sin_family = AF_INET;
|
|
|
|
taddr.sin_port = htons(target_port);
|
|
|
|
|
2010-06-16 19:11:07 +01:00
|
|
|
/* Resolve target address */
|
|
|
|
if (resolve_host(&taddr.sin_addr, target_host) < -1) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("Could not resolve target address: %s\n",
|
|
|
|
strerror(errno));
|
2010-06-16 19:11:07 +01:00
|
|
|
}
|
|
|
|
|
2010-06-04 23:10:06 +01:00
|
|
|
if (connect(tsock, (struct sockaddr *) &taddr, sizeof(taddr)) < 0) {
|
2010-09-10 19:05:48 +01:00
|
|
|
handler_emsg("Could not connect to target: %s\n",
|
|
|
|
strerror(errno));
|
2010-06-04 23:10:06 +01:00
|
|
|
close(tsock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-11 21:10:54 +01:00
|
|
|
if ((settings.verbose) && (! settings.daemon)) {
|
2010-09-10 19:05:48 +01:00
|
|
|
printf("%s", traffic_legend);
|
|
|
|
}
|
2010-06-04 23:10:06 +01:00
|
|
|
|
|
|
|
do_proxy(ws_ctx, tsock);
|
|
|
|
|
|
|
|
close(tsock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2010-07-17 18:13:54 +01:00
|
|
|
int fd, c, option_index = 0;
|
wsproxy, wstelnet: wrap command, WS telnet client.
wswrapper:
Getting the wswrapper.c LD_PRELOAD model working has turned out to
involve too many dark corners of the glibc/POSIX file descriptor
space. I realized that 95% of what I want can be accomplished by
adding a "wrap command" mode to wsproxy.
The code is still there for now, but consider it experimental at
best. Minor fix to dup2 and add dup and dup3 logging.
wsproxy Wrap Command:
In wsproxy wrap command mode, a command line is specified instead
of a target address and port. wsproxy then uses a much simpler
LD_PRELOAD library, rebind.so, to move intercept any bind() system
calls made by the program. If the bind() call is for the wsproxy
listen port number then the real bind() system call is issued for
an alternate (free high) port on loopback/localhost. wsproxy then
forwards from the listen address/port to the moved port.
The --wrap-mode argument takes three options that determine the
behavior of wsproxy when the wrapped command returns an exit code
(exit or daemonizing): ignore, exit, respawn.
For example, this runs vncserver on turns port 5901 into
a WebSockets port (rebind.so must be built first):
./utils/wsproxy.py --wrap-mode=ignore 5901 -- vncserver :1
The vncserver command backgrounds itself so the wrap mode is set
to "ignore" so that wsproxy keeps running even after it receives
an exit code from vncserver.
wstelnet:
To demonstrate the wrap command mode, I added WebSockets telnet
client.
For example, this runs telnetd (krb5-telnetd) on turns port 2023
into a WebSockets port (using "respawn" mode since telnetd exits
after each connection closes):
sudo ./utils/wsproxy.py --wrap-mode=respawn 2023 -- telnetd -debug 2023
Then the utils/wstelnet.html page can be used to connect to the
telnetd server on port 2023. The telnet client includes VT100.js
(from http://code.google.com/p/sshconsole) which handles the
terminal emulation and rendering.
rebind:
The rebind LD_PRELOAD library is used by wsproxy in wrap command
mode to intercept bind() system calls and move the port to
a different port on loopback/localhost. The rebind.so library can
be built by running make in the utils directory.
The rebind library can be used separately from wsproxy by setting
the REBIND_OLD_PORT and REBIND_NEW_PORT environment variables
prior to executing a command. For example:
export export REBIND_PORT_OLD="23"
export export REBIND_PORT_NEW="65023"
LD_PRELOAD=./rebind.so telnetd -debug 23
Alternately, the rebind script does the same thing:
rebind 23 65023 telnetd -debug 23
Other changes/notes:
- wsproxy no longer daemonizes by default. Remove -f/--foreground
option and add -D/--deamon option.
- When wsproxy is used to wrap a command in "respawn" mode, the
command will not be respawn more often than 3 times within 10
seconds.
- Move getKeysym routine out of Canvas object so that it can be called
directly.
2011-01-12 19:15:11 +00:00
|
|
|
static int ssl_only = 0, daemon = 0, verbose = 0;
|
2010-06-17 22:06:18 +01:00
|
|
|
char *found;
|
2010-06-16 19:58:00 +01:00
|
|
|
static struct option long_options[] = {
|
2010-09-11 21:10:54 +01:00
|
|
|
{"verbose", no_argument, &verbose, 'v'},
|
2010-06-17 22:06:18 +01:00
|
|
|
{"ssl-only", no_argument, &ssl_only, 1 },
|
wsproxy, wstelnet: wrap command, WS telnet client.
wswrapper:
Getting the wswrapper.c LD_PRELOAD model working has turned out to
involve too many dark corners of the glibc/POSIX file descriptor
space. I realized that 95% of what I want can be accomplished by
adding a "wrap command" mode to wsproxy.
The code is still there for now, but consider it experimental at
best. Minor fix to dup2 and add dup and dup3 logging.
wsproxy Wrap Command:
In wsproxy wrap command mode, a command line is specified instead
of a target address and port. wsproxy then uses a much simpler
LD_PRELOAD library, rebind.so, to move intercept any bind() system
calls made by the program. If the bind() call is for the wsproxy
listen port number then the real bind() system call is issued for
an alternate (free high) port on loopback/localhost. wsproxy then
forwards from the listen address/port to the moved port.
The --wrap-mode argument takes three options that determine the
behavior of wsproxy when the wrapped command returns an exit code
(exit or daemonizing): ignore, exit, respawn.
For example, this runs vncserver on turns port 5901 into
a WebSockets port (rebind.so must be built first):
./utils/wsproxy.py --wrap-mode=ignore 5901 -- vncserver :1
The vncserver command backgrounds itself so the wrap mode is set
to "ignore" so that wsproxy keeps running even after it receives
an exit code from vncserver.
wstelnet:
To demonstrate the wrap command mode, I added WebSockets telnet
client.
For example, this runs telnetd (krb5-telnetd) on turns port 2023
into a WebSockets port (using "respawn" mode since telnetd exits
after each connection closes):
sudo ./utils/wsproxy.py --wrap-mode=respawn 2023 -- telnetd -debug 2023
Then the utils/wstelnet.html page can be used to connect to the
telnetd server on port 2023. The telnet client includes VT100.js
(from http://code.google.com/p/sshconsole) which handles the
terminal emulation and rendering.
rebind:
The rebind LD_PRELOAD library is used by wsproxy in wrap command
mode to intercept bind() system calls and move the port to
a different port on loopback/localhost. The rebind.so library can
be built by running make in the utils directory.
The rebind library can be used separately from wsproxy by setting
the REBIND_OLD_PORT and REBIND_NEW_PORT environment variables
prior to executing a command. For example:
export export REBIND_PORT_OLD="23"
export export REBIND_PORT_NEW="65023"
LD_PRELOAD=./rebind.so telnetd -debug 23
Alternately, the rebind script does the same thing:
rebind 23 65023 telnetd -debug 23
Other changes/notes:
- wsproxy no longer daemonizes by default. Remove -f/--foreground
option and add -D/--deamon option.
- When wsproxy is used to wrap a command in "respawn" mode, the
command will not be respawn more often than 3 times within 10
seconds.
- Move getKeysym routine out of Canvas object so that it can be called
directly.
2011-01-12 19:15:11 +00:00
|
|
|
{"daemon", no_argument, &daemon, 'D'},
|
2010-06-16 19:58:00 +01:00
|
|
|
/* ---- */
|
2010-06-17 22:06:18 +01:00
|
|
|
{"cert", required_argument, 0, 'c'},
|
2010-11-06 15:55:09 +00:00
|
|
|
{"key", required_argument, 0, 'k'},
|
2010-06-16 19:58:00 +01:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2010-06-04 23:10:06 +01:00
|
|
|
|
2010-06-17 23:05:33 +01:00
|
|
|
settings.cert = realpath("self.pem", NULL);
|
2011-01-04 19:14:46 +00:00
|
|
|
if (!settings.cert) {
|
|
|
|
/* Make sure it's always set to something */
|
|
|
|
settings.cert = "self.pem";
|
|
|
|
}
|
2010-11-06 15:55:09 +00:00
|
|
|
settings.key = "";
|
2010-06-17 22:06:18 +01:00
|
|
|
|
2010-06-16 19:58:00 +01:00
|
|
|
while (1) {
|
wsproxy, wstelnet: wrap command, WS telnet client.
wswrapper:
Getting the wswrapper.c LD_PRELOAD model working has turned out to
involve too many dark corners of the glibc/POSIX file descriptor
space. I realized that 95% of what I want can be accomplished by
adding a "wrap command" mode to wsproxy.
The code is still there for now, but consider it experimental at
best. Minor fix to dup2 and add dup and dup3 logging.
wsproxy Wrap Command:
In wsproxy wrap command mode, a command line is specified instead
of a target address and port. wsproxy then uses a much simpler
LD_PRELOAD library, rebind.so, to move intercept any bind() system
calls made by the program. If the bind() call is for the wsproxy
listen port number then the real bind() system call is issued for
an alternate (free high) port on loopback/localhost. wsproxy then
forwards from the listen address/port to the moved port.
The --wrap-mode argument takes three options that determine the
behavior of wsproxy when the wrapped command returns an exit code
(exit or daemonizing): ignore, exit, respawn.
For example, this runs vncserver on turns port 5901 into
a WebSockets port (rebind.so must be built first):
./utils/wsproxy.py --wrap-mode=ignore 5901 -- vncserver :1
The vncserver command backgrounds itself so the wrap mode is set
to "ignore" so that wsproxy keeps running even after it receives
an exit code from vncserver.
wstelnet:
To demonstrate the wrap command mode, I added WebSockets telnet
client.
For example, this runs telnetd (krb5-telnetd) on turns port 2023
into a WebSockets port (using "respawn" mode since telnetd exits
after each connection closes):
sudo ./utils/wsproxy.py --wrap-mode=respawn 2023 -- telnetd -debug 2023
Then the utils/wstelnet.html page can be used to connect to the
telnetd server on port 2023. The telnet client includes VT100.js
(from http://code.google.com/p/sshconsole) which handles the
terminal emulation and rendering.
rebind:
The rebind LD_PRELOAD library is used by wsproxy in wrap command
mode to intercept bind() system calls and move the port to
a different port on loopback/localhost. The rebind.so library can
be built by running make in the utils directory.
The rebind library can be used separately from wsproxy by setting
the REBIND_OLD_PORT and REBIND_NEW_PORT environment variables
prior to executing a command. For example:
export export REBIND_PORT_OLD="23"
export export REBIND_PORT_NEW="65023"
LD_PRELOAD=./rebind.so telnetd -debug 23
Alternately, the rebind script does the same thing:
rebind 23 65023 telnetd -debug 23
Other changes/notes:
- wsproxy no longer daemonizes by default. Remove -f/--foreground
option and add -D/--deamon option.
- When wsproxy is used to wrap a command in "respawn" mode, the
command will not be respawn more often than 3 times within 10
seconds.
- Move getKeysym routine out of Canvas object so that it can be called
directly.
2011-01-12 19:15:11 +00:00
|
|
|
c = getopt_long (argc, argv, "vDc:k:",
|
2010-06-16 19:58:00 +01:00
|
|
|
long_options, &option_index);
|
2010-06-16 18:37:03 +01:00
|
|
|
|
2010-06-16 19:58:00 +01:00
|
|
|
/* Detect the end */
|
|
|
|
if (c == -1) { break; }
|
|
|
|
|
|
|
|
switch (c) {
|
2010-06-17 22:06:18 +01:00
|
|
|
case 0:
|
|
|
|
break; // ignore
|
|
|
|
case 1:
|
|
|
|
break; // ignore
|
2010-09-11 21:10:54 +01:00
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
wsproxy, wstelnet: wrap command, WS telnet client.
wswrapper:
Getting the wswrapper.c LD_PRELOAD model working has turned out to
involve too many dark corners of the glibc/POSIX file descriptor
space. I realized that 95% of what I want can be accomplished by
adding a "wrap command" mode to wsproxy.
The code is still there for now, but consider it experimental at
best. Minor fix to dup2 and add dup and dup3 logging.
wsproxy Wrap Command:
In wsproxy wrap command mode, a command line is specified instead
of a target address and port. wsproxy then uses a much simpler
LD_PRELOAD library, rebind.so, to move intercept any bind() system
calls made by the program. If the bind() call is for the wsproxy
listen port number then the real bind() system call is issued for
an alternate (free high) port on loopback/localhost. wsproxy then
forwards from the listen address/port to the moved port.
The --wrap-mode argument takes three options that determine the
behavior of wsproxy when the wrapped command returns an exit code
(exit or daemonizing): ignore, exit, respawn.
For example, this runs vncserver on turns port 5901 into
a WebSockets port (rebind.so must be built first):
./utils/wsproxy.py --wrap-mode=ignore 5901 -- vncserver :1
The vncserver command backgrounds itself so the wrap mode is set
to "ignore" so that wsproxy keeps running even after it receives
an exit code from vncserver.
wstelnet:
To demonstrate the wrap command mode, I added WebSockets telnet
client.
For example, this runs telnetd (krb5-telnetd) on turns port 2023
into a WebSockets port (using "respawn" mode since telnetd exits
after each connection closes):
sudo ./utils/wsproxy.py --wrap-mode=respawn 2023 -- telnetd -debug 2023
Then the utils/wstelnet.html page can be used to connect to the
telnetd server on port 2023. The telnet client includes VT100.js
(from http://code.google.com/p/sshconsole) which handles the
terminal emulation and rendering.
rebind:
The rebind LD_PRELOAD library is used by wsproxy in wrap command
mode to intercept bind() system calls and move the port to
a different port on loopback/localhost. The rebind.so library can
be built by running make in the utils directory.
The rebind library can be used separately from wsproxy by setting
the REBIND_OLD_PORT and REBIND_NEW_PORT environment variables
prior to executing a command. For example:
export export REBIND_PORT_OLD="23"
export export REBIND_PORT_NEW="65023"
LD_PRELOAD=./rebind.so telnetd -debug 23
Alternately, the rebind script does the same thing:
rebind 23 65023 telnetd -debug 23
Other changes/notes:
- wsproxy no longer daemonizes by default. Remove -f/--foreground
option and add -D/--deamon option.
- When wsproxy is used to wrap a command in "respawn" mode, the
command will not be respawn more often than 3 times within 10
seconds.
- Move getKeysym routine out of Canvas object so that it can be called
directly.
2011-01-12 19:15:11 +00:00
|
|
|
case 'D':
|
|
|
|
daemon = 1;
|
2010-06-17 22:06:18 +01:00
|
|
|
break;
|
|
|
|
case 'c':
|
2010-06-17 23:05:33 +01:00
|
|
|
settings.cert = realpath(optarg, NULL);
|
|
|
|
if (! settings.cert) {
|
2010-06-17 23:24:54 +01:00
|
|
|
usage("No cert file at %s\n", optarg);
|
2010-06-17 23:05:33 +01:00
|
|
|
}
|
2010-06-17 22:06:18 +01:00
|
|
|
break;
|
2010-11-06 15:55:09 +00:00
|
|
|
case 'k':
|
|
|
|
settings.key = realpath(optarg, NULL);
|
|
|
|
if (! settings.key) {
|
|
|
|
usage("No key file at %s\n", optarg);
|
|
|
|
}
|
|
|
|
break;
|
2010-06-17 22:06:18 +01:00
|
|
|
default:
|
2010-06-17 23:50:15 +01:00
|
|
|
usage("");
|
2010-06-16 19:58:00 +01:00
|
|
|
}
|
2010-06-04 23:10:06 +01:00
|
|
|
}
|
2010-09-11 21:10:54 +01:00
|
|
|
settings.verbose = verbose;
|
2010-09-10 19:05:48 +01:00
|
|
|
settings.ssl_only = ssl_only;
|
wsproxy, wstelnet: wrap command, WS telnet client.
wswrapper:
Getting the wswrapper.c LD_PRELOAD model working has turned out to
involve too many dark corners of the glibc/POSIX file descriptor
space. I realized that 95% of what I want can be accomplished by
adding a "wrap command" mode to wsproxy.
The code is still there for now, but consider it experimental at
best. Minor fix to dup2 and add dup and dup3 logging.
wsproxy Wrap Command:
In wsproxy wrap command mode, a command line is specified instead
of a target address and port. wsproxy then uses a much simpler
LD_PRELOAD library, rebind.so, to move intercept any bind() system
calls made by the program. If the bind() call is for the wsproxy
listen port number then the real bind() system call is issued for
an alternate (free high) port on loopback/localhost. wsproxy then
forwards from the listen address/port to the moved port.
The --wrap-mode argument takes three options that determine the
behavior of wsproxy when the wrapped command returns an exit code
(exit or daemonizing): ignore, exit, respawn.
For example, this runs vncserver on turns port 5901 into
a WebSockets port (rebind.so must be built first):
./utils/wsproxy.py --wrap-mode=ignore 5901 -- vncserver :1
The vncserver command backgrounds itself so the wrap mode is set
to "ignore" so that wsproxy keeps running even after it receives
an exit code from vncserver.
wstelnet:
To demonstrate the wrap command mode, I added WebSockets telnet
client.
For example, this runs telnetd (krb5-telnetd) on turns port 2023
into a WebSockets port (using "respawn" mode since telnetd exits
after each connection closes):
sudo ./utils/wsproxy.py --wrap-mode=respawn 2023 -- telnetd -debug 2023
Then the utils/wstelnet.html page can be used to connect to the
telnetd server on port 2023. The telnet client includes VT100.js
(from http://code.google.com/p/sshconsole) which handles the
terminal emulation and rendering.
rebind:
The rebind LD_PRELOAD library is used by wsproxy in wrap command
mode to intercept bind() system calls and move the port to
a different port on loopback/localhost. The rebind.so library can
be built by running make in the utils directory.
The rebind library can be used separately from wsproxy by setting
the REBIND_OLD_PORT and REBIND_NEW_PORT environment variables
prior to executing a command. For example:
export export REBIND_PORT_OLD="23"
export export REBIND_PORT_NEW="65023"
LD_PRELOAD=./rebind.so telnetd -debug 23
Alternately, the rebind script does the same thing:
rebind 23 65023 telnetd -debug 23
Other changes/notes:
- wsproxy no longer daemonizes by default. Remove -f/--foreground
option and add -D/--deamon option.
- When wsproxy is used to wrap a command in "respawn" mode, the
command will not be respawn more often than 3 times within 10
seconds.
- Move getKeysym routine out of Canvas object so that it can be called
directly.
2011-01-12 19:15:11 +00:00
|
|
|
settings.daemon = daemon;
|
2010-06-04 23:10:06 +01:00
|
|
|
|
2010-06-16 19:58:00 +01:00
|
|
|
if ((argc-optind) != 2) {
|
2010-06-17 23:24:54 +01:00
|
|
|
usage("Invalid number of arguments\n");
|
2010-06-16 18:37:03 +01:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:06:18 +01:00
|
|
|
found = strstr(argv[optind], ":");
|
|
|
|
if (found) {
|
|
|
|
memcpy(settings.listen_host, argv[optind], found-argv[optind]);
|
|
|
|
settings.listen_port = strtol(found+1, NULL, 10);
|
2010-06-16 18:37:03 +01:00
|
|
|
} else {
|
2010-06-17 22:06:18 +01:00
|
|
|
settings.listen_host[0] = '\0';
|
|
|
|
settings.listen_port = strtol(argv[optind], NULL, 10);
|
2010-06-16 18:37:03 +01:00
|
|
|
}
|
2010-06-16 19:58:00 +01:00
|
|
|
optind++;
|
2010-07-17 18:13:54 +01:00
|
|
|
if (settings.listen_port == 0) {
|
2010-06-17 23:24:54 +01:00
|
|
|
usage("Could not parse listen_port\n");
|
2010-06-16 18:37:03 +01:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:06:18 +01:00
|
|
|
found = strstr(argv[optind], ":");
|
|
|
|
if (found) {
|
|
|
|
memcpy(target_host, argv[optind], found-argv[optind]);
|
|
|
|
target_port = strtol(found+1, NULL, 10);
|
2010-06-16 18:37:03 +01:00
|
|
|
} else {
|
2010-06-17 23:24:54 +01:00
|
|
|
usage("Target argument must be host:port\n");
|
|
|
|
}
|
|
|
|
if (target_port == 0) {
|
|
|
|
usage("Could not parse target port\n");
|
2010-06-16 18:37:03 +01:00
|
|
|
}
|
2010-06-17 23:24:54 +01:00
|
|
|
|
|
|
|
if (ssl_only) {
|
2011-01-04 19:14:46 +00:00
|
|
|
if (!access(settings.cert, R_OK)) {
|
|
|
|
usage("SSL only and cert file '%s' not found\n", settings.cert);
|
2010-06-17 23:24:54 +01:00
|
|
|
}
|
2011-01-04 19:14:46 +00:00
|
|
|
} else if (access(settings.cert, R_OK) != 0) {
|
|
|
|
fprintf(stderr, "Warning: '%s' not found\n", settings.cert);
|
2010-06-16 18:37:03 +01:00
|
|
|
}
|
2010-06-04 23:10:06 +01:00
|
|
|
|
2010-09-11 21:10:54 +01:00
|
|
|
//printf(" verbose: %d\n", settings.verbose);
|
2010-11-06 15:55:09 +00:00
|
|
|
//printf(" ssl_only: %d\n", settings.ssl_only);
|
|
|
|
//printf(" daemon: %d\n", settings.daemon);
|
|
|
|
//printf(" cert: %s\n", settings.cert);
|
|
|
|
//printf(" key: %s\n", settings.key);
|
2010-06-17 23:24:54 +01:00
|
|
|
|
2010-06-17 22:06:18 +01:00
|
|
|
settings.handler = proxy_handler;
|
|
|
|
start_server();
|
2010-06-04 23:10:06 +01:00
|
|
|
|
|
|
|
free(tbuf);
|
|
|
|
free(cbuf);
|
|
|
|
free(tbuf_tmp);
|
|
|
|
free(cbuf_tmp);
|
|
|
|
}
|