new: add cli interface with basic webui
This commit is contained in:
@@ -7,54 +7,43 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExecuteCmd(executablePath, args string, background bool) (string, error) {
|
||||
func ExecuteCmd(executablePath string, background bool, args ...string) (string, error) {
|
||||
cwd := filepath.Dir(executablePath)
|
||||
if appimage := os.Getenv("APPIMAGE"); appimage != "" {
|
||||
executablePath = appimage + " HiddifyService"
|
||||
executablePath = appimage
|
||||
if !background {
|
||||
return "Fail", fmt.Errorf("Appimage can not have service")
|
||||
return "Fail", fmt.Errorf("Appimage cannot have service")
|
||||
}
|
||||
}
|
||||
|
||||
if err := execCmdImp([]string{"cocoasudo", "--prompt=Hiddify needs root for tunneling.", executablePath, args}, cwd, background); err == nil {
|
||||
return "Ok", nil
|
||||
}
|
||||
if err := execCmdImp([]string{"gksu", executablePath, args}, cwd, background); err == nil {
|
||||
return "Ok", nil
|
||||
}
|
||||
if err := execCmdImp([]string{"pkexec", executablePath, args}, cwd, background); err == nil {
|
||||
return "Ok", nil
|
||||
}
|
||||
if err := execCmdImp([]string{"xterm", "-e", "sudo " + executablePath + " " + args}, cwd, background); err == nil {
|
||||
return "Ok", nil
|
||||
commands := [][]string{
|
||||
{"cocoasudo", "--prompt=Hiddify needs root for tunneling.", executablePath},
|
||||
{"gksu", executablePath},
|
||||
{"pkexec", executablePath},
|
||||
{"xterm", "-e", "sudo", executablePath, strings.Join(args, " ")},
|
||||
{"sudo", executablePath},
|
||||
}
|
||||
|
||||
if err := execCmdImp([]string{"sudo", executablePath, args}, cwd, background); err == nil {
|
||||
return "Ok", nil
|
||||
var err error
|
||||
var cmd *exec.Cmd
|
||||
for _, command := range commands {
|
||||
cmd = exec.Command(command[0], command[1:]...)
|
||||
cmd.Dir = cwd
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
fmt.Printf("Running command: %v\n", command)
|
||||
if background {
|
||||
err = cmd.Start()
|
||||
} else {
|
||||
err = cmd.Run()
|
||||
}
|
||||
if err == nil {
|
||||
return "Ok", nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Error executing run as root shell command")
|
||||
|
||||
}
|
||||
|
||||
func execCmdImp(commands []string, cwd string, background bool) error {
|
||||
cmd := exec.Command(commands[0], commands[1:]...)
|
||||
cmd.Dir = cwd
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
fmt.Printf("Running command: %v", commands)
|
||||
if background {
|
||||
if err := cmd.Start(); err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -9,18 +9,30 @@ import (
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
func ExecuteCmd(exe string, args string, background bool) (string, error) {
|
||||
func ExecuteCmd(exe string, background bool, args ...string) (string, error) {
|
||||
verb := "runas"
|
||||
cwd, _ := os.Getwd()
|
||||
cwd, err := os.Getwd() // Error handling added
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
verbPtr, _ := syscall.UTF16PtrFromString(verb)
|
||||
exePtr, _ := syscall.UTF16PtrFromString(exe)
|
||||
cwdPtr, _ := syscall.UTF16PtrFromString(cwd)
|
||||
argPtr, _ := syscall.UTF16PtrFromString(args)
|
||||
|
||||
var showCmd int32 = 1 //SW_NORMAL
|
||||
// Convert args to UTF16Ptr slice
|
||||
var argsPtr []*uint16
|
||||
for _, arg := range args {
|
||||
argPtr, err := syscall.UTF16PtrFromString(arg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
argsPtr = append(argsPtr, argPtr)
|
||||
}
|
||||
|
||||
err := windows.ShellExecute(0, verbPtr, exePtr, argPtr, cwdPtr, showCmd)
|
||||
var showCmd int32 = 1 // SW_NORMAL
|
||||
|
||||
err = windows.ShellExecute(0, verbPtr, exePtr, nil, cwdPtr, showCmd)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -94,10 +94,10 @@ func stopTunnelRequest() (bool, error) {
|
||||
func runTunnelService(opt ConfigOptions) (bool, error) {
|
||||
executablePath := getTunnelServicePath()
|
||||
fmt.Printf("Executable path is %s", executablePath)
|
||||
out, err := ExecuteCmd(executablePath, "install", false)
|
||||
out, err := ExecuteCmd(executablePath, false, "tunnel", "install")
|
||||
fmt.Println("Shell command executed:", out, err)
|
||||
if err != nil {
|
||||
out, err = ExecuteCmd(executablePath, "", true)
|
||||
out, err = ExecuteCmd(executablePath, true, "tunnel", "run")
|
||||
fmt.Println("Shell command executed without flag:", out, err)
|
||||
}
|
||||
if err == nil {
|
||||
@@ -112,11 +112,11 @@ func getTunnelServicePath() string {
|
||||
binFolder := filepath.Dir(exePath)
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
fullPath = "HiddifyService.exe"
|
||||
fullPath = "HiddifyCli.exe"
|
||||
case "darwin":
|
||||
fallthrough
|
||||
default:
|
||||
fullPath = "HiddifyService"
|
||||
fullPath = "HiddifyCli"
|
||||
}
|
||||
|
||||
abspath, _ := filepath.Abs(filepath.Join(binFolder, fullPath))
|
||||
|
||||
@@ -61,7 +61,7 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
|
||||
fmt.Printf("config options: %+v\n", opt)
|
||||
|
||||
var options option.Options
|
||||
directDNSDomains := []string{}
|
||||
directDNSDomains := make(map[string]bool)
|
||||
dnsRules := []option.DefaultDNSRule{}
|
||||
|
||||
var bind string
|
||||
@@ -221,7 +221,7 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
|
||||
}
|
||||
parsedUrl, err := url.Parse(fmt.Sprintf("https://%s", remoteDNSAddress))
|
||||
if err == nil && net.ParseIP(parsedUrl.Host) == nil {
|
||||
directDNSDomains = append(directDNSDomains, fmt.Sprintf("full:%s", parsedUrl.Host))
|
||||
directDNSDomains["full:"+parsedUrl.Host] = true
|
||||
//TODO: IS it really needed
|
||||
}
|
||||
|
||||
@@ -420,7 +420,7 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
|
||||
}
|
||||
|
||||
if serverDomain != "" {
|
||||
directDNSDomains = append(directDNSDomains, serverDomain)
|
||||
directDNSDomains[serverDomain] = true
|
||||
}
|
||||
out = *outbound
|
||||
|
||||
@@ -518,7 +518,12 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
|
||||
// trickDnsRule.Server = DNSTricksDirectTag
|
||||
// options.DNS.Rules = append([]option.DNSRule{{Type: C.RuleTypeDefault, DefaultOptions: trickDnsRule}}, options.DNS.Rules...)
|
||||
|
||||
domains := strings.Join(directDNSDomains, ",")
|
||||
directDNSDomainskeys := make([]string, 0, len(directDNSDomains))
|
||||
for key := range directDNSDomains {
|
||||
directDNSDomainskeys = append(directDNSDomainskeys, key)
|
||||
}
|
||||
|
||||
domains := strings.Join(directDNSDomainskeys, ",")
|
||||
directRule := Rule{Domains: domains, Outbound: OutboundBypassTag}
|
||||
dnsRule := directRule.MakeDNSRule()
|
||||
dnsRule.Server = DNSDirectTag
|
||||
|
||||
@@ -96,7 +96,7 @@ func DefaultConfigOptions() *ConfigOptions {
|
||||
EnableDNSRouting: false,
|
||||
},
|
||||
InboundOptions: InboundOptions{
|
||||
EnableTun: true,
|
||||
EnableTun: false,
|
||||
SetSystemProxy: true,
|
||||
MixedPort: 2334,
|
||||
LocalDnsPort: 16450,
|
||||
@@ -117,12 +117,12 @@ func DefaultConfigOptions() *ConfigOptions {
|
||||
},
|
||||
LogLevel: "info",
|
||||
EnableClashApi: true,
|
||||
ClashApiPort: 16756,
|
||||
ClashApiPort: 6756,
|
||||
GeoIPPath: "geoip.db",
|
||||
GeoSitePath: "geosite.db",
|
||||
Rules: []Rule{},
|
||||
MuxOptions: MuxOptions{
|
||||
EnableMux: true,
|
||||
EnableMux: false,
|
||||
MuxPadding: true,
|
||||
MaxStreams: 8,
|
||||
MuxProtocol: "h2mux",
|
||||
|
||||
Reference in New Issue
Block a user