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
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ API_KEY = xxxxxxxx-yyyy-zzzz-mmmm-aaaaaaaaaaa
2222
# STARTUP_LOG_TAIL_SIZE = 200
2323
# STATS_UPDATE_INTERVAL_SECONDS = 10
2424
# STATS_CLEANUP_INTERVAL_SECONDS = 300
25-
# LATENCY_TEST_URL = https://www.gstatic.com/generate_204
26-
# LATENCY_TIMEOUT_SECONDS = 5
2725

2826
### WireGuard host NAT
2927
### Built-in routing enables runtime IPv4 forwarding and manages scoped nft NAT/forwarding rules.

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
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ type Config struct {
2525
StartupLogTailSize int
2626
StatsUpdateIntervalSeconds int
2727
StatsCleanupIntervalSeconds int
28-
LatencyTestURL string
29-
LatencyTimeoutSeconds int
3028
}
3129

3230
func Load() (*Config, error) {
@@ -48,18 +46,12 @@ func Load() (*Config, error) {
4846
StartupLogTailSize: GetEnvAsInt("STARTUP_LOG_TAIL_SIZE", 200),
4947
StatsUpdateIntervalSeconds: GetEnvAsInt("STATS_UPDATE_INTERVAL_SECONDS", 10),
5048
StatsCleanupIntervalSeconds: GetEnvAsInt("STATS_CLEANUP_INTERVAL_SECONDS", 300),
51-
LatencyTestURL: GetEnv("LATENCY_TEST_URL", "https://www.gstatic.com/generate_204"),
52-
LatencyTimeoutSeconds: GetEnvAsInt("LATENCY_TIMEOUT_SECONDS", 5),
5349
}
5450

5551
if cfg.LogBufferSize <= 0 {
5652
log.Printf("[Warning] LOG_BUFFER_SIZE must be greater than 0, got %d. Falling back to 1.", cfg.LogBufferSize)
5753
cfg.LogBufferSize = 1
5854
}
59-
if cfg.LatencyTimeoutSeconds <= 0 {
60-
log.Printf("[Warning] LATENCY_TIMEOUT_SECONDS must be greater than 0, got %d. Falling back to 5.", cfg.LatencyTimeoutSeconds)
61-
cfg.LatencyTimeoutSeconds = 5
62-
}
6355

6456
cfg.ApiKey, err = GetEnvAsUUID("API_KEY")
6557
if err != nil {

0 commit comments

Comments
 (0)