mirror of
https://github.com/wcwq98/realm.git
synced 2025-01-22 12:58:14 -05:00
Add files via upload
This commit is contained in:
parent
92973f0e8b
commit
4aa49266e6
10
web/config.toml
Normal file
10
web/config.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[auth]
|
||||
password = "你的密码"
|
||||
|
||||
[server]
|
||||
port = 8080
|
||||
|
||||
[https]
|
||||
enabled = true
|
||||
cert_file = "./cert.pem"
|
||||
key_file = "./private.key"
|
39
web/go.mod
Normal file
39
web/go.mod
Normal file
@ -0,0 +1,39 @@
|
||||
module realm
|
||||
|
||||
go 1.23.4
|
||||
|
||||
require github.com/BurntSushi/toml v1.4.0
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.11.6 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sessions v1.0.1 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/gin-gonic/gin v1.10.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/gorilla/context v1.1.2 // indirect
|
||||
github.com/gorilla/securecookie v1.1.2 // indirect
|
||||
github.com/gorilla/sessions v1.2.2 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
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
|
||||
golang.org/x/arch v0.8.0 // indirect
|
||||
golang.org/x/crypto v0.23.0 // indirect
|
||||
golang.org/x/net v0.25.0 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
golang.org/x/text v0.15.0 // indirect
|
||||
google.golang.org/protobuf v1.34.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
89
web/go.sum
Normal file
89
web/go.sum
Normal file
@ -0,0 +1,89 @@
|
||||
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||
github.com/gin-contrib/sessions v1.0.1 h1:3hsJyNs7v7N8OtelFmYXFrulAf6zSR7nW/putcPEHxI=
|
||||
github.com/gin-contrib/sessions v1.0.1/go.mod h1:ouxSFM24/OgIud5MJYQJLpy6AwxQ5EYO9yLhbtObGkM=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gorilla/context v1.1.2 h1:WRkNAv2uoa03QNIc1A6u4O7DAGMUVoopZhkiXWA2V1o=
|
||||
github.com/gorilla/context v1.1.2/go.mod h1:KDPwT9i/MeWHiLl90fuTgrt4/wPcv75vFAZLaOOcbxM=
|
||||
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
|
||||
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
|
||||
github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY=
|
||||
github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
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=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
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.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
298
web/main.go
Normal file
298
web/main.go
Normal file
@ -0,0 +1,298 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"sync"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions/cookie"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ForwardingRule struct {
|
||||
Listen string `toml:"listen"`
|
||||
Remote string `toml:"remote"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Network struct {
|
||||
NoTCP bool `toml:"no_tcp"`
|
||||
UseUDP bool `toml:"use_udp"`
|
||||
} `toml:"network"`
|
||||
Endpoints []ForwardingRule `toml:"endpoints"`
|
||||
}
|
||||
|
||||
type PanelConfig struct {
|
||||
Auth struct {
|
||||
Password string `toml:"password"`
|
||||
} `toml:"auth"`
|
||||
Server struct {
|
||||
Port int `toml:"port"`
|
||||
} `toml:"server"`
|
||||
HTTPS struct {
|
||||
Enabled bool `toml:"enabled"`
|
||||
CertFile string `toml:"cert_file"`
|
||||
KeyFile string `toml:"key_file"`
|
||||
} `toml:"https"`
|
||||
}
|
||||
|
||||
var (
|
||||
rules []ForwardingRule
|
||||
mu sync.Mutex
|
||||
config Config
|
||||
panelConfig PanelConfig
|
||||
)
|
||||
|
||||
func LoadConfig() error {
|
||||
data, err := ioutil.ReadFile("/root/.realm/config.toml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := toml.Decode(string(data), &config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rules = config.Endpoints
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadPanelConfig() error {
|
||||
data, err := ioutil.ReadFile("./config.toml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := toml.Decode(string(data), &panelConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveRules() error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
config.Endpoints = rules
|
||||
data, err := toml.Marshal(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile("/root/.realm/config.toml", data, 0644)
|
||||
}
|
||||
|
||||
// 认证中间件
|
||||
func AuthRequired() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
user := session.Get("user")
|
||||
if user == nil {
|
||||
c.Redirect(http.StatusFound, "/login")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := LoadConfig(); err != nil {
|
||||
log.Fatalf("无法加载 realm 配置: %v", err)
|
||||
}
|
||||
|
||||
if err := LoadPanelConfig(); err != nil {
|
||||
log.Fatalf("无法加载面板配置: %v", err)
|
||||
}
|
||||
|
||||
r := gin.Default()
|
||||
|
||||
// 设置 session
|
||||
store := cookie.NewStore([]byte("secret"))
|
||||
r.Use(sessions.Sessions("realm_session", store))
|
||||
|
||||
// 静态文件
|
||||
r.Static("/static", "./static")
|
||||
|
||||
// 登录页面
|
||||
r.GET("/login", func(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
if session.Get("user") != nil {
|
||||
c.Redirect(http.StatusFound, "/")
|
||||
return
|
||||
}
|
||||
c.File("./templates/login.html")
|
||||
})
|
||||
|
||||
// 登录处理
|
||||
r.POST("/login", func(c *gin.Context) {
|
||||
var loginData struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&loginData); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求"})
|
||||
return
|
||||
}
|
||||
|
||||
if loginData.Password == panelConfig.Auth.Password {
|
||||
session := sessions.Default(c)
|
||||
session.Set("user", true)
|
||||
session.Options(sessions.Options{
|
||||
MaxAge: 3600 * 24, // 24小时
|
||||
})
|
||||
if err := session.Save(); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Session保存失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "登录成功"})
|
||||
} else {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "密码错误"})
|
||||
}
|
||||
})
|
||||
|
||||
// 登出
|
||||
r.POST("/logout", AuthRequired(), func(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
session.Clear()
|
||||
session.Save()
|
||||
c.JSON(http.StatusOK, gin.H{"message": "登出成功"})
|
||||
})
|
||||
|
||||
// 需要认证的路由
|
||||
authorized := r.Group("/")
|
||||
authorized.Use(AuthRequired())
|
||||
{
|
||||
// 主页
|
||||
authorized.GET("/", func(c *gin.Context) {
|
||||
c.File("./templates/index.html")
|
||||
})
|
||||
|
||||
// 获取转发规则
|
||||
authorized.GET("/get_rules", func(c *gin.Context) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
c.JSON(200, rules)
|
||||
})
|
||||
|
||||
// 添加转发规则
|
||||
authorized.POST("/add_rule", func(c *gin.Context) {
|
||||
var input struct {
|
||||
Listen string `json:"listen"`
|
||||
Remote string `json:"remote"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(400, gin.H{"error": "Invalid input"})
|
||||
return
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
rules = append(rules, ForwardingRule{
|
||||
Listen: input.Listen,
|
||||
Remote: input.Remote,
|
||||
})
|
||||
mu.Unlock()
|
||||
|
||||
if err := SaveRules(); err != nil {
|
||||
c.JSON(500, gin.H{"error": "Failed to save rules"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(201, input)
|
||||
})
|
||||
|
||||
// 删除转发规则
|
||||
authorized.DELETE("/delete_rule", func(c *gin.Context) {
|
||||
listen := c.Query("listen")
|
||||
|
||||
mu.Lock()
|
||||
for i, rule := range rules {
|
||||
if rule.Listen == listen {
|
||||
rules = append(rules[:i], rules[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
if err := SaveRules(); err != nil {
|
||||
c.JSON(500, gin.H{"error": "Failed to save rules"})
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(200)
|
||||
})
|
||||
|
||||
// 启动服务
|
||||
authorized.POST("/start_service", func(c *gin.Context) {
|
||||
cmd := exec.Command("systemctl", "start", "realm")
|
||||
if err := cmd.Run(); err != nil {
|
||||
c.JSON(500, gin.H{"error": "Failed to start service"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"message": "Service started successfully"})
|
||||
})
|
||||
|
||||
// 停止服务
|
||||
authorized.POST("/stop_service", func(c *gin.Context) {
|
||||
cmd := exec.Command("systemctl", "stop", "realm")
|
||||
if err := cmd.Run(); err != nil {
|
||||
c.JSON(500, gin.H{"error": "Failed to stop service"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"message": "Service stopped successfully"})
|
||||
})
|
||||
|
||||
// 检查服务状态
|
||||
authorized.GET("/check_status", func(c *gin.Context) {
|
||||
cmd := exec.Command("systemctl", "is-active", "--quiet", "realm")
|
||||
err := cmd.Run()
|
||||
|
||||
var status string
|
||||
if err != nil {
|
||||
if exitError, ok := err.(*exec.ExitError); ok {
|
||||
if exitError.ExitCode() == 3 {
|
||||
status = "未启用"
|
||||
} else {
|
||||
status = "未知状态"
|
||||
}
|
||||
} else {
|
||||
status = "检查失败"
|
||||
}
|
||||
} else {
|
||||
status = "启用"
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"status": status})
|
||||
})
|
||||
}
|
||||
|
||||
port := panelConfig.Server.Port
|
||||
if port == 0 {
|
||||
port = 8081 // 默认端口
|
||||
}
|
||||
|
||||
if panelConfig.HTTPS.Enabled {
|
||||
if panelConfig.HTTPS.CertFile == "" || panelConfig.HTTPS.KeyFile == "" {
|
||||
log.Println("警告:HTTPS 已启用,但证书或密钥文件路径未指定。将使用 HTTP 继续。")
|
||||
log.Printf("服务器正在使用 HTTP 运行,端口:%d\n", port)
|
||||
r.Run(fmt.Sprintf(":%d", port))
|
||||
} else {
|
||||
log.Printf("服务器正在使用 HTTPS 运行,端口:%d\n", port)
|
||||
r.RunTLS(fmt.Sprintf(":%d", port), panelConfig.HTTPS.CertFile, panelConfig.HTTPS.KeyFile)
|
||||
}
|
||||
} else {
|
||||
log.Println("警告:未启用 HTTPS,将使用 HTTP 继续。")
|
||||
log.Printf("服务器正在使用 HTTP 运行,端口:%d\n", port)
|
||||
r.Run(fmt.Sprintf(":%d", port))
|
||||
}
|
||||
}
|
||||
|
134
web/static/app.js
Normal file
134
web/static/app.js
Normal file
@ -0,0 +1,134 @@
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const outputDiv = document.getElementById('output');
|
||||
const startButton = document.getElementById('startButton');
|
||||
const stopButton = document.getElementById('stopButton');
|
||||
const addRuleButton = document.getElementById('addRuleButton');
|
||||
const localPortInput = document.getElementById('localPort');
|
||||
const remoteIPInput = document.getElementById('remoteIP');
|
||||
const remotePortInput = document.getElementById('remotePort');
|
||||
|
||||
async function fetchForwardingRules() {
|
||||
try {
|
||||
const response = await fetch('/get_rules');
|
||||
if (!response.ok) {
|
||||
throw new Error('获取规则失败:' + response.statusText);
|
||||
}
|
||||
const rules = await response.json();
|
||||
const tbody = document.querySelector('#forwardingTable tbody');
|
||||
tbody.innerHTML = '';
|
||||
|
||||
rules.forEach((rule, index) => {
|
||||
const [, localPort] = rule.Listen.split(':');
|
||||
const lastColonIndex = rule.Remote.lastIndexOf(':');
|
||||
const remoteIP = rule.Remote.substring(0, lastColonIndex);
|
||||
const remotePort = rule.Remote.substring(lastColonIndex + 1);
|
||||
|
||||
const row = document.createElement('tr');
|
||||
row.innerHTML = `
|
||||
<td>${index + 1}</td>
|
||||
<td>${localPort}</td>
|
||||
<td>${remoteIP}</td>
|
||||
<td>${remotePort}</td>
|
||||
<td><button data-listen="${rule.Listen}" class="delete-btn">删除</button></td>
|
||||
`;
|
||||
tbody.appendChild(row);
|
||||
});
|
||||
|
||||
// 为删除按钮添加事件监听
|
||||
const deleteButtons = document.querySelectorAll('.delete-btn');
|
||||
deleteButtons.forEach(button => {
|
||||
button.addEventListener('click', function() {
|
||||
const listenAddress = this.getAttribute('data-listen');
|
||||
deleteRule(listenAddress);
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('请求失败:', error);
|
||||
outputDiv.textContent = '获取转发规则失败';
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteRule(listenAddress) {
|
||||
try {
|
||||
const response = await fetch(`/delete_rule?listen=${encodeURIComponent(listenAddress)}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('删除规则失败:' + response.statusText);
|
||||
}
|
||||
fetchForwardingRules(); // 重新获取规则列表
|
||||
} catch (error) {
|
||||
console.error('删除规则失败:', error);
|
||||
outputDiv.textContent = '删除规则失败';
|
||||
}
|
||||
}
|
||||
|
||||
async function addRule() {
|
||||
const localPort = localPortInput.value;
|
||||
const remoteIP = remoteIPInput.value;
|
||||
const remotePort = remotePortInput.value;
|
||||
|
||||
if (!localPort || !remoteIP || !remotePort) {
|
||||
outputDiv.textContent = '请填写所有字段';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/add_rule', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
localPort,
|
||||
remoteIP,
|
||||
remotePort
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('添加规则失败:' + response.statusText);
|
||||
}
|
||||
fetchForwardingRules(); // 重新获取规则列表
|
||||
} catch (error) {
|
||||
console.error('添加规则失败:', error);
|
||||
outputDiv.textContent = '添加规则失败';
|
||||
}
|
||||
}
|
||||
|
||||
startButton.addEventListener('click', async () => {
|
||||
try {
|
||||
const response = await fetch('/start_service', {
|
||||
method: 'POST'
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('启动服务失败:' + response.statusText);
|
||||
}
|
||||
const result = await response.json();
|
||||
outputDiv.textContent = result.output;
|
||||
} catch (error) {
|
||||
console.error('启动服务失败:', error);
|
||||
outputDiv.textContent = '启动服务失败';
|
||||
}
|
||||
});
|
||||
|
||||
stopButton.addEventListener('click', async () => {
|
||||
try {
|
||||
const response = await fetch('/stop_service', {
|
||||
method: 'POST'
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('停止服务失败:' + response.statusText);
|
||||
}
|
||||
const result = await response.json();
|
||||
outputDiv.textContent = result.output;
|
||||
} catch (error) {
|
||||
console.error('停止服务失败:', error);
|
||||
outputDiv.textContent = '停止服务失败';
|
||||
}
|
||||
});
|
||||
|
||||
addRuleButton.addEventListener('click', addRule);
|
||||
|
||||
// 初始化时获取规则列表
|
||||
fetchForwardingRules();
|
||||
});
|
330
web/templates/index.html
Normal file
330
web/templates/index.html
Normal file
@ -0,0 +1,330 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Realm 转发管理面板</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
background-color: #f4f4f4;
|
||||
}
|
||||
h1 {
|
||||
color: #333;
|
||||
text-align: center;
|
||||
}
|
||||
.container {
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
background-color: #fff;
|
||||
padding: 20px;
|
||||
border-radius: 5px;
|
||||
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
button {
|
||||
padding: 10px 15px;
|
||||
margin: 5px;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
background-color: #28a745;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
button:hover {
|
||||
background-color: #218838;
|
||||
}
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-top: 20px;
|
||||
}
|
||||
th, td {
|
||||
border: 1px solid #ddd;
|
||||
padding: 10px;
|
||||
text-align: left;
|
||||
}
|
||||
th {
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
.form-group {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
.form-group label {
|
||||
display: block;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
.form-group input {
|
||||
width: calc(100% - 22px);
|
||||
padding: 10px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 5px;
|
||||
}
|
||||
#output {
|
||||
margin-top: 20px;
|
||||
background-color: #fff;
|
||||
padding: 10px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 5px;
|
||||
min-height: 50px;
|
||||
white-space: pre-wrap; /* 保持空格和换行 */
|
||||
}
|
||||
.status-tag {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 4px 8px;
|
||||
font-size: 14px;
|
||||
line-height: 20px;
|
||||
border-radius: 4px;
|
||||
margin-left: 10px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.status-tag.running {
|
||||
background-color: #52c41a;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.status-tag.stopped {
|
||||
background-color: #ff4d4f;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.status-wrapper {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
margin-left: 15px;
|
||||
}
|
||||
|
||||
.status-label {
|
||||
color: #666;
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
.button-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<h1>Realm 转发管理面板</h1>
|
||||
|
||||
<div class="button-group">
|
||||
<button id="startButton">启动服务</button>
|
||||
<button id="stopButton">停止服务</button>
|
||||
<div class="status-wrapper">
|
||||
<span class="status-label">状态:</span>
|
||||
<span id="serviceStatus" class="status-tag">检查中...</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="output"></div>
|
||||
|
||||
<h2>当前转发规则</h2>
|
||||
<table id="forwardingTable">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>序号</th>
|
||||
<th>中转端口</th>
|
||||
<th>落地鸡 IP</th>
|
||||
<th>目标端口</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<!-- 动态填充转发规则 -->
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>添加转发规则</h2>
|
||||
<div class="form-group">
|
||||
<label for="localPort">中转端口:</label>
|
||||
<input type="number" id="localPort" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="remoteIP">落地鸡 IP:</label>
|
||||
<input type="text" id="remoteIP" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="remotePort">目标端口:</label>
|
||||
<input type="number" id="remotePort" required>
|
||||
</div>
|
||||
<button id="addRuleButton">添加规则</button>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function updateServiceStatus() {
|
||||
try {
|
||||
const response = await fetch(`/check_status`, {
|
||||
method: 'GET'
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('检查状态失败:' + response.statusText);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const statusElement = document.getElementById('serviceStatus');
|
||||
|
||||
if (data.status === "启用") {
|
||||
statusElement.textContent = "运行中";
|
||||
statusElement.className = 'status-tag running';
|
||||
} else {
|
||||
statusElement.textContent = "已停止";
|
||||
statusElement.className = 'status-tag stopped';
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('更新状态失败:', error);
|
||||
document.getElementById('serviceStatus').textContent = '未知';
|
||||
document.getElementById('serviceStatus').className = 'status-tag stopped';
|
||||
}
|
||||
}
|
||||
|
||||
// 启动服务
|
||||
document.getElementById('startButton').onclick = async function() {
|
||||
try {
|
||||
const response = await fetch(`/start_service`, {
|
||||
method: 'POST'
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('启动服务失败:' + response.statusText);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
document.getElementById('output').innerText = data.message || '服务启动成功';
|
||||
await updateServiceStatus();
|
||||
} catch (error) {
|
||||
console.error('启动服务失败:', error);
|
||||
document.getElementById('output').innerText = error.message;
|
||||
}
|
||||
};
|
||||
|
||||
// 停止服务
|
||||
document.getElementById('stopButton').onclick = async function() {
|
||||
try {
|
||||
const response = await fetch(`/stop_service`, {
|
||||
method: 'POST'
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('停止服务失败:' + response.statusText);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
document.getElementById('output').innerText = data.message || '服务停止成功';
|
||||
await updateServiceStatus();
|
||||
} catch (error) {
|
||||
console.error('停止服务失败:', error);
|
||||
document.getElementById('output').innerText = error.message;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// 添加转发规则
|
||||
document.getElementById('addRuleButton').onclick = async function() {
|
||||
const localPort = document.getElementById('localPort').value;
|
||||
const remoteIP = document.getElementById('remoteIP').value;
|
||||
const remotePort = document.getElementById('remotePort').value;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/add_rule`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
listen: `0.0.0.0:${localPort}`,
|
||||
remote: `${remoteIP}:${remotePort}`
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('添加规则失败:' + response.statusText);
|
||||
}
|
||||
|
||||
alert('转发规则添加成功!');
|
||||
await fetchForwardingRules(); // 刷新规则列表
|
||||
} catch (error) {
|
||||
console.error('添加规则失败:', error);
|
||||
alert('添加规则失败:' + error.message);
|
||||
}
|
||||
};
|
||||
|
||||
// 删除转发规则
|
||||
async function deleteRule(listenAddress) {
|
||||
try {
|
||||
const response = await fetch(`/delete_rule?listen=${encodeURIComponent(listenAddress)}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('删除规则失败:' + response.statusText);
|
||||
}
|
||||
|
||||
alert('转发规则删除成功!');
|
||||
await fetchForwardingRules(); // 刷新规则列表
|
||||
} catch (error) {
|
||||
console.error('删除规则失败:', error);
|
||||
alert('删除规则失败:' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取当前转发规则
|
||||
async function fetchForwardingRules() {
|
||||
try {
|
||||
const response = await fetch(`/get_rules`);
|
||||
if (!response.ok) {
|
||||
throw new Error('获取规则失败:' + response.statusText);
|
||||
}
|
||||
const rules = await response.json();
|
||||
const tbody = document.querySelector('#forwardingTable tbody');
|
||||
tbody.innerHTML = ''; // 清空现有规则
|
||||
|
||||
rules.forEach((rule, index) => {
|
||||
const row = document.createElement('tr');
|
||||
let localPort = '', remoteIP = '', remotePort = '';
|
||||
|
||||
if (rule.Listen) {
|
||||
[, localPort] = rule.Listen.split(':');
|
||||
}
|
||||
|
||||
if (rule.Remote) {
|
||||
[remoteIP, remotePort] = rule.Remote.split(':');
|
||||
}
|
||||
|
||||
row.innerHTML = `
|
||||
<td>${index + 1}</td>
|
||||
<td>${localPort || 'N/A'}</td>
|
||||
<td>${remoteIP || 'N/A'}</td>
|
||||
<td>${remotePort || 'N/A'}</td>
|
||||
<td><button onclick="deleteRule('${rule.Listen || ''}')">删除</button></td>
|
||||
`;
|
||||
tbody.appendChild(row);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取转发规则失败:', error);
|
||||
alert('获取转发规则失败:' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 页面加载时获取转发规则和服务状态
|
||||
window.onload = async function() {
|
||||
await fetchForwardingRules();
|
||||
await updateServiceStatus();
|
||||
|
||||
// 每10秒更新一次状态
|
||||
setInterval(updateServiceStatus, 10000);
|
||||
};
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
133
web/templates/login.html
Normal file
133
web/templates/login.html
Normal file
@ -0,0 +1,133 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Realm 转发管理面板 - 登录</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
background-color: #f4f4f4;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
.login-container {
|
||||
max-width: 400px;
|
||||
width: 100%;
|
||||
background-color: #fff;
|
||||
padding: 30px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
h1 {
|
||||
color: #333;
|
||||
text-align: center;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
.form-group {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.form-group label {
|
||||
display: block;
|
||||
margin-bottom: 5px;
|
||||
color: #666;
|
||||
}
|
||||
.form-group input {
|
||||
width: 100%;
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
.password-input-wrapper {
|
||||
position: relative;
|
||||
}
|
||||
.password-toggle {
|
||||
position: absolute;
|
||||
right: 10px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
background: none;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
color: #666;
|
||||
}
|
||||
button[type="submit"] {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
background-color: #28a745;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
}
|
||||
button[type="submit"]:hover {
|
||||
background-color: #218838;
|
||||
}
|
||||
.error-message {
|
||||
color: #dc3545;
|
||||
margin-top: 10px;
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="login-container">
|
||||
<h1>Realm 转发管理面板</h1>
|
||||
<form id="loginForm">
|
||||
<div class="form-group">
|
||||
<div class="password-input-wrapper">
|
||||
<input type="password" id="password" placeholder="请输入密码" required>
|
||||
<button type="button" class="password-toggle" id="togglePassword">👁</button>
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit">登录</button>
|
||||
<div id="errorMessage" class="error-message"></div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.getElementById('togglePassword').addEventListener('click', function() {
|
||||
const passwordInput = document.getElementById('password');
|
||||
if (passwordInput.type === 'password') {
|
||||
passwordInput.type = 'text';
|
||||
this.textContent = '👁🗨';
|
||||
} else {
|
||||
passwordInput.type = 'password';
|
||||
this.textContent = '👁';
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('loginForm').addEventListener('submit', async function(e) {
|
||||
e.preventDefault();
|
||||
const password = document.getElementById('password').value;
|
||||
|
||||
try {
|
||||
const response = await fetch('/login', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ password }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
window.location.href = '/';
|
||||
} else {
|
||||
document.getElementById('errorMessage').textContent = data.error || '登录失败';
|
||||
}
|
||||
} catch (error) {
|
||||
document.getElementById('errorMessage').textContent = '登录失败,请重试';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user