add basic auth for config webapi

This commit is contained in:
ginuerzh 2022-02-14 22:50:06 +08:00
parent edca3e0a55
commit 5daefb8e3c
11 changed files with 263 additions and 40 deletions

View File

@ -2,7 +2,9 @@ package api
import ( import (
"bytes" "bytes"
"fmt"
"net/http" "net/http"
"os"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-gost/gost/pkg/config" "github.com/go-gost/gost/pkg/config"
@ -26,6 +28,9 @@ func getConfig(ctx *gin.Context) {
// //
// Get current config. // Get current config.
// //
// Security:
// basicAuth: []
//
// Responses: // Responses:
// 200: getConfigResponse // 200: getConfigResponse
@ -51,3 +56,63 @@ func getConfig(ctx *gin.Context) {
ctx.Data(http.StatusOK, contentType, buf.Bytes()) ctx.Data(http.StatusOK, contentType, buf.Bytes())
} }
// swagger:parameters saveConfigRequest
type saveConfigRequest struct {
// output format, one of yaml|json, default is yaml.
// in: query
Format string `form:"format" json:"format"`
}
// successful operation.
// swagger:response saveConfigResponse
type saveConfigResponse struct {
Data Response
}
func saveConfig(ctx *gin.Context) {
// swagger:route POST /config ConfigManagement saveConfigRequest
//
// Save current config to file (gost.yaml or gost.json).
//
// Security:
// basicAuth: []
//
// Responses:
// 200: saveConfigResponse
var req saveConfigRequest
ctx.ShouldBindQuery(&req)
file := "gost.yaml"
switch req.Format {
case "json":
file = "gost.json"
default:
req.Format = "yaml"
}
f, err := os.Create(file)
if err != nil {
writeError(ctx, &Error{
statusCode: http.StatusInternalServerError,
Code: 40005,
Msg: fmt.Sprintf("create file: %s", err.Error()),
})
return
}
defer f.Close()
if err := config.Global().Write(f, req.Format); err != nil {
writeError(ctx, &Error{
statusCode: http.StatusInternalServerError,
Code: 40006,
Msg: fmt.Sprintf("write: %s", err.Error()),
})
return
}
ctx.JSON(http.StatusOK, Response{
Msg: "OK",
})
}

View File

@ -24,7 +24,10 @@ type createAutherResponse struct {
func createAuther(ctx *gin.Context) { func createAuther(ctx *gin.Context) {
// swagger:route POST /config/authers ConfigManagement createAutherRequest // swagger:route POST /config/authers ConfigManagement createAutherRequest
// //
// create a new auther, the name of the auther must be unique in auther list. // Create a new auther, the name of the auther must be unique in auther list.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: createAutherResponse // 200: createAutherResponse
@ -70,7 +73,10 @@ type updateAutherResponse struct {
func updateAuther(ctx *gin.Context) { func updateAuther(ctx *gin.Context) {
// swagger:route PUT /config/authers/{auther} ConfigManagement updateAutherRequest // swagger:route PUT /config/authers/{auther} ConfigManagement updateAutherRequest
// //
// update auther by name, the auther must already exist. // Update auther by name, the auther must already exist.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: updateAutherResponse // 200: updateAutherResponse
@ -124,7 +130,10 @@ type deleteAutherResponse struct {
func deleteAuther(ctx *gin.Context) { func deleteAuther(ctx *gin.Context) {
// swagger:route DELETE /config/authers/{auther} ConfigManagement deleteAutherRequest // swagger:route DELETE /config/authers/{auther} ConfigManagement deleteAutherRequest
// //
// delete auther by name. // Delete auther by name.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: deleteAutherResponse // 200: deleteAutherResponse

View File

@ -24,7 +24,10 @@ type createBypassResponse struct {
func createBypass(ctx *gin.Context) { func createBypass(ctx *gin.Context) {
// swagger:route POST /config/bypasses ConfigManagement createBypassRequest // swagger:route POST /config/bypasses ConfigManagement createBypassRequest
// //
// create a new bypass, the name of bypass must be unique in bypass list. // Create a new bypass, the name of bypass must be unique in bypass list.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: createBypassResponse // 200: createBypassResponse
@ -71,7 +74,10 @@ type updateBypassResponse struct {
func updateBypass(ctx *gin.Context) { func updateBypass(ctx *gin.Context) {
// swagger:route PUT /config/bypasses/{bypass} ConfigManagement updateBypassRequest // swagger:route PUT /config/bypasses/{bypass} ConfigManagement updateBypassRequest
// //
// update bypass by name, the bypass must already exist. // Update bypass by name, the bypass must already exist.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: updateBypassResponse // 200: updateBypassResponse
@ -126,7 +132,10 @@ type deleteBypassResponse struct {
func deleteBypass(ctx *gin.Context) { func deleteBypass(ctx *gin.Context) {
// swagger:route DELETE /config/bypasses/{bypass} ConfigManagement deleteBypassRequest // swagger:route DELETE /config/bypasses/{bypass} ConfigManagement deleteBypassRequest
// //
// delete bypass by name. // Delete bypass by name.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: deleteBypassResponse // 200: deleteBypassResponse

View File

@ -24,7 +24,10 @@ type createChainResponse struct {
func createChain(ctx *gin.Context) { func createChain(ctx *gin.Context) {
// swagger:route POST /config/chains ConfigManagement createChainRequest // swagger:route POST /config/chains ConfigManagement createChainRequest
// //
// create a new chain, the name of chain must be unique in chain list. // Create a new chain, the name of chain must be unique in chain list.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: createChainResponse // 200: createChainResponse
@ -76,7 +79,10 @@ type updateChainResponse struct {
func updateChain(ctx *gin.Context) { func updateChain(ctx *gin.Context) {
// swagger:route PUT /config/chains/{chain} ConfigManagement updateChainRequest // swagger:route PUT /config/chains/{chain} ConfigManagement updateChainRequest
// //
// update chain by name, the chain must already exist. // Update chain by name, the chain must already exist.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: updateChainResponse // 200: updateChainResponse
@ -135,7 +141,10 @@ type deleteChainResponse struct {
func deleteChain(ctx *gin.Context) { func deleteChain(ctx *gin.Context) {
// swagger:route DELETE /config/chains/{chain} ConfigManagement deleteChainRequest // swagger:route DELETE /config/chains/{chain} ConfigManagement deleteChainRequest
// //
// delete chain by name. // Delete chain by name.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: deleteChainResponse // 200: deleteChainResponse

View File

@ -24,7 +24,10 @@ type createHostsesponse struct {
func createHosts(ctx *gin.Context) { func createHosts(ctx *gin.Context) {
// swagger:route POST /config/hosts ConfigManagement createHostsRequest // swagger:route POST /config/hosts ConfigManagement createHostsRequest
// //
// create a new hosts, the name of the hosts must be unique in hosts list. // Create a new hosts, the name of the hosts must be unique in hosts list.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: createHostsResponse // 200: createHostsResponse
@ -71,7 +74,10 @@ type updateHostsResponse struct {
func updateHosts(ctx *gin.Context) { func updateHosts(ctx *gin.Context) {
// swagger:route PUT /config/hosts/{hosts} ConfigManagement updateHostsRequest // swagger:route PUT /config/hosts/{hosts} ConfigManagement updateHostsRequest
// //
// update hosts by name, the hosts must already exist. // Update hosts by name, the hosts must already exist.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: updateHostsResponse // 200: updateHostsResponse
@ -126,7 +132,10 @@ type deleteHostsResponse struct {
func deleteHosts(ctx *gin.Context) { func deleteHosts(ctx *gin.Context) {
// swagger:route DELETE /config/hosts/{hosts} ConfigManagement deleteHostsRequest // swagger:route DELETE /config/hosts/{hosts} ConfigManagement deleteHostsRequest
// //
// delete hosts by name. // Delete hosts by name.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: deleteHostsResponse // 200: deleteHostsResponse

View File

@ -24,7 +24,10 @@ type createResolverResponse struct {
func createResolver(ctx *gin.Context) { func createResolver(ctx *gin.Context) {
// swagger:route POST /config/resolvers ConfigManagement createResolverRequest // swagger:route POST /config/resolvers ConfigManagement createResolverRequest
// //
// create a new resolver, the name of the resolver must be unique in resolver list. // Create a new resolver, the name of the resolver must be unique in resolver list.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: createResolverResponse // 200: createResolverResponse
@ -75,7 +78,10 @@ type updateResolverResponse struct {
func updateResolver(ctx *gin.Context) { func updateResolver(ctx *gin.Context) {
// swagger:route PUT /config/resolvers/{resolver} ConfigManagement updateResolverRequest // swagger:route PUT /config/resolvers/{resolver} ConfigManagement updateResolverRequest
// //
// update resolver by name, the resolver must already exist. // Update resolver by name, the resolver must already exist.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: updateResolverResponse // 200: updateResolverResponse
@ -134,7 +140,10 @@ type deleteResolverResponse struct {
func deleteResolver(ctx *gin.Context) { func deleteResolver(ctx *gin.Context) {
// swagger:route DELETE /config/resolvers/{resolver} ConfigManagement deleteResolverRequest // swagger:route DELETE /config/resolvers/{resolver} ConfigManagement deleteResolverRequest
// //
// delete resolver by name. // Delete resolver by name.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: deleteResolverResponse // 200: deleteResolverResponse

View File

@ -24,7 +24,10 @@ type createServiceResponse struct {
func createService(ctx *gin.Context) { func createService(ctx *gin.Context) {
// swagger:route POST /config/services ConfigManagement createServiceRequest // swagger:route POST /config/services ConfigManagement createServiceRequest
// //
// create a new service, the name of the service must be unique in service list. // Create a new service, the name of the service must be unique in service list.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: createServiceResponse // 200: createServiceResponse
@ -83,7 +86,10 @@ type updateServiceResponse struct {
func updateService(ctx *gin.Context) { func updateService(ctx *gin.Context) {
// swagger:route PUT /config/services/{service} ConfigManagement updateServiceRequest // swagger:route PUT /config/services/{service} ConfigManagement updateServiceRequest
// //
// update service by name, the service must already exist. // Update service by name, the service must already exist.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: updateServiceResponse // 200: updateServiceResponse
@ -147,7 +153,10 @@ type deleteServiceResponse struct {
func deleteService(ctx *gin.Context) { func deleteService(ctx *gin.Context) {
// swagger:route DELETE /config/services/{service} ConfigManagement deleteServiceRequest // swagger:route DELETE /config/services/{service} ConfigManagement deleteServiceRequest
// //
// delete service by name. // Delete service by name.
//
// Security:
// basicAuth: []
// //
// Responses: // Responses:
// 200: deleteServiceResponse // 200: deleteServiceResponse

View File

@ -11,7 +11,8 @@
// - application/json // - application/json
// //
// SecurityDefinitions: // SecurityDefinitions:
// api_key: // basicAuth:
// type: basic
// //
// swagger:meta // swagger:meta
package api package api

View File

@ -12,6 +12,7 @@ var (
ErrDup = &Error{statusCode: http.StatusBadRequest, Code: 40002, Msg: "instance duplicated"} ErrDup = &Error{statusCode: http.StatusBadRequest, Code: 40002, Msg: "instance duplicated"}
ErrCreate = &Error{statusCode: http.StatusConflict, Code: 40003, Msg: "instance creation failed"} ErrCreate = &Error{statusCode: http.StatusConflict, Code: 40003, Msg: "instance creation failed"}
ErrNotFound = &Error{statusCode: http.StatusBadRequest, Code: 40004, Msg: "instance not found"} ErrNotFound = &Error{statusCode: http.StatusBadRequest, Code: 40004, Msg: "instance not found"}
ErrSave = &Error{statusCode: http.StatusInternalServerError, Code: 40005, Msg: "save config failed"}
) )
// Error is an api error. // Error is an api error.

View File

@ -99,6 +99,7 @@ func (s *Server) Close() error {
func registerConfig(config *gin.RouterGroup) { func registerConfig(config *gin.RouterGroup) {
config.GET("", getConfig) config.GET("", getConfig)
config.POST("", saveConfig)
config.POST("/services", createService) config.POST("/services", createService)
config.PUT("/services/:service", updateService) config.PUT("/services/:service", updateService)

View File

@ -10,6 +10,11 @@ definitions:
addr: addr:
type: string type: string
x-go-name: Addr x-go-name: Addr
auth:
$ref: '#/definitions/AuthConfig'
auther:
type: string
x-go-name: Auther
pathPrefix: pathPrefix:
type: string type: string
x-go-name: PathPrefix x-go-name: PathPrefix
@ -100,6 +105,8 @@ definitions:
x-go-name: Hosts x-go-name: Hosts
log: log:
$ref: '#/definitions/LogConfig' $ref: '#/definitions/LogConfig'
metrics:
$ref: '#/definitions/MetricsConfig'
profiling: profiling:
$ref: '#/definitions/ProfilingConfig' $ref: '#/definitions/ProfilingConfig'
resolvers: resolvers:
@ -282,6 +289,19 @@ definitions:
x-go-name: Output x-go-name: Output
type: object type: object
x-go-package: github.com/go-gost/gost/pkg/config x-go-package: github.com/go-gost/gost/pkg/config
MetricsConfig:
properties:
addr:
type: string
x-go-name: Addr
enable:
type: boolean
x-go-name: Enable
path:
type: string
x-go-name: Path
type: object
x-go-package: github.com/go-gost/gost/pkg/config
NameserverConfig: NameserverConfig:
properties: properties:
addr: addr:
@ -333,9 +353,9 @@ definitions:
addr: addr:
type: string type: string
x-go-name: Addr x-go-name: Addr
enabled: enable:
type: boolean type: boolean
x-go-name: Enabled x-go-name: Enable
type: object type: object
x-go-package: github.com/go-gost/gost/pkg/config x-go-package: github.com/go-gost/gost/pkg/config
ResolverConfig: ResolverConfig:
@ -438,9 +458,29 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/getConfigResponse' $ref: '#/responses/getConfigResponse'
security:
- basicAuth:
- '[]'
summary: Get current config. summary: Get current config.
tags: tags:
- ConfigManagement - ConfigManagement
post:
operationId: saveConfigRequest
parameters:
- description: output format, one of yaml|json, default is yaml.
in: query
name: format
type: string
x-go-name: Format
responses:
"200":
$ref: '#/responses/saveConfigResponse'
security:
- basicAuth:
- '[]'
summary: Save current config to file (gost.yaml or gost.json).
tags:
- ConfigManagement
/config/authers: /config/authers:
post: post:
operationId: createAutherRequest operationId: createAutherRequest
@ -453,7 +493,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/createAutherResponse' $ref: '#/responses/createAutherResponse'
summary: create a new auther, the name of the auther must be unique in auther security:
- basicAuth:
- '[]'
summary: Create a new auther, the name of the auther must be unique in auther
list. list.
tags: tags:
- ConfigManagement - ConfigManagement
@ -469,7 +512,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/deleteAutherResponse' $ref: '#/responses/deleteAutherResponse'
summary: delete auther by name. security:
- basicAuth:
- '[]'
summary: Delete auther by name.
tags: tags:
- ConfigManagement - ConfigManagement
put: put:
@ -488,7 +534,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/updateAutherResponse' $ref: '#/responses/updateAutherResponse'
summary: update auther by name, the auther must already exist. security:
- basicAuth:
- '[]'
summary: Update auther by name, the auther must already exist.
tags: tags:
- ConfigManagement - ConfigManagement
/config/bypasses: /config/bypasses:
@ -503,7 +552,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/createBypassResponse' $ref: '#/responses/createBypassResponse'
summary: create a new bypass, the name of bypass must be unique in bypass list. security:
- basicAuth:
- '[]'
summary: Create a new bypass, the name of bypass must be unique in bypass list.
tags: tags:
- ConfigManagement - ConfigManagement
/config/bypasses/{bypass}: /config/bypasses/{bypass}:
@ -518,7 +570,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/deleteBypassResponse' $ref: '#/responses/deleteBypassResponse'
summary: delete bypass by name. security:
- basicAuth:
- '[]'
summary: Delete bypass by name.
tags: tags:
- ConfigManagement - ConfigManagement
put: put:
@ -537,7 +592,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/updateBypassResponse' $ref: '#/responses/updateBypassResponse'
summary: update bypass by name, the bypass must already exist. security:
- basicAuth:
- '[]'
summary: Update bypass by name, the bypass must already exist.
tags: tags:
- ConfigManagement - ConfigManagement
/config/chains: /config/chains:
@ -552,7 +610,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/createChainResponse' $ref: '#/responses/createChainResponse'
summary: create a new chain, the name of chain must be unique in chain list. security:
- basicAuth:
- '[]'
summary: Create a new chain, the name of chain must be unique in chain list.
tags: tags:
- ConfigManagement - ConfigManagement
/config/chains/{chain}: /config/chains/{chain}:
@ -567,7 +628,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/deleteChainResponse' $ref: '#/responses/deleteChainResponse'
summary: delete chain by name. security:
- basicAuth:
- '[]'
summary: Delete chain by name.
tags: tags:
- ConfigManagement - ConfigManagement
put: put:
@ -586,7 +650,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/updateChainResponse' $ref: '#/responses/updateChainResponse'
summary: update chain by name, the chain must already exist. security:
- basicAuth:
- '[]'
summary: Update chain by name, the chain must already exist.
tags: tags:
- ConfigManagement - ConfigManagement
/config/hosts: /config/hosts:
@ -601,7 +668,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/createHostsResponse' $ref: '#/responses/createHostsResponse'
summary: create a new hosts, the name of the hosts must be unique in hosts list. security:
- basicAuth:
- '[]'
summary: Create a new hosts, the name of the hosts must be unique in hosts list.
tags: tags:
- ConfigManagement - ConfigManagement
/config/hosts/{hosts}: /config/hosts/{hosts}:
@ -616,7 +686,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/deleteHostsResponse' $ref: '#/responses/deleteHostsResponse'
summary: delete hosts by name. security:
- basicAuth:
- '[]'
summary: Delete hosts by name.
tags: tags:
- ConfigManagement - ConfigManagement
put: put:
@ -635,7 +708,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/updateHostsResponse' $ref: '#/responses/updateHostsResponse'
summary: update hosts by name, the hosts must already exist. security:
- basicAuth:
- '[]'
summary: Update hosts by name, the hosts must already exist.
tags: tags:
- ConfigManagement - ConfigManagement
/config/resolvers: /config/resolvers:
@ -650,7 +726,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/createResolverResponse' $ref: '#/responses/createResolverResponse'
summary: create a new resolver, the name of the resolver must be unique in resolver security:
- basicAuth:
- '[]'
summary: Create a new resolver, the name of the resolver must be unique in resolver
list. list.
tags: tags:
- ConfigManagement - ConfigManagement
@ -666,7 +745,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/deleteResolverResponse' $ref: '#/responses/deleteResolverResponse'
summary: delete resolver by name. security:
- basicAuth:
- '[]'
summary: Delete resolver by name.
tags: tags:
- ConfigManagement - ConfigManagement
put: put:
@ -685,7 +767,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/updateResolverResponse' $ref: '#/responses/updateResolverResponse'
summary: update resolver by name, the resolver must already exist. security:
- basicAuth:
- '[]'
summary: Update resolver by name, the resolver must already exist.
tags: tags:
- ConfigManagement - ConfigManagement
/config/services: /config/services:
@ -700,7 +785,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/createServiceResponse' $ref: '#/responses/createServiceResponse'
summary: create a new service, the name of the service must be unique in service security:
- basicAuth:
- '[]'
summary: Create a new service, the name of the service must be unique in service
list. list.
tags: tags:
- ConfigManagement - ConfigManagement
@ -716,7 +804,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/deleteServiceResponse' $ref: '#/responses/deleteServiceResponse'
summary: delete service by name. security:
- basicAuth:
- '[]'
summary: Delete service by name.
tags: tags:
- ConfigManagement - ConfigManagement
put: put:
@ -735,7 +826,10 @@ paths:
responses: responses:
"200": "200":
$ref: '#/responses/updateServiceResponse' $ref: '#/responses/updateServiceResponse'
summary: update service by name, the service must already exist. security:
- basicAuth:
- '[]'
summary: Update service by name, the service must already exist.
tags: tags:
- ConfigManagement - ConfigManagement
produces: produces:
@ -819,6 +913,12 @@ responses:
Config: {} Config: {}
schema: schema:
$ref: '#/definitions/Config' $ref: '#/definitions/Config'
saveConfigResponse:
description: successful operation.
headers:
Data: {}
schema:
$ref: '#/definitions/Response'
updateAutherResponse: updateAutherResponse:
description: successful operation. description: successful operation.
headers: headers:
@ -859,5 +959,6 @@ schemes:
- https - https
- http - http
securityDefinitions: securityDefinitions:
api_key: null basicAuth:
type: basic
swagger: "2.0" swagger: "2.0"