This commit is contained in:
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# 编译输出
|
||||||
|
bin/
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Go
|
||||||
|
*.test
|
||||||
|
*.out
|
||||||
|
go.work
|
||||||
|
go.work.sum
|
||||||
|
|
||||||
|
# IDE
|
||||||
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
*~
|
||||||
|
|
||||||
|
# 系统文件
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
|
|
||||||
|
# 前端 node_modules
|
||||||
|
web/node_modules/
|
||||||
|
|
||||||
|
# 前端构建产物 (源码在 web/dist,嵌入用的在 pkg/webserver/dist)
|
||||||
|
web/dist/
|
||||||
|
pkg/webserver/dist/
|
||||||
|
|
||||||
|
# 日志
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# 配置文件 (包含敏感信息)
|
||||||
|
server.yaml
|
||||||
|
client.yaml
|
||||||
BIN
bin/server
BIN
bin/server
Binary file not shown.
@@ -2,10 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/gotunnel/pkg/config"
|
"github.com/gotunnel/pkg/config"
|
||||||
"github.com/gotunnel/pkg/tunnel"
|
"github.com/gotunnel/pkg/tunnel"
|
||||||
|
"github.com/gotunnel/pkg/webserver"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -18,5 +20,24 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
server := tunnel.NewServer(cfg)
|
server := tunnel.NewServer(cfg)
|
||||||
|
|
||||||
|
// 启动 Web 控制台
|
||||||
|
if cfg.Web.Enabled {
|
||||||
|
ws := webserver.NewWebServer(cfg, *configPath, server)
|
||||||
|
addr := fmt.Sprintf("%s:%d", cfg.Web.BindAddr, cfg.Web.BindPort)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
var err error
|
||||||
|
if cfg.Web.Username != "" && cfg.Web.Password != "" {
|
||||||
|
err = ws.RunWithAuth(addr, cfg.Web.Username, cfg.Web.Password)
|
||||||
|
} else {
|
||||||
|
err = ws.Run(addr)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[Web] Server error: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
log.Fatal(server.Run())
|
log.Fatal(server.Run())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
// ServerConfig 服务端配置
|
// ServerConfig 服务端配置
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
Server ServerSettings `yaml:"server"`
|
Server ServerSettings `yaml:"server"`
|
||||||
|
Web WebSettings `yaml:"web"`
|
||||||
Clients []ClientConfig `yaml:"clients"`
|
Clients []ClientConfig `yaml:"clients"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,6 +23,15 @@ type ServerSettings struct {
|
|||||||
HeartbeatTimeout int `yaml:"heartbeat_timeout"`
|
HeartbeatTimeout int `yaml:"heartbeat_timeout"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WebSettings Web控制台设置
|
||||||
|
type WebSettings struct {
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
BindAddr string `yaml:"bind_addr"`
|
||||||
|
BindPort int `yaml:"bind_port"`
|
||||||
|
Username string `yaml:"username"`
|
||||||
|
Password string `yaml:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
// ClientConfig 客户端配置(服务端维护)
|
// ClientConfig 客户端配置(服务端维护)
|
||||||
type ClientConfig struct {
|
type ClientConfig struct {
|
||||||
ID string `yaml:"id"`
|
ID string `yaml:"id"`
|
||||||
@@ -48,6 +58,14 @@ func LoadServerConfig(path string) (*ServerConfig, error) {
|
|||||||
cfg.Server.HeartbeatTimeout = 90
|
cfg.Server.HeartbeatTimeout = 90
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Web 默认值
|
||||||
|
if cfg.Web.BindAddr == "" {
|
||||||
|
cfg.Web.BindAddr = "0.0.0.0"
|
||||||
|
}
|
||||||
|
if cfg.Web.BindPort == 0 {
|
||||||
|
cfg.Web.BindPort = 7500
|
||||||
|
}
|
||||||
|
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,3 +78,12 @@ func (c *ServerConfig) GetClientRules(clientID string) []protocol.ProxyRule {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SaveServerConfig 保存服务端配置
|
||||||
|
func SaveServerConfig(path string, cfg *ServerConfig) error {
|
||||||
|
data, err := yaml.Marshal(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.WriteFile(path, data, 0644)
|
||||||
|
}
|
||||||
|
|||||||
@@ -300,3 +300,63 @@ func (s *Server) heartbeatLoop(cs *ClientSession) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetClientStatus 获取客户端状态
|
||||||
|
func (s *Server) GetClientStatus(clientID string) (online bool, lastPing string) {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
|
||||||
|
if cs, ok := s.clients[clientID]; ok {
|
||||||
|
cs.mu.Lock()
|
||||||
|
defer cs.mu.Unlock()
|
||||||
|
return true, cs.LastPing.Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllClientStatus 获取所有客户端状态
|
||||||
|
func (s *Server) GetAllClientStatus() map[string]struct {
|
||||||
|
Online bool
|
||||||
|
LastPing string
|
||||||
|
} {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
|
||||||
|
result := make(map[string]struct {
|
||||||
|
Online bool
|
||||||
|
LastPing string
|
||||||
|
})
|
||||||
|
|
||||||
|
for id, cs := range s.clients {
|
||||||
|
cs.mu.Lock()
|
||||||
|
result[id] = struct {
|
||||||
|
Online bool
|
||||||
|
LastPing string
|
||||||
|
}{
|
||||||
|
Online: true,
|
||||||
|
LastPing: cs.LastPing.Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
cs.mu.Unlock()
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReloadConfig 重新加载配置
|
||||||
|
func (s *Server) ReloadConfig() error {
|
||||||
|
// 目前仅返回nil,后续可实现热重载
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetConfig 设置配置
|
||||||
|
func (s *Server) SetConfig(cfg *config.ServerConfig) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
s.config = cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetConfig 获取配置
|
||||||
|
func (s *Server) GetConfig() *config.ServerConfig {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
return s.config
|
||||||
|
}
|
||||||
|
|||||||
322
pkg/webserver/server.go
Normal file
322
pkg/webserver/server.go
Normal file
@@ -0,0 +1,322 @@
|
|||||||
|
package webserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/gotunnel/pkg/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed dist/*
|
||||||
|
var staticFiles embed.FS
|
||||||
|
|
||||||
|
// spaHandler SPA路由处理器
|
||||||
|
type spaHandler struct {
|
||||||
|
fs http.FileSystem
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
path := r.URL.Path
|
||||||
|
f, err := h.fs.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
f, err = h.fs.Open("index.html")
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Not Found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
stat, _ := f.Stat()
|
||||||
|
if stat.IsDir() {
|
||||||
|
f, err = h.fs.Open(path + "/index.html")
|
||||||
|
if err != nil {
|
||||||
|
f, _ = h.fs.Open("index.html")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
http.ServeContent(w, r, path, stat.ModTime(), f.(io.ReadSeeker))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClientStatus 客户端状态
|
||||||
|
type ClientStatus struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Online bool `json:"online"`
|
||||||
|
LastPing string `json:"last_ping,omitempty"`
|
||||||
|
RuleCount int `json:"rule_count"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServerInterface 服务端接口
|
||||||
|
type ServerInterface interface {
|
||||||
|
GetClientStatus(clientID string) (online bool, lastPing string)
|
||||||
|
GetAllClientStatus() map[string]struct {
|
||||||
|
Online bool
|
||||||
|
LastPing string
|
||||||
|
}
|
||||||
|
ReloadConfig() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// WebServer Web控制台服务
|
||||||
|
type WebServer struct {
|
||||||
|
config *config.ServerConfig
|
||||||
|
configPath string
|
||||||
|
server ServerInterface
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWebServer 创建Web服务
|
||||||
|
func NewWebServer(cfg *config.ServerConfig, configPath string, srv ServerInterface) *WebServer {
|
||||||
|
return &WebServer{
|
||||||
|
config: cfg,
|
||||||
|
configPath: configPath,
|
||||||
|
server: srv,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run 启动Web服务
|
||||||
|
func (w *WebServer) Run(addr string) error {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/status", w.handleStatus)
|
||||||
|
mux.HandleFunc("/api/clients", w.handleClients)
|
||||||
|
mux.HandleFunc("/api/client/", w.handleClient)
|
||||||
|
mux.HandleFunc("/api/config/reload", w.handleReload)
|
||||||
|
|
||||||
|
staticFS, err := fs.Sub(staticFiles, "dist")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
mux.Handle("/", spaHandler{fs: http.FS(staticFS)})
|
||||||
|
|
||||||
|
log.Printf("[Web] Console listening on %s", addr)
|
||||||
|
return http.ListenAndServe(addr, mux)
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleStatus 获取服务状态
|
||||||
|
func (w *WebServer) handleStatus(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.mu.RLock()
|
||||||
|
defer w.mu.RUnlock()
|
||||||
|
|
||||||
|
status := map[string]interface{}{
|
||||||
|
"server": map[string]interface{}{
|
||||||
|
"bind_addr": w.config.Server.BindAddr,
|
||||||
|
"bind_port": w.config.Server.BindPort,
|
||||||
|
},
|
||||||
|
"client_count": len(w.config.Clients),
|
||||||
|
}
|
||||||
|
w.jsonResponse(rw, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleClients 获取所有客户端
|
||||||
|
func (w *WebServer) handleClients(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
w.getClients(rw)
|
||||||
|
case http.MethodPost:
|
||||||
|
w.addClient(rw, r)
|
||||||
|
default:
|
||||||
|
http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebServer) getClients(rw http.ResponseWriter) {
|
||||||
|
w.mu.RLock()
|
||||||
|
defer w.mu.RUnlock()
|
||||||
|
|
||||||
|
var clients []ClientStatus
|
||||||
|
statusMap := w.server.GetAllClientStatus()
|
||||||
|
|
||||||
|
for _, c := range w.config.Clients {
|
||||||
|
cs := ClientStatus{ID: c.ID, RuleCount: len(c.Rules)}
|
||||||
|
if s, ok := statusMap[c.ID]; ok {
|
||||||
|
cs.Online = s.Online
|
||||||
|
cs.LastPing = s.LastPing
|
||||||
|
}
|
||||||
|
clients = append(clients, cs)
|
||||||
|
}
|
||||||
|
w.jsonResponse(rw, clients)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebServer) addClient(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
var client config.ClientConfig
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&client); err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if client.ID == "" {
|
||||||
|
http.Error(rw, "client id required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.mu.Lock()
|
||||||
|
for _, c := range w.config.Clients {
|
||||||
|
if c.ID == client.ID {
|
||||||
|
w.mu.Unlock()
|
||||||
|
http.Error(rw, "client already exists", http.StatusConflict)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.config.Clients = append(w.config.Clients, client)
|
||||||
|
w.mu.Unlock()
|
||||||
|
|
||||||
|
if err := w.saveConfig(); err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.jsonResponse(rw, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebServer) handleClient(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
clientID := r.URL.Path[len("/api/client/"):]
|
||||||
|
if clientID == "" {
|
||||||
|
http.Error(rw, "client id required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
w.getClient(rw, clientID)
|
||||||
|
case http.MethodPut:
|
||||||
|
w.updateClient(rw, r, clientID)
|
||||||
|
case http.MethodDelete:
|
||||||
|
w.deleteClient(rw, clientID)
|
||||||
|
default:
|
||||||
|
http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebServer) getClient(rw http.ResponseWriter, clientID string) {
|
||||||
|
w.mu.RLock()
|
||||||
|
defer w.mu.RUnlock()
|
||||||
|
|
||||||
|
for _, c := range w.config.Clients {
|
||||||
|
if c.ID == clientID {
|
||||||
|
online, lastPing := w.server.GetClientStatus(clientID)
|
||||||
|
w.jsonResponse(rw, map[string]interface{}{
|
||||||
|
"id": c.ID, "rules": c.Rules,
|
||||||
|
"online": online, "last_ping": lastPing,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
http.Error(rw, "client not found", http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebServer) updateClient(rw http.ResponseWriter, r *http.Request, clientID string) {
|
||||||
|
var client config.ClientConfig
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&client); err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.mu.Lock()
|
||||||
|
found := false
|
||||||
|
for i, c := range w.config.Clients {
|
||||||
|
if c.ID == clientID {
|
||||||
|
client.ID = clientID
|
||||||
|
w.config.Clients[i] = client
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.mu.Unlock()
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
http.Error(rw, "client not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := w.saveConfig(); err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.jsonResponse(rw, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebServer) deleteClient(rw http.ResponseWriter, clientID string) {
|
||||||
|
w.mu.Lock()
|
||||||
|
found := false
|
||||||
|
for i, c := range w.config.Clients {
|
||||||
|
if c.ID == clientID {
|
||||||
|
w.config.Clients = append(w.config.Clients[:i], w.config.Clients[i+1:]...)
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.mu.Unlock()
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
http.Error(rw, "client not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := w.saveConfig(); err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.jsonResponse(rw, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebServer) handleReload(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := w.server.ReloadConfig(); err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.jsonResponse(rw, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebServer) saveConfig() error {
|
||||||
|
w.mu.RLock()
|
||||||
|
defer w.mu.RUnlock()
|
||||||
|
return config.SaveServerConfig(w.configPath, w.config)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebServer) jsonResponse(rw http.ResponseWriter, data interface{}) {
|
||||||
|
rw.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(rw).Encode(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunWithAuth 启动带认证的Web服务
|
||||||
|
func (w *WebServer) RunWithAuth(addr, username, password string) error {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/status", w.handleStatus)
|
||||||
|
mux.HandleFunc("/api/clients", w.handleClients)
|
||||||
|
mux.HandleFunc("/api/client/", w.handleClient)
|
||||||
|
mux.HandleFunc("/api/config/reload", w.handleReload)
|
||||||
|
|
||||||
|
staticFS, err := fs.Sub(staticFiles, "dist")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to load static files: %v", err)
|
||||||
|
}
|
||||||
|
mux.Handle("/", spaHandler{fs: http.FS(staticFS)})
|
||||||
|
|
||||||
|
handler := &authMiddleware{username, password, mux}
|
||||||
|
log.Printf("[Web] Console listening on %s (auth enabled)", addr)
|
||||||
|
return http.ListenAndServe(addr, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
type authMiddleware struct {
|
||||||
|
username, password string
|
||||||
|
handler http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *authMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
user, pass, ok := r.BasicAuth()
|
||||||
|
if !ok || user != a.username || pass != a.password {
|
||||||
|
w.Header().Set("WWW-Authenticate", `Basic realm="GoTunnel"`)
|
||||||
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
a.handler.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
24
web/.gitignore
vendored
Normal file
24
web/.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
pnpm-debug.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
dist-ssr
|
||||||
|
*.local
|
||||||
|
|
||||||
|
# Editor directories and files
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/extensions.json
|
||||||
|
.idea
|
||||||
|
.DS_Store
|
||||||
|
*.suo
|
||||||
|
*.ntvs*
|
||||||
|
*.njsproj
|
||||||
|
*.sln
|
||||||
|
*.sw?
|
||||||
5
web/README.md
Normal file
5
web/README.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Vue 3 + TypeScript + Vite
|
||||||
|
|
||||||
|
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
|
||||||
|
|
||||||
|
Learn more about the recommended Project Setup and IDE Support in the [Vue Docs TypeScript Guide](https://vuejs.org/guide/typescript/overview.html#project-setup).
|
||||||
13
web/index.html
Normal file
13
web/index.html
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>webui</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
<script type="module" src="/src/main.ts"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
1740
web/package-lock.json
generated
Normal file
1740
web/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
24
web/package.json
Normal file
24
web/package.json
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "webui",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "vue-tsc -b && vite build",
|
||||||
|
"preview": "vite preview"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"axios": "^1.13.2",
|
||||||
|
"vue": "^3.5.24",
|
||||||
|
"vue-router": "^4.6.4"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^24.10.1",
|
||||||
|
"@vitejs/plugin-vue": "^6.0.1",
|
||||||
|
"@vue/tsconfig": "^0.8.1",
|
||||||
|
"typescript": "~5.9.3",
|
||||||
|
"vite": "^7.2.4",
|
||||||
|
"vue-tsc": "^3.1.4"
|
||||||
|
}
|
||||||
|
}
|
||||||
1
web/public/vite.svg
Normal file
1
web/public/vite.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
||||||
|
After Width: | Height: | Size: 1.5 KiB |
55
web/src/App.vue
Normal file
55
web/src/App.vue
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { ref, onMounted } from 'vue'
|
||||||
|
import { RouterView } from 'vue-router'
|
||||||
|
import { getServerStatus } from './api'
|
||||||
|
|
||||||
|
const serverInfo = ref({ bind_addr: '', bind_port: 0 })
|
||||||
|
const clientCount = ref(0)
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
try {
|
||||||
|
const { data } = await getServerStatus()
|
||||||
|
serverInfo.value = data.server
|
||||||
|
clientCount.value = data.client_count
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to get server status', e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="app">
|
||||||
|
<header class="header">
|
||||||
|
<h1>GoTunnel 控制台</h1>
|
||||||
|
<div class="server-info">
|
||||||
|
<span>{{ serverInfo.bind_addr }}:{{ serverInfo.bind_port }}</span>
|
||||||
|
<span class="badge">{{ clientCount }} 客户端</span>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
<main class="main">
|
||||||
|
<RouterView />
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.app { min-height: 100vh; background: #f5f7fa; }
|
||||||
|
.header {
|
||||||
|
background: #fff;
|
||||||
|
padding: 16px 24px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
.header h1 { font-size: 20px; color: #2c3e50; }
|
||||||
|
.server-info { display: flex; align-items: center; gap: 12px; color: #666; }
|
||||||
|
.badge {
|
||||||
|
background: #3498db;
|
||||||
|
color: #fff;
|
||||||
|
padding: 4px 12px;
|
||||||
|
border-radius: 12px;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
.main { padding: 24px; max-width: 1200px; margin: 0 auto; }
|
||||||
|
</style>
|
||||||
17
web/src/api/index.ts
Normal file
17
web/src/api/index.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import axios from 'axios'
|
||||||
|
import type { ClientConfig, ClientStatus, ClientDetail, ServerStatus } from '../types'
|
||||||
|
|
||||||
|
const api = axios.create({
|
||||||
|
baseURL: '/api',
|
||||||
|
timeout: 10000,
|
||||||
|
})
|
||||||
|
|
||||||
|
export const getServerStatus = () => api.get<ServerStatus>('/status')
|
||||||
|
export const getClients = () => api.get<ClientStatus[]>('/clients')
|
||||||
|
export const getClient = (id: string) => api.get<ClientDetail>(`/client/${id}`)
|
||||||
|
export const addClient = (client: ClientConfig) => api.post('/clients', client)
|
||||||
|
export const updateClient = (id: string, client: ClientConfig) => api.put(`/client/${id}`, client)
|
||||||
|
export const deleteClient = (id: string) => api.delete(`/client/${id}`)
|
||||||
|
export const reloadConfig = () => api.post('/config/reload')
|
||||||
|
|
||||||
|
export default api
|
||||||
1
web/src/assets/vue.svg
Normal file
1
web/src/assets/vue.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="37.07" height="36" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 198"><path fill="#41B883" d="M204.8 0H256L128 220.8L0 0h97.92L128 51.2L157.44 0h47.36Z"></path><path fill="#41B883" d="m0 0l128 220.8L256 0h-51.2L128 132.48L50.56 0H0Z"></path><path fill="#35495E" d="M50.56 0L128 133.12L204.8 0h-47.36L128 51.2L97.92 0H50.56Z"></path></svg>
|
||||||
|
After Width: | Height: | Size: 496 B |
41
web/src/components/HelloWorld.vue
Normal file
41
web/src/components/HelloWorld.vue
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
defineProps<{ msg: string }>()
|
||||||
|
|
||||||
|
const count = ref(0)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<h1>{{ msg }}</h1>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<button type="button" @click="count++">count is {{ count }}</button>
|
||||||
|
<p>
|
||||||
|
Edit
|
||||||
|
<code>components/HelloWorld.vue</code> to test HMR
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Check out
|
||||||
|
<a href="https://vuejs.org/guide/quick-start.html#local" target="_blank"
|
||||||
|
>create-vue</a
|
||||||
|
>, the official Vue + Vite starter
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Learn more about IDE Support for Vue in the
|
||||||
|
<a
|
||||||
|
href="https://vuejs.org/guide/scaling-up/tooling.html#ide-support"
|
||||||
|
target="_blank"
|
||||||
|
>Vue Docs Scaling up Guide</a
|
||||||
|
>.
|
||||||
|
</p>
|
||||||
|
<p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.read-the-docs {
|
||||||
|
color: #888;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
6
web/src/main.ts
Normal file
6
web/src/main.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import { createApp } from 'vue'
|
||||||
|
import './style.css'
|
||||||
|
import App from './App.vue'
|
||||||
|
import router from './router'
|
||||||
|
|
||||||
|
createApp(App).use(router).mount('#app')
|
||||||
19
web/src/router/index.ts
Normal file
19
web/src/router/index.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
|
|
||||||
|
const router = createRouter({
|
||||||
|
history: createWebHistory(),
|
||||||
|
routes: [
|
||||||
|
{
|
||||||
|
path: '/',
|
||||||
|
name: 'home',
|
||||||
|
component: () => import('../views/HomeView.vue'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/client/:id',
|
||||||
|
name: 'client',
|
||||||
|
component: () => import('../views/ClientView.vue'),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
export default router
|
||||||
79
web/src/style.css
Normal file
79
web/src/style.css
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
:root {
|
||||||
|
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
|
||||||
|
line-height: 1.5;
|
||||||
|
font-weight: 400;
|
||||||
|
|
||||||
|
color-scheme: light dark;
|
||||||
|
color: rgba(255, 255, 255, 0.87);
|
||||||
|
background-color: #242424;
|
||||||
|
|
||||||
|
font-synthesis: none;
|
||||||
|
text-rendering: optimizeLegibility;
|
||||||
|
-webkit-font-smoothing: antialiased;
|
||||||
|
-moz-osx-font-smoothing: grayscale;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
font-weight: 500;
|
||||||
|
color: #646cff;
|
||||||
|
text-decoration: inherit;
|
||||||
|
}
|
||||||
|
a:hover {
|
||||||
|
color: #535bf2;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
display: flex;
|
||||||
|
place-items: center;
|
||||||
|
min-width: 320px;
|
||||||
|
min-height: 100vh;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
font-size: 3.2em;
|
||||||
|
line-height: 1.1;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
padding: 0.6em 1.2em;
|
||||||
|
font-size: 1em;
|
||||||
|
font-weight: 500;
|
||||||
|
font-family: inherit;
|
||||||
|
background-color: #1a1a1a;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: border-color 0.25s;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
border-color: #646cff;
|
||||||
|
}
|
||||||
|
button:focus,
|
||||||
|
button:focus-visible {
|
||||||
|
outline: 4px auto -webkit-focus-ring-color;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card {
|
||||||
|
padding: 2em;
|
||||||
|
}
|
||||||
|
|
||||||
|
#app {
|
||||||
|
max-width: 1280px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 2rem;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: light) {
|
||||||
|
:root {
|
||||||
|
color: #213547;
|
||||||
|
background-color: #ffffff;
|
||||||
|
}
|
||||||
|
a:hover {
|
||||||
|
color: #747bff;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
}
|
||||||
|
}
|
||||||
38
web/src/types/index.ts
Normal file
38
web/src/types/index.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// 代理规则
|
||||||
|
export interface ProxyRule {
|
||||||
|
name: string
|
||||||
|
local_ip: string
|
||||||
|
local_port: number
|
||||||
|
remote_port: number
|
||||||
|
}
|
||||||
|
|
||||||
|
// 客户端配置
|
||||||
|
export interface ClientConfig {
|
||||||
|
id: string
|
||||||
|
rules: ProxyRule[]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 客户端状态
|
||||||
|
export interface ClientStatus {
|
||||||
|
id: string
|
||||||
|
online: boolean
|
||||||
|
last_ping?: string
|
||||||
|
rule_count: number
|
||||||
|
}
|
||||||
|
|
||||||
|
// 客户端详情
|
||||||
|
export interface ClientDetail {
|
||||||
|
id: string
|
||||||
|
rules: ProxyRule[]
|
||||||
|
online: boolean
|
||||||
|
last_ping?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
// 服务器状态
|
||||||
|
export interface ServerStatus {
|
||||||
|
server: {
|
||||||
|
bind_addr: string
|
||||||
|
bind_port: number
|
||||||
|
}
|
||||||
|
client_count: number
|
||||||
|
}
|
||||||
203
web/src/views/ClientView.vue
Normal file
203
web/src/views/ClientView.vue
Normal file
@@ -0,0 +1,203 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { ref, onMounted } from 'vue'
|
||||||
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
|
import { getClient, updateClient, deleteClient } from '../api'
|
||||||
|
import type { ProxyRule } from '../types'
|
||||||
|
|
||||||
|
const route = useRoute()
|
||||||
|
const router = useRouter()
|
||||||
|
const clientId = route.params.id as string
|
||||||
|
|
||||||
|
const online = ref(false)
|
||||||
|
const lastPing = ref('')
|
||||||
|
const rules = ref<ProxyRule[]>([])
|
||||||
|
const editing = ref(false)
|
||||||
|
const editRules = ref<ProxyRule[]>([])
|
||||||
|
|
||||||
|
const loadClient = async () => {
|
||||||
|
try {
|
||||||
|
const { data } = await getClient(clientId)
|
||||||
|
online.value = data.online
|
||||||
|
lastPing.value = data.last_ping || ''
|
||||||
|
rules.value = data.rules || []
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to load client', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(loadClient)
|
||||||
|
|
||||||
|
const startEdit = () => {
|
||||||
|
editRules.value = JSON.parse(JSON.stringify(rules.value))
|
||||||
|
editing.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
const cancelEdit = () => {
|
||||||
|
editing.value = false
|
||||||
|
}
|
||||||
|
|
||||||
|
const addRule = () => {
|
||||||
|
editRules.value.push({
|
||||||
|
name: '', local_ip: '127.0.0.1', local_port: 80, remote_port: 8080
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const removeRule = (index: number) => {
|
||||||
|
editRules.value.splice(index, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
const saveEdit = async () => {
|
||||||
|
try {
|
||||||
|
await updateClient(clientId, { id: clientId, rules: editRules.value })
|
||||||
|
editing.value = false
|
||||||
|
loadClient()
|
||||||
|
} catch (e) {
|
||||||
|
alert('保存失败')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const confirmDelete = async () => {
|
||||||
|
if (!confirm('确定删除此客户端?')) return
|
||||||
|
try {
|
||||||
|
await deleteClient(clientId)
|
||||||
|
router.push('/')
|
||||||
|
} catch (e) {
|
||||||
|
alert('删除失败')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="client-view">
|
||||||
|
<div class="header">
|
||||||
|
<button class="btn" @click="router.push('/')">← 返回</button>
|
||||||
|
<h2>{{ clientId }}</h2>
|
||||||
|
<span :class="['status-badge', online ? 'online' : 'offline']">
|
||||||
|
{{ online ? '在线' : '离线' }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="lastPing" class="ping-info">最后心跳: {{ lastPing }}</div>
|
||||||
|
|
||||||
|
<div class="rules-section">
|
||||||
|
<div class="section-header">
|
||||||
|
<h3>代理规则</h3>
|
||||||
|
<div v-if="!editing">
|
||||||
|
<button class="btn primary" @click="startEdit">编辑</button>
|
||||||
|
<button class="btn danger" @click="confirmDelete">删除</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 查看模式 -->
|
||||||
|
<table v-if="!editing" class="rules-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>名称</th>
|
||||||
|
<th>本地地址</th>
|
||||||
|
<th>远程端口</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr v-for="rule in rules" :key="rule.name">
|
||||||
|
<td>{{ rule.name }}</td>
|
||||||
|
<td>{{ rule.local_ip }}:{{ rule.local_port }}</td>
|
||||||
|
<td>{{ rule.remote_port }}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<!-- 编辑模式 -->
|
||||||
|
<div v-if="editing" class="edit-form">
|
||||||
|
<div v-for="(rule, i) in editRules" :key="i" class="rule-row">
|
||||||
|
<input v-model="rule.name" placeholder="名称" />
|
||||||
|
<input v-model="rule.local_ip" placeholder="本地IP" />
|
||||||
|
<input v-model.number="rule.local_port" type="number" placeholder="本地端口" />
|
||||||
|
<input v-model.number="rule.remote_port" type="number" placeholder="远程端口" />
|
||||||
|
<button class="btn-icon" @click="removeRule(i)">×</button>
|
||||||
|
</div>
|
||||||
|
<button class="btn secondary" @click="addRule">+ 添加规则</button>
|
||||||
|
<div class="edit-actions">
|
||||||
|
<button class="btn" @click="cancelEdit">取消</button>
|
||||||
|
<button class="btn primary" @click="saveEdit">保存</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 16px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.header h2 { margin: 0; }
|
||||||
|
.status-badge {
|
||||||
|
padding: 4px 12px;
|
||||||
|
border-radius: 12px;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
.status-badge.online { background: #d4edda; color: #155724; }
|
||||||
|
.status-badge.offline { background: #f8d7da; color: #721c24; }
|
||||||
|
.ping-info { color: #666; margin-bottom: 20px; }
|
||||||
|
.rules-section {
|
||||||
|
background: #fff;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 20px;
|
||||||
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
.section-header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
.section-header h3 { margin: 0; }
|
||||||
|
.section-header .btn { margin-left: 8px; }
|
||||||
|
.btn {
|
||||||
|
padding: 8px 16px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
.btn.primary { background: #3498db; color: #fff; }
|
||||||
|
.btn.secondary { background: #95a5a6; color: #fff; }
|
||||||
|
.btn.danger { background: #e74c3c; color: #fff; }
|
||||||
|
.rules-table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
.rules-table th, .rules-table td {
|
||||||
|
padding: 12px;
|
||||||
|
text-align: left;
|
||||||
|
border-bottom: 1px solid #eee;
|
||||||
|
}
|
||||||
|
.rules-table th { font-weight: 600; }
|
||||||
|
.rule-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
.rule-row input {
|
||||||
|
flex: 1;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
.btn-icon {
|
||||||
|
background: #e74c3c;
|
||||||
|
color: #fff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
width: 32px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.edit-actions {
|
||||||
|
display: flex;
|
||||||
|
justify-content: flex-end;
|
||||||
|
gap: 8px;
|
||||||
|
margin-top: 16px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
191
web/src/views/HomeView.vue
Normal file
191
web/src/views/HomeView.vue
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { ref, onMounted } from 'vue'
|
||||||
|
import { useRouter } from 'vue-router'
|
||||||
|
import { getClients, addClient } from '../api'
|
||||||
|
import type { ClientStatus, ProxyRule } from '../types'
|
||||||
|
|
||||||
|
const router = useRouter()
|
||||||
|
const clients = ref<ClientStatus[]>([])
|
||||||
|
const showModal = ref(false)
|
||||||
|
const newClientId = ref('')
|
||||||
|
const newRules = ref<ProxyRule[]>([])
|
||||||
|
|
||||||
|
const loadClients = async () => {
|
||||||
|
try {
|
||||||
|
const { data } = await getClients()
|
||||||
|
clients.value = data || []
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to load clients', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(loadClients)
|
||||||
|
|
||||||
|
const openAddModal = () => {
|
||||||
|
newClientId.value = ''
|
||||||
|
newRules.value = [{ name: '', local_ip: '127.0.0.1', local_port: 80, remote_port: 8080 }]
|
||||||
|
showModal.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
const addRule = () => {
|
||||||
|
newRules.value.push({ name: '', local_ip: '127.0.0.1', local_port: 80, remote_port: 8080 })
|
||||||
|
}
|
||||||
|
|
||||||
|
const removeRule = (index: number) => {
|
||||||
|
newRules.value.splice(index, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
const saveClient = async () => {
|
||||||
|
if (!newClientId.value) return
|
||||||
|
try {
|
||||||
|
await addClient({ id: newClientId.value, rules: newRules.value })
|
||||||
|
showModal.value = false
|
||||||
|
loadClients()
|
||||||
|
} catch (e) {
|
||||||
|
alert('添加失败')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const viewClient = (id: string) => {
|
||||||
|
router.push(`/client/${id}`)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="home">
|
||||||
|
<div class="toolbar">
|
||||||
|
<h2>客户端列表</h2>
|
||||||
|
<button class="btn primary" @click="openAddModal">添加客户端</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="client-grid">
|
||||||
|
<div v-for="client in clients" :key="client.id" class="client-card" @click="viewClient(client.id)">
|
||||||
|
<div class="card-header">
|
||||||
|
<span class="client-id">{{ client.id }}</span>
|
||||||
|
<span :class="['status', client.online ? 'online' : 'offline']"></span>
|
||||||
|
</div>
|
||||||
|
<div class="card-info">
|
||||||
|
<span>{{ client.rule_count }} 条规则</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="clients.length === 0" class="empty">暂无客户端配置</div>
|
||||||
|
|
||||||
|
<!-- 添加客户端模态框 -->
|
||||||
|
<div v-if="showModal" class="modal-overlay" @click.self="showModal = false">
|
||||||
|
<div class="modal">
|
||||||
|
<h3>添加客户端</h3>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>客户端 ID</label>
|
||||||
|
<input v-model="newClientId" placeholder="例如: client-a" />
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>代理规则</label>
|
||||||
|
<div v-for="(rule, i) in newRules" :key="i" class="rule-row">
|
||||||
|
<input v-model="rule.name" placeholder="名称" />
|
||||||
|
<input v-model="rule.local_ip" placeholder="本地IP" />
|
||||||
|
<input v-model.number="rule.local_port" type="number" placeholder="本地端口" />
|
||||||
|
<input v-model.number="rule.remote_port" type="number" placeholder="远程端口" />
|
||||||
|
<button class="btn-icon" @click="removeRule(i)">×</button>
|
||||||
|
</div>
|
||||||
|
<button class="btn secondary" @click="addRule">+ 添加规则</button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-actions">
|
||||||
|
<button class="btn" @click="showModal = false">取消</button>
|
||||||
|
<button class="btn primary" @click="saveClient">保存</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.toolbar {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.toolbar h2 { font-size: 18px; color: #2c3e50; }
|
||||||
|
.btn {
|
||||||
|
padding: 8px 16px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
.btn.primary { background: #3498db; color: #fff; }
|
||||||
|
.btn.secondary { background: #95a5a6; color: #fff; }
|
||||||
|
.client-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
.client-card {
|
||||||
|
background: #fff;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 16px;
|
||||||
|
cursor: pointer;
|
||||||
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||||
|
transition: transform 0.2s;
|
||||||
|
}
|
||||||
|
.client-card:hover { transform: translateY(-2px); }
|
||||||
|
.card-header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
.client-id { font-weight: 600; }
|
||||||
|
.status { width: 10px; height: 10px; border-radius: 50%; }
|
||||||
|
.status.online { background: #27ae60; }
|
||||||
|
.status.offline { background: #95a5a6; }
|
||||||
|
.card-info { font-size: 14px; color: #666; }
|
||||||
|
.empty { text-align: center; color: #999; padding: 40px; }
|
||||||
|
.modal-overlay {
|
||||||
|
position: fixed;
|
||||||
|
top: 0; left: 0; right: 0; bottom: 0;
|
||||||
|
background: rgba(0,0,0,0.5);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.modal {
|
||||||
|
background: #fff;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 24px;
|
||||||
|
width: 500px;
|
||||||
|
max-width: 90%;
|
||||||
|
}
|
||||||
|
.modal h3 { margin-bottom: 16px; }
|
||||||
|
.form-group { margin-bottom: 16px; }
|
||||||
|
.form-group label { display: block; margin-bottom: 8px; font-weight: 500; }
|
||||||
|
.form-group input {
|
||||||
|
width: 100%;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 4px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
.rule-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
.rule-row input { flex: 1; width: auto; }
|
||||||
|
.btn-icon {
|
||||||
|
background: #e74c3c;
|
||||||
|
color: #fff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
width: 32px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.modal-actions {
|
||||||
|
display: flex;
|
||||||
|
justify-content: flex-end;
|
||||||
|
gap: 8px;
|
||||||
|
margin-top: 16px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
16
web/tsconfig.app.json
Normal file
16
web/tsconfig.app.json
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"extends": "@vue/tsconfig/tsconfig.dom.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
||||||
|
"types": ["vite/client"],
|
||||||
|
|
||||||
|
/* Linting */
|
||||||
|
"strict": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"erasableSyntaxOnly": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"noUncheckedSideEffectImports": true
|
||||||
|
},
|
||||||
|
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
|
||||||
|
}
|
||||||
7
web/tsconfig.json
Normal file
7
web/tsconfig.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"files": [],
|
||||||
|
"references": [
|
||||||
|
{ "path": "./tsconfig.app.json" },
|
||||||
|
{ "path": "./tsconfig.node.json" }
|
||||||
|
]
|
||||||
|
}
|
||||||
26
web/tsconfig.node.json
Normal file
26
web/tsconfig.node.json
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
||||||
|
"target": "ES2023",
|
||||||
|
"lib": ["ES2023"],
|
||||||
|
"module": "ESNext",
|
||||||
|
"types": ["node"],
|
||||||
|
"skipLibCheck": true,
|
||||||
|
|
||||||
|
/* Bundler mode */
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"allowImportingTsExtensions": true,
|
||||||
|
"verbatimModuleSyntax": true,
|
||||||
|
"moduleDetection": "force",
|
||||||
|
"noEmit": true,
|
||||||
|
|
||||||
|
/* Linting */
|
||||||
|
"strict": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"erasableSyntaxOnly": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"noUncheckedSideEffectImports": true
|
||||||
|
},
|
||||||
|
"include": ["vite.config.ts"]
|
||||||
|
}
|
||||||
7
web/vite.config.ts
Normal file
7
web/vite.config.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { defineConfig } from 'vite'
|
||||||
|
import vue from '@vitejs/plugin-vue'
|
||||||
|
|
||||||
|
// https://vite.dev/config/
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [vue()],
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user