Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
N
noVNC
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rasky
noVNC
Commits
ce0e28c7
Commit
ce0e28c7
authored
Apr 01, 2010
by
Joel Martin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some wsproxy simplifications.
parent
3292c4a9
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
53 deletions
+49
-53
wsproxy.py
wsproxy.py
+49
-53
No files found.
wsproxy.py
View file @
ce0e28c7
#!/usr/bin/python
#!/usr/bin/python
import
sys
,
os
,
socket
,
time
import
sys
,
os
,
socket
,
time
,
traceback
from
select
import
select
from
select
import
select
server_handshake
=
"""HTTP/1.1 101 Web Socket Protocol Handshake
\r
server_handshake
=
"""HTTP/1.1 101 Web Socket Protocol Handshake
\r
...
@@ -12,72 +12,68 @@ WebSocket-Protocol: sample\r
...
@@ -12,72 +12,68 @@ WebSocket-Protocol: sample\r
\r
\r
"""
"""
cqueue
=
[]
def
handshake
(
client
):
tqueue
=
[]
handshake
=
client
.
recv
(
255
)
req_lines
=
handshake
.
split
(
"
\r\n
"
)
def
start_proxy
(
listen_port
,
target_host
,
target_port
):
_
,
path
,
_
=
req_lines
[
0
]
.
split
(
" "
)
lsock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
_
,
origin
=
req_lines
[
4
]
.
split
(
" "
)
lsock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
_
,
host
=
req_lines
[
3
]
.
split
(
" "
)
lsock
.
bind
((
''
,
listen_port
))
client
.
send
(
server_handshake
%
(
origin
,
host
,
path
))
lsock
.
listen
(
100
)
while
True
:
try
:
print
'listening on port
%
s'
%
listen_port
csock
,
address
=
lsock
.
accept
()
print
'Got client connection'
handshake
(
csock
)
print
"Handshake complete"
print
"Connecting to:
%
s:
%
s"
%
(
target_host
,
target_port
)
tsock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
tsock
.
connect
((
target_host
,
target_port
))
socks
=
[
csock
,
tsock
]
def
proxy
(
client
,
target
):
cqueue
=
[]
tqueue
=
[]
socks
=
[
client
,
target
]
while
True
:
while
True
:
ins
,
outs
,
excepts
=
select
(
socks
,
socks
,
socks
,
1
)
ins
,
outs
,
excepts
=
select
(
socks
,
socks
,
socks
,
1
)
if
excepts
:
raise
Exception
(
"Socket exception"
)
if
excepts
:
raise
Exception
(
"Socket exception"
)
if
csock
in
ins
:
if
client
in
ins
:
buf
=
csock
.
recv
(
1024
)
buf
=
client
.
recv
(
1024
)
if
len
(
buf
)
==
0
:
if
len
(
buf
)
==
0
:
raise
Exception
(
"Client closed"
)
csock
.
close
()
tsock
.
close
()
raise
Exception
(
"Client closed"
)
tqueue
.
append
(
buf
[
1
:
-
1
])
tqueue
.
append
(
buf
[
1
:
-
1
])
print
"Client recv:
%
s (
%
d)"
%
(
buf
[
1
:
-
1
],
len
(
buf
))
print
"Client recv:
%
s (
%
d)"
%
(
buf
[
1
:
-
1
],
len
(
buf
))
if
tsock
in
ins
:
if
target
in
ins
:
buf
=
tsock
.
recv
(
1024
)
buf
=
target
.
recv
(
1024
)
if
len
(
buf
)
==
0
:
if
len
(
buf
)
==
0
:
raise
Exception
(
"Target closed"
)
csock
.
close
()
cqueue
.
append
(
"
\x00
"
+
buf
+
"
\xff
"
)
tsock
.
close
()
print
"Target recv:
%
s (
%
d)"
%
(
buf
,
len
(
buf
))
raise
Exception
(
"Target closed"
)
cqueue
.
append
(
buf
)
print
"Target recv:
%
s (
%
d)"
%
(
buf
[
1
:
-
1
],
len
(
buf
))
if
cqueue
and
csock
in
outs
:
if
cqueue
and
client
in
outs
:
while
cqueue
:
while
cqueue
:
print
"Client send:
%
s"
%
"
\x00
"
+
cqueue
[
0
]
+
"
\xff
"
print
"Client send:
%
s"
%
cqueue
[
0
]
csock
.
send
(
"
\x00
"
+
cqueue
.
pop
(
0
)
+
"
\xff
"
)
client
.
send
(
cqueue
.
pop
(
0
)
)
if
tqueue
and
tsock
in
outs
:
if
tqueue
and
target
in
outs
:
while
tqueue
:
while
tqueue
:
print
"Target send:
%
s"
%
tqueue
[
0
]
print
"Target send:
%
s"
%
tqueue
[
0
]
tsock
.
send
(
tqueue
.
pop
(
0
))
target
.
send
(
tqueue
.
pop
(
0
))
except
Exception
,
e
:
def
start_server
(
listen_port
,
target_host
,
target_port
):
lsock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
lsock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
lsock
.
bind
((
''
,
listen_port
))
lsock
.
listen
(
100
)
while
True
:
try
:
csock
=
tsock
=
None
csock
=
tsock
=
None
print
"Ignoring exception:"
,
e
print
'listening on port
%
s'
%
listen_port
csock
,
address
=
lsock
.
accept
()
print
'Got client connection from
%
s'
%
address
[
0
]
handshake
(
csock
)
print
"Connecting to:
%
s:
%
s"
%
(
target_host
,
target_port
)
tsock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
tsock
.
connect
((
target_host
,
target_port
))
def
handshake
(
client
):
proxy
(
csock
,
tsock
)
handshake
=
client
.
recv
(
255
)
req_lines
=
handshake
.
split
(
"
\r\n
"
)
except
Exception
:
_
,
path
,
_
=
req_lines
[
0
]
.
split
(
" "
)
print
"Ignoring exception:"
_
,
origin
=
req_lines
[
4
]
.
split
(
" "
)
print
traceback
.
format_exc
()
_
,
host
=
req_lines
[
3
]
.
split
(
" "
)
if
csock
:
csock
.
close
()
#print "*** got handshake:\n%s" % handshake
if
tsock
:
tsock
.
close
()
print
"*** client origin:
%
s, location: ws://
%
s
%
s"
%
(
origin
,
host
,
path
)
client
.
send
(
server_handshake
%
(
origin
,
host
,
path
))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
try
:
try
:
...
@@ -88,4 +84,4 @@ if __name__ == '__main__':
...
@@ -88,4 +84,4 @@ if __name__ == '__main__':
except
:
except
:
print
"Usage: <listen_port> <target_host> <target_port>"
print
"Usage: <listen_port> <target_host> <target_port>"
sys
.
exit
(
1
)
sys
.
exit
(
1
)
start_
proxy
(
listen_port
,
target_host
,
target_port
)
start_
server
(
listen_port
,
target_host
,
target_port
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment