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

[Fix] Fix ssh tunnel always return 127.0.0.1 #2808

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 16 additions & 4 deletions sql/utils/ssh_tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sshtunnel import SSHTunnelForwarder
from paramiko import RSAKey
import io
import socket


class SSHConnection(object):
Expand Down Expand Up @@ -55,13 +56,24 @@ def __init__(
)
self.server.start()

# 动态获取本地IP
self.local_ip = self.get_local_ip()

def __del__(self):
self.server.close()

def get_local_ip(self):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# 连接一个公网地址(不需要实际发送数据)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip

def get_ssh(self):
"""
获取ssh映射的端口
:param request:
:return:
获取ssh映射的本地IP和端口
"""
return "127.0.0.1", self.server.local_bind_port
return self.local_ip, self.server.local_bind_port
7 changes: 7 additions & 0 deletions sql/utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from sql.utils.execute_sql import execute, execute_callback
from sql.utils.tasks import add_sql_schedule, del_schedule, task_info
from sql.utils.data_masking import data_masking, brute_mask, simple_column_mask
from sql.utils.ssh_tunnel import SSHConnection

User = Users
__author__ = "hhyo"
Expand Down Expand Up @@ -1512,3 +1513,9 @@ def test_auth_group_users(self):
auth_group_names=[self.agp.name], group_id=self.rgp1.group_id
)
self.assertIn(self.user, users)


# ssh tunnel tests
def test_get_local_ip():
tunnel = SSHConnection("mysql", 3306, "tunnel", 22, "root", "password", "", "")
assert tunnel.get_local_ip() != "127.0.0.1"
Loading