Skip to content

Commit 0286117

Browse files
committed
add license and readme.
1 parent 1a1650d commit 0286117

File tree

3 files changed

+91
-20
lines changed

3 files changed

+91
-20
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Kacper Szurek
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SwaggerHelper
2+
3+
SwaggerHelper是用于启动本地保存的api-docs.json文档,例如在对系统进行**二次**渗透测试时,若目标关闭了Swagger-UI,则可使用本工具在本地启动接口文档(前提是api-docs文档已离线保存在本地),直接调用目标接口。
4+
5+
# 编译
6+
7+
> 编译该工具需要go 1.16或更高版本
8+
9+
`$ go build`
10+
11+
# 使用方法
12+
13+
```
14+
usage: SwaggerHelper [-h|--help] [-L|--listen "<value>"] -F|--apifile "<value>"
15+
[-S|--serverroot "<value>"]
16+
17+
Arguments:
18+
19+
-h --help Print help information
20+
-L --listen bind address.. Default: 127.0.0.1:1323
21+
-F --apifile swagger-ui api-docs file path.
22+
-S --serverroot server override.. Default:
23+
24+
```
25+
26+
若不需要覆盖api-docs内部的host和bashPath,直接执行如下命令:
27+
28+
`SwaggerHelper -F /to/path/api-docs.json`
29+
30+
若因为CORS限制或服务器地址更改需要另外指定API根路径的,执行如下命令:
31+
32+
`SwaggerHelper -F /to/path/api-docs.json -S http://1.2.3.4/api`
33+

swaggerhelper.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,59 @@ func getSwaggerUIFiles() http.FileSystem {
2727
return http.FS(fsys)
2828
}
2929

30+
const (
31+
banner = `
32+
33+
╔══╗─────────────╔╗╔╗
34+
║══╬╦╦╦═╗╔═╦═╦═╦╦╣╚╝╠═╦╗╔═╦═╦╦╗
35+
╠══║║║║╬╚╣╬║╬║╩╣╔╣╔╗║╩╣╚╣╬║╩╣╔╝
36+
╚══╩══╩══╬╗╠╗╠═╩╝╚╝╚╩═╩═╣╔╩═╩╝
37+
─────────╚═╩═╝──────────╚╝
38+
`
39+
)
40+
3041
func main() {
3142
parser := argparse.NewParser("SwaggerHelper", "")
3243
var listenAddress *string = parser.String("L", "listen", &argparse.Options{Required: false, Default: "127.0.0.1:1323", Help: "bind address."})
3344
var swaggerPath *string = parser.String("F", "apifile", &argparse.Options{Required: true, Help: "swagger-ui api-docs file path."})
3445
var serverRoot *string = parser.String("S", "serverroot", &argparse.Options{Required: false, Default: "", Help: "server override."})
3546
err := parser.Parse(os.Args)
3647
exit_on_error("[PARSER ERROR]", err)
37-
48+
useServerOverride := *serverRoot != ""
3849
e := echo.New()
50+
e.HideBanner = true
3951
e.GET("/swagger.json", func(c echo.Context) error {
4052
data := getContent(*swaggerPath)
41-
result, _ := jsonparser.Set(data, []byte("\"\""), "host")
42-
final, _ := jsonparser.Set(result, []byte("\"/backend-api\""), "basePath")
43-
return c.JSONBlob(http.StatusOK, final)
53+
if useServerOverride {
54+
result, _ := jsonparser.Set(data, []byte("\"\""), "host")
55+
final, _ := jsonparser.Set(result, []byte("\"/backend-api\""), "basePath")
56+
return c.JSONBlob(http.StatusOK, final)
57+
}
58+
return c.JSONBlob(http.StatusOK, data)
4459
})
45-
46-
backend, err := url.Parse(*serverRoot)
47-
if err != nil {
48-
e.Logger.Fatal(err)
49-
}
50-
targets := []*middleware.ProxyTarget{
51-
{
52-
URL: backend,
60+
if useServerOverride {
61+
backend, err := url.Parse(*serverRoot)
62+
if err != nil {
63+
e.Logger.Fatal(err)
64+
}
65+
targets := []*middleware.ProxyTarget{
66+
{
67+
URL: backend,
68+
},
69+
}
70+
proxyBackend := e.Group("/backend-api")
71+
proxyBackend.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{
72+
Balancer: middleware.NewRoundRobinBalancer(targets),
73+
Rewrite: map[string]string{
74+
"^/backend-api/*": "/$1",
75+
},
5376
},
77+
))
5478
}
55-
proxyBackend := e.Group("/backend-api")
56-
proxyBackend.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{
57-
Balancer: middleware.NewRoundRobinBalancer(targets),
58-
Rewrite: map[string]string{
59-
"^/backend-api/*": "/$1",
60-
},
61-
},
62-
))
6379

6480
assetHandler := http.FileServer(getSwaggerUIFiles())
6581
e.GET("/*", echo.WrapHandler(assetHandler))
82+
println(banner)
6683
e.Logger.Fatal(e.Start(*listenAddress))
6784
}
6885

0 commit comments

Comments
 (0)