Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
webssh
Overview
Overview
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
郑天保
webssh
Commits
1e4ece58
Commit
1e4ece58
authored
Aug 10, 2018
by
Sheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added functions for validating ip and port
parent
babd9bd2
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
6 deletions
+68
-6
test_utils.py
tests/test_utils.py
+30
-0
handler.py
webssh/handler.py
+9
-6
utils.py
webssh/utils.py
+29
-0
No files found.
tests/test_utils.py
0 → 100644
View file @
1e4ece58
import
unittest
from
webssh.utils
import
(
is_valid_ipv4_address
,
is_valid_ipv6_address
,
is_valid_port
,
to_str
)
class
TestUitls
(
unittest
.
TestCase
):
def
test_to_str
(
self
):
b
=
b
'hello'
u
=
u'hello'
self
.
assertEqual
(
to_str
(
b
),
u
)
self
.
assertEqual
(
to_str
(
u
),
u
)
def
test_is_valid_ipv4_address
(
self
):
self
.
assertFalse
(
is_valid_ipv4_address
(
'127.0.0'
))
self
.
assertFalse
(
is_valid_ipv4_address
(
b
'127.0.0'
))
self
.
assertTrue
(
is_valid_ipv4_address
(
'127.0.0.1'
))
self
.
assertTrue
(
is_valid_ipv4_address
(
b
'127.0.0.1'
))
def
test_is_valid_ipv6_address
(
self
):
self
.
assertFalse
(
is_valid_ipv6_address
(
'abc'
))
self
.
assertFalse
(
is_valid_ipv6_address
(
b
'abc'
))
self
.
assertTrue
(
is_valid_ipv6_address
(
'::1'
))
self
.
assertTrue
(
is_valid_ipv6_address
(
b
'::1'
))
def
test_is_valid_port
(
self
):
self
.
assertTrue
(
is_valid_port
(
80
))
self
.
assertFalse
(
is_valid_port
(
0
))
self
.
assertFalse
(
is_valid_port
(
65536
))
webssh/handler.py
View file @
1e4ece58
...
@@ -12,6 +12,8 @@ import tornado.web
...
@@ -12,6 +12,8 @@ import tornado.web
from
tornado.ioloop
import
IOLoop
from
tornado.ioloop
import
IOLoop
from
tornado.util
import
basestring_type
from
tornado.util
import
basestring_type
from
webssh.worker
import
Worker
,
recycle_worker
,
workers
from
webssh.worker
import
Worker
,
recycle_worker
,
workers
from
webssh.utils
import
(
is_valid_ipv4_address
,
is_valid_ipv6_address
,
is_valid_port
)
try
:
try
:
from
concurrent.futures
import
Future
from
concurrent.futures
import
Future
...
@@ -40,15 +42,16 @@ class MixinHandler(object):
...
@@ -40,15 +42,16 @@ class MixinHandler(object):
ip
=
self
.
request
.
headers
.
get
(
'X-Real-Ip'
)
ip
=
self
.
request
.
headers
.
get
(
'X-Real-Ip'
)
port
=
self
.
request
.
headers
.
get
(
'X-Real-Port'
)
port
=
self
.
request
.
headers
.
get
(
'X-Real-Port'
)
if
ip
is
None
and
port
is
None
:
if
ip
is
None
and
port
is
None
:
# suppose the server doesn't use nginx
return
return
if
is_valid_ipv4_address
(
ip
)
or
is_valid_ipv6_address
(
ip
):
try
:
try
:
port
=
int
(
port
)
port
=
int
(
port
)
except
(
TypeError
,
ValueError
):
except
(
TypeError
,
ValueError
):
pass
pass
else
:
else
:
if
ip
:
# does not validate ip and port here
if
is_valid_port
(
port
):
return
(
ip
,
port
)
return
(
ip
,
port
)
logging
.
warning
(
'Bad nginx configuration.'
)
logging
.
warning
(
'Bad nginx configuration.'
)
...
@@ -101,9 +104,9 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
...
@@ -101,9 +104,9 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
try
:
try
:
port
=
int
(
value
)
port
=
int
(
value
)
except
ValueError
:
except
ValueError
:
p
ort
=
0
p
ass
else
:
if
0
<
port
<
65536
:
if
is_valid_port
(
port
)
:
return
port
return
port
raise
ValueError
(
'Invalid port {}'
.
format
(
value
))
raise
ValueError
(
'Invalid port {}'
.
format
(
value
))
...
@@ -135,7 +138,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
...
@@ -135,7 +138,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
except
paramiko
.
SSHException
:
except
paramiko
.
SSHException
:
result
=
None
result
=
None
else
:
else
:
data
=
stdout
.
read
()
.
decode
()
data
=
stdout
.
read
()
.
decode
(
'utf-8'
)
result
=
parse_encoding
(
data
)
result
=
parse_encoding
(
data
)
return
result
if
result
else
'utf-8'
return
result
if
result
else
'utf-8'
...
...
webssh/utils.py
0 → 100644
View file @
1e4ece58
import
ipaddress
def
to_str
(
s
):
if
isinstance
(
s
,
bytes
):
return
s
.
decode
(
'utf-8'
)
return
s
def
is_valid_ipv4_address
(
ipstr
):
ipstr
=
to_str
(
ipstr
)
try
:
ipaddress
.
IPv4Address
(
ipstr
)
except
ipaddress
.
AddressValueError
:
return
False
return
True
def
is_valid_ipv6_address
(
ipstr
):
ipstr
=
to_str
(
ipstr
)
try
:
ipaddress
.
IPv6Address
(
ipstr
)
except
ipaddress
.
AddressValueError
:
return
False
return
True
def
is_valid_port
(
port
):
return
0
<
port
<
65536
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