Commit f4353af9 by Szeberényi Imre

Merge branch 'vncproxy36jo' into 'master'

Vncproxy36jo

See merge request !1
parents ac315429 b14033ba
......@@ -4,6 +4,7 @@
from os import environ
from django.conf import settings
from twisted.internet import defer
from twisted.internet import protocol
from twisted.internet.protocol import Factory, Protocol
......@@ -77,9 +78,12 @@ class ProxyClientFactory(protocol.ReconnectingClientFactory):
class VNCWebSocketHandler(Protocol):
src = None
def makeConnection(self, transport):
try:
value = signing.loads(transport.request.args['d'][0],
if not settings.configured:
settings.configure()
value = signing.loads(transport.request.args[b'd'][0].decode(),
key=PROXY_SECRET, max_age=KEY_MAX_AGE)
try:
self.src = transport.request.requestHeaders.getRawHeaders(
......@@ -91,7 +95,7 @@ class VNCWebSocketHandler(Protocol):
except Exception as e:
src = getattr(self, 'src', None)
logger.warning('Server(%s): bad connection, key=%s err=%s',
src, transport.request.args['d'][0], e)
src, transport.request.args[b'd'][0], e)
transport.loseConnection()
return
logger.info("Server(%s): new connection, host=%s, port=%s",
......@@ -134,7 +138,7 @@ if __name__ == "__main__":
opts, args = parser.parse_args()
logging.basicConfig(level=opts.loglevel)
resource = File('.')
resource.putChild('vnc', WebSocketsResource(
resource.putChild(b'vnc', WebSocketsResource(
lookupProtocolForFactory(VNCWebSocketFactory())))
reactor.listenTCP(9999, Site(resource))
reactor.run()
cryptography==2.0
Django==1.5.4
Twisted==13.1.0
pyOpenSSL==18.0.0
cryptography==36.0.1
Django==3.2.3
Twisted==22.4.0
pyOpenSSL==21.0.0
service-identity
......@@ -12,8 +12,9 @@ factory.
@since: 13.2
"""
import six
import base64
import struct
from hashlib import sha1
from struct import pack, unpack
......@@ -88,7 +89,7 @@ def _makeAccept(key):
@rtype: C{str}
@return: An encoded response.
"""
return sha1("%s%s" % (key, _WS_GUID)).digest().encode("base64").strip()
return base64.b64encode(sha1((key + _WS_GUID).encode("ascii")).digest()).decode("ascii")
def _mask(buf, key):
......@@ -104,11 +105,11 @@ def _mask(buf, key):
@rtype: C{str}
@return: A masked buffer of bytes.
"""
key = [ord(i) for i in key]
key = [i for i in key]
buf = list(buf)
for i, char in enumerate(buf):
buf[i] = chr(ord(char) ^ key[i % 4])
return "".join(buf)
buf[i] = chr(char ^ key[i % 4])
return six.b("".join(buf))
def _makeFrame(buf, opcode, fin, mask=None):
......@@ -129,7 +130,6 @@ def _makeFrame(buf, opcode, fin, mask=None):
@type mask: C{int} or C{NoneType}
@param mask: If specified, the masking key to apply on the created frame.
@rtype: C{str}
@return: A packed frame.
"""
......@@ -140,11 +140,11 @@ def _makeFrame(buf, opcode, fin, mask=None):
lengthMask = 0
if bufferLength > 0xffff:
length = "%s%s" % (chr(lengthMask | 0x7f), pack(">Q", bufferLength))
length = bytes(chr(lengthMask | 0x7f), 'UTF-8') + pack(">Q", bufferLength)
elif bufferLength > 0x7d:
length = "%s%s" % (chr(lengthMask | 0x7e), pack(">H", bufferLength))
length = bytes(chr(lengthMask | 0x7e), 'UTF-8') + pack(">H", bufferLength)
else:
length = chr(lengthMask | bufferLength)
length = bytes( chr(lengthMask | bufferLength), 'UTF-8')
if fin:
header = 0x80
......@@ -154,7 +154,7 @@ def _makeFrame(buf, opcode, fin, mask=None):
header = chr(header | opcode.value)
if mask is not None:
buf = "%s%s" % (mask, _mask(buf, mask))
frame = "%s%s%s" % (header, length, buf)
frame = bytes( header, 'raw_unicode_escape' ).strip() + length + buf
return frame
......@@ -164,13 +164,13 @@ def _parseFrames(frameBuffer, needMask=True):
removing the parsed content from it.
@param frameBuffer: A buffer of bytes.
@type frameBuffer: C{list}
@type frameBuffer: C{list} --- NO C(bytes) !!!!!
@param needMask: If C{True}, refuse any frame which is not masked.
@type needMask: C{bool}
"""
start = 0
payload = "".join(frameBuffer)
payload = b"".join(frameBuffer)
while True:
# If there's not at least two bytes in the buffer, bail.
......@@ -178,7 +178,7 @@ def _parseFrames(frameBuffer, needMask=True):
break
# Grab the header. This single byte holds some flags and an opcode
header = ord(payload[start])
header = payload[start]
if header & 0x70:
# At least one of the reserved flags is set. Pork chop sandwiches!
raise _WSException("Reserved flag in frame (%d)" % (header,))
......@@ -195,7 +195,7 @@ def _parseFrames(frameBuffer, needMask=True):
# Get the payload length and determine whether we need to look for an
# extra length.
length = ord(payload[start + 1])
length = payload[start + 1]
masked = length & 0x80
if not masked and needMask:
......@@ -343,9 +343,13 @@ class WebSocketsTransport(object):
# Send a closing frame. It's only polite. (And might keep the browser
# from hanging.)
if not self._disconnecting:
data = "%s%s" % (pack(">H", code.value), reason)
frame = _makeFrame(data, CONTROLS.CLOSE, True)
self._transport.write(frame)
msg = b''
if code is not None:
msg += struct.pack(">H", code.value )
if reason is not None:
msg += reason.encode("UTF-8")
self._transport.write(msg)
self._disconnecting = True
self._transport.loseConnection()
......@@ -462,7 +466,7 @@ class _WebSocketsProtocolWrapperReceiver():
return
self._messages.append(data)
if fin:
content = "".join(self._messages)
content = b"".join(self._messages)
self._messages[:] = []
self._wrappedProtocol.dataReceived(content)
......@@ -618,7 +622,7 @@ class WebSocketsResource(object):
# If we fail at all, we'll fail with 400 and no response.
failed = False
if request.method != "GET":
if request.method != b"GET":
# 4.2.1.1 GET is required.
failed = True
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment