From 3819823d9502add99a4c713ca21e568c97c70f08 Mon Sep 17 00:00:00 2001 From: Owen Date: Sun, 23 Feb 2025 16:50:11 -0500 Subject: [PATCH] Basic relay working! --- main.go | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 5748236..17fa1b6 100644 --- a/main.go +++ b/main.go @@ -123,7 +123,10 @@ func mapToWireGuardLogLevel(level logger.LogLevel) int { } func resolveDomain(domain string) (string, error) { - // Check if there's a port in the domain + // First handle any protocol prefix + domain = strings.TrimPrefix(strings.TrimPrefix(domain, "https://"), "http://") + + // Now split host and port host, port, err := net.SplitHostPort(domain) if err != nil { // No port found, use the domain as is @@ -131,13 +134,6 @@ func resolveDomain(domain string) (string, error) { port = "" } - // Remove any protocol prefix if present - if strings.HasPrefix(host, "http://") { - host = strings.TrimPrefix(host, "http://") - } else if strings.HasPrefix(host, "https://") { - host = strings.TrimPrefix(host, "https://") - } - // Lookup IP addresses ips, err := net.LookupIP(host) if err != nil { @@ -304,14 +300,18 @@ func sendUDPHolePunch(serverAddr string, olmID string, sourcePort uint16) error } func keepSendingUDPHolePunch(endpoint string, olmID string, sourcePort uint16) { - var host = endpoint - if strings.HasPrefix(host, "http://") { - host = strings.TrimPrefix(host, "http://") - } else if strings.HasPrefix(host, "https://") { - host = strings.TrimPrefix(host, "https://") + host, err := resolveDomain(endpoint) + if err != nil { + logger.Error("Failed to resolve endpoint: %v", err) + return } - ticker := time.NewTicker(1 * time.Second) + // Execute once immediately before starting the loop + if err := sendUDPHolePunch(host+":21820", olmID, sourcePort); err != nil { + logger.Error("Failed to send UDP hole punch: %v", err) + } + + ticker := time.NewTicker(250 * time.Millisecond) defer ticker.Stop() for { @@ -340,7 +340,7 @@ func sendRegistration(olm *websocket.Client, publicKey string) error { } func keepSendingRegistration(olm *websocket.Client, publicKey string) { - ticker := time.NewTicker(2 * time.Second) + ticker := time.NewTicker(1 * time.Second) defer ticker.Stop() for { @@ -610,18 +610,24 @@ func main() { logger.Info("UAPI listener started") - endpoint, err := resolveDomain(wgData.Endpoint) - if err != nil { - logger.Error("Failed to resolve endpoint: %v", err) - return - } + // endpoint, err := resolveDomain(wgData.Endpoint) + // if err != nil { + // logger.Error("Failed to resolve endpoint: %v", err) + // return + // } // Configure WireGuard + // config := fmt.Sprintf(`private_key=%s + // public_key=%s + // allowed_ip=%s/32 + // endpoint=%s + // persistent_keepalive_interval=1`, fixKey(privateKey.String()), fixKey(wgData.PublicKey), wgData.ServerIP, endpoint) + config := fmt.Sprintf(`private_key=%s public_key=%s allowed_ip=%s/32 -endpoint=%s -persistent_keepalive_interval=1`, fixKey(privateKey.String()), fixKey(wgData.PublicKey), wgData.ServerIP, endpoint) +endpoint=18.212.58.121:21820 +persistent_keepalive_interval=1`, fixKey(privateKey.String()), fixKey(wgData.PublicKey), wgData.ServerIP) err = dev.IpcSet(config) if err != nil {