From 6514c1ccd026ef02f669dd77d4f5f866dd0a491a Mon Sep 17 00:00:00 2001 From: werhner Date: Thu, 9 Nov 2023 16:07:18 +0800 Subject: [PATCH 1/2] ssh support proxy command --- core/remote_file.py | 16 ++++++++++------ lsp_bridge.py | 14 ++++++++++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/core/remote_file.py b/core/remote_file.py index ee5c7d550a..8d41470373 100755 --- a/core/remote_file.py +++ b/core/remote_file.py @@ -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. @@ -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)) @@ -52,20 +52,24 @@ 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 + print("connecting" ,proxy_command) + 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()) diff --git a/lsp_bridge.py b/lsp_bridge.py index 8aae97306c..5ea62c8a16 100755 --- a/lsp_bridge.py +++ b/lsp_bridge.py @@ -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}" @@ -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 @@ -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() @@ -260,8 +262,11 @@ 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) + print(proxy_command) - 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] + ":" @@ -279,7 +284,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}" @@ -410,7 +415,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() From eda3699db648769efbc335c4379a9b89d2737663 Mon Sep 17 00:00:00 2001 From: werhner Date: Thu, 9 Nov 2023 16:17:09 +0800 Subject: [PATCH 2/2] remove print --- core/remote_file.py | 1 - lsp_bridge.py | 1 - 2 files changed, 2 deletions(-) diff --git a/core/remote_file.py b/core/remote_file.py index 8d41470373..4ad732d90b 100755 --- a/core/remote_file.py +++ b/core/remote_file.py @@ -58,7 +58,6 @@ def connect_ssh(self, use_gssapi, proxy_command): ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) proxy = None - print("connecting" ,proxy_command) if proxy_command: proxy = paramiko.ProxyCommand(proxy_command) diff --git a/lsp_bridge.py b/lsp_bridge.py index 5ea62c8a16..a22882f402 100755 --- a/lsp_bridge.py +++ b/lsp_bridge.py @@ -263,7 +263,6 @@ def sync_tramp_remote(self, server_username, server_host, ssh_port, filename): ssh_port = conf.get('port', ssh_port) use_gssapi = conf.get('gssapiauthentication', 'no') in ('yes') proxy_command = conf.get('proxycommand', None) - print(proxy_command) self.host_names[alias] = {"server_host": server_host, "username": server_username, "ssh_port": ssh_port, "use_gssapi": use_gssapi, "proxy_command": proxy_command}