github.com/igoogolx/clash@v1.19.8/hub/route/inbounds.go (about)

     1  package route
     2  
     3  import (
     4  	"net/http"
     5  
     6  	C "github.com/igoogolx/clash/constant"
     7  	"github.com/igoogolx/clash/listener"
     8  	"github.com/igoogolx/clash/tunnel"
     9  
    10  	"github.com/go-chi/chi/v5"
    11  	"github.com/go-chi/render"
    12  )
    13  
    14  func inboundRouter() http.Handler {
    15  	r := chi.NewRouter()
    16  	r.Get("/", getInbounds)
    17  	r.Put("/", updateInbounds)
    18  	return r
    19  }
    20  
    21  func getInbounds(w http.ResponseWriter, r *http.Request) {
    22  	inbounds := listener.GetInbounds()
    23  	render.JSON(w, r, render.M{
    24  		"inbounds": inbounds,
    25  	})
    26  }
    27  
    28  func updateInbounds(w http.ResponseWriter, r *http.Request) {
    29  	var req []C.Inbound
    30  	if err := render.DecodeJSON(r.Body, &req); err != nil {
    31  		render.Status(r, http.StatusBadRequest)
    32  		render.JSON(w, r, ErrBadRequest)
    33  		return
    34  	}
    35  	tcpIn := tunnel.TCPIn()
    36  	udpIn := tunnel.UDPIn()
    37  	listener.ReCreateListeners(req, tcpIn, udpIn)
    38  	render.NoContent(w, r)
    39  }