add support for regenrate config, make parallel call for warp config

This commit is contained in:
Hiddify
2024-02-15 14:47:27 +01:00
parent d72e42cf6a
commit be58cb333f
5 changed files with 86 additions and 130 deletions

View File

@@ -1,35 +1,15 @@
package config
import (
"bufio"
"encoding/json"
"fmt"
"math/rand"
"net/netip"
"os"
"strings"
"github.com/bepass-org/wireguard-go/warp"
T "github.com/sagernet/sing-box/option"
)
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"`
@@ -42,7 +22,7 @@ type SingboxConfig struct {
MTU int `json:"mtu"`
}
func wireGuardToSingbox(wgConfig WireGuardConfig, server string, port uint16) (*T.Outbound, error) {
func wireGuardToSingbox(wgConfig warp.WireguardConfig, server string, port uint16) (*T.Outbound, error) {
// splt := strings.Split(wgConfig.Peer.Endpoint, ":")
// port, err := strconv.Atoi(splt[1])
// if err != nil {
@@ -58,73 +38,26 @@ func wireGuardToSingbox(wgConfig WireGuardConfig, server string, port uint16) (*
ServerPort: port,
},
PrivateKey: wgConfig.Interface.PrivateKey,
PeerPublicKey: wgConfig.Peer.PublicKey,
PrivateKey: wgConfig.PrivateKey,
PeerPublicKey: wgConfig.PeerPublicKey,
Reserved: []uint8{0, 0, 0},
MTU: 1280,
},
}
for _, addr := range wgConfig.Interface.Address {
ips := []string{wgConfig.LocalAddressIPv4 + "/24", wgConfig.LocalAddressIPv6 + "/128"}
for _, addr := range ips {
if addr == "" {
continue
}
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])
}
}
var warpIPList = []string{
"162.159.192.0/24",
@@ -170,26 +103,19 @@ func generateWarp(license string, host string, port uint16, fakePackets string,
fakePacketsDelay = "20-250"
}
// warp.UpdatePath("./secondary")
if _, err := os.Stat("./wgcf-identity.json"); err == nil {
os.Remove("./wgcf-identity.json")
}
if !warp.CheckProfileExists(license) {
fmt.Printf("profile s not exit! ---%s---", license)
err := warp.LoadOrCreateIdentity(license)
if err != nil {
return nil, err
}
}
wgConfig, err := readWireGuardConfig("wgcf-profile.ini")
_, _, wgConfig, err := warp.LoadOrCreateIdentityHiddify(license, nil)
if err != nil {
fmt.Println("Error reading WireGuard configuration:", err)
return nil, err
}
// fmt.Printf("%v", wgConfig)
singboxConfig, err := wireGuardToSingbox(wgConfig, host, port)
if wgConfig == nil {
return nil, fmt.Errorf("invalid warp config")
}
fmt.Printf("%v", wgConfig)
singboxConfig, err := wireGuardToSingbox(*wgConfig, host, port)
if err != nil {
fmt.Printf("%v %v", singboxConfig, err)
return nil, err
}
singboxConfig.WireGuardOptions.FakePackets = fakePackets
singboxConfig.WireGuardOptions.FakePacketsSize = fakePacketsSize
@@ -203,3 +129,21 @@ func generateWarp(license string, host string, port uint16, fakePackets string,
fmt.Println(string(singboxJSON))
return singboxConfig, nil
}
func GenerateWarpInfo(license string, oldAccountId string, oldAccessToken string) (*warp.AccountData, string, *warp.WireguardConfig, error) {
if oldAccountId != "" && oldAccessToken != "" {
accountData := warp.AccountData{
AccountID: oldAccountId,
AccessToken: oldAccessToken,
}
err := warp.RemoveDevice(accountData)
if err != nil {
fmt.Printf("Error in removing old device: %v\n", err)
} else {
fmt.Printf("Old Device Removed")
}
}
return warp.LoadOrCreateIdentityHiddify(license, nil)
}