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

@@ -14,19 +14,19 @@ import (
"github.com/spf13/cobra"
)
var defaultConfigs config.ConfigOptions
var commandBuildOutputPath string
var (
hiddifySettingPath string
configPath string
defaultConfigs config.ConfigOptions = *config.DefaultConfigOptions()
commandBuildOutputPath string
)
var commandBuild = &cobra.Command{
Use: "build",
Short: "Build configuration",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var optionsPath string
if len(args) > 1 {
optionsPath = args[1]
}
err := build(args[0], optionsPath)
err := build(configPath, hiddifySettingPath)
if err != nil {
log.Fatal(err)
}
@@ -36,9 +36,8 @@ var commandBuild = &cobra.Command{
var commandCheck = &cobra.Command{
Use: "check",
Short: "Check configuration",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := check(args[0])
err := check(configPath)
if err != nil {
log.Fatal(err)
}
@@ -139,11 +138,21 @@ func readConfigOptionsAt(path string) (*config.ConfigOptions, error) {
return nil, err
}
}
if options.Warp2.WireguardConfigStr != "" {
err := json.Unmarshal([]byte(options.Warp2.WireguardConfigStr), &options.Warp2.WireguardConfig)
if err != nil {
return nil, err
}
}
return &options, nil
}
func addHConfigFlags(commandRun *cobra.Command) {
commandRun.Flags().StringVarP(&configPath, "config", "c", "", "proxy config path or url")
commandRun.MarkFlagRequired("config")
commandRun.Flags().StringVarP(&hiddifySettingPath, "hiddify", "d", "", "Hiddify Setting JSON Path")
commandRun.Flags().BoolVar(&defaultConfigs.EnableFullConfig, "full-config", false, "allows including tags other than output")
commandRun.Flags().StringVar(&defaultConfigs.LogLevel, "log", "warn", "log level")
commandRun.Flags().BoolVar(&defaultConfigs.InboundOptions.EnableTun, "tun", false, "Enable Tun")
@@ -161,5 +170,6 @@ func addHConfigFlags(commandRun *cobra.Command) {
commandRun.Flags().StringVar(&defaultConfigs.RemoteDnsAddress, "dns-remote", "1.1.1.1", "RemoteDNS (1.1.1.1, https://1.1.1.1/dns-query)")
commandRun.Flags().StringVar(&defaultConfigs.DirectDnsAddress, "dns-direct", "1.1.1.1", "DirectDNS (1.1.1.1, https://1.1.1.1/dns-query)")
commandRun.Flags().StringVar(&defaultConfigs.ClashApiSecret, "web-secret", "", "Web Server Secret")
commandRun.Flags().Uint16Var(&defaultConfigs.ClashApiPort, "web-port", 6756, "Web Server Port")
}

View File

@@ -6,11 +6,6 @@ import (
"github.com/spf13/cobra"
)
var (
hiddifySettingPath string
configPath string
)
var commandRun = &cobra.Command{
Use: "run",
Short: "run",
@@ -19,11 +14,10 @@ var commandRun = &cobra.Command{
}
func init() {
commandRun.PersistentFlags().BoolP("help", "", false, "help for this command")
commandRun.Flags().StringVarP(&hiddifySettingPath, "hiddify", "h", "", "Hiddify Setting JSON Path")
commandRun.Flags().StringVarP(&configPath, "config", "c", "", "proxy config path or url")
// commandRun.PersistentFlags().BoolP("help", "", false, "help for this command")
// commandRun.Flags().StringVarP(&hiddifySettingPath, "hiddify", "d", "", "Hiddify Setting JSON Path")
addHConfigFlags(commandRun)
commandRun.MarkFlagRequired("config")
mainCommand.AddCommand(commandRun)
}

View File

@@ -4,7 +4,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"net/netip"
"os"
"strings"
@@ -62,40 +62,6 @@ type SingboxConfig struct {
MTU int `json:"mtu"`
}
func wireGuardToSingbox(wgConfig WireGuardConfig, 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
// }
out := T.Outbound{
Type: "wireguard",
Tag: "WARP",
WireGuardOptions: T.WireGuardOutboundOptions{
ServerOptions: T.ServerOptions{
Server: server,
ServerPort: port,
},
PrivateKey: wgConfig.Interface.PrivateKey,
PeerPublicKey: wgConfig.Peer.PublicKey,
Reserved: []uint8{0, 0, 0},
MTU: 1280,
},
}
for _, addr := range wgConfig.Interface.Address {
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 {