Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
strudel2_backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hpc-team
strudel2_backend
Commits
bb65eb31
Commit
bb65eb31
authored
6 years ago
by
Chris Hines
Browse files
Options
Downloads
Patches
Plain Diff
improved, fixed a shutdown bug wth teh connection was never going to authorise
parent
47f7efda
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
twsproxy/__init__.py
+24
-14
24 additions, 14 deletions
twsproxy/__init__.py
twsproxy/__main__.py
+3
-0
3 additions, 0 deletions
twsproxy/__main__.py
twsproxy/server/__init__.py
+6
-5
6 additions, 5 deletions
twsproxy/server/__init__.py
with
33 additions
and
19 deletions
twsproxy/__init__.py
+
24
−
14
View file @
bb65eb31
...
...
@@ -2,7 +2,9 @@ import threading
import
socket
,
array
import
select
import
logging
#TES = 'http://localhost:8080/'
TES
=
'
http://localhost:5000/
'
failthresh
=
10
class
TWSProxy
(
threading
.
Thread
):
TIMEOUT
=
10
...
...
@@ -30,7 +32,9 @@ class TWSProxy(threading.Thread):
bytessofar
=
0
header
=
bytearray
(
TWSProxy
.
MAXHEADERS
)
keepreading
=
True
initcount
=
0
while
keepreading
:
initcount
=
initcount
+
1
r
,
w
,
e
=
select
.
select
([
self
.
csock
],[],[],
5
)
if
len
(
r
)
>
0
:
partial
=
self
.
csock
.
recv
(
TWSProxy
.
MAXBUFF
)
...
...
@@ -42,6 +46,8 @@ class TWSProxy(threading.Thread):
else
:
port
=
TWSProxy
.
verifyauth
(
header
[
0
:
bytessofar
])
keepreading
=
False
if
initcount
>
failthresh
:
keepreading
=
False
if
port
is
not
None
:
self
.
ssock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
...
...
@@ -83,19 +89,17 @@ class TWSProxy(threading.Thread):
def
verifyauth
(
header
):
import
re
import
requests
print
(
"
in verifyauth
"
)
token
=
b
'
twsproxyauth=(?P<authtok>\w+)[\W|$]
'
m
=
re
.
search
(
token
,
header
)
if
m
:
print
(
m
.
groupdict
()[
'
authtok
'
])
authtok
=
m
.
groupdict
()[
'
authtok
'
]
s
=
requests
.
Session
()
url
=
TES
+
'
tunnelstat/
'
+
authtok
.
decode
()
print
(
url
)
r
=
s
.
get
(
url
)
print
(
r
.
text
)
port
=
r
.
json
(
)
try
:
r
=
s
.
get
(
url
)
port
=
r
.
json
()
except
:
raise
Exception
(
'
unable to get a port number for the authtok
'
)
return
port
return
None
# if m:
...
...
@@ -175,21 +179,22 @@ class TWSProxy(threading.Thread):
logger
=
logging
.
getLogger
()
closed
=
False
name
=
threading
.
current_thread
().
name
while
not
closed
:
failcount
=
0
failthresh
=
10
while
not
closed
and
failcount
<
failthresh
:
r
,
w
,
e
=
select
.
select
([
src
],[],[],
TWSProxy
.
TIMEOUT
)
if
len
(
r
)
>
0
:
failcount
=
0
buff
=
None
msglength
=
-
1
try
:
buff
=
src
.
recv
(
TWSProxy
.
MAXBUFF
)
if
buff
is
None
:
print
(
"
buff is none ... is this normal?
"
)
continue
except
ConnectionResetError
as
e
:
close
=
True
continue
except
Exception
as
e
:
print
(
e
)
import
traceback
print
(
traceback
.
format_exc
())
# closed = True
...
...
@@ -201,15 +206,20 @@ class TWSProxy(threading.Thread):
dest
.
shutdown
(
shuttype
)
initshutdown
.
set
()
closed
=
True
if
len
(
w
)
==
0
and
len
(
r
)
==
0
and
len
(
r
)
==
0
:
if
initshutdown
.
isSet
():
print
(
"
or possibly initshutdown
"
)
else
:
failcount
=
failcount
+
1
if
failcount
>
failthresh
:
dest
.
shutdown
(
shuttype
)
initshutdown
.
set
()
def
main
():
from
.
import
server
import
logging
logging
.
basicConfig
(
filename
=
"
/var/log/tws.log
"
,
format
=
"
%(asctime)s %(levelname)s:%(process)s: %(message)s
"
)
logger
=
logging
.
getLogger
()
logger
.
setLevel
(
logging
.
DEBUG
)
logger
.
debug
(
"
starting TWS proxy
"
)
server
=
server
.
TWSServer
()
logger
.
debug
(
"
initialised server object
"
)
server
.
run
()
This diff is collapsed.
Click to expand it.
twsproxy/__main__.py
+
3
−
0
View file @
bb65eb31
from
.
import
server
import
logging
logging
.
basicConfig
(
filename
=
"
/var/log/tws.log
"
,
format
=
"
%(asctime)s %(levelname)s:%(process)s: %(message)s
"
)
logger
=
logging
.
getLogger
()
logger
.
setLevel
(
logging
.
DEBUG
)
logger
.
debug
(
"
starting TWS proxy
"
)
print
(
"
starting TWS proxy
"
)
server
=
server
.
TWSServer
()
server
.
run
()
This diff is collapsed.
Click to expand it.
twsproxy/server/__init__.py
+
6
−
5
View file @
bb65eb31
import
socket
from
..
import
TWSProxy
import
logging
class
TWSServer
:
import
socket
...
...
@@ -7,6 +8,8 @@ class TWSServer:
MAXCONN
=
5
def
run
(
self
):
logger
=
logging
.
getLogger
()
logger
.
debug
(
"
starting up server
"
)
serversocket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
#bind the socket to a public host,
...
...
@@ -16,18 +19,17 @@ class TWSServer:
serversocket
.
bind
((
'
127.0.0.1
'
,
port
))
#become a server socket
serversocket
.
listen
(
self
.
MAXCONN
)
print
(
"
bind success on
"
,
port
)
logger
.
debug
(
"
Server listening on port {}
"
.
format
(
port
)
)
break
except
Exception
as
e
:
print
(
"
bind failure
"
)
print
(
e
)
pass
print
(
"
listening on port
"
,
port
)
openconnections
=
[]
logger
.
debug
(
"
waiting for a connection
"
)
while
1
:
(
clientsocket
,
address
)
=
serversocket
.
accept
()
logger
.
debug
(
"
accepted connection on {}
"
.
format
(
clientsocket
))
clientsocket
.
setblocking
(
True
)
print
(
"
accepting connection
"
)
tunnel
=
TWSProxy
(
clientsocket
)
tunnel
.
daemon
=
True
tunnel
.
start
()
...
...
@@ -36,5 +38,4 @@ class TWSServer:
if
not
c
.
is_alive
():
c
.
join
()
openconnections
.
remove
(
c
)
print
(
"
there are
"
,
len
(
openconnections
),
"
current connections
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment