Resolve dns thing?

This commit is contained in:
Owen Schwartz
2024-11-24 13:06:31 -05:00
parent 79c16a5705
commit 6eac4b5147
2 changed files with 58 additions and 3 deletions

57
main.go
View File

@@ -8,6 +8,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"math/rand" "math/rand"
"net"
"net/netip" "net/netip"
"newt/logger" "newt/logger"
"newt/proxy" "newt/proxy"
@@ -125,6 +126,54 @@ func mapToWireGuardLogLevel(level logger.LogLevel) int {
} }
} }
func resolveDomain(domain string) (string, error) {
// Check if there's a port in the domain
host, port, err := net.SplitHostPort(domain)
if err != nil {
// No port found, use the domain as is
host = domain
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 {
return "", fmt.Errorf("DNS lookup failed: %v", err)
}
if len(ips) == 0 {
return "", fmt.Errorf("no IP addresses found for domain %s", host)
}
// Get the first IPv4 address if available
var ipAddr string
for _, ip := range ips {
if ipv4 := ip.To4(); ipv4 != nil {
ipAddr = ipv4.String()
break
}
}
// If no IPv4 found, use the first IP (might be IPv6)
if ipAddr == "" {
ipAddr = ips[0].String()
}
// Add port back if it existed
if port != "" {
ipAddr = net.JoinHostPort(ipAddr, port)
}
return ipAddr, nil
}
func main() { func main() {
var ( var (
endpoint string endpoint string
@@ -206,12 +255,18 @@ func main() {
"wireguard: ", "wireguard: ",
)) ))
endpoint, err := resolveDomain(wgData.Endpoint)
if err != nil {
logger.Error("Failed to resolve endpoint: %v", err)
return
}
// Configure WireGuard // Configure WireGuard
config := fmt.Sprintf(`private_key=%s config := fmt.Sprintf(`private_key=%s
public_key=%s public_key=%s
allowed_ip=%s/32 allowed_ip=%s/32
endpoint=%s endpoint=%s
persistent_keepalive_interval=5`, fixKey(fmt.Sprintf("%s", privateKey)), fixKey(wgData.PublicKey), wgData.ServerIP, wgData.Endpoint) persistent_keepalive_interval=5`, fixKey(fmt.Sprintf("%s", privateKey)), fixKey(wgData.PublicKey), wgData.ServerIP, endpoint)
err = dev.IpcSet(config) err = dev.IpcSet(config)
if err != nil { if err != nil {

View File

@@ -40,7 +40,7 @@ func NewClient(newtID, secret string, opts ...ClientOption) (*Client, error) {
client := &Client{ client := &Client{
config: config, config: config,
baseURL: "http://localhost:3000", // default value baseURL: "https://fossorial.io", // default value
handlers: make(map[string]MessageHandler), handlers: make(map[string]MessageHandler),
done: make(chan struct{}), done: make(chan struct{}),
} }
@@ -73,7 +73,7 @@ func (c *Client) Connect() error {
} }
// Connect to WebSocket // Connect to WebSocket
wsURL := fmt.Sprintf("ws://%s/ws", c.baseURL[7:]) // Remove http:// prefix wsURL := fmt.Sprintf("wss://%s/ws", "fossorial.io") // Remove http:// prefix
u, err := url.Parse(wsURL) u, err := url.Parse(wsURL)
if err != nil { if err != nil {
return fmt.Errorf("failed to parse WebSocket URL: %w", err) return fmt.Errorf("failed to parse WebSocket URL: %w", err)