add auther api

This commit is contained in:
ginuerzh 2022-02-12 14:12:09 +08:00
parent 345338d410
commit fdd67a6086
10 changed files with 293 additions and 13 deletions

View File

@ -58,6 +58,10 @@ func Init(cfg *config.APIConfig) {
config.PUT("/chains/:chain", updateChain) config.PUT("/chains/:chain", updateChain)
config.DELETE("/chains/:chain", deleteChain) config.DELETE("/chains/:chain", deleteChain)
config.POST("/authers", createAuther)
config.PUT("/authers/:auther", updateAuther)
config.DELETE("/authers/:auther", deleteAuther)
config.POST("/bypasses", createBypass) config.POST("/bypasses", createBypass)
config.PUT("/bypasses/:bypass", updateBypass) config.PUT("/bypasses/:bypass", updateBypass)
config.DELETE("/bypasses/:bypass", deleteBypass) config.DELETE("/bypasses/:bypass", deleteBypass)

156
pkg/api/config_auther.go Normal file
View File

@ -0,0 +1,156 @@
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 createAutherRequest
type createAutherRequest struct {
// in: body
Data config.AutherConfig `json:"data"`
}
// successful operation.
// swagger:response createAutherResponse
type createAutherResponse struct {
Data Response
}
func createAuther(ctx *gin.Context) {
// swagger:route POST /config/authers ConfigManagement createAutherRequest
//
// create a new auther, the name of the auther must be unique in auther list.
//
// Responses:
// 200: createAutherResponse
var req createAutherRequest
ctx.ShouldBindJSON(&req.Data)
if req.Data.Name == "" {
writeError(ctx, ErrInvalid)
return
}
v := parsing.ParseAuther(&req.Data)
if err := registry.Auther().Register(req.Data.Name, v); err != nil {
writeError(ctx, ErrDup)
return
}
cfg := config.Global()
cfg.Authers = append(cfg.Authers, &req.Data)
config.SetGlobal(cfg)
ctx.JSON(http.StatusOK, Response{
Msg: "OK",
})
}
// swagger:parameters updateAutherRequest
type updateAutherRequest struct {
// in: path
// required: true
Auther string `uri:"auther" json:"auther"`
// in: body
Data config.AutherConfig `json:"data"`
}
// successful operation.
// swagger:response updateAutherResponse
type updateAutherResponse struct {
Data Response
}
func updateAuther(ctx *gin.Context) {
// swagger:route PUT /config/authers/{auther} ConfigManagement updateAutherRequest
//
// update auther by name, the auther must already exist.
//
// Responses:
// 200: updateAutherResponse
var req updateAutherRequest
ctx.ShouldBindUri(&req)
ctx.ShouldBindJSON(&req.Data)
if !registry.Auther().IsRegistered(req.Auther) {
writeError(ctx, ErrNotFound)
return
}
req.Data.Name = req.Auther
v := parsing.ParseAuther(&req.Data)
registry.Auther().Unregister(req.Auther)
if err := registry.Auther().Register(req.Auther, v); err != nil {
writeError(ctx, ErrDup)
return
}
cfg := config.Global()
for i := range cfg.Authers {
if cfg.Authers[i].Name == req.Auther {
cfg.Authers[i] = &req.Data
break
}
}
config.SetGlobal(cfg)
ctx.JSON(http.StatusOK, Response{
Msg: "OK",
})
}
// swagger:parameters deleteAutherRequest
type deleteAutherRequest struct {
// in: path
// required: true
Auther string `uri:"auther" json:"auther"`
}
// successful operation.
// swagger:response deleteAutherResponse
type deleteAutherResponse struct {
Data Response
}
func deleteAuther(ctx *gin.Context) {
// swagger:route DELETE /config/authers/{auther} ConfigManagement deleteAutherRequest
//
// delete auther by name.
//
// Responses:
// 200: deleteAutherResponse
var req deleteAutherRequest
ctx.ShouldBindUri(&req)
svc := registry.Auther().Get(req.Auther)
if svc == nil {
writeError(ctx, ErrNotFound)
return
}
registry.Auther().Unregister(req.Auther)
cfg := config.Global()
authers := cfg.Authers
cfg.Authers = nil
for _, s := range authers {
if s.Name == req.Auther {
continue
}
cfg.Authers = append(cfg.Authers, s)
}
config.SetGlobal(cfg)
ctx.JSON(http.StatusOK, Response{
Msg: "OK",
})
}

View File

@ -1,6 +1,6 @@
// Documentation of Web API. // Documentation of Web API.
// //
// Schemes: http // Schemes: https, http
// BasePath: / // BasePath: /
// Version: 1.0.0 // Version: 1.0.0
// //

View File

@ -25,6 +25,22 @@ definitions:
x-go-name: Username x-go-name: Username
type: object type: object
x-go-package: github.com/go-gost/gost/pkg/config x-go-package: github.com/go-gost/gost/pkg/config
AutherConfig:
properties:
auths:
items:
$ref: '#/definitions/AuthConfig'
type: array
x-go-name: Auths
name:
type: string
x-go-name: Name
type:
description: inline, file, redis, etc.
type: string
x-go-name: Type
type: object
x-go-package: github.com/go-gost/gost/pkg/config
BypassConfig: BypassConfig:
properties: properties:
matchers: matchers:
@ -38,6 +54,10 @@ definitions:
reverse: reverse:
type: boolean type: boolean
x-go-name: Reverse x-go-name: Reverse
type:
description: inline, file, etc.
type: string
x-go-name: Type
type: object type: object
x-go-package: github.com/go-gost/gost/pkg/config x-go-package: github.com/go-gost/gost/pkg/config
ChainConfig: ChainConfig:
@ -58,6 +78,11 @@ definitions:
properties: properties:
api: api:
$ref: '#/definitions/APIConfig' $ref: '#/definitions/APIConfig'
authers:
items:
$ref: '#/definitions/AutherConfig'
type: array
x-go-name: Authers
bypasses: bypasses:
items: items:
$ref: '#/definitions/BypassConfig' $ref: '#/definitions/BypassConfig'
@ -144,11 +169,11 @@ definitions:
x-go-package: github.com/go-gost/gost/pkg/config x-go-package: github.com/go-gost/gost/pkg/config
HandlerConfig: HandlerConfig:
properties: properties:
auths: auth:
items: $ref: '#/definitions/AuthConfig'
$ref: '#/definitions/AuthConfig' auther:
type: array type: string
x-go-name: Auths x-go-name: Auther
chain: chain:
type: string type: string
x-go-name: Chain x-go-name: Chain
@ -216,15 +241,19 @@ definitions:
name: name:
type: string type: string
x-go-name: Name x-go-name: Name
type:
description: inline, file, etc.
type: string
x-go-name: Type
type: object type: object
x-go-package: github.com/go-gost/gost/pkg/config x-go-package: github.com/go-gost/gost/pkg/config
ListenerConfig: ListenerConfig:
properties: properties:
auths: auth:
items: $ref: '#/definitions/AuthConfig'
$ref: '#/definitions/AuthConfig' auther:
type: array type: string
x-go-name: Auths x-go-name: Auther
chain: chain:
type: string type: string
x-go-name: Chain x-go-name: Chain
@ -319,6 +348,10 @@ definitions:
$ref: '#/definitions/NameserverConfig' $ref: '#/definitions/NameserverConfig'
type: array type: array
x-go-name: Nameservers x-go-name: Nameservers
type:
description: inline, file, etc.
type: string
x-go-name: Type
type: object type: object
x-go-package: github.com/go-gost/gost/pkg/config x-go-package: github.com/go-gost/gost/pkg/config
Response: Response:
@ -408,6 +441,56 @@ paths:
summary: Get current config. summary: Get current config.
tags: tags:
- ConfigManagement - ConfigManagement
/config/authers:
post:
operationId: createAutherRequest
parameters:
- in: body
name: data
schema:
$ref: '#/definitions/AutherConfig'
x-go-name: Data
responses:
"200":
$ref: '#/responses/createAutherResponse'
summary: create a new auther, the name of the auther must be unique in auther
list.
tags:
- ConfigManagement
/config/authers/{auther}:
delete:
operationId: deleteAutherRequest
parameters:
- in: path
name: auther
required: true
type: string
x-go-name: Auther
responses:
"200":
$ref: '#/responses/deleteAutherResponse'
summary: delete auther by name.
tags:
- ConfigManagement
put:
operationId: updateAutherRequest
parameters:
- in: path
name: auther
required: true
type: string
x-go-name: Auther
- in: body
name: data
schema:
$ref: '#/definitions/AutherConfig'
x-go-name: Data
responses:
"200":
$ref: '#/responses/updateAutherResponse'
summary: update auther by name, the auther must already exist.
tags:
- ConfigManagement
/config/bypasses: /config/bypasses:
post: post:
operationId: createBypassRequest operationId: createBypassRequest
@ -658,6 +741,12 @@ paths:
produces: produces:
- application/json - application/json
responses: responses:
createAutherResponse:
description: successful operation.
headers:
Data: {}
schema:
$ref: '#/definitions/Response'
createBypassResponse: createBypassResponse:
description: successful operation. description: successful operation.
headers: headers:
@ -688,6 +777,12 @@ responses:
Data: {} Data: {}
schema: schema:
$ref: '#/definitions/Response' $ref: '#/definitions/Response'
deleteAutherResponse:
description: successful operation.
headers:
Data: {}
schema:
$ref: '#/definitions/Response'
deleteBypassResponse: deleteBypassResponse:
description: successful operation. description: successful operation.
headers: headers:
@ -724,6 +819,12 @@ responses:
Config: {} Config: {}
schema: schema:
$ref: '#/definitions/Config' $ref: '#/definitions/Config'
updateAutherResponse:
description: successful operation.
headers:
Data: {}
schema:
$ref: '#/definitions/Response'
updateBypassResponse: updateBypassResponse:
description: successful operation. description: successful operation.
headers: headers:
@ -755,6 +856,7 @@ responses:
schema: schema:
$ref: '#/definitions/Response' $ref: '#/definitions/Response'
schemes: schemes:
- https
- http - http
securityDefinitions: securityDefinitions:
api_key: null api_key: null

View File

@ -18,8 +18,11 @@ type autherRegistry struct {
m sync.Map m sync.Map
} }
func (r *autherRegistry) Register(name string, auth auth.Authenticator) error { func (r *autherRegistry) Register(name string, auther auth.Authenticator) error {
if _, loaded := r.m.LoadOrStore(name, auth); loaded { if name == "" || auther == nil {
return nil
}
if _, loaded := r.m.LoadOrStore(name, auther); loaded {
return ErrDup return ErrDup
} }

View File

@ -19,6 +19,9 @@ type bypassRegistry struct {
} }
func (r *bypassRegistry) Register(name string, bypass bypass.Bypass) error { func (r *bypassRegistry) Register(name string, bypass bypass.Bypass) error {
if name == "" || bypass == nil {
return nil
}
if _, loaded := r.m.LoadOrStore(name, bypass); loaded { if _, loaded := r.m.LoadOrStore(name, bypass); loaded {
return ErrDup return ErrDup
} }

View File

@ -19,6 +19,9 @@ type chainRegistry struct {
} }
func (r *chainRegistry) Register(name string, chain chain.Chainer) error { func (r *chainRegistry) Register(name string, chain chain.Chainer) error {
if name == "" || chain == nil {
return nil
}
if _, loaded := r.m.LoadOrStore(name, chain); loaded { if _, loaded := r.m.LoadOrStore(name, chain); loaded {
return ErrDup return ErrDup
} }

View File

@ -20,6 +20,9 @@ type hostsRegistry struct {
} }
func (r *hostsRegistry) Register(name string, hosts hosts.HostMapper) error { func (r *hostsRegistry) Register(name string, hosts hosts.HostMapper) error {
if name == "" || hosts == nil {
return nil
}
if _, loaded := r.m.LoadOrStore(name, hosts); loaded { if _, loaded := r.m.LoadOrStore(name, hosts); loaded {
return ErrDup return ErrDup
} }

View File

@ -21,6 +21,9 @@ type resolverRegistry struct {
} }
func (r *resolverRegistry) Register(name string, resolver resolver.Resolver) error { func (r *resolverRegistry) Register(name string, resolver resolver.Resolver) error {
if name == "" || resolver == nil {
return nil
}
if _, loaded := r.m.LoadOrStore(name, resolver); loaded { if _, loaded := r.m.LoadOrStore(name, resolver); loaded {
return ErrDup return ErrDup
} }

View File

@ -19,6 +19,9 @@ type serviceRegistry struct {
} }
func (r *serviceRegistry) Register(name string, svc *service.Service) error { func (r *serviceRegistry) Register(name string, svc *service.Service) error {
if name == "" || svc == nil {
return nil
}
if _, loaded := r.m.LoadOrStore(name, svc); loaded { if _, loaded := r.m.LoadOrStore(name, svc); loaded {
return ErrDup return ErrDup
} }