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.
This commit is contained in:
+65
-23
@@ -11,7 +11,7 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates
|
||||
|
||||
'''
|
||||
|
||||
import sys, socket, ssl, struct, traceback
|
||||
import sys, socket, ssl, struct, traceback, select
|
||||
import os, resource, errno, signal # daemonizing
|
||||
from SimpleHTTPServer import SimpleHTTPRequestHandler
|
||||
from cStringIO import StringIO
|
||||
@@ -26,7 +26,7 @@ from cgi import parse_qsl
|
||||
class WebSocketServer():
|
||||
"""
|
||||
WebSockets server class.
|
||||
Must be sub-classed with handler method definition.
|
||||
Must be sub-classed with new_client method definition.
|
||||
"""
|
||||
|
||||
server_handshake = """HTTP/1.1 101 Web Socket Protocol Handshake\r
|
||||
@@ -70,6 +70,21 @@ Connection: Upgrade\r
|
||||
|
||||
self.handler_id = 1
|
||||
|
||||
print "WebSocket server settings:"
|
||||
print " - Listen on %s:%s" % (
|
||||
self.listen_host, self.listen_port)
|
||||
print " - Flash security policy server"
|
||||
if self.web:
|
||||
print " - Web server"
|
||||
if os.path.exists(self.cert):
|
||||
print " - SSL/TLS support"
|
||||
if self.ssl_only:
|
||||
print " - Deny non-SSL/TLS connections"
|
||||
else:
|
||||
print " - No SSL/TLS support (no cert file)"
|
||||
if self.daemon:
|
||||
print " - Backgrounding (daemon)"
|
||||
|
||||
#
|
||||
# WebSocketServer static methods
|
||||
#
|
||||
@@ -284,16 +299,34 @@ Connection: Upgrade\r
|
||||
return retsock
|
||||
|
||||
|
||||
def handler(self, client):
|
||||
#
|
||||
# Events that can/should be overridden in sub-classes
|
||||
#
|
||||
def started(self):
|
||||
""" Called after WebSockets startup """
|
||||
self.vmsg("WebSockets server started")
|
||||
|
||||
def poll(self):
|
||||
""" Run periodically while waiting for connections. """
|
||||
self.msg("Running poll()")
|
||||
|
||||
def do_SIGCHLD(self, sig, stack):
|
||||
self.vmsg("Got SIGCHLD, ignoring")
|
||||
|
||||
def do_SIGINT(self, sig, stack):
|
||||
self.msg("Got SIGINT, exiting")
|
||||
sys.exit(0)
|
||||
|
||||
def new_client(self, client):
|
||||
""" Do something with a WebSockets client connection. """
|
||||
raise("WebSocketServer.handler() must be overloaded")
|
||||
raise("WebSocketServer.new_client() must be overloaded")
|
||||
|
||||
def start_server(self):
|
||||
"""
|
||||
Daemonize if requested. Listen for for connections. Run
|
||||
do_handshake() method for each connection. If the connection
|
||||
is a WebSockets client then call handler() method (which must
|
||||
be overridden) for each connection.
|
||||
is a WebSockets client then call new_client() method (which must
|
||||
be overridden) for each new client connection.
|
||||
"""
|
||||
|
||||
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
@@ -301,37 +334,46 @@ Connection: Upgrade\r
|
||||
lsock.bind((self.listen_host, self.listen_port))
|
||||
lsock.listen(100)
|
||||
|
||||
print "WebSocket server settings:"
|
||||
print " - Listening on %s:%s" % (
|
||||
self.listen_host, self.listen_port)
|
||||
if self.daemon:
|
||||
print " - Backgrounding (daemon)"
|
||||
print " - Flash security policy server"
|
||||
if self.web:
|
||||
print " - Web server"
|
||||
if os.path.exists(self.cert):
|
||||
print " - SSL/TLS support"
|
||||
if self.ssl_only:
|
||||
print " - Deny non-SSL/TLS connections"
|
||||
|
||||
if self.daemon:
|
||||
self.daemonize(self, keepfd=lsock.fileno())
|
||||
|
||||
self.started() # Some things need to happen after daemonizing
|
||||
|
||||
# Reep zombies
|
||||
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
|
||||
signal.signal(signal.SIGCHLD, self.do_SIGCHLD)
|
||||
signal.signal(signal.SIGINT, self.do_SIGINT)
|
||||
|
||||
while True:
|
||||
try:
|
||||
csock = startsock = None
|
||||
pid = 0
|
||||
startsock, address = lsock.accept()
|
||||
pid = err = 0
|
||||
|
||||
try:
|
||||
self.poll()
|
||||
|
||||
ready = select.select([lsock], [], [], 1)[0];
|
||||
if lsock in ready:
|
||||
startsock, address = lsock.accept()
|
||||
else:
|
||||
continue
|
||||
except Exception, exc:
|
||||
if hasattr(exc, 'errno'):
|
||||
err = exc.errno
|
||||
elif type(exc) == select.error:
|
||||
err = exc[0]
|
||||
if err == errno.EINTR:
|
||||
self.vmsg("Ignoring interrupted syscall()")
|
||||
continue
|
||||
else:
|
||||
raise
|
||||
|
||||
self.vmsg('%s: forking handler' % address[0])
|
||||
pid = os.fork()
|
||||
|
||||
if pid == 0:
|
||||
# handler process
|
||||
csock = self.do_handshake(startsock, address)
|
||||
self.handler(csock)
|
||||
self.new_client(csock)
|
||||
else:
|
||||
# parent process
|
||||
self.handler_id += 1
|
||||
|
||||
Reference in New Issue
Block a user