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
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
75 additions
and
13 deletions
+75
-13
test_utils.py
tests/test_utils.py
+30
-0
handler.py
webssh/handler.py
+16
-13
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
from
tornado.ioloop
import
IOLoop
from
tornado.util
import
basestring_type
from
webssh.worker
import
Worker
,
recycle_worker
,
workers
from
webssh.utils
import
(
is_valid_ipv4_address
,
is_valid_ipv6_address
,
is_valid_port
)
try
:
from
concurrent.futures
import
Future
...
...
@@ -40,16 +42,17 @@ class MixinHandler(object):
ip
=
self
.
request
.
headers
.
get
(
'X-Real-Ip'
)
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
try
:
port
=
int
(
port
)
except
(
TypeError
,
ValueError
):
pass
else
:
if
ip
:
# does not validate ip and port here
return
(
ip
,
port
)
if
is_valid_ipv4_address
(
ip
)
or
is_valid_ipv6_address
(
ip
):
try
:
port
=
int
(
port
)
except
(
TypeError
,
ValueError
):
pass
else
:
if
is_valid_port
(
port
):
return
(
ip
,
port
)
logging
.
warning
(
'Bad nginx configuration.'
)
return
False
...
...
@@ -101,10 +104,10 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
try
:
port
=
int
(
value
)
except
ValueError
:
p
ort
=
0
if
0
<
port
<
65536
:
return
port
p
ass
else
:
if
is_valid_port
(
port
)
:
return
port
raise
ValueError
(
'Invalid port {}'
.
format
(
value
))
...
...
@@ -135,7 +138,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
except
paramiko
.
SSHException
:
result
=
None
else
:
data
=
stdout
.
read
()
.
decode
()
data
=
stdout
.
read
()
.
decode
(
'utf-8'
)
result
=
parse_encoding
(
data
)
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