Refine WireGuard client routing and authentication UI

This commit is contained in:
opsx committed 2026-10-21 21:10:53 +08:00
1 parent eabb3e4472
commit 41fb9d5631
33 files changed
+1709 -469

No files matched your search

+18 -7
View File
@@ -269,12 +269,13 @@ func authorizedLines(username, password, otp string) ([]model.VPNUser, bool, err
}
type clientLine struct {
TunnelID uint `json:"tunnel_id"`
Name string `json:"name"`
Remark string `json:"remark"`
State string `json:"state"`
Endpoint string `json:"endpoint"`
ExpireAt *time.Time `json:"expire_at"`
TunnelID uint `json:"tunnel_id"`
Name string `json:"name"`
Remark string `json:"remark"`
State string `json:"state"`
Endpoint string `json:"endpoint"`
RoutingMode string `json:"routing_mode"`
ExpireAt *time.Time `json:"expire_at"`
}
// linesForUsers 把授权拨号记录映射为 WireGuard 线路列表。
@@ -290,15 +291,25 @@ func linesForUsers(users []model.VPNUser) []clientLine {
if err != nil || t.Protocol != "wireguard" || (t.ExpireAt != nil && time.Now().After(*t.ExpireAt)) {
continue
}
line := clientLine{TunnelID: t.ID, Name: t.Name, Remark: t.Remark, State: t.State, ExpireAt: t.ExpireAt}
line := clientLine{TunnelID: t.ID, Name: t.Name, Remark: t.Remark, State: t.State, ExpireAt: t.ExpireAt, RoutingMode: "split"}
if cfg, err := store.TunnelConfig(t); err == nil {
line.Endpoint = wireguardEndpoint(cfg)
line.RoutingMode = routingModeForAllowedIPs(fmt.Sprint(cfg["allowed_ips"]))
}
lines = append(lines, line)
}
return lines
}
func routingModeForAllowedIPs(value string) string {
for _, entry := range strings.Split(value, ",") {
if strings.TrimSpace(entry) == "0.0.0.0/0" {
return "global_ipv4"
}
}
return "split"
}
func wireguardEndpoint(cfg map[string]interface{}) string {
if ep, ok := cfg["public_endpoint"].(string); ok && ep != "" {
return ep
@@ -0,0 +1,16 @@
package api
import "testing"
func TestRoutingModeReflectsOnlyAuthorizedDefaultRoute(t *testing.T) {
for _, tc := range []struct{ allowed, want string }{
{"10.88.0.0/24", "split"},
{"10.88.0.0/24, 0.0.0.0/0", "global_ipv4"},
{"0.0.0.0/00", "split"},
{"", "split"},
} {
if got := routingModeForAllowedIPs(tc.allowed); got != tc.want {
t.Errorf("%q: got %q, want %q", tc.allowed, got, tc.want)
}
}
}