new: add tunnel service

This commit is contained in:
Hiddify
2024-01-29 21:55:01 +01:00
parent 25d5b8a1c5
commit 5fa28220b4
28 changed files with 364 additions and 231 deletions

19
cmd/cmd_admin_service.go Normal file
View 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])
},
}

View File

@@ -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
}

View File

@@ -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())
}

View File

@@ -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
View 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
}

View File

@@ -1,10 +0,0 @@
package main
import (
"github.com/spf13/cobra"
)
var commandService = &cobra.Command{
Use: "service",
Short: "Sign box service",
}

View File

@@ -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,
}

View File

@@ -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,
}

View File

@@ -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,
}

View File

@@ -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)