mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-07-05 18:11:32 +08:00
feat(i18n): 收口多语言功能业务代码
This commit is contained in:
@@ -120,12 +120,12 @@ func RegisterSSHNetwork(sshConfig connection.SSHConfig) (string, error) {
|
||||
func DialContextThroughSSH(ctx context.Context, config connection.SSHConfig, network, address string) (net.Conn, error) {
|
||||
client, err := GetOrCreateSSHClient(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("建立 SSH 连接失败:%w", err)
|
||||
return nil, fmt.Errorf("failed to establish SSH connection: %w", err)
|
||||
}
|
||||
|
||||
conn, err := dialContext(ctx, client, network, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("通过 SSH 隧道连接到 %s 失败:%w", address, err)
|
||||
return nil, fmt.Errorf("failed to connect to %s through SSH tunnel: %w", address, err)
|
||||
}
|
||||
|
||||
logger.Infof("已通过 SSH 隧道连接到:%s", address)
|
||||
@@ -203,13 +203,13 @@ type LocalForwarder struct {
|
||||
func NewLocalForwarder(sshConfig connection.SSHConfig, remoteHost string, remotePort int) (*LocalForwarder, error) {
|
||||
client, err := GetOrCreateSSHClient(sshConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("建立 SSH 连接失败:%w", err)
|
||||
return nil, fmt.Errorf("failed to establish SSH connection: %w", err)
|
||||
}
|
||||
|
||||
// Listen on localhost with a random port
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建本地监听器失败:%w", err)
|
||||
return nil, fmt.Errorf("failed to create local listener: %w", err)
|
||||
}
|
||||
|
||||
localAddr := listener.Addr().String()
|
||||
@@ -410,12 +410,12 @@ func GetOrCreateSSHClient(config connection.SSHConfig) (*ssh.Client, error) {
|
||||
func DialThroughSSH(config connection.SSHConfig, network, address string) (net.Conn, error) {
|
||||
client, err := GetOrCreateSSHClient(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("建立 SSH 连接失败:%w", err)
|
||||
return nil, fmt.Errorf("failed to establish SSH connection: %w", err)
|
||||
}
|
||||
|
||||
conn, err := client.Dial(network, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("通过 SSH 隧道连接到 %s 失败:%w", address, err)
|
||||
return nil, fmt.Errorf("failed to connect to %s through SSH tunnel: %w", address, err)
|
||||
}
|
||||
|
||||
logger.Infof("已通过 SSH 隧道连接到:%s", address)
|
||||
|
||||
94
internal/ssh/ssh_i18n_test.go
Normal file
94
internal/ssh/ssh_i18n_test.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode"
|
||||
|
||||
"GoNavi-Wails/internal/connection"
|
||||
)
|
||||
|
||||
func TestSSHConnectionFailureWrappersUseEnglish(t *testing.T) {
|
||||
t.Cleanup(CloseAllSSHClients)
|
||||
|
||||
config := connection.SSHConfig{
|
||||
Host: "127.0.0.1",
|
||||
Port: 1,
|
||||
User: "root",
|
||||
Password: "password",
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
run func() error
|
||||
}{
|
||||
{
|
||||
name: "context dial",
|
||||
run: func() error {
|
||||
_, err := DialContextThroughSSH(context.Background(), config, "tcp", "127.0.0.1:3306")
|
||||
return err
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "direct dial",
|
||||
run: func() error {
|
||||
_, err := DialThroughSSH(config, "tcp", "127.0.0.1:3306")
|
||||
return err
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "local forwarder",
|
||||
run: func() error {
|
||||
_, err := NewLocalForwarder(config, "127.0.0.1", 3306)
|
||||
return err
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.run()
|
||||
if err == nil {
|
||||
t.Fatalf("expected SSH connection failure")
|
||||
}
|
||||
message := err.Error()
|
||||
if containsHan(message) {
|
||||
t.Fatalf("expected English wrapper without Han characters, got %q", message)
|
||||
}
|
||||
if !strings.Contains(message, "failed to establish SSH connection") {
|
||||
t.Fatalf("expected English SSH connection wrapper, got %q", message)
|
||||
}
|
||||
if !strings.Contains(message, "127.0.0.1:1") {
|
||||
t.Fatalf("expected raw SSH address to stay in detail, got %q", message)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSHSourceDoesNotKeepLegacyChineseErrorWrappers(t *testing.T) {
|
||||
source, err := os.ReadFile("ssh.go")
|
||||
if err != nil {
|
||||
t.Fatalf("read ssh.go: %v", err)
|
||||
}
|
||||
text := string(source)
|
||||
for _, legacy := range []string{
|
||||
"fmt.Errorf(\"\u5efa\u7acb SSH \u8fde\u63a5\u5931\u8d25\uff1a%w\", err)",
|
||||
"fmt.Errorf(\"\u901a\u8fc7 SSH \u96a7\u9053\u8fde\u63a5\u5230 %s \u5931\u8d25\uff1a%w\", address, err)",
|
||||
"fmt.Errorf(\"\u521b\u5efa\u672c\u5730\u76d1\u542c\u5668\u5931\u8d25\uff1a%w\", err)",
|
||||
} {
|
||||
if strings.Contains(text, legacy) {
|
||||
t.Fatalf("legacy Chinese SSH wrapper still exists: %s", legacy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func containsHan(text string) bool {
|
||||
for _, r := range text {
|
||||
if unicode.Is(unicode.Han, r) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user