add support for regenrate config, make parallel call for warp config
This commit is contained in:
@@ -2,7 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
dns "github.com/sagernet/sing-dns"
|
||||
"github.com/sagernet/sing/common/batch"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -73,10 +72,10 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
|
||||
ClashAPI: &option.ClashAPIOptions{
|
||||
ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", opt.ClashApiPort),
|
||||
},
|
||||
// CacheFile: &option.CacheFileOptions{
|
||||
// Enabled: true,
|
||||
// Path: "clash.db",
|
||||
// },
|
||||
CacheFile: &option.CacheFileOptions{
|
||||
Enabled: true,
|
||||
Path: "clash.db",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,10 +407,9 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
|
||||
case C.TypeCustom:
|
||||
continue
|
||||
default:
|
||||
if strings.Contains(out.Tag, "§hide§") {
|
||||
continue
|
||||
if !strings.Contains(out.Tag, "§hide§") {
|
||||
tags = append(tags, out.Tag)
|
||||
}
|
||||
tags = append(tags, out.Tag)
|
||||
outbounds = append(outbounds, out)
|
||||
}
|
||||
}
|
||||
@@ -473,26 +471,26 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
|
||||
}...,
|
||||
)
|
||||
if len(directDNSDomains) > 0 {
|
||||
trickDnsDomains := []string{}
|
||||
directDNSDomains = removeDuplicateStr(directDNSDomains)
|
||||
b, _ := batch.New(context.Background(), batch.WithConcurrencyNum[bool](10))
|
||||
for _, d := range directDNSDomains {
|
||||
b.Go(d, func() (bool, error) {
|
||||
return isBlockedDomain(d), nil
|
||||
})
|
||||
}
|
||||
b.Wait()
|
||||
for domain, isBlock := range b.Result() {
|
||||
if isBlock.Value {
|
||||
trickDnsDomains = append(trickDnsDomains, domain)
|
||||
}
|
||||
}
|
||||
// trickDnsDomains := []string{}
|
||||
// directDNSDomains = removeDuplicateStr(directDNSDomains)
|
||||
// b, _ := batch.New(context.Background(), batch.WithConcurrencyNum[bool](10))
|
||||
// for _, d := range directDNSDomains {
|
||||
// b.Go(d, func() (bool, error) {
|
||||
// return isBlockedDomain(d), nil
|
||||
// })
|
||||
// }
|
||||
// b.Wait()
|
||||
// for domain, isBlock := range b.Result() {
|
||||
// if isBlock.Value {
|
||||
// trickDnsDomains = append(trickDnsDomains, domain)
|
||||
// }
|
||||
// }
|
||||
|
||||
trickDomains := strings.Join(trickDnsDomains, ",")
|
||||
trickRule := Rule{Domains: trickDomains, Outbound: OutboundBypassTag}
|
||||
trickDnsRule := trickRule.MakeDNSRule()
|
||||
trickDnsRule.Server = DNSTricksDirectTag
|
||||
options.DNS.Rules = append([]option.DNSRule{{Type: C.RuleTypeDefault, DefaultOptions: trickDnsRule}}, options.DNS.Rules...)
|
||||
// trickDomains := strings.Join(trickDnsDomains, ",")
|
||||
// trickRule := Rule{Domains: trickDomains, Outbound: OutboundBypassTag}
|
||||
// trickDnsRule := trickRule.MakeDNSRule()
|
||||
// trickDnsRule.Server = DNSTricksDirectTag
|
||||
// options.DNS.Rules = append([]option.DNSRule{{Type: C.RuleTypeDefault, DefaultOptions: trickDnsRule}}, options.DNS.Rules...)
|
||||
|
||||
domains := strings.Join(directDNSDomains, ",")
|
||||
directRule := Rule{Domains: domains, Outbound: OutboundBypassTag}
|
||||
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"github.com/hiddify/ray2sing/ray2sing"
|
||||
"github.com/sagernet/sing-box/experimental/libbox"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common/batch"
|
||||
SJ "github.com/sagernet/sing/common/json"
|
||||
"github.com/xmdhs/clash2singbox/convert"
|
||||
"github.com/xmdhs/clash2singbox/model/clash"
|
||||
@@ -80,14 +82,26 @@ func patchConfig(content []byte, name string) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("[SingboxParser] unmarshal error: %w", err)
|
||||
}
|
||||
|
||||
for i, base := range options.Outbounds {
|
||||
err := patchWarp(&base)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("[Warp] patch warp error: %w", err)
|
||||
b, _ := batch.New(context.Background(), batch.WithConcurrencyNum[*option.Outbound](10))
|
||||
for _, base := range options.Outbounds {
|
||||
out := base
|
||||
b.Go(base.Tag, func() (*option.Outbound, error) {
|
||||
err := patchWarp(&out)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("[Warp] patch warp error: %w", err)
|
||||
}
|
||||
// options.Outbounds[i] = base
|
||||
return &out, nil
|
||||
})
|
||||
}
|
||||
if res, err := b.WaitAndGetResult(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
options.Outbounds = []option.Outbound{}
|
||||
for s := range res {
|
||||
fmt.Println(s)
|
||||
options.Outbounds = append(options.Outbounds, *res[s].Value)
|
||||
}
|
||||
options.Outbounds[i] = base
|
||||
|
||||
}
|
||||
|
||||
content, _ = json.MarshalIndent(options, "", " ")
|
||||
|
||||
128
config/warp.go
128
config/warp.go
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user