Skip to content

Commit

Permalink
fix: wireguard
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Mar 8, 2024
1 parent 96fae12 commit 61c2276
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions ray2sing/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ray2sing

import (
"encoding/base64"
"fmt"
"net"
"net/url"
"strconv"
Expand Down Expand Up @@ -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")
}
21 changes: 12 additions & 9 deletions ray2sing/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down

0 comments on commit 61c2276

Please sign in to comment.