2023-03-23 12:42:01 +00:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2023-04-02 07:16:42 +00:00
|
|
|
"fmt"
|
2023-03-23 12:42:01 +00:00
|
|
|
"net/http"
|
2023-04-02 07:16:42 +00:00
|
|
|
"os"
|
2023-03-23 12:42:01 +00:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/hub/updater"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
2023-04-02 07:16:42 +00:00
|
|
|
"github.com/go-chi/render"
|
2023-03-23 12:42:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func upgradeRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Post("/", upgrade)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func upgrade(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/home/controlupdate.go#L108
|
|
|
|
log.Infoln("start update")
|
2023-04-02 07:16:42 +00:00
|
|
|
execPath, err := os.Executable()
|
2023-03-23 12:42:01 +00:00
|
|
|
if err != nil {
|
2023-04-02 07:16:42 +00:00
|
|
|
render.Status(r, http.StatusInternalServerError)
|
|
|
|
render.JSON(w, r, newError(fmt.Sprintf("getting path: %s", err)))
|
2023-03-26 16:49:32 +00:00
|
|
|
return
|
2023-03-23 12:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-02 07:16:42 +00:00
|
|
|
err = updater.Update(execPath)
|
|
|
|
if err != nil {
|
2023-05-16 16:17:23 +00:00
|
|
|
log.Warnln("%s", err)
|
2023-04-02 07:16:42 +00:00
|
|
|
render.Status(r, http.StatusInternalServerError)
|
2023-05-16 16:17:23 +00:00
|
|
|
render.JSON(w, r, newError(fmt.Sprintf("%s", err)))
|
2023-04-02 07:16:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, render.M{"status": "ok"})
|
|
|
|
if f, ok := w.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
go runRestart(execPath)
|
2023-03-23 12:42:01 +00:00
|
|
|
}
|