mirror of
https://github.com/fosrl/olm.git
synced 2026-02-08 05:56:41 +00:00
Basic relay working!
This commit is contained in:
50
main.go
50
main.go
@@ -123,7 +123,10 @@ func mapToWireGuardLogLevel(level logger.LogLevel) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func resolveDomain(domain string) (string, error) {
|
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)
|
host, port, err := net.SplitHostPort(domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// No port found, use the domain as is
|
// No port found, use the domain as is
|
||||||
@@ -131,13 +134,6 @@ func resolveDomain(domain string) (string, error) {
|
|||||||
port = ""
|
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
|
// Lookup IP addresses
|
||||||
ips, err := net.LookupIP(host)
|
ips, err := net.LookupIP(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -304,14 +300,18 @@ func sendUDPHolePunch(serverAddr string, olmID string, sourcePort uint16) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func keepSendingUDPHolePunch(endpoint string, olmID string, sourcePort uint16) {
|
func keepSendingUDPHolePunch(endpoint string, olmID string, sourcePort uint16) {
|
||||||
var host = endpoint
|
host, err := resolveDomain(endpoint)
|
||||||
if strings.HasPrefix(host, "http://") {
|
if err != nil {
|
||||||
host = strings.TrimPrefix(host, "http://")
|
logger.Error("Failed to resolve endpoint: %v", err)
|
||||||
} else if strings.HasPrefix(host, "https://") {
|
return
|
||||||
host = strings.TrimPrefix(host, "https://")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -340,7 +340,7 @@ func sendRegistration(olm *websocket.Client, publicKey string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func keepSendingRegistration(olm *websocket.Client, publicKey string) {
|
func keepSendingRegistration(olm *websocket.Client, publicKey string) {
|
||||||
ticker := time.NewTicker(2 * time.Second)
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -610,18 +610,24 @@ func main() {
|
|||||||
|
|
||||||
logger.Info("UAPI listener started")
|
logger.Info("UAPI listener started")
|
||||||
|
|
||||||
endpoint, err := resolveDomain(wgData.Endpoint)
|
// endpoint, err := resolveDomain(wgData.Endpoint)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
logger.Error("Failed to resolve endpoint: %v", err)
|
// logger.Error("Failed to resolve endpoint: %v", err)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Configure WireGuard
|
// 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
|
config := fmt.Sprintf(`private_key=%s
|
||||||
public_key=%s
|
public_key=%s
|
||||||
allowed_ip=%s/32
|
allowed_ip=%s/32
|
||||||
endpoint=%s
|
endpoint=18.212.58.121:21820
|
||||||
persistent_keepalive_interval=1`, fixKey(privateKey.String()), fixKey(wgData.PublicKey), wgData.ServerIP, endpoint)
|
persistent_keepalive_interval=1`, fixKey(privateKey.String()), fixKey(wgData.PublicKey), wgData.ServerIP)
|
||||||
|
|
||||||
err = dev.IpcSet(config)
|
err = dev.IpcSet(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user