feat: latency test url for wg · PasarGuard/node@e4e1946 · GitHub
Skip to content

Commit e4e1946

Browse files
committed
feat: latency test url for wg
1 parent c86de3a commit e4e1946

5 files changed

Lines changed: 58 additions & 17 deletions

File tree

.env.example

Lines changed: 0 additions & 2 deletions

backend/wireguard/config.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313

1414
// Config represents the WireGuard configuration
1515
type Config struct {
16-
InterfaceName string `json:"interface_name"`
17-
PrivateKey string `json:"private_key"`
18-
PreSharedKey string `json:"pre_shared_key,omitempty"`
19-
ListenPort int `json:"listen_port"`
20-
Address []string `json:"address"`
16+
InterfaceName string `json:"interface_name"`
17+
PrivateKey string `json:"private_key"`
18+
PreSharedKey string `json:"pre_shared_key,omitempty"`
19+
ListenPort int `json:"listen_port"`
20+
Address []string `json:"address"`
21+
Latency *LatencyConfig `json:"latency,omitempty"`
2122

2223
privateKeyValue wgtypes.Key
2324
privateKeySet bool
@@ -27,6 +28,11 @@ type Config struct {
2728
mu sync.RWMutex
2829
}
2930

31+
type LatencyConfig struct {
32+
TestURL string `json:"test_url,omitempty"`
33+
TimeoutSeconds int `json:"timeout_seconds,omitempty"`
34+
}
35+
3036
// PeerInfo stores information about a WireGuard peer
3137
type PeerInfo struct {
3238
Email string `json:"email"`
@@ -62,6 +68,15 @@ func NewConfig(config string) (*Config, error) {
6268
if wgConfig.ListenPort <= 0 {
6369
wgConfig.ListenPort = 51820
6470
}
71+
if wgConfig.Latency == nil {
72+
wgConfig.Latency = &LatencyConfig{}
73+
}
74+
if strings.TrimSpace(wgConfig.Latency.TestURL) == "" {
75+
wgConfig.Latency.TestURL = "https://www.gstatic.com/generate_204"
76+
}
77+
if wgConfig.Latency.TimeoutSeconds <= 0 {
78+
wgConfig.Latency.TimeoutSeconds = 5
79+
}
6580

6681
return &wgConfig, nil
6782
}

backend/wireguard/config_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,38 @@ func TestNewWireGuardConfigDefaults(t *testing.T) {
6666
if config.ListenPort != 51820 {
6767
t.Errorf("Expected default listen_port 51820, got: %d", config.ListenPort)
6868
}
69+
if config.Latency == nil {
70+
t.Fatal("expected default latency config")
71+
}
72+
if config.Latency.TestURL != "https://www.gstatic.com/generate_204" {
73+
t.Errorf("expected default latency.test_url, got: %s", config.Latency.TestURL)
74+
}
75+
if config.Latency.TimeoutSeconds != 5 {
76+
t.Errorf("expected default latency.timeout_seconds 5, got: %d", config.Latency.TimeoutSeconds)
77+
}
78+
}
79+
80+
func TestNewWireGuardConfigLatencyNested(t *testing.T) {
81+
configJSON := `{
82+
"latency": {
83+
"test_url": "https://example.com/generate_204",
84+
"timeout_seconds": 9
85+
}
86+
}`
87+
88+
config, err := NewConfig(configJSON)
89+
if err != nil {
90+
t.Fatalf("NewConfig failed: %v", err)
91+
}
92+
if config.Latency == nil {
93+
t.Fatal("expected latency config")
94+
}
95+
if config.Latency.TestURL != "https://example.com/generate_204" {
96+
t.Errorf("expected latency.test_url to round-trip, got: %s", config.Latency.TestURL)
97+
}
98+
if config.Latency.TimeoutSeconds != 9 {
99+
t.Errorf("expected latency.timeout_seconds 9, got: %d", config.Latency.TimeoutSeconds)
100+
}
69101
}
70102

71103
func TestNewWireGuardConfigInvalidJSON(t *testing.T) {

backend/wireguard/latency.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ func (wg *WireGuard) latencyProbeInterface() string {
2828
func (wg *WireGuard) GetOutboundsLatency(ctx context.Context, request *common.LatencyRequest) (*common.LatencyResponse, error) {
2929
wg.mu.RLock()
3030
state := wg.state
31-
testURL := wg.cfg.LatencyTestURL
32-
timeoutSeconds := wg.cfg.LatencyTimeoutSeconds
31+
testURL := ""
32+
timeoutSeconds := 0
33+
if wg.config != nil && wg.config.Latency != nil {
34+
testURL = wg.config.Latency.TestURL
35+
timeoutSeconds = wg.config.Latency.TimeoutSeconds
36+
}
3337
wg.mu.RUnlock()
3438

3539
if state != lifecycleRunning {

config/config.go

Lines changed: 0 additions & 8 deletions

0 commit comments

Comments
 (0)