This commit is contained in:
ginuerzh
2022-09-22 18:45:15 +08:00
parent 5237f79740
commit cf20abf656
13 changed files with 620 additions and 283 deletions

175
api/config_hop.go Normal file
View File

@ -0,0 +1,175 @@
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-gost/x/config"
"github.com/go-gost/x/config/parsing"
"github.com/go-gost/x/registry"
)
// swagger:parameters createHopRequest
type createHopRequest struct {
// in: body
Data config.HopConfig `json:"data"`
}
// successful operation.
// swagger:response createHopResponse
type createHopResponse struct {
Data Response
}
func createHop(ctx *gin.Context) {
// swagger:route POST /config/hops Hop createHopRequest
//
// Create a new hop, the name of hop must be unique in hop list.
//
// Security:
// basicAuth: []
//
// Responses:
// 200: createHopResponse
var req createHopRequest
ctx.ShouldBindJSON(&req.Data)
if req.Data.Name == "" {
writeError(ctx, ErrInvalid)
return
}
v, err := parsing.ParseHop(&req.Data)
if err != nil {
writeError(ctx, ErrCreate)
return
}
if err := registry.HopRegistry().Register(req.Data.Name, v); err != nil {
writeError(ctx, ErrDup)
return
}
cfg := config.Global()
cfg.Hops = append(cfg.Hops, &req.Data)
config.SetGlobal(cfg)
ctx.JSON(http.StatusOK, Response{
Msg: "OK",
})
}
// swagger:parameters updateHopRequest
type updateHopRequest struct {
// in: path
// required: true
// hop name
Hop string `uri:"hop" json:"hop"`
// in: body
Data config.HopConfig `json:"data"`
}
// successful operation.
// swagger:response updateHopResponse
type updateHopResponse struct {
Data Response
}
func updateHop(ctx *gin.Context) {
// swagger:route PUT /config/hops/{hop} Hop updateHopRequest
//
// Update hop by name, the hop must already exist.
//
// Security:
// basicAuth: []
//
// Responses:
// 200: updateHopResponse
var req updateHopRequest
ctx.ShouldBindUri(&req)
ctx.ShouldBindJSON(&req.Data)
if !registry.HopRegistry().IsRegistered(req.Hop) {
writeError(ctx, ErrNotFound)
return
}
req.Data.Name = req.Hop
v, err := parsing.ParseHop(&req.Data)
if err != nil {
writeError(ctx, ErrCreate)
return
}
registry.HopRegistry().Unregister(req.Hop)
if err := registry.HopRegistry().Register(req.Hop, v); err != nil {
writeError(ctx, ErrDup)
return
}
cfg := config.Global()
for i := range cfg.Hops {
if cfg.Hops[i].Name == req.Hop {
cfg.Hops[i] = &req.Data
break
}
}
config.SetGlobal(cfg)
ctx.JSON(http.StatusOK, Response{
Msg: "OK",
})
}
// swagger:parameters deleteHopRequest
type deleteHopRequest struct {
// in: path
// required: true
Hop string `uri:"hop" json:"hop"`
}
// successful operation.
// swagger:response deleteHopResponse
type deleteHopResponse struct {
Data Response
}
func deleteHop(ctx *gin.Context) {
// swagger:route DELETE /config/hops/{hop} Hop deleteHopRequest
//
// Delete hop by name.
//
// Security:
// basicAuth: []
//
// Responses:
// 200: deleteHopResponse
var req deleteHopRequest
ctx.ShouldBindUri(&req)
if !registry.HopRegistry().IsRegistered(req.Hop) {
writeError(ctx, ErrNotFound)
return
}
registry.HopRegistry().Unregister(req.Hop)
cfg := config.Global()
hops := cfg.Hops
cfg.Hops = nil
for _, s := range hops {
if s.Name == req.Hop {
continue
}
cfg.Hops = append(cfg.Hops, s)
}
config.SetGlobal(cfg)
ctx.JSON(http.StatusOK, Response{
Msg: "OK",
})
}

View File

