new: add tunnel service
This commit is contained in:
19
cmd/cmd_admin_service.go
Normal file
19
cmd/cmd_admin_service.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"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) {
|
||||
if len(args) < 2 {
|
||||
admin_service.StartService("")
|
||||
}
|
||||
admin_service.StartService(args[1])
|
||||
},
|
||||
}
|
||||
@@ -75,7 +75,7 @@ func build(path string, optionsPath string) error {
|
||||
}
|
||||
if commandBuildOutputPath != "" {
|
||||
outputPath, _ := filepath.Abs(filepath.Join(workingDir, commandBuildOutputPath))
|
||||
err = os.WriteFile(outputPath, []byte(config), 0777)
|
||||
err = os.WriteFile(outputPath, []byte(config), 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/hiddify/libcore/utils"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
)
|
||||
|
||||
var commandGenerateCertification = &cobra.Command{
|
||||
Use: "gen-cert",
|
||||
Short: "Generate certification for web server",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := os.MkdirAll("cert", 600)
|
||||
err := os.MkdirAll("cert", 0644)
|
||||
if err != nil {
|
||||
panic("Error: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ func parse(path string) error {
|
||||
}
|
||||
if commandParseOutputPath != "" {
|
||||
outputPath, _ := filepath.Abs(filepath.Join(workingDir, commandParseOutputPath))
|
||||
err = os.WriteFile(outputPath, config, 0777)
|
||||
err = os.WriteFile(outputPath, config, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
53
cmd/cmd_run.go
Normal file
53
cmd/cmd_run.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/hiddify/libcore/config"
|
||||
"github.com/hiddify/libcore/global"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var commandRunInputPath 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)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
commandRun.Flags().StringVarP(&commandRunInputPath, "config", "c", "", "read config")
|
||||
mainCommand.AddCommand(commandRun)
|
||||
|
||||
}
|
||||
|
||||
func runSingbox(configPath string) error {
|
||||
options, err := readConfigAt(configPath)
|
||||
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)
|
||||
return err
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var commandService = &cobra.Command{
|
||||
Use: "service",
|
||||
Short: "Sign box service",
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/hiddify/libcore/service"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var commandServiceInstall = &cobra.Command{
|
||||
Use: "install",
|
||||
Short: "install the service",
|
||||
Run: service.InstallService,
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/hiddify/libcore/service"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var commandServiceStart = &cobra.Command{
|
||||
Use: "start",
|
||||
Short: "Start a sign box instance",
|
||||
Run: service.StartService,
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/hiddify/libcore/service"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var commandServiceStop = &cobra.Command{
|
||||
Use: "stop",
|
||||
Short: "stop sign box",
|
||||
Run: service.StopService,
|
||||
}
|
||||
@@ -25,12 +25,6 @@ func init() {
|
||||
mainCommand.AddCommand(commandService)
|
||||
mainCommand.AddCommand(commandGenerateCertification)
|
||||
|
||||
commandService.AddCommand(commandServiceStart)
|
||||
commandService.AddCommand(commandServiceStop)
|
||||
commandService.AddCommand(commandServiceInstall)
|
||||
|
||||
commandServiceStart.Flags().Int("port", 8080, "Webserver port number")
|
||||
|
||||
mainCommand.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory")
|
||||
mainCommand.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output")
|
||||
|
||||
@@ -49,7 +43,7 @@ func preRun(cmd *cobra.Command, args []string) {
|
||||
if workingDir != "" {
|
||||
_, err := os.Stat(workingDir)
|
||||
if err != nil {
|
||||
os.MkdirAll(workingDir, 0o777)
|
||||
os.MkdirAll(workingDir, 0o0644)
|
||||
}
|
||||
if err := os.Chdir(workingDir); err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user