Files
umbrix-libcore/config/warp.go

147 lines
4.1 KiB
Go
Raw Normal View History

2024-01-25 18:37:08 +00:00
package config
import (
"encoding/base64"
2024-01-25 18:37:08 +00:00
"fmt"
"net/netip"
"os"
"github.com/bepass-org/warp-plus/warp"
// "github.com/bepass-org/wireguard-go/warp"
"log/slog"
2024-01-25 18:37:08 +00:00
T "github.com/sagernet/sing-box/option"
)
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 WarpWireguardConfig, server string, port uint16) (*T.Outbound, error) {
2024-01-25 18:37:08 +00:00
// 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)
2024-01-25 18:37:08 +00:00
out := T.Outbound{
Type: "wireguard",
Tag: "WARP",
WireGuardOptions: T.WireGuardOutboundOptions{
ServerOptions: T.ServerOptions{
Server: server,
ServerPort: port,
},
PrivateKey: wgConfig.PrivateKey,
PeerPublicKey: wgConfig.PeerPublicKey,
Reserved: []uint8{clientID[0], clientID[1], clientID[2]},
MTU: 1330,
2024-01-25 18:37:08 +00:00
},
}
ips := []string{wgConfig.LocalAddressIPv4 + "/24", wgConfig.LocalAddressIPv6 + "/128"}
for _, addr := range ips {
if addr == "" {
continue
}
2024-01-25 18:37:08 +00:00
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
}
2024-01-26 13:29:37 +01:00
func getRandomIP() string {
ipPort, err := warp.RandomWarpEndpoint(true, true)
2024-01-26 13:29:37 +01:00
if err == nil {
return ipPort.Addr().String()
2024-01-26 13:29:37 +01:00
}
return "engage.cloudflareclient.com"
}
func generateWarp(license string, host string, port uint16, fakePackets string, fakePacketsSize string, fakePacketsDelay string) (*T.Outbound, error) {
2024-02-20 07:56:47 +01:00
_, _, wgConfig, err := GenerateWarpInfo(license, "", "")
2024-02-20 07:56:47 +01:00
if err != nil {
return nil, err
}
if wgConfig == nil {
return nil, fmt.Errorf("invalid warp config")
}
fmt.Printf("%v", wgConfig)
return GenerateWarpSingbox(*wgConfig, host, port, fakePackets, fakePacketsSize, fakePacketsDelay)
2024-02-20 07:56:47 +01:00
}
func GenerateWarpSingbox(wgConfig WarpWireguardConfig, host string, port uint16, fakePackets string, fakePacketsSize string, fakePacketsDelay string) (*T.Outbound, error) {
2024-02-20 09:18:53 +01:00
if host == "" {
2024-01-26 13:29:37 +01:00
host = "auto"
}
2024-01-26 14:01:13 +01:00
if host == "auto" && fakePackets == "" {
fakePackets = "8-15"
2024-01-26 14:01:13 +01:00
}
if fakePackets != "" && fakePacketsSize == "" {
fakePacketsSize = "40-100"
}
if fakePackets != "" && fakePacketsDelay == "" {
2024-02-13 18:10:47 +01:00
fakePacketsDelay = "20-250"
}
2024-02-20 07:56:47 +01:00
singboxConfig, err := wireGuardToSingbox(wgConfig, host, port)
2024-01-25 18:37:08 +00:00
if err != nil {
fmt.Printf("%v %v", singboxConfig, err)
2024-01-25 18:37:08 +00:00
return nil, err
}
2024-01-26 14:01:13 +01:00
singboxConfig.WireGuardOptions.FakePackets = fakePackets
singboxConfig.WireGuardOptions.FakePacketsSize = fakePacketsSize
singboxConfig.WireGuardOptions.FakePacketsDelay = fakePacketsDelay
2024-02-02 13:47:41 +01:00
2024-01-25 18:37:08 +00:00
return singboxConfig, nil
}
func GenerateWarpInfo(license string, oldAccountId string, oldAccessToken string) (*warp.Identity, string, *WarpWireguardConfig, error) {
if oldAccountId != "" && oldAccessToken != "" {
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,
2024-05-29 18:47:04 +02:00
PeerPublicKey: identity.Config.Peers[0].PublicKey,
LocalAddressIPv4: identity.Config.Interface.Addresses.V4,
LocalAddressIPv6: identity.Config.Interface.Addresses.V6,
ClientID: identity.Config.ClientID,
}
}
return &identity, res, &warpcfg, err
}