@ -110,6 +110,10 @@ func registerConfig(config *gin.RouterGroup) {
config.PUT("/chains/:chain", updateChain)
config.DELETE("/chains/:chain", deleteChain)
config.POST("/hops", createHop)
config.PUT("/hops/:hop", updateHop)
config.DELETE("/hops/:hop", deleteHop)
config.POST("/authers", createAuther)
config.PUT("/authers/:auther", updateAuther)
config.DELETE("/authers/:auther", deleteAuther)

View File

@ -24,6 +24,8 @@ definitions:
properties:
file:
$ref: '#/definitions/FileLoader'
http:
$ref: '#/definitions/HTTPLoader'
matchers:
items:
type: string
@ -64,6 +66,8 @@ definitions:
x-go-name: Auths
file:
$ref: '#/definitions/FileLoader'
http:
$ref: '#/definitions/HTTPLoader'
name:
type: string
x-go-name: Name
@ -77,6 +81,8 @@ definitions:
properties:
file:
$ref: '#/definitions/FileLoader'
http:
$ref: '#/definitions/HTTPLoader'
matchers:
items:
type: string
@ -101,6 +107,9 @@ definitions:
ChainConfig:
properties:
hops:
description: |-
REMOVED since beta.6
Selector *SelectorConfig `yaml:",omitempty" json:"selector,omitempty"`
items:
$ref: '#/definitions/HopConfig'
type: array
@ -112,8 +121,6 @@ definitions:
name:
type: string
x-go-name: Name
selector:
$ref: '#/definitions/SelectorConfig'
type: object
x-go-package: github.com/go-gost/x/config
ChainGroupConfig:
@ -156,6 +163,11 @@ definitions:
$ref: '#/definitions/LimiterConfig'
type: array
x-go-name: CLimiters
hops:
items:
$ref: '#/definitions/HopConfig'
type: array
x-go-name: Hops
hosts:
items:
$ref: '#/definitions/HostsConfig'
@ -251,11 +263,32 @@ definitions:
x-go-name: Sep
type: object
x-go-package: github.com/go-gost/x/config
ForwardNodeConfig:
properties:
addr:
type: string
x-go-name: Addr
bypass:
type: string
x-go-name: Bypass
bypasses:
items:
type: string
type: array
x-go-name: Bypasses
name:
type: string
x-go-name: Name
type: object
x-go-package: github.com/go-gost/x/config
ForwarderConfig:
properties:
name:
type: string
x-go-name: Name
nodes:
items:
$ref: '#/definitions/NodeConfig'
$ref: '#/definitions/ForwardNodeConfig'
type: array
x-go-name: Nodes
selector:
@ -268,6 +301,15 @@ definitions:
x-go-name: Targets
type: object
x-go-package: github.com/go-gost/x/config
HTTPLoader:
properties:
timeout:
$ref: '#/definitions/Duration'
url:
type: string
x-go-name: URL
type: object
x-go-package: github.com/go-gost/x/config
HandlerConfig:
properties:
auth:
@ -352,6 +394,8 @@ definitions:
properties:
file:
$ref: '#/definitions/FileLoader'
http:
$ref: '#/definitions/HTTPLoader'
mappings:
items:
$ref: '#/definitions/HostMappingConfig'
@ -370,6 +414,8 @@ definitions:
properties:
file:
$ref: '#/definitions/FileLoader'
http:
$ref: '#/definitions/HTTPLoader'
limits:
items:
type: string
@ -1025,6 +1071,64 @@ paths:
summary: Update conn limiter by name, the limiter must already exist.
tags:
- Limiter
/config/hops:
post:
operationId: createHopRequest
parameters:
- in: body
name: data
schema:
$ref: '#/definitions/HopConfig'
x-go-name: Data
responses:
"200":
$ref: '#/responses/createHopResponse'
security:
- basicAuth:
- '[]'
summary: Create a new hop, the name of hop must be unique in hop list.
tags:
- Hop
/config/hops/{hop}:
delete:
operationId: deleteHopRequest
parameters:
- in: path
name: hop
required: true
type: string
x-go-name: Hop
responses:
"200":
$ref: '#/responses/deleteHopResponse'
security:
- basicAuth:
- '[]'
summary: Delete hop by name.
tags:
- Hop
put:
operationId: updateHopRequest
parameters:
- in: path
name: hop
required: true
type: string
x-go-name: Hop
- in: body
name: data
schema:
$ref: '#/definitions/HopConfig'
x-go-name: Data
responses:
"200":
$ref: '#/responses/updateHopResponse'
security:
- basicAuth:
- '[]'
summary: Update hop by name, the hop must already exist.
tags:
- Hop
/config/hosts:
post:
operationId: createHostsRequest
@ -1348,6 +1452,12 @@ responses:
Data: {}
schema:
$ref: '#/definitions/Response'
createHopResponse:
description: successful operation.
headers:
Data: {}
schema:
$ref: '#/definitions/Response'
createHostsResponse:
description: successful operation.
headers:
@ -1408,6 +1518,12 @@ responses:
Data: {}
schema:
$ref: '#/definitions/Response'
deleteHopResponse:
description: successful operation.
headers:
Data: {}
schema:
$ref: '#/definitions/Response'
deleteHostsResponse:
description: successful operation.
headers:
@ -1480,6 +1596,12 @@ responses:
Data: {}
schema:
$ref: '#/definitions/Response'
updateHopResponse:
description: successful operation.
headers:
Data: {}
schema:
$ref: '#/definitions/Response'
updateHostsResponse:
description: successful operation.
headers: