Skip to content

Commit ed2584d

Browse files
committed
添加proxy
1 parent 1d80813 commit ed2584d

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
ktool.exe
2-
*.rar
2+
*.rar
3+
server.yaml
4+
public

ctl/clientConfig.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/pkg/sftp"
1717
"golang.org/x/crypto/ssh"
18+
"golang.org/x/net/proxy"
1819
)
1920

2021
const (
@@ -33,7 +34,7 @@ type ClientConfig struct {
3334
LastResult string
3435
}
3536

36-
func CreateClient(host string, port string, username, password string) *ClientConfig {
37+
func CreateClient(host string, port string, username, password string, proxyUrl string) *ClientConfig {
3738
cf := &ClientConfig{}
3839
var (
3940
sshClient *ssh.Client
@@ -57,19 +58,46 @@ func CreateClient(host string, port string, username, password string) *ClientCo
5758

5859
addr := fmt.Sprintf("%s:%s", cf.Host, cf.Port)
5960

60-
if sshClient, err = ssh.Dial("tcp", addr, &config); err != nil {
61-
log.Fatalf("connect ssh error: %s", err.Error())
62-
return nil
63-
}
61+
if proxyUrl != "" {
62+
// 创建SOCKS5代理拨号器
63+
log.Printf("Start proxy: %s", proxyUrl)
64+
dialer, err := proxy.SOCKS5("tcp", proxyUrl, nil, proxy.Direct)
65+
if err != nil {
66+
log.Fatal(err)
67+
}
68+
69+
conn, err := dialer.Dial("tcp", addr)
70+
if err != nil {
71+
fmt.Println(err)
72+
return nil
73+
}
74+
75+
c, cn, req, err := ssh.NewClientConn(conn, addr, &config)
76+
if err != nil {
77+
log.Fatalf("connect ssh error: %s", err.Error())
78+
return nil
79+
}
80+
81+
sshClient = ssh.NewClient(c, cn, req)
82+
83+
cf.sshClient = sshClient
84+
} else {
6485

65-
cf.sshClient = sshClient
86+
if sshClient, err = ssh.Dial("tcp", addr, &config); err != nil {
87+
log.Fatalf("connect ssh error: %s", err.Error())
88+
return nil
89+
}
90+
91+
cf.sshClient = sshClient
92+
}
6693

6794
if sftpClient, err = sftp.NewClient(sshClient); err != nil {
6895
log.Fatalf("connect sftp error: %s", err.Error())
6996
return nil
7097
}
7198

7299
cf.sftpClient = sftpClient
100+
log.Println("connect server success...")
73101
return cf
74102
}
75103

ctl/parse.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type ServerAddr struct {
2020
Port string `yaml:"port"`
2121
Account string `yaml:"account"`
2222
Password string `yaml:"password"`
23+
Proxy string `yarml:"proxy"`
2324
}
2425

2526
type FileCtl struct {
@@ -47,7 +48,7 @@ func NewYaml(path string) *Yaml {
4748

4849
func (yal Yaml) Run() {
4950

50-
srv := CreateClient(yal.Server.IP, yal.Server.Port, yal.Server.Account, yal.Server.Password)
51+
srv := CreateClient(yal.Server.IP, yal.Server.Port, yal.Server.Account, yal.Server.Password, yal.Server.Proxy)
5152
for _, v := range yal.UploadFiles {
5253
srv.Upload(v.Src, v.Dst)
5354
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/pkg/sftp v1.13.5
77
github.com/spf13/cobra v1.7.0
88
golang.org/x/crypto v0.9.0
9+
golang.org/x/net v0.10.0
910
gopkg.in/yaml.v2 v2.4.0
1011
)
1112

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0
2121
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
2222
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
2323
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
24+
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
25+
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
2426
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2527
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2628
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 commit comments

Comments
 (0)