Skip to content

[ISSUE #18] Add custom header support for invoke_shell() #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
520707a
Fix event listener loss on terminal reset.
uakfdotb Jul 4, 2015
60032df
bugfix
sunyi00 Sep 7, 2015
7b33b14
bugfix: json dumps crashes when input unicode
sunyi00 Sep 7, 2015
c6e87a4
bump version to 0.1.1
sunyi00 Sep 7, 2015
78e3c3a
update gitignore
sunyi00 Sep 7, 2015
2d3d170
Add custom header param to invoke_shell()
Nov 19, 2015
d1f3fe7
Update setup.py
Nov 19, 2015
58c444b
Update .gitignore
Nov 20, 2015
2d036bc
Add header support
Nov 23, 2015
0b636c1
Disable ssl cert verification
Nov 23, 2015
3d1d1f4
Fix an import error
Nov 23, 2015
6069700
Merge remote-tracking branch 'sunyi/master'
Nov 23, 2015
c37fa6a
Update .gitignore
Nov 23, 2015
3e8cd6e
Revert "Fix an import error"
Nov 23, 2015
c297054
Revert "Revert "Fix an import error""
Nov 23, 2015
bda2ed9
Revert "Fix an import error"
Nov 23, 2015
c4d188e
Revert
Nov 23, 2015
ba44435
Remove no-use import
Nov 23, 2015
410c9f8
Fix a bug of Chinese chars input
Nov 24, 2015
380c8b9
Add a missed import
Nov 24, 2015
fd4b86e
Fix a bug of signature of functions
Nov 24, 2015
5c0abf2
Update for test
Nov 25, 2015
e9e2f07
Reverse modify
Nov 25, 2015
abd5b48
Update version to 0.2
Nov 26, 2015
84938d5
Merge branch '0.2' into 'master'
Nov 26, 2015
314eea8
Update client to adjust error output
Nov 26, 2015
f633acd
For debug
Nov 27, 2015
b60f2cc
Add simulate `exit` command. Cann't do much
Nov 27, 2015
d4d0959
Fix missed import
Nov 27, 2015
7adf7a5
Add close command in outbound failed
Nov 27, 2015
0fe1f7d
Merge remote-tracking branch 'lain/master' into fix_bugs
Apr 25, 2016
5ad6625
Merge pull request #1 from ericpai/fix_bugs
ericpai Apr 25, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
*.py[co]
*.egg
*.egg-info
*.tmp
dist
build
eggs
*.DS_Store
2 changes: 1 addition & 1 deletion examples/flask_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def index():

if __name__ == '__main__':
from gevent.pywsgi import WSGIServer
from geventwebsocket import WebSocketHandler
from geventwebsocket.handler import WebSocketHandler

app.debug = True
http_server = WSGIServer(('localhost', 5000), app,
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

setup(
name='wssh',
version='0.1.0',
author='Andrea Luzzardi <aluzzardi@gmail.com>',
version='0.2.1',
author='EricPai <ericpai94@hotmail.com>',
packages=[
'wssh'
],
Expand Down
2 changes: 1 addition & 1 deletion wssh/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from server import WSSHBridge

__version__ = '0.1.0'
__version__ = '0.2.1'
51 changes: 27 additions & 24 deletions wssh/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import codecs
import sys
import signal
import errno
Expand All @@ -10,68 +11,69 @@
import tty
import fcntl
import struct

import platform
import ssl

try:
import simplejson as json
except ImportError:
import json


class ConnectionError(Exception):
pass


def _pty_size():
def _pty_size(utf_in, utf_out):
rows, cols = 24, 80
# Can't do much for Windows
if platform.system() == 'Windows':
return rows, cols
fmt = 'HH'
buffer = struct.pack(fmt, 0, 0)
result = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ,
result = fcntl.ioctl(utf_out.fileno(), termios.TIOCGWINSZ,
buffer)
rows, cols = struct.unpack(fmt, result)
return rows, cols


def _resize(ws):
rows, cols = _pty_size()
def _resize(ws, utf_in, utf_out):
rows, cols = _pty_size(utf_in, utf_out)
ws.send(json.dumps({'resize': {'width': cols, 'height': rows}}))


def invoke_shell(endpoint):
ssh = websocket.create_connection(endpoint)
_resize(ssh)
oldtty = termios.tcgetattr(sys.stdin)
def invoke_shell(endpoint, header = None):
UTF8Reader = codecs.getreader('utf8')
utf_in = UTF8Reader(sys.stdin)
UTF8Writer = codecs.getwriter('utf8')
utf_out = UTF8Writer(sys.stdout)
ssh = websocket.create_connection(url = endpoint, header = header, sslopt={"cert_reqs": ssl.CERT_NONE})
_resize(ssh, utf_in, utf_out)
oldtty = termios.tcgetattr(utf_in)
old_handler = signal.getsignal(signal.SIGWINCH)

def on_term_resize(signum, frame):
_resize(ssh)
_resize(ssh, utf_in, utf_out)
signal.signal(signal.SIGWINCH, on_term_resize)

try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
tty.setraw(utf_in.fileno())
tty.setcbreak(utf_in.fileno())

rows, cols = _pty_size()
rows, cols = _pty_size(utf_in, utf_out)
ssh.send(json.dumps({'resize': {'width': cols, 'height': rows}}))

while True:
try:
r, w, e = select.select([ssh.sock, sys.stdin], [], [])
r, w, e = select.select([ssh.sock, utf_in], [], [])
if ssh.sock in r:
data = ssh.recv()
if not data:
break
message = json.loads(data)
if 'error' in message:
raise ConnectionError(message['error'])
sys.stdout.write(message['data'])
sys.stdout.flush()
if sys.stdin in r:
x = sys.stdin.read(1)
utf_out.write(message['error'])
break
utf_out.write(message['data'])
utf_out.flush()
if utf_in in r:
x = os.read(utf_in.fileno(), 3)
if len(x) == 0:
break
ssh.send(json.dumps({'data': x}))
Expand All @@ -83,5 +85,6 @@ def on_term_resize(signum, frame):
except websocket.WebSocketException:
raise
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
termios.tcsetattr(utf_in, termios.TCSADRAIN, oldtty)
signal.signal(signal.SIGWINCH, old_handler)
print '\n'
8 changes: 7 additions & 1 deletion wssh/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from paramiko.ssh_exception import SSHException

import socket
import time

try:
import simplejson as json
Expand Down Expand Up @@ -111,6 +112,8 @@ def _forward_inbound(self, channel):
while True:
data = self._websocket.receive()
if not data:
channel.send('\x03\rexit\r')
time.sleep(10)
return
data = json.loads(str(data))
if 'resize' in data:
Expand All @@ -127,10 +130,13 @@ def _forward_outbound(self, channel):
try:
while True:
wait_read(channel.fileno())
data = channel.recv(1024)
data = channel.recv(10240)
if not len(data):
return
self._websocket.send(json.dumps({'data': data}))
except Exception as e:
channel.send('\x03\rexit\r')
time.sleep(10)
finally:
self.close()

Expand Down
5 changes: 3 additions & 2 deletions wssh/static/term.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ function Terminal(cols, rows, handler) {
this.rows = rows || Terminal.geometry[1];

if (handler) {
this.on('data', handler);
this.dataHandler = handler;
this.on('data', this.dataHandler);
}

this.ybase = 0;
Expand Down Expand Up @@ -2432,7 +2433,7 @@ Terminal.prototype.reverseIndex = function() {

// ESC c Full Reset (RIS).
Terminal.prototype.reset = function() {
Terminal.call(this, this.cols, this.rows);
Terminal.call(this, this.cols, this.rows, this.dataHandler);
this.refresh(0, this.rows - 1);
};

Expand Down