-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (53 loc) · 1.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"flag"
"github.com/Duke1616/vuefinder-go/pkg/finder"
"github.com/Duke1616/vuefinder-go/pkg/ginx"
"github.com/Duke1616/vuefinder-go/pkg/web"
"github.com/gin-gonic/gin"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"log"
)
func main() {
// 定义命令行参数
host := flag.String("host", "127.0.0.1:22", "SSH server host and port")
user := flag.String("user", "", "SSH username")
password := flag.String("password", "", "SSH password")
// 解析命令行参数
flag.Parse()
// 检查必填参数
if *user == "" || *password == "" {
log.Fatal("Username and password are required")
}
// 连接到 SSH 服务器
client, err := ConnectSSH(*host, *user, *password)
sftpClient, err := sftp.NewClient(client)
f := finder.NewSftpFinder(sftpClient)
handler := web.NewHandler()
handler.SetFinder(20, f)
mlds := ginx.NewMiddleware()
engine := gin.Default()
engine.Use(mlds...)
handler.RegisterRoutes(engine)
err = engine.Run(":8350")
if err != nil {
panic(err)
}
}
func ConnectSSH(host, user, password string) (*ssh.Client, error) {
// 创建 SSH 客户端配置
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // 不推荐在生产环境中使用
}
// 连接到 SSH 服务器
client, err := ssh.Dial("tcp", host, config)
if err != nil {
return nil, err
}
return client, nil
}