Skip to content

Commit

Permalink
#1271: support for websocket connections from the python client
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@13558 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Sep 4, 2016
1 parent c5383d4 commit cdf9c87
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion src/xpra/scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ def parse_host_string(host):
username, password, host, port = parse_host_string(host)
assert port>0, "no port specified in %s" % host
return desc
elif display_name.startswith("vsock:"):
elif display_name.startswith("vsock:") or display_name.startswith("vsock/"):
#use the vsock specified:
vsock_str = display_name[len("vsock:"):]
cid, iport = parse_vsock(vsock_str)
Expand All @@ -1369,6 +1369,37 @@ def parse_host_string(host):
})
opts.display = display_name
return desc
elif display_name.startswith("ws:") or display_name.startswith("wss:") or display_name.startswith("ws/") or display_name.startswith("wss/"):
if display_name.startswith("wss"):
separator = display_name[3] # ":" or "/"
else:
assert display_name.startswith("ws")
separator = display_name[2] # ":" or "/"
try:
import websocket
assert websocket
except ImportError as e:
raise InitException("the websocket client module cannot be loaded: %s" % e)
ws_proto, host = display_name.split(separator, 1)
if host.find("?")>=0:
host, _ = host.split("?", 1)
iport = 0
if host.find(":")>=0:
host, port = host.split(":", 1)
if port.find("/")>=0:
port, extra = port.split("/", 1)
host += extra
iport = int(port)
#TODO: parse attrs after "?"
desc.update({
"type" : ws_proto, #"ws" or "wss"
"local" : False,
"display" : display_name,
"host" : host,
})
if iport>0:
desc["port"] = iport
return desc
elif sys.platform.startswith("win") or display_name.startswith("named-pipe:"):
pipe_name = display_name
if display_name.startswith("named-pipe:"):
Expand Down Expand Up @@ -1642,6 +1673,50 @@ def sockpathfail_cb(msg):
from xpra.net.bytestreams import SocketConnection
return SocketConnection(sock, "local", "host", (CID_TYPES.get(cid, cid), iport), dtype)

elif dtype in ("ws", "wss"):
host = display_desc["host"]
port = display_desc.get("port", 0)
if port>0:
host += ":%i" % port
import websocket
#websocket.enableTrace(True)
url = "%s://%s/" % (dtype, host)
ws = websocket.create_connection(url, SOCKET_TIMEOUT, subprotocols=["binary", "base64"])
from xpra.net.bytestreams import Connection, log as connlog
class WebSocketClientConnection(Connection):
def __init__(self, ws, target, socktype):
Connection.__init__(self, target, socktype)
self._socket = ws

def peek(self, n):
return None

def read(self, n):
return self._read(self._socket.recv)

def write(self, buf):
return self._write(self._socket.send, buf)

def close(self):
try:
i = self.get_socket_info()
except:
i = self._socket
connlog("%s.close() for socket=%s", self, i)
Connection.close(self)
self._socket.close()
self._socket = None
connlog("%s.close() done", self)

def __repr__(self):
return "%s %s" % (self.socktype, self.target)

def get_info(self):
d = Connection.get_info(self)
d["protocol-type"] = "websocket"
return d
return WebSocketClientConnection(ws, "websocket", host)

elif dtype in ("tcp", "ssl"):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(SOCKET_TIMEOUT)
Expand Down

0 comments on commit cdf9c87

Please sign in to comment.