2021-11-27 04:16:41 -05:00
|
|
|
package restfulapi
|
2021-09-05 09:28:30 -04:00
|
|
|
|
|
|
|
import (
|
2021-10-28 06:34:19 -04:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-07 13:23:08 -04:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2021-09-13 13:10:41 -04:00
|
|
|
"github.com/go-chi/render"
|
2021-09-07 13:23:08 -04:00
|
|
|
"github.com/go-playground/validator/v10"
|
2021-10-28 06:34:19 -04:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v5"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet"
|
2021-09-05 09:28:30 -04:00
|
|
|
)
|
|
|
|
|
2021-09-07 13:23:08 -04:00
|
|
|
var validate *validator.Validate
|
|
|
|
|
2021-09-13 13:10:41 -04:00
|
|
|
type StatsBound struct { // Better name?
|
2021-09-07 13:23:08 -04:00
|
|
|
Uplink int64 `json:"uplink"`
|
|
|
|
Downlink int64 `json:"downlink"`
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:10:41 -04:00
|
|
|
func (rs *restfulService) tagStats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
boundType := chi.URLParam(r, "bound_type")
|
|
|
|
tag := chi.URLParam(r, "tag")
|
2021-09-05 09:28:30 -04:00
|
|
|
|
2021-09-13 13:10:41 -04:00
|
|
|
if validate.Var(boundType, "required,oneof=inbounds outbounds") != nil ||
|
|
|
|
validate.Var(tag, "required,min=1,max=255") != nil {
|
|
|
|
render.Status(r, http.StatusUnprocessableEntity)
|
|
|
|
render.JSON(w, r, render.M{})
|
|
|
|
return
|
2021-09-07 13:23:08 -04:00
|
|
|
}
|
|
|
|
|
2021-09-13 13:10:41 -04:00
|
|
|
bound := boundType[:len(boundType)-1]
|
|
|
|
upCounter := rs.stats.GetCounter(bound + ">>>" + tag + ">>>traffic>>>uplink")
|
|
|
|
downCounter := rs.stats.GetCounter(bound + ">>>" + tag + ">>>traffic>>>downlink")
|
|
|
|
if upCounter == nil || downCounter == nil {
|
|
|
|
render.Status(r, http.StatusNotFound)
|
|
|
|
render.JSON(w, r, render.M{})
|
|
|
|
return
|
2021-09-07 13:23:08 -04:00
|
|
|
}
|
|
|
|
|
2021-09-13 13:10:41 -04:00
|
|
|
render.JSON(w, r, &StatsBound{
|
|
|
|
Uplink: upCounter.Value(),
|
|
|
|
Downlink: downCounter.Value(),
|
|
|
|
})
|
2021-09-05 09:28:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-13 13:10:41 -04:00
|
|
|
func (rs *restfulService) version(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.JSON(w, r, render.M{"version": core.Version()})
|
2021-09-05 09:28:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:23:08 -04:00
|
|
|
func (rs *restfulService) TokenAuthMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-09-13 13:10:41 -04:00
|
|
|
header := r.Header.Get("Authorization")
|
|
|
|
text := strings.SplitN(header, " ", 2)
|
|
|
|
|
|
|
|
hasInvalidHeader := text[0] != "Bearer"
|
|
|
|
hasInvalidSecret := len(text) != 2 || text[1] != rs.config.AuthToken
|
|
|
|
if hasInvalidHeader || hasInvalidSecret {
|
|
|
|
render.Status(r, http.StatusUnauthorized)
|
|
|
|
render.JSON(w, r, render.M{})
|
2021-09-05 09:28:30 -04:00
|
|
|
return
|
|
|
|
}
|
2021-09-13 13:10:41 -04:00
|
|
|
|
2021-09-07 13:23:08 -04:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2021-09-05 09:28:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:23:08 -04:00
|
|
|
func (rs *restfulService) start() error {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(middleware.Heartbeat("/ping"))
|
2021-09-06 08:11:30 -04:00
|
|
|
|
2021-09-13 13:10:41 -04:00
|
|
|
validate = validator.New()
|
2021-09-07 13:23:08 -04:00
|
|
|
r.Route("/v1", func(r chi.Router) {
|
2021-09-13 13:10:41 -04:00
|
|
|
r.Get("/{bound_type}/{tag}/stats", rs.tagStats)
|
2021-09-05 09:28:30 -04:00
|
|
|
})
|
2021-09-13 13:10:41 -04:00
|
|
|
r.Get("/version", rs.version)
|
2021-09-05 09:28:30 -04:00
|
|
|
|
2021-09-07 13:45:21 -04:00
|
|
|
var listener net.Listener
|
|
|
|
var err error
|
|
|
|
address := net.ParseAddress(rs.config.ListenAddr)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case address.Family().IsIP():
|
|
|
|
listener, err = internet.ListenSystem(rs.ctx, &net.TCPAddr{IP: address.IP(), Port: int(rs.config.ListenPort)}, nil)
|
|
|
|
case strings.EqualFold(address.Domain(), "localhost"):
|
|
|
|
listener, err = internet.ListenSystem(rs.ctx, &net.TCPAddr{IP: net.IP{127, 0, 0, 1}, Port: int(rs.config.ListenPort)}, nil)
|
|
|
|
default:
|
|
|
|
return newError("restful api cannot listen on the address: ", address)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return newError("restful api cannot listen on the port ", rs.config.ListenPort).Base(err)
|
|
|
|
}
|
|
|
|
|
2021-09-06 08:11:30 -04:00
|
|
|
go func() {
|
2021-09-07 13:45:21 -04:00
|
|
|
err := http.Serve(listener, r)
|
2021-09-07 13:23:08 -04:00
|
|
|
if err != nil {
|
2021-09-06 08:11:30 -04:00
|
|
|
newError("unable to serve restful api").WriteToLog()
|
|
|
|
}
|
|
|
|
}()
|
2021-09-05 09:28:30 -04:00
|
|
|
return nil
|
|
|
|
}
|