add warp in warp & reserved & more more

This commit is contained in:
Hiddify
2024-05-31 13:08:55 +02:00
parent 0bbd581277
commit bc48ec07a8
19 changed files with 288 additions and 263 deletions

View File

@@ -219,8 +219,8 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
Listen: option.NewListenAddress(netip.MustParseAddr(bind)),
ListenPort: opt.LocalDnsPort,
},
OverrideAddress: "1.1.1.1",
OverridePort: 53,
// OverrideAddress: "1.1.1.1",
// OverridePort: 53,
},
},
)
@@ -406,9 +406,24 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
OutboundMainProxyTag = OutboundSelectTag
//inbound==warp over proxies
//outbound==proxies over warp
fmt.Printf("opt.Warp=%+v\n", opt.Warp)
if opt.Warp.EnableWarp {
for _, out := range input.Outbounds {
if out.Type == C.TypeCustom {
if warp, ok := out.CustomOptions["warp"].(map[string]interface{}); ok {
key, _ := warp["key"].(string)
if key == "p1" {
opt.Warp.EnableWarp = false
break
}
}
}
if out.Type == C.TypeWireGuard && (out.WireGuardOptions.PrivateKey == opt.Warp.WireguardConfig.PrivateKey || out.WireGuardOptions.PrivateKey == "p1") {
opt.Warp.EnableWarp = false
break
}
}
}
if opt.Warp.EnableWarp && (opt.Warp.Mode == "warp_over_proxy" || opt.Warp.Mode == "proxy_over_warp") {
out, err := GenerateWarpSingbox(opt.Warp.WireguardConfig, opt.Warp.CleanIP, opt.Warp.CleanPort, opt.Warp.FakePackets, opt.Warp.FakePacketSize, opt.Warp.FakePacketDelay)
if err != nil {
return nil, fmt.Errorf("failed to generate warp config: %v", err)
@@ -420,7 +435,7 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
} else {
out.WireGuardOptions.Detour = OutboundDirectTag
}
patchWarp(out, &opt)
patchWarp(out, &opt, true)
outbounds = append(outbounds, *out)
// tags = append(tags, out.Tag)
}

View File

