From 61c2276f23ae1cfc420ca294226418f80c6cc6bf Mon Sep 17 00:00:00 2001 From: hiddify Date: Fri, 8 Mar 2024 10:25:48 +0100 Subject: [PATCH] fix: wireguard --- main.go | 1 + ray2sing/common.go | 10 ++++++++++ ray2sing/wireguard.go | 21 ++++++++++++--------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 3bda790..e0f3677 100644 --- a/main.go +++ b/main.go @@ -45,6 +45,7 @@ var examples = map[string][]string{ }, "wg": { "wg://[server]:222/?pk=[private_key]&local_address=10.0.0.2/24&peer_pk=[peer_public_key]&pre_shared_key=[pre_shared_key]&workers=[workers]&mtu=[mtu]&reserved=0,0,0", + "wg://server.com:5319?publicKey=verypublicKeyss&privateKey=veryprivatekey&presharedKey=verysharedkey&ip=10.0.0.1&mtu=1380&keepalive=30&udp=1&reserved=0,0,0&ifp=5-10#direct%20WireGuard", }, "warp": { "warp://key@host:1234/#WARPkey", diff --git a/ray2sing/common.go b/ray2sing/common.go index d1e75db..c0e0bdd 100644 --- a/ray2sing/common.go +++ b/ray2sing/common.go @@ -2,6 +2,7 @@ package ray2sing import ( "encoding/base64" + "fmt" "net" "net/url" "strconv" @@ -233,3 +234,12 @@ func toInt16(s string, defaultPort uint16) uint16 { func isIPOnly(s string) bool { return net.ParseIP(s) != nil } + +func getOneOf(dic map[string]string, headers ...string) (string, error) { + for _, h := range headers { + if str, ok := dic[h]; ok { + return str, nil + } + } + return "", fmt.Errorf("not found") +} diff --git a/ray2sing/wireguard.go b/ray2sing/wireguard.go index 81aff61..9dd7186 100644 --- a/ray2sing/wireguard.go +++ b/ray2sing/wireguard.go @@ -18,20 +18,19 @@ func WiregaurdSingbox(url string) (*T.Outbound, error) { Tag: u.Name, WireGuardOptions: T.WireGuardOutboundOptions{ ServerOptions: u.GetServerOption(), - - PrivateKey: u.Params["pk"], - PeerPublicKey: u.Params["peerpub"], - PreSharedKey: u.Params["psk"], FakePackets: u.Params["ifp"], }, } - if pk, ok := u.Params["privatekey"]; ok { + + if pk, err := getOneOf(u.Params, "privatekey", "pk"); err == nil { out.WireGuardOptions.PrivateKey = pk } - if pub, ok := u.Params["peerpublickey"]; ok { + + if pub, err := getOneOf(u.Params, "peerpublickey", "publickey", "pub", "peerpub"); err == nil { out.WireGuardOptions.PeerPublicKey = pub } - if psk, ok := u.Params["presharedkey"]; ok { + + if psk, err := getOneOf(u.Params, "presharedkey", "psk"); err == nil { out.WireGuardOptions.PreSharedKey = psk } @@ -58,9 +57,13 @@ func WiregaurdSingbox(url string) (*T.Outbound, error) { out.WireGuardOptions.Reserved = append(out.WireGuardOptions.Reserved, uint8(num)) } } - if localAddressStr, ok := u.Params["localaddress"]; ok { - localAddressParts := strings.Split(localAddressStr, ",") + + if localAddress, err := getOneOf(u.Params, "localaddress", "ip"); err == nil { + localAddressParts := strings.Split(localAddress, ",") for _, part := range localAddressParts { + if !strings.Contains(part, "/") { + part += "/24" + } prefix, err := netip.ParsePrefix(part) if err != nil { return nil, err // Handle the error appropriately