!121 fix: rdp ssh

* fix: rdp ssh
This commit is contained in:
zongyangleo
2024-05-21 04:06:13 +00:00
committed by Coder慌
parent c4cb4234fd
commit a7632fbf58
5 changed files with 47 additions and 19 deletions

View File

@@ -261,7 +261,7 @@ func (m *Machine) WsGuacamole(g *gin.Context) {
return return
} }
err = mi.IfUseSshTunnelChangeIpPort() err = mi.IfUseSshTunnelChangeIpPort(true)
if err != nil { if err != nil {
return return
} }

View File

@@ -5,6 +5,7 @@ import (
tagentity "mayfly-go/internal/tag/domain/entity" tagentity "mayfly-go/internal/tag/domain/entity"
"mayfly-go/pkg/errorx" "mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx" "mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/netx"
"net" "net"
"time" "time"
@@ -15,8 +16,8 @@ import (
type MachineInfo struct { type MachineInfo struct {
Key string `json:"key"` // 缓存key Key string `json:"key"` // 缓存key
Id uint64 `json:"id"` Id uint64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"` Name string `json:"name"`
Code string `json:"code"`
Protocol int `json:"protocol"` Protocol int `json:"protocol"`
Ip string `json:"ip"` // IP地址 Ip string `json:"ip"` // IP地址
@@ -35,12 +36,12 @@ type MachineInfo struct {
CodePath []string `json:"codePath"` CodePath []string `json:"codePath"`
} }
func (m *MachineInfo) UseSshTunnel() bool { func (mi *MachineInfo) UseSshTunnel() bool {
return m.SshTunnelMachine != nil return mi.SshTunnelMachine != nil
} }
func (m *MachineInfo) GetTunnelId() string { func (mi *MachineInfo) GetTunnelId() string {
return fmt.Sprintf("machine:%d", m.Id) return fmt.Sprintf("machine:%d", mi.Id)
} }
// 连接 // 连接
@@ -48,7 +49,7 @@ func (mi *MachineInfo) Conn() (*Cli, error) {
logx.Infof("[%s]机器连接:%s:%d", mi.Name, mi.Ip, mi.Port) logx.Infof("[%s]机器连接:%s:%d", mi.Name, mi.Ip, mi.Port)
// 如果使用了ssh隧道则修改机器ip port为暴露的ip port // 如果使用了ssh隧道则修改机器ip port为暴露的ip port
err := mi.IfUseSshTunnelChangeIpPort() err := mi.IfUseSshTunnelChangeIpPort(false)
if err != nil { if err != nil {
return nil, errorx.NewBiz("ssh隧道连接失败: %s", err.Error()) return nil, errorx.NewBiz("ssh隧道连接失败: %s", err.Error())
} }
@@ -66,33 +67,39 @@ func (mi *MachineInfo) Conn() (*Cli, error) {
} }
// 如果使用了ssh隧道则修改机器ip port为暴露的ip port // 如果使用了ssh隧道则修改机器ip port为暴露的ip port
func (me *MachineInfo) IfUseSshTunnelChangeIpPort() error { func (mi *MachineInfo) IfUseSshTunnelChangeIpPort(out bool) error {
if !me.UseSshTunnel() { if !mi.UseSshTunnel() {
return nil return nil
} }
originId := me.Id originId := mi.Id
if originId == 0 { if originId == 0 {
// 随机设置一个id如果使用了隧道则用于临时保存隧道 // 随机设置一个id如果使用了隧道则用于临时保存隧道
me.Id = uint64(time.Now().Nanosecond()) mi.Id = uint64(time.Now().Nanosecond())
} }
sshTunnelMachine, err := GetSshTunnelMachine(int(me.SshTunnelMachine.Id), func(u uint64) (*MachineInfo, error) { sshTunnelMachine, err := GetSshTunnelMachine(int(mi.SshTunnelMachine.Id), func(u uint64) (*MachineInfo, error) {
return me.SshTunnelMachine, nil return mi.SshTunnelMachine, nil
}) })
if err != nil { if err != nil {
return err return err
} }
exposeIp, exposePort, err := sshTunnelMachine.OpenSshTunnel(me.GetTunnelId(), me.Ip, me.Port) exposeIp, exposePort, err := sshTunnelMachine.OpenSshTunnel(mi.GetTunnelId(), mi.Ip, mi.Port)
if err != nil { if err != nil {
return err return err
} }
// 是否获取局域网的本地IP
if out {
exposeIp = netx.GetOutBoundIP()
}
// 修改机器ip地址 // 修改机器ip地址
me.Ip = exposeIp mi.Ip = exposeIp
me.Port = exposePort mi.Port = exposePort
// 代理之后置空跳板机信息,防止重复跳 // 代理之后置空跳板机信息,防止重复跳
me.TempSshMachineId = me.SshTunnelMachine.Id mi.TempSshMachineId = mi.SshTunnelMachine.Id
me.SshTunnelMachine = nil mi.SshTunnelMachine = nil
return nil return nil
} }

View File

@@ -89,7 +89,7 @@ func (stm *SshTunnelMachine) OpenSshTunnel(id string, ip string, port int) (expo
return "", 0, err return "", 0, err
} }
localHost := "127.0.0.1" localHost := "0.0.0.0"
localAddr := fmt.Sprintf("%s:%d", localHost, localPort) localAddr := fmt.Sprintf("%s:%d", localHost, localPort)
listener, err := net.Listen("tcp", localAddr) listener, err := net.Listen("tcp", localAddr)
if err != nil { if err != nil {

View File

@@ -3,6 +3,7 @@ package netx
import ( import (
"mayfly-go/pkg/logx" "mayfly-go/pkg/logx"
"net" "net"
"strings"
"github.com/lionsoul2014/ip2region/binding/golang/xdb" "github.com/lionsoul2014/ip2region/binding/golang/xdb"
) )
@@ -68,3 +69,13 @@ func Ip2Region(ip string) string {
} }
return region return region
} }
func GetOutBoundIP() string {
conn, err := net.Dial("udp", "8.8.8.8:53")
if err != nil {
return "0.0.0.0"
}
localAddr := conn.LocalAddr().(*net.UDPAddr)
ip := strings.Split(localAddr.String(), ":")[0]
return ip
}

View File

@@ -0,0 +1,10 @@
package netx
import (
"fmt"
"testing"
)
func TestIp(t *testing.T) {
fmt.Println(GetOutBoundIP())
}