add admission
This commit is contained in:
166
pkg/api/config_admission.go
Normal file
166
pkg/api/config_admission.go
Normal file
@ -0,0 +1,166 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-gost/gost/pkg/config"
|
||||
"github.com/go-gost/gost/pkg/config/parsing"
|
||||
"github.com/go-gost/gost/pkg/registry"
|
||||
)
|
||||
|
||||
// swagger:parameters createAdmissionRequest
|
||||
type createAdmissionRequest struct {
|
||||
// in: body
|
||||
Data config.AdmissionConfig `json:"data"`
|
||||
}
|
||||
|
||||
// successful operation.
|
||||
// swagger:response createAdmissionResponse
|
||||
type createAdmissionResponse struct {
|
||||
Data Response
|
||||
}
|
||||
|
||||
func createAdmission(ctx *gin.Context) {
|
||||
// swagger:route POST /config/admissions ConfigManagement createAdmissionRequest
|
||||
//
|
||||
// Create a new admission, the name of admission must be unique in admission list.
|
||||
//
|
||||
// Security:
|
||||
// basicAuth: []
|
||||
//
|
||||
// Responses:
|
||||
// 200: createAdmissionResponse
|
||||
|
||||
var req createAdmissionRequest
|
||||
ctx.ShouldBindJSON(&req.Data)
|
||||
|
||||
if req.Data.Name == "" {
|
||||
writeError(ctx, ErrInvalid)
|
||||
return
|
||||
}
|
||||
|
||||
v := parsing.ParseAdmission(&req.Data)
|
||||
|
||||
if err := registry.Admission().Register(req.Data.Name, v); err != nil {
|
||||
writeError(ctx, ErrDup)
|
||||
return
|
||||
}
|
||||
|
||||
cfg := config.Global()
|
||||
cfg.Admissions = append(cfg.Admissions, &req.Data)
|
||||
config.SetGlobal(cfg)
|
||||
|
||||
ctx.JSON(http.StatusOK, Response{
|
||||
Msg: "OK",
|
||||
})
|
||||
}
|
||||
|
||||
// swagger:parameters updateAdmissionRequest
|
||||
type updateAdmissionRequest struct {
|
||||
// in: path
|
||||
// required: true
|
||||
Admission string `uri:"admission" json:"admission"`
|
||||
// in: body
|
||||
Data config.AdmissionConfig `json:"data"`
|
||||
}
|
||||
|
||||
// successful operation.
|
||||
// swagger:response updateAdmissionResponse
|
||||
type updateAdmissionResponse struct {
|
||||
Data Response
|
||||
}
|
||||
|
||||
func updateAdmission(ctx *gin.Context) {
|
||||
// swagger:route PUT /config/admissions/{admission} ConfigManagement updateAdmissionRequest
|
||||
//
|
||||
// Update admission by name, the admission must already exist.
|
||||
//
|
||||
// Security:
|
||||
// basicAuth: []
|
||||
//
|
||||
// Responses:
|
||||
// 200: updateAdmissionResponse
|
||||
|
||||
var req updateAdmissionRequest
|
||||
ctx.ShouldBindUri(&req)
|
||||
ctx.ShouldBindJSON(&req.Data)
|
||||
|
||||
if !registry.Admission().IsRegistered(req.Admission) {
|
||||
writeError(ctx, ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
req.Data.Name = req.Admission
|
||||
|
||||
v := parsing.ParseAdmission(&req.Data)
|
||||
|
||||
registry.Admission().Unregister(req.Admission)
|
||||
|
||||
if err := registry.Admission().Register(req.Admission, v); err != nil {
|
||||
writeError(ctx, ErrDup)
|
||||
return
|
||||
}
|
||||
|
||||
cfg := config.Global()
|
||||
for i := range cfg.Admissions {
|
||||
if cfg.Admissions[i].Name == req.Admission {
|
||||
cfg.Admissions[i] = &req.Data
|
||||
break
|
||||
}
|
||||
}
|
||||
config.SetGlobal(cfg)
|
||||
|
||||
ctx.JSON(http.StatusOK, Response{
|
||||
Msg: "OK",
|
||||
})
|
||||
}
|
||||
|
||||
// swagger:parameters deleteAdmissionRequest
|
||||
type deleteAdmissionRequest struct {
|
||||
// in: path
|
||||
// required: true
|
||||
Admission string `uri:"admission" json:"admission"`
|
||||
}
|
||||
|
||||
// successful operation.
|
||||
// swagger:response deleteAdmissionResponse
|
||||
type deleteAdmissionResponse struct {
|
||||
Data Response
|
||||
}
|
||||
|
||||
func deleteAdmission(ctx *gin.Context) {
|
||||
// swagger:route DELETE /config/admissions/{admission} ConfigManagement deleteAdmissionRequest
|
||||
//
|
||||
// Delete admission by name.
|
||||
//
|
||||
// Security:
|
||||
// basicAuth: []
|
||||
//
|
||||
// Responses:
|
||||
// 200: deleteAdmissionResponse
|
||||
|
||||
var req deleteAdmissionRequest
|
||||
ctx.ShouldBindUri(&req)
|
||||
|
||||
if !registry.Admission().IsRegistered(req.Admission) {
|
||||
writeError(ctx, ErrNotFound)
|
||||
return
|
||||
}
|
||||
registry.Admission().Unregister(req.Admission)
|
||||
|
||||
cfg := config.Global()
|
||||
admissiones := cfg.Admissions
|
||||
cfg.Admissions = nil
|
||||
for _, s := range admissiones {
|
||||
if s.Name == req.Admission {
|
||||
continue
|
||||
}
|
||||
cfg.Admissions = append(cfg.Admissions, s)
|
||||
}
|
||||
config.SetGlobal(cfg)
|
||||
|
||||
ctx.JSON(http.StatusOK, Response{
|
||||
Msg: "OK",
|
||||
})
|
||||
}
|
@ -141,8 +141,7 @@ func deleteAuther(ctx *gin.Context) {
|
||||
var req deleteAutherRequest
|
||||
ctx.ShouldBindUri(&req)
|
||||
|
||||
svc := registry.Auther().Get(req.Auther)
|
||||
if svc == nil {
|
||||
if !registry.Auther().IsRegistered(req.Auther) {
|
||||
writeError(ctx, ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
@ -143,8 +143,7 @@ func deleteBypass(ctx *gin.Context) {
|
||||
var req deleteBypassRequest
|
||||
ctx.ShouldBindUri(&req)
|
||||
|
||||
svc := registry.Bypass().Get(req.Bypass)
|
||||
if svc == nil {
|
||||
if !registry.Bypass().IsRegistered(req.Bypass) {
|
||||
writeError(ctx, ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
@ -152,8 +152,7 @@ func deleteChain(ctx *gin.Context) {
|
||||
var req deleteChainRequest
|
||||
ctx.ShouldBindUri(&req)
|
||||
|
||||
svc := registry.Chain().Get(req.Chain)
|
||||
if svc == nil {
|
||||
if !registry.Chain().IsRegistered(req.Chain) {
|
||||
writeError(ctx, ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
@ -143,8 +143,7 @@ func deleteHosts(ctx *gin.Context) {
|
||||
var req deleteHostsRequest
|
||||
ctx.ShouldBindUri(&req)
|
||||
|
||||
svc := registry.Hosts().Get(req.Hosts)
|
||||
if svc == nil {
|
||||
if !registry.Hosts().IsRegistered(req.Hosts) {
|
||||
writeError(ctx, ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
@ -151,8 +151,7 @@ func deleteResolver(ctx *gin.Context) {
|
||||
var req deleteResolverRequest
|
||||
ctx.ShouldBindUri(&req)
|
||||
|
||||
svc := registry.Resolver().Get(req.Resolver)
|
||||
if svc == nil {
|
||||
if !registry.Resolver().IsRegistered(req.Resolver) {
|
||||
writeError(ctx, ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalid = &Error{statusCode: http.StatusBadRequest, Code: 40001, Msg: "instance invalid"}
|
||||
ErrDup = &Error{statusCode: http.StatusBadRequest, Code: 40002, Msg: "instance duplicated"}
|
||||
ErrCreate = &Error{statusCode: http.StatusConflict, Code: 40003, Msg: "instance creation failed"}
|
||||
ErrNotFound = &Error{statusCode: http.StatusBadRequest, Code: 40004, Msg: "instance not found"}
|
||||
ErrInvalid = &Error{statusCode: http.StatusBadRequest, Code: 40001, Msg: "object invalid"}
|
||||
ErrDup = &Error{statusCode: http.StatusBadRequest, Code: 40002, Msg: "object duplicated"}
|
||||
ErrCreate = &Error{statusCode: http.StatusConflict, Code: 40003, Msg: "object creation failed"}
|
||||
ErrNotFound = &Error{statusCode: http.StatusBadRequest, Code: 40004, Msg: "object not found"}
|
||||
ErrSave = &Error{statusCode: http.StatusInternalServerError, Code: 40005, Msg: "save config failed"}
|
||||
)
|
||||
|
||||
|
@ -113,6 +113,10 @@ func registerConfig(config *gin.RouterGroup) {
|
||||
config.PUT("/authers/:auther", updateAuther)
|
||||
config.DELETE("/authers/:auther", deleteAuther)
|
||||
|
||||
config.POST("/admissions", createAdmission)
|
||||
config.PUT("/admissions/:admission", updateAdmission)
|
||||
config.DELETE("/admissions/:admission", deleteAdmission)
|
||||
|
||||
config.POST("/bypasses", createBypass)
|
||||
config.PUT("/bypasses/:bypass", updateBypass)
|
||||
config.DELETE("/bypasses/:bypass", deleteBypass)
|
||||
|
@ -20,6 +20,25 @@ definitions:
|
||||
x-go-name: PathPrefix
|
||||
type: object
|
||||
x-go-package: github.com/go-gost/gost/pkg/config
|
||||
AdmissionConfig:
|
||||
properties:
|
||||
matchers:
|
||||
items:
|
||||
type: string
|
||||
type: array
|
||||
x-go-name: Matchers
|
||||
name:
|
||||
type: string
|
||||
x-go-name: Name
|
||||
reverse:
|
||||
type: boolean
|
||||
x-go-name: Reverse
|
||||
type:
|
||||
description: inline, file, etc.
|
||||
type: string
|
||||
x-go-name: Type
|
||||
type: object
|
||||
x-go-package: github.com/go-gost/gost/pkg/config
|
||||
AuthConfig:
|
||||
properties:
|
||||
password:
|
||||
@ -81,6 +100,11 @@ definitions:
|
||||
x-go-package: github.com/go-gost/gost/pkg/config
|
||||
Config:
|
||||
properties:
|
||||
admissions:
|
||||
items:
|
||||
$ref: '#/definitions/AdmissionConfig'
|
||||
type: array
|
||||
x-go-name: Admissions
|
||||
api:
|
||||
$ref: '#/definitions/APIConfig'
|
||||
authers:
|
||||
@ -403,6 +427,9 @@ definitions:
|
||||
addr:
|
||||
type: string
|
||||
x-go-name: Addr
|
||||
admission:
|
||||
type: string
|
||||
x-go-name: Admission
|
||||
bypass:
|
||||
type: string
|
||||
x-go-name: Bypass
|
||||
@ -481,6 +508,65 @@ paths:
|
||||
summary: Save current config to file (gost.yaml or gost.json).
|
||||
tags:
|
||||
- ConfigManagement
|
||||
/config/admissions:
|
||||
post:
|
||||
operationId: createAdmissionRequest
|
||||
parameters:
|
||||
- in: body
|
||||
name: data
|
||||
schema:
|
||||
$ref: '#/definitions/AdmissionConfig'
|
||||
x-go-name: Data
|
||||
responses:
|
||||
"200":
|
||||
$ref: '#/responses/createAdmissionResponse'
|
||||
security:
|
||||
- basicAuth:
|
||||
- '[]'
|
||||
summary: Create a new admission, the name of admission must be unique in admission
|
||||
list.
|
||||
tags:
|
||||
- ConfigManagement
|
||||
/config/admissions/{admission}:
|
||||
delete:
|
||||
operationId: deleteAdmissionRequest
|
||||
parameters:
|
||||
- in: path
|
||||
name: admission
|
||||
required: true
|
||||
type: string
|
||||
x-go-name: Admission
|
||||
responses:
|
||||
"200":
|
||||
$ref: '#/responses/deleteAdmissionResponse'
|
||||
security:
|
||||
- basicAuth:
|
||||
- '[]'
|
||||
summary: Delete admission by name.
|
||||
tags:
|
||||
- ConfigManagement
|
||||
put:
|
||||
operationId: updateAdmissionRequest
|
||||
parameters:
|
||||
- in: path
|
||||
name: admission
|
||||
required: true
|
||||
type: string
|
||||
x-go-name: Admission
|
||||
- in: body
|
||||
name: data
|
||||
schema:
|
||||
$ref: '#/definitions/AdmissionConfig'
|
||||
x-go-name: Data
|
||||
responses:
|
||||
"200":
|
||||
$ref: '#/responses/updateAdmissionResponse'
|
||||
security:
|
||||
- basicAuth:
|
||||
- '[]'
|
||||
summary: Update admission by name, the admission must already exist.
|
||||
tags:
|
||||
- ConfigManagement
|
||||
/config/authers:
|
||||
post:
|
||||
operationId: createAutherRequest
|
||||
@ -835,6 +921,12 @@ paths:
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
createAdmissionResponse:
|
||||
description: successful operation.
|
||||
headers:
|
||||
Data: {}
|
||||
schema:
|
||||
$ref: '#/definitions/Response'
|
||||
createAutherResponse:
|
||||
description: successful operation.
|
||||
headers:
|
||||
@ -871,6 +963,12 @@ responses:
|
||||
Data: {}
|
||||
schema:
|
||||
$ref: '#/definitions/Response'
|
||||
deleteAdmissionResponse:
|
||||
description: successful operation.
|
||||
headers:
|
||||
Data: {}
|
||||
schema:
|
||||
$ref: '#/definitions/Response'
|
||||
deleteAutherResponse:
|
||||
description: successful operation.
|
||||
headers:
|
||||
@ -919,6 +1017,12 @@ responses:
|
||||
Data: {}
|
||||
schema:
|
||||
$ref: '#/definitions/Response'
|
||||
updateAdmissionResponse:
|
||||
description: successful operation.
|
||||
headers:
|
||||
Data: {}
|
||||
schema:
|
||||
$ref: '#/definitions/Response'
|
||||
updateAutherResponse:
|
||||
description: successful operation.
|
||||
headers:
|
||||
|
Reference in New Issue
Block a user