2011-03-26 20:27:08 +00:00
|
|
|
#!/usr/bin/env python
|
2010-04-01 00:21:03 +01:00
|
|
|
|
2010-04-30 22:41:09 +01:00
|
|
|
'''
|
|
|
|
A WebSocket to TCP socket proxy with support for "wss://" encryption.
|
2011-05-18 17:09:10 +01:00
|
|
|
Copyright 2011 Joel Martin
|
2010-07-17 18:05:58 +01:00
|
|
|
Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
|
2010-04-30 22:41:09 +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
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2012-09-20 15:46:04 +01:00
|
|
|
import signal, socket, optparse, time, os, sys, subprocess
|
2010-04-01 00:21:03 +01:00
|
|
|
from select import select
|
2012-05-23 15:20:08 +01:00
|
|
|
import websocket
|
2012-07-13 01:10:12 +01:00
|
|
|
try: from urllib.parse import parse_qs, urlparse
|
|
|
|
except: from urlparse import parse_qs, urlparse
|
2010-04-01 00:21:03 +01:00
|
|
|
|
2012-05-23 15:20:08 +01:00
|
|
|
class WebSocketProxy(websocket.WebSocketServer):
|
2011-01-08 21:29:01 +00:00
|
|
|
"""
|
|
|
|
Proxy traffic to and from a WebSockets client to a normal TCP
|
|
|
|
socket server target. All traffic to/from the client is base64
|
|
|
|
encoded/decoded to allow binary data to be sent/received to/from
|
|
|
|
the target.
|
|
|
|
"""
|
2010-04-16 22:34:19 +01:00
|
|
|
|
2011-01-08 21:29:01 +00:00
|
|
|
buffer_size = 65536
|
|
|
|
|
|
|
|
traffic_legend = """
|
2010-04-17 23:16:08 +01:00
|
|
|
Traffic Legend:
|
|
|
|
} - Client receive
|
|
|
|
}. - Client receive partial
|
|
|
|
{ - Target receive
|
|
|
|
|
|
|
|
> - Target send
|
|
|
|
>. - Target send partial
|
|
|
|
< - Client send
|
|
|
|
<. - Client send partial
|
|
|
|
"""
|
|
|
|
|
2011-01-08 21:29:01 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
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
|
|
|
# Save off proxy specific options
|
2012-09-21 13:08:04 +01:00
|
|
|
self.target_host = kwargs.pop('target_host', None)
|
|
|
|
self.target_port = kwargs.pop('target_port', None)
|
|
|
|
self.wrap_cmd = kwargs.pop('wrap_cmd', None)
|
|
|
|
self.wrap_mode = kwargs.pop('wrap_mode', None)
|
|
|
|
self.unix_target = kwargs.pop('unix_target', None)
|
|
|
|
self.ssl_target = kwargs.pop('ssl_target', None)
|
|
|
|
self.target_cfg = kwargs.pop('target_cfg', None)
|
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
|
|
|
# Last 3 timestamps command was run
|
|
|
|
self.wrap_times = [0, 0, 0]
|
|
|
|
|
|
|
|
if self.wrap_cmd:
|
|
|
|
rebinder_path = ['./', os.path.dirname(sys.argv[0])]
|
|
|
|
self.rebinder = None
|
|
|
|
|
|
|
|
for rdir in rebinder_path:
|
|
|
|
rpath = os.path.join(rdir, "rebind.so")
|
|
|
|
if os.path.exists(rpath):
|
|
|
|
self.rebinder = rpath
|
|
|
|
break
|
|
|
|
|
|
|
|
if not self.rebinder:
|
|
|
|
raise Exception("rebind.so not found, perhaps you need to run make")
|
2012-05-31 15:20:01 +01:00
|
|
|
self.rebinder = os.path.abspath(self.rebinder)
|
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
|
|
|
|
|
|
|
self.target_host = "127.0.0.1" # Loopback
|
|
|
|
# Find a free high port
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.bind(('', 0))
|
|
|
|
self.target_port = sock.getsockname()[1]
|
|
|
|
sock.close()
|
|
|
|
|
|
|
|
os.environ.update({
|
|
|
|
"LD_PRELOAD": self.rebinder,
|
|
|
|
"REBIND_OLD_PORT": str(kwargs['listen_port']),
|
|
|
|
"REBIND_NEW_PORT": str(self.target_port)})
|
|
|
|
|
2012-07-13 19:17:56 +01:00
|
|
|
if self.target_cfg:
|
|
|
|
self.target_cfg = os.path.abspath(self.target_cfg)
|
2012-07-13 01:10:12 +01:00
|
|
|
|
2012-05-23 15:20:08 +01:00
|
|
|
websocket.WebSocketServer.__init__(self, *args, **kwargs)
|
2011-01-08 21:29:01 +00:00
|
|
|
|
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
|
|
|
def run_wrap_cmd(self):
|
2011-05-18 17:09:10 +01:00
|
|
|
print("Starting '%s'" % " ".join(self.wrap_cmd))
|
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
|
|
|
self.wrap_times.append(time.time())
|
|
|
|
self.wrap_times.pop(0)
|
|
|
|
self.cmd = subprocess.Popen(
|
2012-09-20 15:46:04 +01:00
|
|
|
self.wrap_cmd, env=os.environ, preexec_fn=_subprocess_setup)
|
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
|
|
|
self.spawn_message = True
|
|
|
|
|
|
|
|
def started(self):
|
|
|
|
"""
|
|
|
|
Called after Websockets server startup (i.e. after daemonize)
|
|
|
|
"""
|
|
|
|
# Need to call wrapped command after daemonization so we can
|
|
|
|
# know when the wrapped command exits
|
|
|
|
if self.wrap_cmd:
|
2012-05-31 15:17:51 +01:00
|
|
|
dst_string = "'%s' (port %s)" % (" ".join(self.wrap_cmd), self.target_port)
|
|
|
|
elif self.unix_target:
|
|
|
|
dst_string = self.unix_target
|
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
|
|
|
else:
|
2012-05-31 15:17:51 +01:00
|
|
|
dst_string = "%s:%s" % (self.target_host, self.target_port)
|
|
|
|
|
2012-07-13 19:17:56 +01:00
|
|
|
if self.target_cfg:
|
2012-07-11 15:00:13 +01:00
|
|
|
msg = " - proxying from %s:%s to targets in %s" % (
|
2012-07-13 19:17:56 +01:00
|
|
|
self.listen_host, self.listen_port, self.target_cfg)
|
2012-07-11 15:00:13 +01:00
|
|
|
else:
|
|
|
|
msg = " - proxying from %s:%s to %s" % (
|
2012-05-31 15:17:51 +01:00
|
|
|
self.listen_host, self.listen_port, dst_string)
|
2012-05-23 15:20:08 +01:00
|
|
|
|
|
|
|
if self.ssl_target:
|
|
|
|
msg += " (using SSL)"
|
|
|
|
|
|
|
|
print(msg + "\n")
|
|
|
|
|
|
|
|
if self.wrap_cmd:
|
|
|
|
self.run_wrap_cmd()
|
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
|
|
|
|
|
|
|
def poll(self):
|
|
|
|
# If we are wrapping a command, check it's status
|
|
|
|
|
|
|
|
if self.wrap_cmd and self.cmd:
|
|
|
|
ret = self.cmd.poll()
|
|
|
|
if ret != None:
|
|
|
|
self.vmsg("Wrapped command exited (or daemon). Returned %s" % ret)
|
|
|
|
self.cmd = None
|
|
|
|
|
|
|
|
if self.wrap_cmd and self.cmd == None:
|
|
|
|
# Response to wrapped command being gone
|
|
|
|
if self.wrap_mode == "ignore":
|
|
|
|
pass
|
|
|
|
elif self.wrap_mode == "exit":
|
|
|
|
sys.exit(ret)
|
|
|
|
elif self.wrap_mode == "respawn":
|
|
|
|
now = time.time()
|
|
|
|
avg = sum(self.wrap_times)/len(self.wrap_times)
|
|
|
|
if (now - avg) < 10:
|
|
|
|
# 3 times in the last 10 seconds
|
|
|
|
if self.spawn_message:
|
2011-05-18 17:09:10 +01:00
|
|
|
print("Command respawning too fast")
|
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
|
|
|
self.spawn_message = False
|
|
|
|
else:
|
|
|
|
self.run_wrap_cmd()
|
|
|
|
|
2012-09-20 15:46:04 +01:00
|
|
|
#
|
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
|
|
|
# Routines above this point are run in the master listener
|
|
|
|
# process.
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Routines below this point are connection handler routines and
|
|
|
|
# will be run in a separate forked process for each connection.
|
|
|
|
#
|
|
|
|
|
2011-05-02 04:17:04 +01:00
|
|
|
def new_client(self):
|
2011-01-08 21:29:01 +00:00
|
|
|
"""
|
|
|
|
Called after a new WebSocket connection has been established.
|
|
|
|
"""
|
2012-07-13 01:10:12 +01:00
|
|
|
# Checks if we receive a token, and look
|
|
|
|
# for a valid target for it then
|
2012-07-13 19:17:56 +01:00
|
|
|
if self.target_cfg:
|
|
|
|
(self.target_host, self.target_port) = self.get_target(self.target_cfg, self.path)
|
2012-07-13 01:10:12 +01:00
|
|
|
|
2011-01-08 21:29:01 +00:00
|
|
|
# Connect to the target
|
2012-05-31 15:17:51 +01:00
|
|
|
if self.wrap_cmd:
|
2012-09-21 13:28:08 +01:00
|
|
|
msg = "connecting to command: '%s' (port %s)" % (" ".join(self.wrap_cmd), self.target_port)
|
2012-05-31 15:17:51 +01:00
|
|
|
elif self.unix_target:
|
|
|
|
msg = "connecting to unix socket: %s" % self.unix_target
|
2012-05-22 15:09:07 +01:00
|
|
|
else:
|
2012-05-28 12:09:07 +01:00
|
|
|
msg = "connecting to: %s:%s" % (
|
|
|
|
self.target_host, self.target_port)
|
2012-09-20 15:46:04 +01:00
|
|
|
|
2012-05-23 15:20:08 +01:00
|
|
|
if self.ssl_target:
|
|
|
|
msg += " (using SSL)"
|
|
|
|
self.msg(msg)
|
|
|
|
|
2011-07-09 19:34:53 +01:00
|
|
|
tsock = self.socket(self.target_host, self.target_port,
|
2012-05-31 15:17:51 +01:00
|
|
|
connect=True, use_ssl=self.ssl_target, unix_socket=self.unix_target)
|
2011-07-07 17:45:19 +01:00
|
|
|
|
2011-01-08 21:29:01 +00:00
|
|
|
if self.verbose and not self.daemon:
|
2011-05-18 17:09:10 +01:00
|
|
|
print(self.traffic_legend)
|
2011-01-08 21:29:01 +00:00
|
|
|
|
2011-05-02 04:17:04 +01:00
|
|
|
# Start proxying
|
2011-01-08 21:29:01 +00:00
|
|
|
try:
|
2011-05-02 04:17:04 +01:00
|
|
|
self.do_proxy(tsock)
|
2011-01-08 21:29:01 +00:00
|
|
|
except:
|
2011-01-13 18:22:22 +00:00
|
|
|
if tsock:
|
2011-06-26 19:26:59 +01:00
|
|
|
tsock.shutdown(socket.SHUT_RDWR)
|
2011-01-13 18:22:22 +00:00
|
|
|
tsock.close()
|
2012-08-14 21:14:00 +01:00
|
|
|
self.vmsg("%s:%s: Closed target" %(
|
2011-01-13 18:22:22 +00:00
|
|
|
self.target_host, self.target_port))
|
2011-01-08 21:29:01 +00:00
|
|
|
raise
|
|
|
|
|
2012-07-13 19:17:56 +01:00
|
|
|
def get_target(self, target_cfg, path):
|
2012-07-13 01:10:12 +01:00
|
|
|
"""
|
|
|
|
Parses the path, extracts a token, and looks for a valid
|
|
|
|
target for that token in the configuration file(s). Sets
|
|
|
|
target_host and target_port if successful
|
|
|
|
"""
|
|
|
|
# The files in targets contain the lines
|
2012-07-13 01:34:27 +01:00
|
|
|
# in the form of token: host:port
|
2012-07-13 01:10:12 +01:00
|
|
|
|
|
|
|
# Extract the token parameter from url
|
|
|
|
args = parse_qs(urlparse(path)[4]) # 4 is the query from url
|
|
|
|
|
2012-10-29 22:05:23 +00:00
|
|
|
if not args.has_key('token') or not len(args['token']):
|
2012-07-13 01:10:12 +01:00
|
|
|
raise self.EClose("Token not present")
|
|
|
|
|
|
|
|
token = args['token'][0].rstrip('\n')
|
|
|
|
|
2012-07-13 19:17:56 +01:00
|
|
|
# target_cfg can be a single config file or directory of
|
2012-07-13 01:34:27 +01:00
|
|
|
# config files
|
2012-07-13 19:17:56 +01:00
|
|
|
if os.path.isdir(target_cfg):
|
|
|
|
cfg_files = [os.path.join(target_cfg, f)
|
|
|
|
for f in os.listdir(target_cfg)]
|
2012-07-13 01:10:12 +01:00
|
|
|
else:
|
2012-07-13 19:17:56 +01:00
|
|
|
cfg_files = [target_cfg]
|
2012-07-13 01:10:12 +01:00
|
|
|
|
2012-07-13 01:34:27 +01:00
|
|
|
targets = {}
|
|
|
|
for f in cfg_files:
|
|
|
|
for line in [l.strip() for l in file(f).readlines()]:
|
|
|
|
if line and not line.startswith('#'):
|
|
|
|
ttoken, target = line.split(': ')
|
|
|
|
targets[ttoken] = target.strip()
|
2012-07-13 01:10:12 +01:00
|
|
|
|
2012-07-13 01:34:27 +01:00
|
|
|
self.vmsg("Target config: %s" % repr(targets))
|
2012-07-13 01:10:12 +01:00
|
|
|
|
2012-07-13 01:34:27 +01:00
|
|
|
if targets.has_key(token):
|
|
|
|
return targets[token].split(':')
|
|
|
|
else:
|
|
|
|
raise self.EClose("Token '%s' not found" % token)
|
2012-07-13 01:10:12 +01:00
|
|
|
|
2011-05-02 04:17:04 +01:00
|
|
|
def do_proxy(self, target):
|
2011-01-08 21:29:01 +00:00
|
|
|
"""
|
|
|
|
Proxy client WebSocket to normal target socket.
|
|
|
|
"""
|
|
|
|
cqueue = []
|
2011-05-02 04:17:04 +01:00
|
|
|
c_pend = 0
|
2011-01-08 21:29:01 +00:00
|
|
|
tqueue = []
|
2011-05-02 04:17:04 +01:00
|
|
|
rlist = [self.client, target]
|
2011-01-08 21:29:01 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
wlist = []
|
|
|
|
|
|
|
|
if tqueue: wlist.append(target)
|
2011-05-02 04:17:04 +01:00
|
|
|
if cqueue or c_pend: wlist.append(self.client)
|
2011-01-08 21:29:01 +00:00
|
|
|
ins, outs, excepts = select(rlist, wlist, [], 1)
|
|
|
|
if excepts: raise Exception("Socket exception")
|
|
|
|
|
2012-10-29 23:12:54 +00:00
|
|
|
if self.client in outs:
|
|
|
|
# Send queued target data to the client
|
|
|
|
c_pend = self.send_frames(cqueue)
|
|
|
|
|
|
|
|
cqueue = []
|
|
|
|
|
|
|
|
if self.client in ins:
|
|
|
|
# Receive client data, decode it, and queue for target
|
|
|
|
bufs, closed = self.recv_frames()
|
|
|
|
tqueue.extend(bufs)
|
|
|
|
|
|
|
|
if closed:
|
|
|
|
# TODO: What about blocking on client socket?
|
|
|
|
self.vmsg("%s:%s: Client closed connection" %(
|
|
|
|
self.target_host, self.target_port))
|
|
|
|
raise self.CClose(closed['code'], closed['reason'])
|
|
|
|
|
|
|
|
|
2011-01-08 21:29:01 +00:00
|
|
|
if target in outs:
|
|
|
|
# Send queued client data to the target
|
|
|
|
dat = tqueue.pop(0)
|
|
|
|
sent = target.send(dat)
|
|
|
|
if sent == len(dat):
|
|
|
|
self.traffic(">")
|
|
|
|
else:
|
|
|
|
# requeue the remaining data
|
|
|
|
tqueue.insert(0, dat[sent:])
|
|
|
|
self.traffic(".>")
|
|
|
|
|
|
|
|
|
|
|
|
if target in ins:
|
|
|
|
# Receive target data, encode it and queue for client
|
|
|
|
buf = target.recv(self.buffer_size)
|
2012-04-25 19:44:01 +01:00
|
|
|
if len(buf) == 0:
|
2012-08-14 21:14:00 +01:00
|
|
|
self.vmsg("%s:%s: Target closed connection" %(
|
|
|
|
self.target_host, self.target_port))
|
2012-04-25 19:44:01 +01:00
|
|
|
raise self.CClose(1000, "Target closed")
|
2011-01-08 21:29:01 +00:00
|
|
|
|
2011-05-02 04:17:04 +01:00
|
|
|
cqueue.append(buf)
|
2011-01-08 21:29:01 +00:00
|
|
|
self.traffic("{")
|
|
|
|
|
2011-05-02 04:17:04 +01:00
|
|
|
|
2012-09-20 15:46:04 +01:00
|
|
|
|
|
|
|
def _subprocess_setup():
|
|
|
|
# Python installs a SIGPIPE handler by default. This is usually not what
|
|
|
|
# non-Python successfulbprocesses expect.
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
|
|
|
|
|
2012-02-18 08:43:12 +00:00
|
|
|
def websockify_init():
|
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
|
|
|
usage = "\n %prog [options]"
|
2012-07-11 15:00:13 +01:00
|
|
|
usage += " [source_addr:]source_port [target_addr:target_port]"
|
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
|
|
|
usage += "\n %prog [options]"
|
|
|
|
usage += " [source_addr:]source_port -- WRAP_COMMAND_LINE"
|
2010-06-16 18:37:03 +01:00
|
|
|
parser = optparse.OptionParser(usage=usage)
|
2010-09-11 21:10:54 +01:00
|
|
|
parser.add_option("--verbose", "-v", action="store_true",
|
|
|
|
help="verbose messages and per frame traffic")
|
2010-06-16 19:58:00 +01:00
|
|
|
parser.add_option("--record",
|
2010-09-23 14:17:00 +01:00
|
|
|
help="record sessions to FILE.[session_number]", metavar="FILE")
|
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
|
|
|
parser.add_option("--daemon", "-D",
|
|
|
|
dest="daemon", action="store_true",
|
|
|
|
help="become a daemon (background process)")
|
2011-09-22 21:52:02 +01:00
|
|
|
parser.add_option("--run-once", action="store_true",
|
|
|
|
help="handle a single WebSocket connection and exit")
|
|
|
|
parser.add_option("--timeout", type=int, default=0,
|
|
|
|
help="after TIMEOUT seconds exit when not connected")
|
2012-08-31 15:24:09 +01:00
|
|
|
parser.add_option("--idle-timeout", type=int, default=0,
|
|
|
|
help="server exits after TIMEOUT seconds if there are no "
|
|
|
|
"active connections")
|
2010-11-06 15:55:09 +00:00
|
|
|
parser.add_option("--cert", default="self.pem",
|
|
|
|
help="SSL certificate file")
|
|
|
|
parser.add_option("--key", default=None,
|
|
|
|
help="SSL key file (if separate from cert)")
|
2010-06-16 19:58:00 +01:00
|
|
|
parser.add_option("--ssl-only", action="store_true",
|
2012-05-23 15:20:08 +01:00
|
|
|
help="disallow non-encrypted client connections")
|
2012-05-20 18:56:58 +01:00
|
|
|
parser.add_option("--ssl-target", action="store_true",
|
2012-05-23 15:20:08 +01:00
|
|
|
help="connect to SSL target as SSL client")
|
2012-05-31 15:17:51 +01:00
|
|
|
parser.add_option("--unix-target",
|
|
|
|
help="connect to unix socket target", metavar="FILE")
|
2011-01-07 00:26:54 +00:00
|
|
|
parser.add_option("--web", default=None, metavar="DIR",
|
|
|
|
help="run webserver on same port. Serve files from DIR.")
|
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
|
|
|
parser.add_option("--wrap-mode", default="exit", metavar="MODE",
|
|
|
|
choices=["exit", "ignore", "respawn"],
|
|
|
|
help="action to take when the wrapped program exits "
|
|
|
|
"or daemonizes: exit (default), ignore, respawn")
|
2012-06-26 12:57:50 +01:00
|
|
|
parser.add_option("--prefer-ipv6", "-6",
|
|
|
|
action="store_true", dest="source_is_ipv6",
|
|
|
|
help="prefer IPv6 when resolving source_addr")
|
2012-07-13 19:17:56 +01:00
|
|
|
parser.add_option("--target-config", metavar="FILE",
|
|
|
|
dest="target_cfg",
|
|
|
|
help="Configuration file containing valid targets "
|
|
|
|
"in the form 'token: host:port' or, alternatively, a "
|
|
|
|
"directory containing configuration files of this form")
|
2011-01-08 21:29:01 +00:00
|
|
|
(opts, args) = parser.parse_args()
|
2010-06-01 23:58:14 +01:00
|
|
|
|
2011-01-08 21:29:01 +00:00
|
|
|
# Sanity checks
|
2012-09-18 08:20:39 +01:00
|
|
|
if len(args) < 2 and not (opts.target_cfg or opts.unix_target):
|
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
|
|
|
parser.error("Too few arguments")
|
|
|
|
if sys.argv.count('--'):
|
|
|
|
opts.wrap_cmd = args[1:]
|
|
|
|
else:
|
|
|
|
opts.wrap_cmd = None
|
|
|
|
if len(args) > 2:
|
|
|
|
parser.error("Too many arguments")
|
2011-01-08 21:29:01 +00:00
|
|
|
|
2012-05-23 15:20:08 +01:00
|
|
|
if not websocket.ssl and opts.ssl_target:
|
2012-05-20 18:56:58 +01:00
|
|
|
parser.error("SSL target requested and Python SSL module not loaded.");
|
2012-09-20 15:46:04 +01:00
|
|
|
|
2011-01-08 21:29:01 +00:00
|
|
|
if opts.ssl_only and not os.path.exists(opts.cert):
|
|
|
|
parser.error("SSL only and %s not found" % opts.cert)
|
|
|
|
|
|
|
|
# Parse host:port and convert ports to numbers
|
2010-06-16 18:37:03 +01:00
|
|
|
if args[0].count(':') > 0:
|
2011-08-10 23:48:33 +01:00
|
|
|
opts.listen_host, opts.listen_port = args[0].rsplit(':', 1)
|
2012-06-26 12:55:19 +01:00
|
|
|
opts.listen_host = opts.listen_host.strip('[]')
|
2010-06-16 18:37:03 +01:00
|
|
|
else:
|
2011-01-08 21:29:01 +00:00
|
|
|
opts.listen_host, opts.listen_port = '', args[0]
|
2011-07-07 21:13:02 +01:00
|
|
|
|
2011-01-08 21:29:01 +00:00
|
|
|
try: opts.listen_port = int(opts.listen_port)
|
2010-06-01 23:58:14 +01:00
|
|
|
except: parser.error("Error parsing listen port")
|
2011-07-07 21:13:02 +01:00
|
|
|
|
2012-07-13 19:17:56 +01:00
|
|
|
if opts.wrap_cmd or opts.unix_target or opts.target_cfg:
|
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
|
|
|
opts.target_host = None
|
|
|
|
opts.target_port = None
|
|
|
|
else:
|
2012-05-31 15:17:51 +01:00
|
|
|
if args[1].count(':') > 0:
|
|
|
|
opts.target_host, opts.target_port = args[1].rsplit(':', 1)
|
2012-06-26 12:55:19 +01:00
|
|
|
opts.target_host = opts.target_host.strip('[]')
|
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
|
|
|
else:
|
2012-05-31 15:17:51 +01:00
|
|
|
parser.error("Error parsing target")
|
|
|
|
try: opts.target_port = int(opts.target_port)
|
|
|
|
except: parser.error("Error parsing target port")
|
2010-06-01 23:58:14 +01:00
|
|
|
|
2011-01-08 21:29:01 +00:00
|
|
|
# Create and start the WebSockets proxy
|
|
|
|
server = WebSocketProxy(**opts.__dict__)
|
|
|
|
server.start_server()
|
2012-02-18 08:43:12 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
websockify_init()
|