Browse Source

stash

pull/530/head
ibuler 8 years ago
parent
commit
ab18fe466b
  1. 5
      apps/terminal/hands.py
  2. 0
      apps/terminal/keys/README.md
  3. 60
      apps/terminal/server.py

5
apps/terminal/hands.py

@ -0,0 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from users.utils import ssh_key_gen

0
apps/terminal/keys/README.md

60
server.py → apps/terminal/server.py

@ -1,22 +1,39 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
import sys
import os
import django
BASE_DIR = os.path.dirname(__file__)
APP_DIR = os.path.abspath(os.path.dirname(BASE_DIR))
sys.path.append(APP_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings'
try:
django.setup()
except IndexError:
pass
import base64 import base64
from binascii import hexlify from binascii import hexlify
import os
import sys import sys
import threading import threading
import traceback import traceback
import tty import tty
import termios import termios
import struct, fcntl, signal, socket, select import struct
import fcntl
import signal
import socket
import select
import errno import errno
import paramiko import paramiko
from paramiko.py3compat import b, u, decodebytes from paramiko.py3compat import b, u, decodebytes
from .hands import ssh_key_gen
paramiko.util.log_to_file('demo_server.log') paramiko.util.log_to_file('demo_server.log')
@ -24,24 +41,42 @@ host_key = paramiko.RSAKey(filename='test_rsa.key')
class SSHService(paramiko.ServerInterface): class SSHService(paramiko.ServerInterface):
# 'data' is the output of base64.encodestring(str(key)) # data = (b'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp'
# (using the "user_rsa_key" files) # b'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC'
data = (b'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' # b'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT'
b'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' # b'UWT10hcuO4Ks8=')
b'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' # good_pub_key = paramiko.RSAKey(data=decodebytes(data))
b'UWT10hcuO4Ks8=')
good_pub_key = paramiko.RSAKey(data=decodebytes(data)) ssh_key_path = os.path.join(BASE_DIR, 'keys', 'ssh_host_key')
ssh_pub_key_path = ssh_key_path + '.pub'
def __init__(self): def __init__(self):
self.event = threading.Event() self.event = threading.Event()
@classmethod
def get_host_key(cls):
if os.path.isfile(cls.ssh_pub_key_path):
with open(cls.ssh_pub_key_path) as f:
ssh_pub_key = f.read()
else:
ssh_key, ssh_pub_key = cls.host_key_gen()
return ssh_pub_key
@classmethod
def host_key_gen(cls):
ssh_key, ssh_pub_key = ssh_key_gen()
with open(cls.ssh_key_path, 'w') as f:
with open(cls.ssh_pub_key_path, 'w') as f2:
f.write(ssh_key)
f2.write(ssh_pub_key)
return ssh_key, ssh_pub_key
def check_channel_request(self, kind, chanid): def check_channel_request(self, kind, chanid):
if kind == 'session': if kind == 'session':
return paramiko.OPEN_SUCCEEDED return paramiko.OPEN_SUCCEEDED
return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
def check_auth_password(self, username, password): def check_auth_password(self, username, password):
print(username, password)
if (username == 'robey') and (password == 'foo'): if (username == 'robey') and (password == 'foo'):
return paramiko.AUTH_SUCCESSFUL return paramiko.AUTH_SUCCESSFUL
return paramiko.AUTH_FAILED return paramiko.AUTH_FAILED
@ -151,7 +186,6 @@ class SSHServer:
pass pass
print(server_data) print(server_data)
except Exception as e: except Exception as e:
print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e))
traceback.print_exc() traceback.print_exc()
@ -167,7 +201,7 @@ class SSHServer:
try: try:
client, addr = self.sock.accept() client, addr = self.sock.accept()
print('Listening for connection ...') print('Listening for connection ...')
threading.Thread(target=self.handle_ssh_request, args=( client, addr)).start() threading.Thread(target=self.handle_ssh_request, args=(client, addr)).start()
except Exception as e: except Exception as e:
print('*** Bind failed: ' + str(e)) print('*** Bind failed: ' + str(e))
traceback.print_exc() traceback.print_exc()
Loading…
Cancel
Save