feat: 添加基于webview实现的包含GUI的版本,添加通过upx进行压缩的版本,优化分享功能。

This commit is contained in:
539943419 committed 2025-03-22 15:47:35 +08:00
1 parent 7f26b46cde
commit f9b93624e4
14 files changed
+512 -45

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
package api
import (
"net"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
)
// AddressesHandler 返回服务器所有可用的访问地址
func AddressesHandler(serverPort string) gin.HandlerFunc {
return func(c *gin.Context) {
addresses := []gin.H{}
addresses = append(addresses, gin.H{
"title": "localhost",
"url": "http://localhost" + serverPort,
})
host, _ := os.Hostname()
addrs, _ := net.LookupHost(host)
for _, addr := range addrs {
if strings.Contains(addr, "%") {
addr = strings.Split(addr, "%")[0]
}
var url string
var title string
if strings.Contains(addr, ":") {
url = "http://[" + addr + "]" + serverPort
title = "IPv6 Address (" + addr + ")"
} else {
url = "http://" + addr + serverPort
title = "IPv4 Address (" + addr + ")"
}
addresses = append(addresses, gin.H{
"title": title,
"url": url,
})
}
c.JSON(http.StatusOK, addresses)
}
}
// 判断是否是私有IP地址
func isPrivateIP(ip string) bool {
ipAddr := net.ParseIP(ip)
if ipAddr == nil {
return false
}
// 检查常见的私有IP范围
// 10.0.0.0/8
if ipAddr[0] == 10 {
return true
}
// 172.16.0.0/12
if ipAddr[0] == 172 && ipAddr[1] >= 16 && ipAddr[1] <= 31 {
return true
}
// 192.168.0.0/16
if ipAddr[0] == 192 && ipAddr[1] == 168 {
return true
}
// 127.0.0.0/8 (本地回环)
if ipAddr[0] == 127 {
return true
}
return false
}
+4 -1
View File
@@ -16,6 +16,8 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/jchv/go-webview2 v0.0.0-20221223143126-dc24628cff85 // indirect
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect
github.com/josephspurrier/goversioninfo v1.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
@@ -26,10 +28,11 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/webview/webview v0.0.0-20250319170551-f1f42f0f164e // indirect
golang.org/x/arch v0.9.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
+11
View File
@@ -28,6 +28,11 @@ github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaC
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jchv/go-webview2 v0.0.0-20221223143126-dc24628cff85 h1:t6lhRbwURcdWgp8OsJlq6sOfWMOXP21YuiCicjutHv4=
github.com/jchv/go-webview2 v0.0.0-20221223143126-dc24628cff85/go.mod h1:/BNVc0Sw3Wj6Sz9uSxPwhCEUhhWs92hPde75K2YV24A=
github.com/jchv/go-winloader v0.0.0-20200815041850-dec1ee9a7fd5/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs=
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e h1:Q3+PugElBCf4PFpxhErSzU3/PY5sFL5Z6rfv4AbGAck=
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs=
github.com/josephspurrier/goversioninfo v1.4.0 h1:Puhl12NSHUSALHSuzYwPYQkqa2E1+7SrtAPJorKK0C8=
github.com/josephspurrier/goversioninfo v1.4.0/go.mod h1:JWzv5rKQr+MmW+LvM412ToT/IkYDZjaclF2pKDss8IY=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -64,16 +69,22 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/webview/webview v0.0.0-20250319170551-f1f42f0f164e h1:Kz8a4/xfC3vH5sXgPfNfwu2qwwTUrD79N0bFRS6E4tI=
github.com/webview/webview v0.0.0-20250319170551-f1f42f0f164e/go.mod h1:Zk81X+8/mp/MNoeJXXK4Noydn8pcea09e00dFfAwrxg=
golang.org/x/arch v0.9.0 h1:ub9TgUInamJ8mrZIGlBG6/4TqWeMszd4N8lNorbrr6k=
golang.org/x/arch v0.9.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210218145245-beda7e5e158e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
+19 -1
View File
@@ -39,6 +39,9 @@ func main() {
router.POST("/rename/*path", api.RenameHandler(fsOperateEventCenter))
router.POST("/delete/*path", api.DeleteHandler(fsOperateEventCenter))
// 添加获取所有可用地址的API
router.GET("/addresses", api.AddressesHandler(port))
// Redirect from '/' to '/web/'
router.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/web/")
@@ -54,7 +57,22 @@ func main() {
address := "[::]" + port
printListeningAddresses(port)
router.Run(address)
// 在非阻塞的goroutine中启动服务器
go func() {
if err := router.Run(address); err != nil {
fmt.Printf("服务器启动失败: %v\n", err)
os.Exit(1)
}
}()
// 如果启用了webview,则启动webview窗口
if useWebView {
startWebView(port)
} else {
// 如果不使用webview,则阻塞主线程,保持服务器运行
select {}
}
}
func corsMiddleware() gin.HandlerFunc {
+38
View File
@@ -0,0 +1,38 @@
//go:build webview
// +build webview
package main
import (
"fmt"
"github.com/jchv/go-webview2"
)
// 启动WebView窗口
func startWebView(port string) {
// 构建本地服务URL
serverURL := fmt.Sprintf("http://localhost%s", port)
// 创建webview窗口
w := webview2.NewWithOptions(webview2.WebViewOptions{
Debug: false,
AutoFocus: true,
WindowOptions: webview2.WindowOptions{
Title: "MiaoDirectory Server",
Width: 1024,
Height: 768,
Center: true,
},
})
defer w.Destroy()
// 导航到本地服务器
w.Navigate(serverURL)
// 启动webview窗口
w.Run()
}
// 定义是否使用WebView的变量
var useWebView = true
+12
View File
@@ -0,0 +1,12 @@
//go:build !webview
// +build !webview
package main
// 定义是否使用WebView的变量
var useWebView = false
// 空函数,在非webview版本中不执行任何操作
func startWebView(port string) {
// 在非webview版本中不做任何事情
}