Skip to content

Commit

Permalink
Merge pull request #768 from werhner/proxy
Browse files Browse the repository at this point in the history
Support ssh ProxyCommand option
  • Loading branch information
manateelazycat authored Nov 9, 2023
2 parents 9dcff32 + eda3699 commit f1724d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
15 changes: 9 additions & 6 deletions core/remote_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RemoteFileClient(threading.Thread):

remote_password_dict = {}

def __init__(self, ssh_host, ssh_user, ssh_port, server_port, callback, use_gssapi=False):
def __init__(self, ssh_host, ssh_user, ssh_port, server_port, callback, use_gssapi=False, proxy_command=None):
threading.Thread.__init__(self)

# Init.
Expand All @@ -42,7 +42,7 @@ def __init__(self, ssh_host, ssh_user, ssh_port, server_port, callback, use_gssa


# Build SSH channel between local client and remote server.
self.ssh = self.connect_ssh(use_gssapi)
self.ssh = self.connect_ssh(use_gssapi, proxy_command)
self.transport = self.ssh.get_transport()
self.chan = self.transport.open_channel("direct-tcpip", (self.ssh_host, self.server_port), ('0.0.0.0', 0))

Expand All @@ -52,20 +52,23 @@ def ssh_pub_key(self):
pub_keys = glob.glob(os.path.join(ssh_dir, '*.pub'))
return pub_keys[0]

def connect_ssh(self, use_gssapi):
def connect_ssh(self, use_gssapi, proxy_command):
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

proxy = None
if proxy_command:
proxy = paramiko.ProxyCommand(proxy_command)

try:
if use_gssapi:
import gssapi
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.ssh_host, port=self.ssh_port, username=self.ssh_user, gss_auth=True, gss_kex=True)
ssh.connect(self.ssh_host, port=self.ssh_port, username=self.ssh_user, gss_auth=True, gss_kex=True, sock=proxy)
else:
# Login server with ssh public key.
pub_key = self.ssh_pub_key()
ssh.connect(self.ssh_host, port=self.ssh_port, username=self.ssh_user, key_filename=pub_key)
ssh.connect(self.ssh_host, port=self.ssh_port, username=self.ssh_user, key_filename=pub_key, sock=proxy)
except:
print(traceback.format_exc())

Expand Down
13 changes: 9 additions & 4 deletions lsp_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def open_remote_file(self, path, jump_define_pos):
ssh_port = 22

if server_username and ssh_port:
self.host_names[server_host] = {"username": server_username, "ssh_port": ssh_port, "use_gssapi": False}
self.host_names[server_host] = {"username": server_username, "ssh_port": ssh_port, "use_gssapi": False, "proxy_command": None}

try:
client_id = f"{server_host}:{REMOTE_FILE_ELISP_CHANNEL}"
Expand All @@ -242,6 +242,7 @@ def open_remote_file(self, path, jump_define_pos):
@threaded
def sync_tramp_remote(self, server_username, server_host, ssh_port, filename):
use_gssapi = False
proxy_command = None
alias = None
if not is_valid_ip(server_host):
alias = server_host
Expand All @@ -250,6 +251,7 @@ def sync_tramp_remote(self, server_username, server_host, ssh_port, filename):
server_username = self.host_names[alias]["username"]
ssh_port = self.host_names[alias]["ssh_port"]
use_gssapi = self.host_names[alias]["use_gssapi"]
proxy_command = self.host_names[alias]["proxy_command"]
else:
import paramiko
ssh_config = paramiko.SSHConfig()
Expand All @@ -260,8 +262,10 @@ def sync_tramp_remote(self, server_username, server_host, ssh_port, filename):
server_username = conf.get('user', server_username)
ssh_port = conf.get('port', ssh_port)
use_gssapi = conf.get('gssapiauthentication', 'no') in ('yes')
proxy_command = conf.get('proxycommand', None)

self.host_names[alias] = {"server_host": server_host, "username": server_username, "ssh_port": ssh_port, "use_gssapi": use_gssapi}
self.host_names[alias] = {"server_host": server_host, "username": server_username, "ssh_port": ssh_port, "use_gssapi": use_gssapi,
"proxy_command": proxy_command}

tramp_file_split = filename.rsplit(":", 1)
tramp_method = tramp_file_split[0] + ":"
Expand All @@ -279,7 +283,7 @@ def sync_tramp_remote(self, server_username, server_host, ssh_port, filename):
else:
ssh_port = 22

self.host_names[server_host] = {"username": server_username, "ssh_port": ssh_port, "use_gssapi": use_gssapi}
self.host_names[server_host] = {"username": server_username, "ssh_port": ssh_port, "use_gssapi": use_gssapi, "proxy_command": proxy_command}

try:
client_id = f"{server_host}:{REMOTE_FILE_ELISP_CHANNEL}"
Expand Down Expand Up @@ -410,7 +414,8 @@ def get_socket_client(self, server_host, server_port):
self.host_names[server_host]["ssh_port"],
server_port,
lambda message: self.receive_socket_message(message, server_port),
self.host_names[server_host]["use_gssapi"]
self.host_names[server_host]["use_gssapi"],
self.host_names[server_host]["proxy_command"]
)
client.start()

Expand Down

0 comments on commit f1724d1

Please sign in to comment.