Skip to content

Commit

Permalink
new: add warp config
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Jan 24, 2024
1 parent 7b1f9cf commit d86817d
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 3 deletions.
170 changes: 170 additions & 0 deletions cmd/cmd_warp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package main

import (
"bufio"
"encoding/json"
"fmt"
"net/netip"
"os"
"strings"

"github.com/sagernet/sing-box/log"
T "github.com/sagernet/sing-box/option"
"github.com/spf13/cobra"
"github.com/uoosef/wireguard-go/warp"
)

var warpKey string

var commandWarp = &cobra.Command{
Use: "warp",
Short: "warp configuration",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
err := generateWarp()
if err != nil {
log.Fatal(err)
}
},
}

func init() {
// commandWarp.Flags().StringVarP(&warpKey, "key", "k", "", "warp key")
mainCommand.AddCommand(commandWarp)
}

type WireGuardConfig struct {
Interface InterfaceConfig `json:"Interface"`
Peer PeerConfig `json:"Peer"`
}

type InterfaceConfig struct {
PrivateKey string `json:"PrivateKey"`
DNS string `json:"DNS"`
Address []string `json:"Address"`
}

type PeerConfig struct {
PublicKey string `json:"PublicKey"`
AllowedIPs []string `json:"AllowedIPs"`
Endpoint string `json:"Endpoint"`
}

type SingboxConfig struct {
Type string `json:"type"`
Tag string `json:"tag"`
Server string `json:"server"`
ServerPort int `json:"server_port"`
LocalAddress []string `json:"local_address"`
PrivateKey string `json:"private_key"`
PeerPublicKey string `json:"peer_public_key"`
Reserved []int `json:"reserved"`
MTU int `json:"mtu"`
}

func wireGuardToSingbox(wgConfig WireGuardConfig, server string, port uint16) (*T.Outbound, error) {
// splt := strings.Split(wgConfig.Peer.Endpoint, ":")
// port, err := strconv.Atoi(splt[1])
// if err != nil {
// fmt.Printf("%v", err)
// return nil
// }
out := T.Outbound{
Type: "wireguard",
Tag: "WARP",
WireGuardOptions: T.WireGuardOutboundOptions{
ServerOptions: T.ServerOptions{
Server: server,
ServerPort: port,
},

PrivateKey: wgConfig.Interface.PrivateKey,
PeerPublicKey: wgConfig.Peer.PublicKey,
Reserved: []uint8{0, 0, 0},
MTU: 1280,
},
}

for _, addr := range wgConfig.Interface.Address {
prefix, err := netip.ParsePrefix(addr)
if err != nil {
return nil, err // Handle the error appropriately
}
out.WireGuardOptions.LocalAddress = append(out.WireGuardOptions.LocalAddress, prefix)

}

return &out, nil
}
func readWireGuardConfig(filePath string) (WireGuardConfig, error) {
file, err := os.Open(filePath)
if err != nil {
return WireGuardConfig{}, err
}
defer file.Close()

scanner := bufio.NewScanner(file)

var wgConfig WireGuardConfig
var currentSection string

for scanner.Scan() {
line := scanner.Text()

if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
currentSection = strings.TrimSpace(line[1 : len(line)-1])
continue
}

if currentSection == "Interface" {
parseInterfaceConfig(&wgConfig.Interface, line)
} else if currentSection == "Peer" {
parsePeerConfig(&wgConfig.Peer, line)
}
}

return wgConfig, nil
}

func parseInterfaceConfig(interfaceConfig *InterfaceConfig, line string) {
if strings.HasPrefix(line, "PrivateKey") {
interfaceConfig.PrivateKey = strings.TrimSpace(strings.SplitN(line, "=", 2)[1])
} else if strings.HasPrefix(line, "DNS") {
interfaceConfig.DNS = strings.TrimSpace(strings.SplitN(line, "=", 2)[1])
} else if strings.HasPrefix(line, "Address") {
interfaceConfig.Address = append(interfaceConfig.Address, strings.TrimSpace(strings.SplitN(line, "=", 2)[1]))
}
}