@@ -5,10 +5,8 @@ import (
"fmt"
"net"
"github.com/bepass-org/warp-plus/warp"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
T "github.com/sagernet/sing-box/option"
)
type outboundMap map[string]interface{}
@@ -124,7 +122,7 @@ func patchOutbound(base option.Outbound, configOpt ConfigOptions) (*option.Outbo
formatErr := func(err error) error {
return fmt.Errorf("error patching outbound[%s][%s]: %w", base.Tag, base.Type, err)
}
err := patchWarp(&base, &configOpt)
err := patchWarp(&base, &configOpt, true)
if err != nil {
return nil, "", formatErr(err)
}
@@ -167,60 +165,6 @@ func patchOutbound(base option.Outbound, configOpt ConfigOptions) (*option.Outbo
return &outbound, serverDomain, nil
}
func patchWarp(base *option.Outbound, configOpt *ConfigOptions) error {
if base.Type == C.TypeWireGuard {
host := base.WireGuardOptions.Server
if host == "default" || host == "random" || host == "auto" || isBlockedDomain(host) {
randomIpPort, _ := warp.RandomWarpEndpoint(true, false)
base.WireGuardOptions.Server = randomIpPort.Addr().String()
}
if base.WireGuardOptions.ServerPort == 0 {
port := warp.RandomWarpPort()
base.WireGuardOptions.ServerPort = port
}
// if base.WireGuardOptions.Detour == "" {
// base.WireGuardOptions.GSO = runtime.GOOS != "windows"
// }
}
if base.Type == C.TypeCustom {
if warp, ok := base.CustomOptions["warp"].(map[string]interface{}); ok {
key, _ := warp["key"].(string)
host, _ := warp["host"].(string)
port, _ := warp["port"].(float64)
detour, _ := warp["detour"].(string)
fakePackets, _ := warp["fake_packets"].(string)
fakePacketsSize, _ := warp["fake_packets_size"].(string)
fakePacketsDelay, _ := warp["fake_packets_delay"].(string)
var warpConfig *T.Outbound
var err error
if configOpt != nil && (key == "p1" || key == "p2") {
warpConfig = base
} else {
warpConfig, err = generateWarp(key, host, uint16(port), fakePackets, fakePacketsSize, fakePacketsDelay)
}
if err != nil {
fmt.Printf("Error generating warp config: %v", err)
return err
}
base.Type = C.TypeWireGuard
warpConfig.WireGuardOptions.Detour = detour
if detour != "" {
if warpConfig.WireGuardOptions.MTU > 1000 {
warpConfig.WireGuardOptions.MTU -= 50
}
warpConfig.WireGuardOptions.FakePackets = ""
}
base.WireGuardOptions = warpConfig.WireGuardOptions
}
}
return nil
}
// func (o outboundMap) transportType() string {
// if transport, ok := o["transport"].(map[string]interface{}); ok {
// if transportType, ok := transport["type"].(string); ok {

View File

@@ -95,7 +95,7 @@ func patchConfig(content []byte, name string, configOpt *ConfigOptions) ([]byte,
for _, base := range options.Outbounds {
out := base
b.Go(base.Tag, func() (*option.Outbound, error) {
err := patchWarp(&out, configOpt)
err := patchWarp(&out, configOpt, false)
if err != nil {
return nil, fmt.Errorf("[Warp] patch warp error: %w", err)
}

View File

@@ -7,10 +7,12 @@ import (
"os"
"github.com/bepass-org/warp-plus/warp"
C "github.com/sagernet/sing-box/constant"
// "github.com/bepass-org/wireguard-go/warp"
"log/slog"
"github.com/sagernet/sing-box/option"
T "github.com/sagernet/sing-box/option"
)
@@ -27,14 +29,10 @@ type SingboxConfig struct {
}
func wireGuardToSingbox(wgConfig WarpWireguardConfig, 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
// }
clientID, _ := base64.StdEncoding.DecodeString(wgConfig.ClientID)
if len(clientID) < 2 {
clientID = []byte{0, 0, 0}
}
out := T.Outbound{
Type: "wireguard",
Tag: "WARP",
@@ -47,11 +45,12 @@ func wireGuardToSingbox(wgConfig WarpWireguardConfig, server string, port uint16
PrivateKey: wgConfig.PrivateKey,
PeerPublicKey: wgConfig.PeerPublicKey,
Reserved: []uint8{clientID[0], clientID[1], clientID[2]},
MTU: 1330,
// Reserved: []uint8{0, 0, 0},
MTU: 1330,
},
}
ips := []string{wgConfig.LocalAddressIPv4 + "/24", wgConfig.LocalAddressIPv6 + "/128"}
for _, addr := range ips {
if addr == "" {
continue
@@ -83,7 +82,6 @@ func generateWarp(license string, host string, port uint16, fakePackets string,
if wgConfig == nil {
return nil, fmt.Errorf("invalid warp config")
}
fmt.Printf("%v", wgConfig)
return GenerateWarpSingbox(*wgConfig, host, port, fakePackets, fakePacketsSize, fakePacketsDelay)
}
@@ -144,3 +142,68 @@ func GenerateWarpInfo(license string, oldAccountId string, oldAccessToken string
return &identity, res, &warpcfg, err
}
func patchWarp(base *option.Outbound, configOpt *ConfigOptions, final bool) error {
if base.Type == C.TypeCustom {
if warp, ok := base.CustomOptions["warp"].(map[string]interface{}); ok {
key, _ := warp["key"].(string)
host, _ := warp["host"].(string)
port, _ := warp["port"].(uint16)
detour, _ := warp["detour"].(string)
fakePackets, _ := warp["fake_packets"].(string)
fakePacketsSize, _ := warp["fake_packets_size"].(string)
fakePacketsDelay, _ := warp["fake_packets_delay"].(string)
var warpConfig *T.Outbound
var err error
if configOpt == nil && (key == "p1" || key == "p2") {
warpConfig = base
return nil
} else if key == "p1" {
warpConfig, err = GenerateWarpSingbox(configOpt.Warp.WireguardConfig, host, port, fakePackets, fakePacketsSize, fakePacketsDelay)
} else if key == "p2" {
warpConfig, err = GenerateWarpSingbox(configOpt.Warp2.WireguardConfig, host, port, fakePackets, fakePacketsSize, fakePacketsDelay)
} else {
warpConfig, err = generateWarp(key, host, uint16(port), fakePackets, fakePacketsSize, fakePacketsDelay)
}
if err != nil {
fmt.Printf("Error generating warp config: %v", err)
return err
}
warpConfig.WireGuardOptions.Detour = detour
base.Type = C.TypeWireGuard
base.WireGuardOptions = warpConfig.WireGuardOptions
}
}
if final && base.Type == C.TypeWireGuard {
host := base.WireGuardOptions.Server
if host == "default" || host == "random" || host == "auto" || isBlockedDomain(host) {
randomIpPort, _ := warp.RandomWarpEndpoint(true, false)
base.WireGuardOptions.Server = randomIpPort.Addr().String()
}
if base.WireGuardOptions.ServerPort == 0 {
port := warp.RandomWarpPort()
base.WireGuardOptions.ServerPort = port
}
if base.WireGuardOptions.Detour != "" {
if base.WireGuardOptions.MTU > 1000 {
base.WireGuardOptions.MTU -= 50
}
base.WireGuardOptions.FakePackets = ""
base.WireGuardOptions.FakePacketsDelay = ""
base.WireGuardOptions.FakePacketsSize = ""
}
// if base.WireGuardOptions.Detour == "" {
// base.WireGuardOptions.GSO = runtime.GOOS != "windows"
// }
}
return nil
}

View File

@@ -2,7 +2,6 @@ package config
import (
"encoding/json"
)
type WarpAccount struct {
@@ -15,7 +14,7 @@ type WarpWireguardConfig struct {
LocalAddressIPv4 string `json:"local-address-ipv4"`
LocalAddressIPv6 string `json:"local-address-ipv6"`
PeerPublicKey string `json:"peer-public-key"`
ClientID string `json:"ClientID"`
ClientID string `json:"client-id"`
}
type WarpGenerationResponse struct {
@@ -39,6 +38,7 @@ func GenerateWarpAccount(licenseKey string, accountId string, accessToken string
LocalAddressIPv4: wg.LocalAddressIPv4,
LocalAddressIPv6: wg.LocalAddressIPv6,
PeerPublicKey: wg.PeerPublicKey,
ClientID: wg.ClientID,
}
response := WarpGenerationResponse{warpAccount, log, warpConfig}