1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-28 18:25:23 +00:00

added instance state to restful controller

This commit is contained in:
Shelikhoo 2021-09-06 14:04:30 +01:00
parent 8c857858dc
commit 07c96195cf
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316

View File

@ -13,7 +13,7 @@ type StatsUser struct {
email string `form:"email" binging:"required_without=uuid,email"` email string `form:"email" binging:"required_without=uuid,email"`
} }
func statsUser(c *gin.Context) { func (r *restfulService) statsUser(c *gin.Context) {
var statsUser StatsUser var statsUser StatsUser
if err := c.BindQuery(&statsUser); err != nil { if err := c.BindQuery(&statsUser); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
@ -39,7 +39,7 @@ type StatsResponse struct {
Outbound StatsBound `json:"outbound"` Outbound StatsBound `json:"outbound"`
} }
func stats(c *gin.Context) { func (r *restfulService) statsRequest(c *gin.Context) {
var stats Stats var stats Stats
if err := c.BindQuery(&stats); err != nil { if err := c.BindQuery(&stats); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
@ -58,14 +58,20 @@ func stats(c *gin.Context) {
c.JSON(http.StatusOK, response) c.JSON(http.StatusOK, response)
} }
func loggerReboot(c *gin.Context) { func (r *restfulService) loggerReboot(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{}) c.JSON(http.StatusOK, gin.H{})
} }
func TokenAuthMiddleware() gin.HandlerFunc { func (r *restfulService) TokenAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
auth := c.GetHeader("Authorization") auth := c.GetHeader("Authorization")
if auth[6:] != "token123" { // tip: Bearer: token123 const prefix = "Bearer "
if !strings.HasPrefix(auth, prefix) {
c.JSON(http.StatusUnauthorized, "unauthorized")
c.Abort()
}
auth = strings.TrimPrefix(auth, prefix)
if auth != r.config.AuthToken { // tip: Bearer: token123
c.JSON(http.StatusUnauthorized, "unauthorized") c.JSON(http.StatusUnauthorized, "unauthorized")
c.Abort() c.Abort()
return return
@ -84,11 +90,11 @@ func (r *restfulService) start() error {
}) })
v1 := r.Group("/v1") v1 := r.Group("/v1")
v1.Use(TokenAuthMiddleware()) v1.Use(r.TokenAuthMiddleware())
{ {
v1.GET("/stats/user", statsUser) v1.GET("/stats/user", r.statsUser)
v1.GET("/stats", stats) v1.GET("/stats", r.statsRequest)
v1.POST("/logger/reboot", loggerReboot) v1.POST("/logger/reboot", r.loggerReboot)
} }
var listener net.Listener var listener net.Listener