Skip to content

Commit

Permalink
#2121: make code more generic and pass an actual client host
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@21837 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Feb 23, 2019
1 parent 5885761 commit 42bef37
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/xpra/net/websockets/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@ def make_websocket_accept_hash(key):
b"Sec-WebSocket-Protocol" : "binary",
}

def client_upgrade(conn):
def client_upgrade(read, write, client_host):
lines = [b"GET / HTTP/1.1"]
key = b64encode(uuid.uuid4().bytes)
headers = HEADERS.copy()
headers[b"Sec-WebSocket-Key"] = key
#FIXME: get this from connection!
headers[b"Host"] = "localhost:10000"
if client_host:
headers[b"Host"] = client_host
for k,v in headers.items():
lines.append(b"%s: %s" % (k, v))
lines.append(b"")
lines.append(b"")
http_request = b"\r\n".join(lines)
log("client_upgrade(%s) sending http headers: %s", conn, headers)
log("client_upgrade: sending http headers: %s", headers)
now = monotonic_time()
MAX_WRITE_TIME = 5
while http_request and monotonic_time()-now<MAX_WRITE_TIME:
w = conn.write(http_request)
w = write(http_request)
http_request = http_request[w:]

now = monotonic_time()
MAX_READ_TIME = 5
response = b""
while response.find("Sec-WebSocket-Protocol")<0 and monotonic_time()-now<MAX_READ_TIME:
response += conn.read(4096)
response += read(4096)
headers = parse_response_header(response)
verify_response_headers(headers, key)
log("client_upgrade(%s) done", conn)
log("client_upgrade: done")

def parse_response_header(response):
#parse response:
Expand Down
6 changes: 5 additions & 1 deletion src/xpra/scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,11 @@ def sockpathfail_cb(msg):
except ImportError as e:
raise InitExit(EXIT_UNSUPPORTED, "cannot handle websocket connection: %s" % e)
else:
client_upgrade(conn)
try:
client_host = ":".join(str(x) for x in conn.local[:2])
except (ValueError, IndexError):
client_host = None
client_upgrade(conn.read, conn.write, client_host)
return conn
raise InitException("unsupported display type: %s" % dtype)

Expand Down

0 comments on commit 42bef37

Please sign in to comment.