clash/hub/route/restart.go

68 lines
1.7 KiB
Go
Raw Normal View History

2023-03-14 14:18:55 +00:00
package route
import (
"fmt"
"net/http"
"os"
"os/exec"
"runtime"
"syscall"
2023-11-03 13:01:45 +00:00
"github.com/metacubex/mihomo/hub/executor"
"github.com/metacubex/mihomo/log"
2023-03-14 14:18:55 +00:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
func restartRouter() http.Handler {
r := chi.NewRouter()
r.Post("/", restart)
return r
}
func restart(w http.ResponseWriter, r *http.Request) {
2023-03-14 14:38:42 +00:00
// modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/home/controlupdate.go#L108
2023-03-14 14:18:55 +00:00
execPath, err := os.Executable()
if err != nil {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError(fmt.Sprintf("getting path: %s", err)))
return
}
2023-03-14 14:38:42 +00:00
render.JSON(w, r, render.M{"status": "ok"})
if f, ok := w.(http.Flusher); ok {
f.Flush()
2023-03-14 14:18:55 +00:00
}
2023-03-14 14:38:42 +00:00
// modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/home/controlupdate.go#L180
// The background context is used because the underlying functions wrap it
// with timeout and shut down the server, which handles current request. It
// also should be done in a separate goroutine for the same reason.
2023-08-30 07:52:41 +00:00
go restartExecutable(execPath)
2023-04-02 07:16:42 +00:00
}
2023-03-14 14:18:55 +00:00
2023-08-30 07:52:41 +00:00
func restartExecutable(execPath string) {
2023-04-02 07:16:42 +00:00
var err error
executor.Shutdown()
2023-04-02 07:16:42 +00:00
if runtime.GOOS == "windows" {
cmd := exec.Command(execPath, os.Args[1:]...)
2023-03-14 14:38:42 +00:00
log.Infoln("restarting: %q %q", execPath, os.Args[1:])
2023-04-02 07:16:42 +00:00
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
2023-03-14 14:38:42 +00:00
if err != nil {
log.Fatalln("restarting: %s", err)
}
2023-04-02 07:16:42 +00:00
os.Exit(0)
}
log.Infoln("restarting: %q %q", execPath, os.Args[1:])
err = syscall.Exec(execPath, os.Args, os.Environ())
if err != nil {
log.Fatalln("restarting: %s", err)
}
2023-03-14 14:18:55 +00:00
}