mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-08-12 09:34:05 +08:00
- 保留 Nacos 远端 Host、TLS SNI 与默认 8848 端口 - 规范化 HTTP、SOCKS5 与 SSH 代理组合路径 - 让停滞的 HTTP CONNECT 响应上下文取消并保持成功隧道可用
189 lines
5.0 KiB
Go
189 lines
5.0 KiB
Go
package app
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"GoNavi-Wails/internal/connection"
|
|
proxytunnel "GoNavi-Wails/internal/proxy"
|
|
)
|
|
|
|
func TestResolveDialConfigWithProxy_MongoKeepsTargetAddress(t *testing.T) {
|
|
hosts := []string{"10.20.30.40:27017", "10.20.30.41:27017"}
|
|
raw := connection.ConnectionConfig{
|
|
Type: "mongodb",
|
|
Host: "10.20.30.40",
|
|
Port: 27017,
|
|
UseProxy: true,
|
|
Proxy: connection.ProxyConfig{
|
|
Type: "socks5",
|
|
Host: "127.0.0.1",
|
|
Port: 1080,
|
|
},
|
|
Hosts: hosts,
|
|
}
|
|
|
|
got, err := resolveDialConfigWithProxy(raw)
|
|
if err != nil {
|
|
t.Fatalf("resolveDialConfigWithProxy returned error: %v", err)
|
|
}
|
|
if got.Host != raw.Host || got.Port != raw.Port {
|
|
t.Fatalf("mongo target address should be kept, got=%s:%d want=%s:%d", got.Host, got.Port, raw.Host, raw.Port)
|
|
}
|
|
if !got.UseProxy {
|
|
t.Fatalf("mongo should keep UseProxy=true for driver-level dialer")
|
|
}
|
|
if !reflect.DeepEqual(got.Hosts, hosts) {
|
|
t.Fatalf("mongo hosts should be kept, got=%v want=%v", got.Hosts, hosts)
|
|
}
|
|
}
|
|
|
|
func TestResolveDialConfigWithProxy_MongoSRVKeepsTargetAddress(t *testing.T) {
|
|
raw := connection.ConnectionConfig{
|
|
Type: "mongodb",
|
|
Host: "cluster0.example.com",
|
|
Port: 27017,
|
|
MongoSRV: true,
|
|
UseProxy: true,
|
|
Proxy: connection.ProxyConfig{
|
|
Type: "http",
|
|
Host: "127.0.0.1",
|
|
Port: 7890,
|
|
},
|
|
}
|
|
|
|
got, err := resolveDialConfigWithProxy(raw)
|
|
if err != nil {
|
|
t.Fatalf("resolveDialConfigWithProxy returned error: %v", err)
|
|
}
|
|
if got.Host != raw.Host || got.Port != raw.Port {
|
|
t.Fatalf("mongo SRV target address should be kept, got=%s:%d want=%s:%d", got.Host, got.Port, raw.Host, raw.Port)
|
|
}
|
|
if !got.UseProxy {
|
|
t.Fatalf("mongo SRV should keep UseProxy=true for driver-level dialer")
|
|
}
|
|
}
|
|
|
|
func TestDefaultPortByType_NacosProxyTargetUsesDefaultPort(t *testing.T) {
|
|
if got := defaultPortByType("nacos"); got != 8848 {
|
|
t.Fatalf("defaultPortByType(nacos) = %d, want 8848", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveDialConfigWithProxy_NacosKeepsRemoteAuthority(t *testing.T) {
|
|
proxytunnel.CloseAllForwarders()
|
|
t.Cleanup(proxytunnel.CloseAllForwarders)
|
|
|
|
tests := []struct {
|
|
name string
|
|
raw connection.ConnectionConfig
|
|
want connection.ProxyConfig
|
|
}{
|
|
{
|
|
name: "explicit socks5 proxy",
|
|
raw: connection.ConnectionConfig{
|
|
Type: "nacos",
|
|
Host: "secure-nacos.internal.test",
|
|
Port: 8848,
|
|
UseProxy: true,
|
|
Proxy: connection.ProxyConfig{
|
|
Type: "socks5h",
|
|
Host: "127.0.0.1",
|
|
Port: 1080,
|
|
},
|
|
},
|
|
want: connection.ProxyConfig{
|
|
Type: "socks5",
|
|
Host: "127.0.0.1",
|
|
Port: 1080,
|
|
},
|
|
},
|
|
{
|
|
name: "HTTP tunnel",
|
|
raw: connection.ConnectionConfig{
|
|
Type: "nacos",
|
|
Host: "secure-nacos.internal.test",
|
|
Port: 8848,
|
|
UseHTTPTunnel: true,
|
|
HTTPTunnel: connection.HTTPTunnelConfig{
|
|
Host: "tunnel.internal.test",
|
|
Port: 8080,
|
|
User: "tunnel-user",
|
|
Password: "tunnel-password",
|
|
},
|
|
},
|
|
want: connection.ProxyConfig{
|
|
Type: "http",
|
|
Host: "tunnel.internal.test",
|
|
Port: 8080,
|
|
User: "tunnel-user",
|
|
Password: "tunnel-password",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, testCase := range tests {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
got, err := resolveDialConfigWithProxy(testCase.raw)
|
|
if err != nil {
|
|
t.Fatalf("resolveDialConfigWithProxy: %v", err)
|
|
}
|
|
if got.Host != testCase.raw.Host || got.Port != testCase.raw.Port {
|
|
t.Fatalf(
|
|
"Nacos target authority = %s:%d, want %s:%d",
|
|
got.Host,
|
|
got.Port,
|
|
testCase.raw.Host,
|
|
testCase.raw.Port,
|
|
)
|
|
}
|
|
if !got.UseProxy || got.Proxy != testCase.want {
|
|
t.Fatalf("Nacos proxy = %#v (enabled=%v), want %#v", got.Proxy, got.UseProxy, testCase.want)
|
|
}
|
|
if got.UseHTTPTunnel || got.HTTPTunnel != (connection.HTTPTunnelConfig{}) {
|
|
t.Fatalf("HTTP tunnel was not normalized into proxy config: %#v", got.HTTPTunnel)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestResolveDialConfigWithProxy_NacosProxyWithSSHForwardsGatewayOnly(t *testing.T) {
|
|
proxytunnel.CloseAllForwarders()
|
|
t.Cleanup(proxytunnel.CloseAllForwarders)
|
|
|
|
raw := connection.ConnectionConfig{
|
|
Type: "nacos",
|
|
Host: "secure-nacos.internal.test",
|
|
Port: 8848,
|
|
UseProxy: true,
|
|
Proxy: connection.ProxyConfig{
|
|
Type: "socks5",
|
|
Host: "127.0.0.1",
|
|
Port: 1080,
|
|
},
|
|
UseSSH: true,
|
|
SSH: connection.SSHConfig{
|
|
Host: "ssh-gateway.internal.test",
|
|
Port: 22,
|
|
User: "ssh-user",
|
|
},
|
|
}
|
|
|
|
got, err := resolveDialConfigWithProxy(raw)
|
|
if err != nil {
|
|
t.Fatalf("resolveDialConfigWithProxy: %v", err)
|
|
}
|
|
if got.Host != raw.Host || got.Port != raw.Port {
|
|
t.Fatalf("Nacos SSH target = %s:%d, want %s:%d", got.Host, got.Port, raw.Host, raw.Port)
|
|
}
|
|
if !got.UseSSH {
|
|
t.Fatal("Nacos SSH tunnel was disabled")
|
|
}
|
|
if got.SSH.Host == raw.SSH.Host || got.SSH.Port == raw.SSH.Port {
|
|
t.Fatalf("SSH gateway was not replaced by its proxy forwarder: %#v", got.SSH)
|
|
}
|
|
if got.UseProxy || got.Proxy != (connection.ProxyConfig{}) {
|
|
t.Fatalf("proxy should only wrap the SSH gateway, got enabled=%v config=%#v", got.UseProxy, got.Proxy)
|
|
}
|
|
}
|