mirror of https://github.com/huashengdun/webssh
Added connect_with_options function
parent
ee7fd101c6
commit
4710d8c0e6
|
@ -213,12 +213,13 @@ class TestApp(AsyncHTTPTestCase):
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': content_type, 'content-length': str(len(body))
|
'Content-Type': content_type, 'content-length': str(len(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
response = yield client.fetch(url, method='POST', headers=headers,
|
response = yield client.fetch(url, method='POST', headers=headers,
|
||||||
body=body)
|
body=body)
|
||||||
data = json.loads(to_str(response.body))
|
data = json.loads(to_str(response.body))
|
||||||
self.assertIsNone(data['id'])
|
self.assertIsNone(data['id'])
|
||||||
self.assertIsNone(data['encoding'])
|
self.assertIsNone(data['encoding'])
|
||||||
self.assertTrue(data['status'].startswith('Invalid private key'))
|
self.assertIn('Bad Request (Invalid unicode', data['status'])
|
||||||
|
|
||||||
@tornado.testing.gen_test
|
@tornado.testing.gen_test
|
||||||
def test_app_post_form_with_large_body_size(self):
|
def test_app_post_form_with_large_body_size(self):
|
||||||
|
|
|
@ -66,21 +66,23 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
|
||||||
self.loop = loop
|
self.loop = loop
|
||||||
self.policy = policy
|
self.policy = policy
|
||||||
self.host_keys_settings = host_keys_settings
|
self.host_keys_settings = host_keys_settings
|
||||||
|
self.filename = None
|
||||||
|
|
||||||
def get_privatekey(self):
|
def get_privatekey(self):
|
||||||
lst = self.request.files.get('privatekey')
|
lst = self.request.files.get('privatekey') # multipart form
|
||||||
if not lst: # no privatekey provided
|
if not lst:
|
||||||
return
|
|
||||||
|
|
||||||
self.filename = lst[0]['filename']
|
|
||||||
data = lst[0]['body']
|
|
||||||
if len(data) < KEY_MAX_SIZE:
|
|
||||||
try:
|
try:
|
||||||
return to_str(data)
|
return self.get_argument('privatekey') # urlencoded form
|
||||||
except (UnicodeDecodeError, ValueError, SyntaxError):
|
except tornado.web.MissingArgumentError:
|
||||||
pass
|
pass
|
||||||
|
else:
|
||||||
raise ValueError('Invalid private key: {}'.format(self.filename))
|
self.filename = lst[0]['filename']
|
||||||
|
data = lst[0]['body']
|
||||||
|
if len(data) > KEY_MAX_SIZE:
|
||||||
|
raise ValueError(
|
||||||
|
'Invalid private key: {}'.format(self.filename)
|
||||||
|
)
|
||||||
|
return self.decode_argument(data, name=self.filename)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_specific_pkey(cls, pkeycls, privatekey, password):
|
def get_specific_pkey(cls, pkeycls, privatekey, password):
|
||||||
|
@ -119,7 +121,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
|
||||||
value = self.get_value('hostname')
|
value = self.get_value('hostname')
|
||||||
if not (is_valid_hostname(value) | is_valid_ipv4_address(value) |
|
if not (is_valid_hostname(value) | is_valid_ipv4_address(value) |
|
||||||
is_valid_ipv6_address(value)):
|
is_valid_ipv6_address(value)):
|
||||||
raise ValueError('Invalid hostname: {}.'.format(value))
|
raise ValueError('Invalid hostname: {}'.format(value))
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def get_port(self):
|
def get_port(self):
|
||||||
|
@ -132,7 +134,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
|
||||||
if is_valid_port(port):
|
if is_valid_port(port):
|
||||||
return port
|
return port
|
||||||
|
|
||||||
raise ValueError('Invalid port: {}.'.format(value))
|
raise ValueError('Invalid port: {}'.format(value))
|
||||||
|
|
||||||
def get_value(self, name):
|
def get_value(self, name):
|
||||||
value = self.get_argument(name)
|
value = self.get_argument(name)
|
||||||
|
|
|
@ -105,7 +105,7 @@ jQuery(function($){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function callback(resp) {
|
function ajax_complete_callback(resp) {
|
||||||
btn.prop('disabled', false);
|
btn.prop('disabled', false);
|
||||||
|
|
||||||
if (resp.status !== 200) {
|
if (resp.status !== 200) {
|
||||||
|
@ -116,6 +116,7 @@ jQuery(function($){
|
||||||
|
|
||||||
var msg = resp.responseJSON;
|
var msg = resp.responseJSON;
|
||||||
if (msg.status) {
|
if (msg.status) {
|
||||||
|
console.log(msg);
|
||||||
status.text(msg.status);
|
status.text(msg.status);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -275,7 +276,7 @@ jQuery(function($){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function connect() {
|
function connect_without_options() {
|
||||||
if (connected) {
|
if (connected) {
|
||||||
console.log('This client was already connected.');
|
console.log('This client was already connected.');
|
||||||
return;
|
return;
|
||||||
|
@ -298,7 +299,7 @@ jQuery(function($){
|
||||||
url: url,
|
url: url,
|
||||||
type: 'post',
|
type: 'post',
|
||||||
data: data,
|
data: data,
|
||||||
complete: callback,
|
complete: ajax_complete_callback,
|
||||||
cache: false,
|
cache: false,
|
||||||
contentType: false,
|
contentType: false,
|
||||||
processData: false
|
processData: false
|
||||||
|
@ -323,11 +324,11 @@ jQuery(function($){
|
||||||
var pk = data.get('privatekey');
|
var pk = data.get('privatekey');
|
||||||
if (pk) {
|
if (pk) {
|
||||||
if (pk.size > key_max_size) {
|
if (pk.size > key_max_size) {
|
||||||
status.text('Invalid private key: ' + pk.name);
|
console.log('Invalid private key: ' + pk.name);
|
||||||
} else {
|
} else {
|
||||||
read_file_as_text(pk, function(text) {
|
read_file_as_text(pk, function(text) {
|
||||||
if (text === undefined) {
|
if (text === undefined) {
|
||||||
status.text('Invalid private key: ' + pk.name);
|
console.log('Invalid private key: ' + pk.name);
|
||||||
} else {
|
} else {
|
||||||
ajax_post();
|
ajax_post();
|
||||||
}
|
}
|
||||||
|
@ -338,11 +339,71 @@ jQuery(function($){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wssh.connect = connect;
|
|
||||||
|
function connect_with_options(opts) {
|
||||||
|
if (connected) {
|
||||||
|
console.log('This client was already connected.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var form = document.querySelector(form_id),
|
||||||
|
xsrf = form.querySelector('input[name="_xsrf"]').value,
|
||||||
|
url = opts.url || form.action,
|
||||||
|
hostname = opts.hostname || '',
|
||||||
|
port = opts.port || '',
|
||||||
|
username = opts.username || '',
|
||||||
|
password = opts.password || '',
|
||||||
|
privatekey = opts.privatekey || '',
|
||||||
|
data = {
|
||||||
|
'_xsrf': xsrf,
|
||||||
|
'username': username,
|
||||||
|
'hostname': hostname,
|
||||||
|
'port': port,
|
||||||
|
'password': password,
|
||||||
|
'privatekey': privatekey
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
if (!hostname || !port || !username) {
|
||||||
|
console.log('Fields hostname, port and username are all required.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hostname_tester.test(hostname)) {
|
||||||
|
console.log('Invalid hostname: ' + hostname);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (port <= 0 || port > 63335) {
|
||||||
|
console.log('Invalid port: ' + port);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (privatekey && privatekey.length > key_max_size) {
|
||||||
|
console.log('Invalid private key: ' + privatekey);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: url,
|
||||||
|
type: 'post',
|
||||||
|
data: data,
|
||||||
|
complete: ajax_complete_callback
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
wssh.connect = function(opts) {
|
||||||
|
if (opts === undefined) {
|
||||||
|
connect_without_options();
|
||||||
|
} else {
|
||||||
|
connect_with_options(opts);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
$(form_id).submit(function(event){
|
$(form_id).submit(function(event){
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
connect();
|
connect_without_options();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue