update to singbox 1.9.0 and update to warp-plus

This commit is contained in:
Hiddify
2024-05-29 17:38:13 +02:00
parent dd0c3614c9
commit ed423fec45
14 changed files with 168 additions and 172 deletions

View File

@@ -404,7 +404,7 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
fmt.Printf("opt.Warp=%+v\n", opt.Warp)
if opt.Warp.EnableWarp && (opt.Warp.Mode == "warp_over_proxy" || opt.Warp.Mode == "proxy_over_warp") {
out, err := generateWarpSingbox(opt.Warp.WireguardConfig.ToWireguardConfig(), opt.Warp.CleanIP, opt.Warp.CleanPort, opt.Warp.FakePackets, opt.Warp.FakePacketSize, opt.Warp.FakePacketDelay)
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)
}
@@ -415,7 +415,7 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
} else {
out.WireGuardOptions.Detour = OutboundDirectTag
}
patchWarp(out)
patchWarp(out, &opt)
outbounds = append(outbounds, *out)
// tags = append(tags, out.Tag)
}

View File

@@ -15,6 +15,7 @@ type ConfigOptions struct {
GeoSitePath string `json:"geosite-path"`
Rules []Rule `json:"rules"`
Warp WarpOptions `json:"warp"`
Warp2 WarpOptions `json:"warp"`
Mux MuxOptions `json:"mux"`
TLSTricks TLSTricks `json:"tls-tricks"`
DNSOptions

View File

@@ -5,8 +5,10 @@ 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{}
@@ -122,7 +124,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)
err := patchWarp(&base, &configOpt)
if err != nil {
return nil, "", formatErr(err)
}
@@ -165,14 +167,17 @@ func patchOutbound(base option.Outbound, configOpt ConfigOptions) (*option.Outbo
return &outbound, serverDomain, nil
}
func patchWarp(base *option.Outbound) error {
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) {
base.WireGuardOptions.Server = getRandomIP()
randomIpPort, _ := warp.RandomWarpEndpoint(true, false)
base.WireGuardOptions.Server = randomIpPort.Addr().String()
}
if base.WireGuardOptions.ServerPort == 0 {
base.WireGuardOptions.ServerPort = generateRandomPort()
port := warp.RandomWarpPort()
base.WireGuardOptions.ServerPort = port
}
// if base.WireGuardOptions.Detour == "" {
// base.WireGuardOptions.GSO = runtime.GOOS != "windows"
@@ -187,7 +192,13 @@ func patchWarp(base *option.Outbound) error {
fakePackets, _ := warp["fake_packets"].(string)
fakePacketsSize, _ := warp["fake_packets_size"].(string)
fakePacketsDelay, _ := warp["fake_packets_delay"].(string)
warpConfig, err := generateWarp(key, host, uint16(port), fakePackets, fakePacketsSize, fakePacketsDelay)
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
@@ -197,7 +208,7 @@ func patchWarp(base *option.Outbound) error {
warpConfig.WireGuardOptions.Detour = detour
if detour != "" {
if warpConfig.WireGuardOptions.MTU > 1000 {
warpConfig.WireGuardOptions.MTU -= 160
warpConfig.WireGuardOptions.MTU -= 50
}
warpConfig.WireGuardOptions.FakePackets = ""
}

View File

@@ -29,9 +29,9 @@ func ParseConfig(path string, debug bool) ([]byte, error) {
if err != nil {
return nil, err
}
return ParseConfigContent(string(content), debug, false)
return ParseConfigContent(string(content), debug, nil, false)
}
func ParseConfigContent(contentstr string, debug bool, enableFullConfig bool) ([]byte, error) {
func ParseConfigContent(contentstr string, debug bool, configOpt *ConfigOptions, fullConfig bool) ([]byte, error) {
content := []byte(contentstr)
var jsonObj map[string]interface{} = make(map[string]interface{})
@@ -43,7 +43,7 @@ func ParseConfigContent(contentstr string, debug bool, enableFullConfig bool) ([
if tmpJsonObj["outbounds"] == nil {
jsonObj["outbounds"] = []interface{}{jsonObj}
} else {
if enableFullConfig {
if configOpt.EnableFullConfig || fullConfig {
jsonObj = tmpJsonObj
} else {
jsonObj["outbounds"] = tmpJsonObj["outbounds"]
@@ -57,12 +57,12 @@ func ParseConfigContent(contentstr string, debug bool, enableFullConfig bool) ([
}
newContent, _ := json.MarshalIndent(jsonObj, "", " ")
return patchConfig(newContent, "SingboxParser")
return patchConfig(newContent, "SingboxParser", configOpt)
}
v2rayStr, err := ray2sing.Ray2Singbox(string(content))
if err == nil {
return patchConfig([]byte(v2rayStr), "V2rayParser")
return patchConfig([]byte(v2rayStr), "V2rayParser", configOpt)
}
fmt.Printf("Convert using clash\n")
clashObj := clash.Clash{}
@@ -79,13 +79,13 @@ func ParseConfigContent(contentstr string, debug bool, enableFullConfig bool) ([
if err != nil {
return nil, fmt.Errorf("[ClashParser] patching clash config error: %w", err)
}
return patchConfig(output, "ClashParser")
return patchConfig(output, "ClashParser", configOpt)
}
return nil, fmt.Errorf("unable to determine config format")
}
func patchConfig(content []byte, name string) ([]byte, error) {
func patchConfig(content []byte, name string, configOpt *ConfigOptions) ([]byte, error) {
options := option.Options{}
err := json.Unmarshal(content, &options)
if err != nil {
@@ -95,7 +95,7 @@ func patchConfig(content []byte, name string) ([]byte, error) {
for _, base := range options.Outbounds {
out := base
b.Go(base.Tag, func() (*option.Outbound, error) {
err := patchWarp(&out)
err := patchWarp(&out, configOpt)
if err != nil {
return nil, fmt.Errorf("[Warp] patch warp error: %w", err)
}

View File

@@ -22,6 +22,7 @@ func String(s string) *string {
func (s *server) ParseConfig(ctx context.Context, in *ParseConfigRequest) (*ParseConfigResponse, error) {
config, err := ParseConfig(in.TempPath, in.Debug)
if err != nil {
return &ParseConfigResponse{Error: String(err.Error())}, nil
}

View File

@@ -1,11 +1,16 @@
package config
import (
"encoding/base64"
"fmt"
"math/rand"
"net/netip"
"os"
"github.com/bepass-org/warp-plus/warp"
// "github.com/bepass-org/wireguard-go/warp"
"log/slog"
"github.com/bepass-org/wireguard-go/warp"
T "github.com/sagernet/sing-box/option"
)
@@ -21,13 +26,15 @@ type SingboxConfig struct {
MTU int `json:"mtu"`
}
func wireGuardToSingbox(wgConfig warp.WireguardConfig, server string, port uint16) (*T.Outbound, error) {
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)
out := T.Outbound{
Type: "wireguard",
Tag: "WARP",
@@ -39,8 +46,8 @@ func wireGuardToSingbox(wgConfig warp.WireguardConfig, server string, port uint1
PrivateKey: wgConfig.PrivateKey,
PeerPublicKey: wgConfig.PeerPublicKey,
Reserved: []uint8{0, 0, 0},
MTU: 1280,
Reserved: []uint8{clientID[0], clientID[1], clientID[2]},
MTU: 1330,
},
}
@@ -58,38 +65,18 @@ func wireGuardToSingbox(wgConfig warp.WireguardConfig, server string, port uint1
return &out, nil
}
var warpIPList = []string{
"162.159.192.0/24",
"162.159.193.0/24",
"162.159.195.0/24",
"162.159.204.0/24",
"188.114.96.0/24",
"188.114.97.0/24",
"188.114.98.0/24",
"188.114.99.0/24",
}
var warpPorts = []uint16{500, 854, 859, 864, 878, 880, 890, 891, 894, 903, 908, 928, 934, 939, 942,
943, 945, 946, 955, 968, 987, 988, 1002, 1010, 1014, 1018, 1070, 1074, 1180, 1387, 1701,
1843, 2371, 2408, 2506, 3138, 3476, 3581, 3854, 4177, 4198, 4233, 4500, 5279,
5956, 7103, 7152, 7156, 7281, 7559, 8319, 8742, 8854, 8886}
func getRandomIP() string {
randomRange := warpIPList[rand.Intn(len(warpIPList))]
ip, err := warp.RandomIPFromRange(randomRange)
ipPort, err := warp.RandomWarpEndpoint(true, true)
if err == nil {
return ip.String()
return ipPort.Addr().String()
}
return "engage.cloudflareclient.com"
}
func generateRandomPort() uint16 {
return warpPorts[rand.Intn(len(warpPorts))]
}
func generateWarp(license string, host string, port uint16, fakePackets string, fakePacketsSize string, fakePacketsDelay string) (*T.Outbound, error) {
_, _, wgConfig, err := warp.LoadOrCreateIdentityHiddify(license, nil)
_, _, wgConfig, err := GenerateWarpInfo(license, "", "")
if err != nil {
return nil, err
}
@@ -98,10 +85,10 @@ func generateWarp(license string, host string, port uint16, fakePackets string,
}
fmt.Printf("%v", wgConfig)
return generateWarpSingbox(*wgConfig, host, port, fakePackets, fakePacketsSize, fakePacketsDelay)
return GenerateWarpSingbox(*wgConfig, host, port, fakePackets, fakePacketsSize, fakePacketsDelay)
}
func generateWarpSingbox(wgConfig warp.WireguardConfig, host string, port uint16, fakePackets string, fakePacketsSize string, fakePacketsDelay string) (*T.Outbound, error) {
func GenerateWarpSingbox(wgConfig WarpWireguardConfig, host string, port uint16, fakePackets string, fakePacketsSize string, fakePacketsDelay string) (*T.Outbound, error) {
if host == "" {
host = "auto"
}
@@ -128,20 +115,31 @@ func generateWarpSingbox(wgConfig warp.WireguardConfig, host string, port uint16
return singboxConfig, nil
}
func GenerateWarpInfo(license string, oldAccountId string, oldAccessToken string) (*warp.AccountData, string, *warp.WireguardConfig, error) {
func GenerateWarpInfo(license string, oldAccountId string, oldAccessToken string) (*warp.Identity, string, *WarpWireguardConfig, error) {
if oldAccountId != "" && oldAccessToken != "" {
accountData := warp.AccountData{
AccountID: oldAccountId,
AccessToken: oldAccessToken,
}
err := warp.RemoveDevice(accountData)
err := warp.DeleteDevice(oldAccessToken, oldAccountId)
if err != nil {
fmt.Printf("Error in removing old device: %v\n", err)
} else {
fmt.Printf("Old Device Removed")
}
}
l := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
identity, err := warp.CreateIdentityOnly(l, license)
res := "Error!"
var warpcfg WarpWireguardConfig
if err == nil {
res = "Success"
res = fmt.Sprintf("Warp+ enabled: %t\n", identity.Account.WarpPlus)
res += fmt.Sprintf("\nAccount type: %s\n", identity.Account.AccountType)
warpcfg = WarpWireguardConfig{
PrivateKey: identity.PrivateKey,
LocalAddressIPv4: identity.Config.Interface.Addresses.V4,
LocalAddressIPv6: identity.Config.Interface.Addresses.V6,
ClientID: identity.Config.ClientID,
}
}
return warp.LoadOrCreateIdentityHiddify(license, nil)
return &identity, res, &warpcfg, err
}

View File

@@ -2,8 +2,7 @@ package config
import (
"encoding/json"
"github.com/bepass-org/wireguard-go/warp"
)
type WarpAccount struct {
@@ -16,15 +15,7 @@ type WarpWireguardConfig struct {
LocalAddressIPv4 string `json:"local-address-ipv4"`
LocalAddressIPv6 string `json:"local-address-ipv6"`
PeerPublicKey string `json:"peer-public-key"`
}
func (wg WarpWireguardConfig) ToWireguardConfig() warp.WireguardConfig {
return warp.WireguardConfig{
PrivateKey: wg.PrivateKey,
LocalAddressIPv4: wg.LocalAddressIPv4,
LocalAddressIPv6: wg.LocalAddressIPv6,
PeerPublicKey: wg.PeerPublicKey,
}
ClientID string `json:"ClientID"`
}
type WarpGenerationResponse struct {
@@ -34,14 +25,14 @@ type WarpGenerationResponse struct {
}
func GenerateWarpAccount(licenseKey string, accountId string, accessToken string) (string, error) {
account, log, wg, err := GenerateWarpInfo(licenseKey, accountId, accessToken)
identity, log, wg, err := GenerateWarpInfo(licenseKey, accountId, accessToken)
if err != nil {
return "", err
}
warpAccount := WarpAccount{
AccountID: account.AccountID,
AccessToken: account.AccessToken,
AccountID: identity.ID,
AccessToken: identity.Token,
}
warpConfig := WarpWireguardConfig{
PrivateKey: wg.PrivateKey,