-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathmain.go
More file actions
134 lines (123 loc) · 3.56 KB
/
main.go
File metadata and controls
134 lines (123 loc) · 3.56 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"embed"
"flag"
"fmt"
"io/fs"
"net/http"
"os"
"strconv"
"strings"
"time"
"webssh/controller"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)
//go:embed public/*
var f embed.FS
var (
port = flag.Int("p", 8888, "服务运行端口")
v = flag.Bool("v", false, "显示版本号")
authInfo = flag.String("a", "", "开启账号密码登录验证, '-a user:pass'的格式传参")
timeout int
savePass bool
version string
buildDate string
goVersion string
gitVersion string
username string
password string
)
func init() {
flag.IntVar(&timeout, "t", 120, "ssh连接超时时间(min)")
flag.BoolVar(&savePass, "s", true, "保存ssh密码")
if envVal, ok := os.LookupEnv("savePass"); ok {
if b, err := strconv.ParseBool(envVal); err == nil {
savePass = b
}
}
if envVal, ok := os.LookupEnv("authInfo"); ok {
*authInfo = envVal
}
if envVal, ok := os.LookupEnv("port"); ok {
if b, err := strconv.Atoi(envVal); err == nil {
*port = b
}
}
flag.Parse()
if *v {
fmt.Printf("Version: %s\n\n", version)
fmt.Printf("BuildDate: %s\n\n", buildDate)
fmt.Printf("GoVersion: %s\n\n", goVersion)
fmt.Printf("GitVersion: %s\n\n", gitVersion)
os.Exit(0)
}
if *authInfo != "" {
accountInfo := strings.Split(*authInfo, ":")
if len(accountInfo) != 2 || accountInfo[0] == "" || accountInfo[1] == "" {
fmt.Println("请按'user:pass'的格式来传参或设置环境变量, 且账号密码都不能为空!")
os.Exit(0)
}
username, password = accountInfo[0], accountInfo[1]
}
}
func main() {
server := gin.New()
server.Use(gin.Recovery())
server.SetTrustedProxies(nil)
server.Use(gzip.Gzip(gzip.DefaultCompression))
// --- API Routes ---
// No BasicAuth for API routes as per original logic.
// If auth is needed for APIs, these routes should be moved inside the auth-enabled group below.
server.GET("/term", func(c *gin.Context) {
controller.TermWs(c, time.Duration(timeout)*time.Minute)
})
server.GET("/check", func(c *gin.Context) {
responseBody := controller.CheckSSH(c)
responseBody.Data = map[string]interface{}{
"savePass": savePass,
}
c.JSON(200, responseBody)
})
file := server.Group("/file")
{
file.GET("/list", func(c *gin.Context) {
c.JSON(200, controller.FileList(c))
})
file.GET("/download", func(c *gin.Context) {
controller.DownloadFile(c)
})
file.POST("/upload", func(c *gin.Context) {
c.JSON(200, controller.UploadFile(c))
})
file.GET("/progress", func(c *gin.Context) {
controller.UploadProgressWs(c)
})
}
// --- Static Files & SPA Frontend ---
// Serve static files from the 'static' directory
staticFS, _ := fs.Sub(f, "public/static")
server.StaticFS("/static", http.FS(staticFS))
// For any other route, serve the index.html file.
// This makes it compatible with Vue Router's history mode.
server.NoRoute(func(c *gin.Context) {
if *authInfo != "" {
// If auth is enabled, check credentials.
// This is a simplified check. For production, use a proper session/token mechanism.
user, pass, hasAuth := c.Request.BasicAuth()
if !hasAuth || user != username || pass != password {
c.Header("WWW-Authenticate", `Basic realm="Restricted"`)
c.AbortWithStatus(http.StatusUnauthorized)
return
}
}
indexHTML, err := f.ReadFile("public/index.html")
if err != nil {
c.String(http.StatusInternalServerError, "index.html not found")
return
}
c.Data(http.StatusOK, "text/html; charset=utf-8", indexHTML)
})
fmt.Printf("Github:https://github.com/eooce/webssh\n")
server.Run(fmt.Sprintf(":%d", *port))
}