mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-10-11 17:58:13 +08:00
feat: add -post-up and -post-down flags for cmdline
This commit is contained in:
1 parent
2cfff83e85
commit
d701d1c0e3
2 files changed
+35
No files matched your search
@@ -3,6 +3,7 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -34,3 +35,19 @@ func splitArgs(cmd string) []string {
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
func ExecShell(shellStr string) (string, error) {
|
||||
var cmd *exec.Cmd
|
||||
if runtime.GOOS == "windows" {
|
||||
cmd = exec.Command("cmd.exe", "/C", shellStr)
|
||||
} else {
|
||||
cmd = exec.Command("sh", "-c", shellStr)
|
||||
}
|
||||
|
||||
prepareBackgroundCommand(cmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%v, %s", err, string(out))
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/metacubex/mihomo/common/cmd"
|
||||
"github.com/metacubex/mihomo/component/generator"
|
||||
"github.com/metacubex/mihomo/component/geodata"
|
||||
"github.com/metacubex/mihomo/component/updater"
|
||||
@@ -41,6 +42,8 @@ var (
|
||||
externalControllerUnix string
|
||||
externalControllerPipe string
|
||||
secret string
|
||||
postUp string
|
||||
postDown string
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -52,6 +55,8 @@ func init() {
|
||||
flag.StringVar(&externalControllerUnix, "ext-ctl-unix", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER_UNIX"), "override external controller unix address")
|
||||
flag.StringVar(&externalControllerPipe, "ext-ctl-pipe", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER_PIPE"), "override external controller pipe address")
|
||||
flag.StringVar(&secret, "secret", os.Getenv("CLASH_OVERRIDE_SECRET"), "override secret for RESTful API")
|
||||
flag.StringVar(&postUp, "post-up", os.Getenv("CLASH_POST_UP"), "set post-up script")
|
||||
flag.StringVar(&postDown, "post-down", os.Getenv("CLASH_POST_DOWN"), "set post-down script")
|
||||
flag.BoolVar(&geodataMode, "m", false, "set geodata mode")
|
||||
flag.BoolVar(&version, "v", false, "show current version of mihomo")
|
||||
flag.BoolVar(&testConfig, "t", false, "test configuration and exit")
|
||||
@@ -184,6 +189,19 @@ func main() {
|
||||
updater.RegisterGeoUpdater()
|
||||
}
|
||||
|
||||
if postDown != "" {
|
||||
defer func() {
|
||||
if _, err := cmd.ExecShell(postDown); err != nil {
|
||||
log.Errorln("post-down script error: %s", err.Error())
|
||||
}
|
||||
}()
|
||||
}
|
||||
if postUp != "" {
|
||||
if _, err := cmd.ExecShell(postUp); err != nil {
|
||||
log.Fatalln("post-up script error: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
defer executor.Shutdown()
|
||||
|
||||
termSign := make(chan os.Signal, 1)
|
||||
|
||||
Reference in new issue
Block a user