func parsePeerConfig(peerConfig *PeerConfig, line string) {
if strings.HasPrefix(line, "PublicKey") {
peerConfig.PublicKey = strings.TrimSpace(strings.SplitN(line, "=", 2)[1])
} else if strings.HasPrefix(line, "AllowedIPs") {
peerConfig.AllowedIPs = append(peerConfig.AllowedIPs, strings.TrimSpace(strings.SplitN(line, "=", 2)[1]))
} else if strings.HasPrefix(line, "Endpoint") {
peerConfig.Endpoint = strings.TrimSpace(strings.SplitN(line, "=", 2)[1])
}
}
func generateWarp() *T.Outbound {
license := ""
endpoint := "engage.cloudflareclient.com:2408"

if !warp.CheckProfileExists(license) {
warp.LoadOrCreateIdentity(license, endpoint)
}

wgConfig, err := readWireGuardConfig("wgcf-profile.ini")
if err != nil {
fmt.Println("Error reading WireGuard configuration:", err)
return nil
}
// fmt.Printf("%v", wgConfig)
singboxConfig, err := wireGuardToSingbox(wgConfig, "162.159.192.91", 939)
singboxJSON, err := json.MarshalIndent(singboxConfig, "", " ")
if err != nil {
fmt.Println("Error marshaling Singbox configuration:", err)
return nil
}
fmt.Println(string(singboxJSON))
return nil
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/sagernet/sing-box v1.8.2
github.com/sagernet/sing-dns v0.1.12
github.com/spf13/cobra v1.8.0
github.com/uoosef/wireguard-go v0.0.2-alpha
github.com/xmdhs/clash2singbox v0.0.2
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -59,6 +60,8 @@ require (
github.com/pires/go-proxyproto v0.7.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.4.1 // indirect
github.com/quic-go/quic-go v0.37.4 // indirect
github.com/refraction-networking/utls v1.6.1 // indirect
github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a // indirect
github.com/sagernet/cloudflare-tls v0.0.0-20231208171750-a4483c1b7cd1 // indirect
github.com/sagernet/gvisor v0.0.0-20231209105102-8d27a30e436e // indirect
Expand Down Expand Up @@ -99,4 +102,4 @@ require (
lukechampine.com/blake3 v1.2.1 // indirect
)

replace github.com/sagernet/sing-box => github.com/hiddify/hiddify-sing-box v1.7.9-0.20240121092547-68ac7a67eee1
replace github.com/sagernet/sing-box => github.com/hiddify/hiddify-sing-box v1.7.9-0.20240123220037-92acce523aa6
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a h1:fEBsGL/sjAuJrgah5X
github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/hiddify/hiddify-sing-box v1.7.9-0.20240121092547-68ac7a67eee1 h1:bzGnAZ2C8SLkra6RsJuU/eGh00I6kmFEm88Kg77QyZo=
github.com/hiddify/hiddify-sing-box v1.7.9-0.20240121092547-68ac7a67eee1/go.mod h1:UjYAsVOhYPgx2Yl53iPjIwHOp7HghictAi16Lmi+CZw=
github.com/hiddify/hiddify-sing-box v1.7.9-0.20240123220037-92acce523aa6 h1:2WTZz+09BLS0jhMrr11UrmsGKf7rANd1t8bzGziZ9a4=
github.com/hiddify/hiddify-sing-box v1.7.9-0.20240123220037-92acce523aa6/go.mod h1:OPT7VAz77rWOHNOwQ9xgXo2TW4orQGunODQxIcHiX+E=
github.com/hiddify/ray2sing v0.0.0-20240121100507-259925872efd h1:UkGX1fEji0A7qAuTZECUe/QIBZKkeWBYGCWjFGQVPvg=
github.com/hiddify/ray2sing v0.0.0-20240121100507-259925872efd/go.mod h1:Hu3sHxOP4odkokDUqzJUL24uml5/I8ddTRERIMrio34=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down Expand Up @@ -114,6 +114,10 @@ github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/qtls-go1-20 v0.4.1 h1:D33340mCNDAIKBqXuAvexTNMUByrYmFYVfKfDN5nfFs=
github.com/quic-go/qtls-go1-20 v0.4.1/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k=
github.com/quic-go/quic-go v0.37.4 h1:ke8B73yMCWGq9MfrCCAw0Uzdm7GaViC3i39dsIdDlH4=
github.com/quic-go/quic-go v0.37.4/go.mod h1:YsbH1r4mSHPJcLF4k4zruUkLBqctEMBDR6VPvcYjIsU=
github.com/refraction-networking/utls v1.6.1 h1:n1JG5karzdGWsI6iZmGrOv3SNzR4c+4M8J6KWGsk3lA=
github.com/refraction-networking/utls v1.6.1/go.mod h1:+EbcQOvQvXoFV9AEKbuGlljt1doLRKAVY1jJHe9EtDo=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a h1:+NkI2670SQpQWvkkD2QgdTuzQG263YZ+2emfpeyGqW0=
github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a/go.mod h1:63s7jpZqcDAIpj8oI/1v4Izok+npJOHACFCU6+huCkM=
Expand Down Expand Up @@ -177,6 +181,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 h1:tHNk7XK9GkmKUR6Gh8gVBKXc2MVSZ4G/NnWLtzw4gNA=
github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923/go.mod h1:eLL9Nub3yfAho7qB0MzZizFhTU2QkLeoVsWdHtDW264=
github.com/uoosef/wireguard-go v0.0.2-alpha h1:fJw/0+MYfuj2IyfyzuCXXJqs186K+gSPiUDWIf0vHKs=
github.com/uoosef/wireguard-go v0.0.2-alpha/go.mod h1:uEnuKQ/PHpFdYwftT4bzwI8F3d2l9zsWZ9iDbLBIm0c=
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg=
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/xmdhs/clash2singbox v0.0.2 h1:/gxaFm8fmv+UcUZzK508Z0yR01wg1LHrrq872Qibk1I=
Expand Down

0 comments on commit d86817d

Please sign in to comment.