Skip to content
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

Migrate SSH tunneling from pyzmq #335

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
24 changes: 12 additions & 12 deletions jupyter_client/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@


def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0, hb_port=0,
control_port=0, ip='', key=b'', transport='tcp',
signature_scheme='hmac-sha256', kernel_name=''
):
control_port=0, ip='', key=b'', transport='tcp',
signature_scheme='hmac-sha256', kernel_name=''
):
"""Generates a JSON config file, including the selection of random ports.

Parameters
Expand Down Expand Up @@ -193,7 +193,7 @@ def find_connection_file(filename='kernel-*.json', path=None, profile=None):
path = ['.', jupyter_runtime_dir()]
if isinstance(path, string_types):
path = [path]

try:
# first, try explicit name
return filefind(filename, path)
Expand All @@ -208,11 +208,11 @@ def find_connection_file(filename='kernel-*.json', path=None, profile=None):
else:
# accept any substring match
pat = '*%s*' % filename

matches = []
for p in path:
matches.extend(glob.glob(os.path.join(p, pat)))

matches = [ os.path.abspath(m) for m in matches ]
if not matches:
raise IOError("Could not find %r in %r" % (filename, path))
Expand Down Expand Up @@ -249,7 +249,7 @@ def tunnel_to_kernel(connection_info, sshserver, sshkey=None):
(shell, iopub, stdin, hb) : ints
The four ports on localhost that have been forwarded to the kernel.
"""
from zmq.ssh import tunnel
from jupyter_core.ssh import tunnel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should now be from .ssh

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if isinstance(connection_info, string_types):
# it's a path, unpack it
with open(connection_info) as f:
Expand Down Expand Up @@ -289,11 +289,11 @@ def tunnel_to_kernel(connection_info, sshserver, sshkey=None):

class ConnectionFileMixin(LoggingConfigurable):
"""Mixin for configurable classes that work with connection files"""

data_dir = Unicode()
def _data_dir_default(self):
return jupyter_data_dir()

# The addresses for the communication channels
connection_file = Unicode('', config=True,
help="""JSON file in which to store connection info [default: kernel-<pid>.json]
Expand Down Expand Up @@ -480,7 +480,7 @@ def write_connection_file(self):

def load_connection_file(self, connection_file=None):
"""Load connection info from JSON dict in self.connection_file.

Parameters
----------
connection_file: unicode, optional
Expand All @@ -496,10 +496,10 @@ def load_connection_file(self, connection_file=None):

def load_connection_info(self, info):
"""Load connection info from a dict containing connection info.

Typically this data comes from a connection file
and is called by load_connection_file.

Parameters
----------
info: dict
Expand Down
1 change: 1 addition & 0 deletions jupyter_client/ssh/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from jupyter_client.ssh.tunnel import *
92 changes: 92 additions & 0 deletions jupyter_client/ssh/forward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#
# This file is adapted from a paramiko demo, and thus licensed under LGPL 2.1.
# Original Copyright (C) 2003-2007 Robey Pointer <robeypointer@gmail.com>
# Edits Copyright (C) 2010 The IPython Team
#
# Paramiko is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA.

"""
Sample script showing how to do local port forwarding over paramiko.

This script connects to the requested SSH server and sets up local port
forwarding (the openssh -L option) from a local port through a tunneled
connection to a destination reachable from the SSH server machine.
"""

from __future__ import print_function

import logging
import select
try: # Python 3
import socketserver
except ImportError: # Python 2
import SocketServer as socketserver

logger = logging.getLogger('ssh')


class ForwardServer (socketserver.ThreadingTCPServer):
daemon_threads = True
allow_reuse_address = True


class Handler (socketserver.BaseRequestHandler):

def handle(self):
try:
chan = self.ssh_transport.open_channel('direct-tcpip',
(self.chain_host, self.chain_port),
self.request.getpeername())
except Exception as e:
logger.debug('Incoming request to %s:%d failed: %s' % (self.chain_host,
self.chain_port,
repr(e)))
return
if chan is None:
logger.debug('Incoming request to %s:%d was rejected by the SSH server.' %
(self.chain_host, self.chain_port))
return

logger.debug('Connected! Tunnel open %r -> %r -> %r' % (self.request.getpeername(),
chan.getpeername(), (self.chain_host, self.chain_port)))
while True:
r, w, x = select.select([self.request, chan], [], [])
if self.request in r:
data = self.request.recv(1024)
if len(data) == 0:
break
chan.send(data)
if chan in r:
data = chan.recv(1024)
if len(data) == 0:
break
self.request.send(data)
chan.close()
self.request.close()
logger.debug('Tunnel closed ')


def forward_tunnel(local_port, remote_host, remote_port, transport):
# this is a little convoluted, but lets me configure things for the Handler
# object. (SocketServer doesn't give Handlers any way to access the outer
# server normally.)
class SubHander (Handler):
chain_host = remote_host
chain_port = remote_port
ssh_transport = transport
ForwardServer(('127.0.0.1', local_port), SubHander).serve_forever()


__all__ = ['forward_tunnel']
Loading