sing-box/experimental/clashapi/cache.go

34 lines
742 B
Go
Raw Normal View History

2022-07-19 14:16:49 +00:00
package clashapi
import (
2023-11-28 04:00:28 +00:00
"context"
2022-07-19 14:16:49 +00:00
"net/http"
2023-03-25 04:03:23 +00:00
"github.com/sagernet/sing-box/adapter"
2023-11-28 04:00:28 +00:00
"github.com/sagernet/sing/service"
2023-03-25 04:03:23 +00:00
2022-07-19 14:16:49 +00:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
2023-11-28 04:00:28 +00:00
func cacheRouter(ctx context.Context) http.Handler {
2022-07-19 14:16:49 +00:00
r := chi.NewRouter()
2023-11-28 04:00:28 +00:00
r.Post("/fakeip/flush", flushFakeip(ctx))
2022-07-19 14:16:49 +00:00
return r
}
2023-11-28 04:00:28 +00:00
func flushFakeip(ctx context.Context) func(w http.ResponseWriter, r *http.Request) {
2023-03-25 04:03:23 +00:00
return func(w http.ResponseWriter, r *http.Request) {
2023-11-28 04:00:28 +00:00
cacheFile := service.FromContext[adapter.CacheFile](ctx)
if cacheFile != nil {
2023-03-25 04:03:23 +00: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 14:16:49 +00:00
}