sing-box/experimental/clashapi/cache.go

34 lines
742 B
Go
Raw Normal View History

2022-07-19 22:16:49 +08:00
package clashapi
import (
2023-11-28 12:00:28 +08:00
"context"
2022-07-19 22:16:49 +08:00
"net/http"
2023-03-25 12:03:23 +08:00
"github.com/sagernet/sing-box/adapter"
2023-11-28 12:00:28 +08:00
"github.com/sagernet/sing/service"
2023-03-25 12:03:23 +08:00
2022-07-19 22:16:49 +08:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
2023-11-28 12:00:28 +08:00
func cacheRouter(ctx context.Context) http.Handler {
2022-07-19 22:16:49 +08:00
r := chi.NewRouter()
2023-11-28 12:00:28 +08:00
r.Post("/fakeip/flush", flushFakeip(ctx))
2022-07-19 22:16:49 +08:00
return r
}
2023-11-28 12:00:28 +08:00
func flushFakeip(ctx context.Context) func(w http.ResponseWriter, r *http.Request) {
2023-03-25 12:03:23 +08:00
return func(w http.ResponseWriter, r *http.Request) {
2023-11-28 12:00:28 +08:00
cacheFile := service.FromContext[adapter.CacheFile](ctx)
if cacheFile != nil {
2023-03-25 12:03:23 +08:00
err := cacheFile.FakeIPReset()
if err != nil {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError(err.Error()))
return
}
}
render.NoContent(w, r)
}
2022-07-19 22:16:49 +08:00
}