Skip to content

Commit

Permalink
add license and readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
whwlsfb committed Sep 24, 2021
1 parent 1a1650d commit 0286117
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 20 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Kacper Szurek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SwaggerHelper

SwaggerHelper是用于启动本地保存的api-docs.json文档,例如在对系统进行**二次**渗透测试时,若目标关闭了Swagger-UI,则可使用本工具在本地启动接口文档(前提是api-docs文档已离线保存在本地),直接调用目标接口。

# 编译

> 编译该工具需要go 1.16或更高版本
`$ go build`

# 使用方法

```
usage: SwaggerHelper [-h|--help] [-L|--listen "<value>"] -F|--apifile "<value>"
[-S|--serverroot "<value>"]
Arguments:
-h --help Print help information
-L --listen bind address.. Default: 127.0.0.1:1323
-F --apifile swagger-ui api-docs file path.
-S --serverroot server override.. Default:
```

若不需要覆盖api-docs内部的host和bashPath,直接执行如下命令:

`SwaggerHelper -F /to/path/api-docs.json`

若因为CORS限制或服务器地址更改需要另外指定API根路径的,执行如下命令:

`SwaggerHelper -F /to/path/api-docs.json -S http://1.2.3.4/api`

57 changes: 37 additions & 20 deletions swaggerhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,59 @@ func getSwaggerUIFiles() http.FileSystem {
return http.FS(fsys)
}

const (
banner = `
╔══╗─────────────╔╗╔╗
║══╬╦╦╦═╗╔═╦═╦═╦╦╣╚╝╠═╦╗╔═╦═╦╦╗
╠══║║║║╬╚╣╬║╬║╩╣╔╣╔╗║╩╣╚╣╬║╩╣╔╝
╚══╩══╩══╬╗╠╗╠═╩╝╚╝╚╩═╩═╣╔╩═╩╝
─────────╚═╩═╝──────────╚╝
`
)

func main() {
parser := argparse.NewParser("SwaggerHelper", "")
var listenAddress *string = parser.String("L", "listen", &argparse.Options{Required: false, Default: "127.0.0.1:1323", Help: "bind address."})
var swaggerPath *string = parser.String("F", "apifile", &argparse.Options{Required: true, Help: "swagger-ui api-docs file path."})
var serverRoot *string = parser.String("S", "serverroot", &argparse.Options{Required: false, Default: "", Help: "server override."})
err := parser.Parse(os.Args)
exit_on_error("[PARSER ERROR]", err)

useServerOverride := *serverRoot != ""
e := echo.New()
e.HideBanner = true
e.GET("/swagger.json", func(c echo.Context) error {
data := getContent(*swaggerPath)
result, _ := jsonparser.Set(data, []byte("\"\""), "host")
final, _ := jsonparser.Set(result, []byte("\"/backend-api\""), "basePath")
return c.JSONBlob(http.StatusOK, final)
if useServerOverride {
result, _ := jsonparser.Set(data, []byte("\"\""), "host")
final, _ := jsonparser.Set(result, []byte("\"/backend-api\""), "basePath")
return c.JSONBlob(http.StatusOK, final)
}
return c.JSONBlob(http.StatusOK, data)
})

backend, err := url.Parse(*serverRoot)
if err != nil {
e.Logger.Fatal(err)
}
targets := []*middleware.ProxyTarget{
{
URL: backend,
if useServerOverride {
backend, err := url.Parse(*serverRoot)
if err != nil {
e.Logger.Fatal(err)
}
targets := []*middleware.ProxyTarget{
{
URL: backend,
},
}
proxyBackend := e.Group("/backend-api")
proxyBackend.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{
Balancer: middleware.NewRoundRobinBalancer(targets),
Rewrite: map[string]string{
"^/backend-api/*": "/$1",
},
},
))
}
proxyBackend := e.Group("/backend-api")
proxyBackend.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{
Balancer: middleware.NewRoundRobinBalancer(targets),
Rewrite: map[string]string{
"^/backend-api/*": "/$1",
},
},
))

assetHandler := http.FileServer(getSwaggerUIFiles())
e.GET("/*", echo.WrapHandler(assetHandler))
println(banner)
e.Logger.Fatal(e.Start(*listenAddress))
}

Expand Down

0 comments on commit 0286117

Please sign in to comment.