new: add cli interface with basic webui
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hiddify/libcore/admin_service"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var commandService = &cobra.Command{
|
||||
Use: "admin-service",
|
||||
Short: "Sign box service start/stop/install/uninstall",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
arg := ""
|
||||
if len(args) > 1 {
|
||||
arg = args[1]
|
||||
}
|
||||
code, out := admin_service.StartService(arg)
|
||||
fmt.Printf("exitCode:%d msg=%s", code, out)
|
||||
|
||||
},
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -109,6 +109,15 @@ func readConfigAt(path string) (*option.Options, error) {
|
||||
return &options, nil
|
||||
}
|
||||
|
||||
func readConfigBytes(content []byte) (*option.Options, error) {
|
||||
var options option.Options
|
||||
err := options.UnmarshalJSON(content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &options, nil
|
||||
}
|
||||
|
||||
func readConfigOptionsAt(path string) (*config.ConfigOptions, error) {
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,65 +1,33 @@
|
||||
package main
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/hiddify/libcore/config"
|
||||
"github.com/hiddify/libcore/global"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var commandRunInputPath string
|
||||
var (
|
||||
hiddifySettingPath string
|
||||
configPath string
|
||||
)
|
||||
|
||||
var commandRun = &cobra.Command{
|
||||
Use: "run",
|
||||
Short: "run",
|
||||
Args: cobra.ExactArgs(0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := runSingbox(commandRunInputPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
Args: cobra.OnlyValidArgs,
|
||||
Run: runCommand,
|
||||
}
|
||||
|
||||
func init() {
|
||||
commandRun.Flags().StringVarP(&commandRunInputPath, "config", "c", "", "read config")
|
||||
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.MarkFlagRequired("config")
|
||||
|
||||
mainCommand.AddCommand(commandRun)
|
||||
|
||||
}
|
||||
|
||||
func runSingbox(configPath string) error {
|
||||
options, err := readConfigAt(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options.Log = &option.LogOptions{}
|
||||
options.Log.Disabled = false
|
||||
options.Log.Level = "trace"
|
||||
options.Log.Output = ""
|
||||
options.Log.DisableColor = false
|
||||
|
||||
err = global.SetupC("./", "./", "./tmp", false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
configStr, err := config.ToJson(*options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go global.StartServiceC(false, configStr)
|
||||
fmt.Printf("Waiting for 30 seconds\n")
|
||||
// <-time.After(time.Second * 30)
|
||||
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
||||
<-sigChan
|
||||
|
||||
return err
|
||||
func runCommand(cmd *cobra.Command, args []string) {
|
||||
global.RunStandalone(hiddifySettingPath, configPath)
|
||||
}
|
||||
|
||||
21
cmd/cmd_tunnel_service.go
Normal file
21
cmd/cmd_tunnel_service.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hiddify/libcore/admin_service"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var commandService = &cobra.Command{
|
||||
Use: "tunnel run/start/stop/install/uninstall",
|
||||
Short: "Tunnel Service run/start/stop/install/uninstall",
|
||||
ValidArgs: []string{"run", "start", "stop", "install", "uninstall"},
|
||||
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
arg := args[0]
|
||||
code, out := admin_service.StartService(arg)
|
||||
fmt.Printf("exitCode:%d msg=%s", code, out)
|
||||
},
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
@@ -17,7 +17,7 @@ var (
|
||||
)
|
||||
|
||||
var mainCommand = &cobra.Command{
|
||||
Use: "hiddify-next",
|
||||
Use: "HiddifyCli",
|
||||
PersistentPreRun: preRun,
|
||||
}
|
||||
|
||||
@@ -30,10 +30,13 @@ func init() {
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := mainCommand.Execute(); err != nil {
|
||||
func ParseCli(args []string) error {
|
||||
mainCommand.SetArgs(args)
|
||||
err := mainCommand.Execute()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func preRun(cmd *cobra.Command, args []string) {
|
||||
Reference in New Issue
Block a user