-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.go
More file actions
283 lines (237 loc) · 7.28 KB
/
Copy pathrequest.go
File metadata and controls
283 lines (237 loc) · 7.28 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Package web implements a web retrieval and caching mechanism
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"slices"
"strconv"
"strings"
"time"
"github.com/puzpuzpuz/xsync"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
"github.com/tuupke/utils/env"
"github.com/tuupke/utils/lifecycle"
)
type (
etagPair struct {
Key, Date string
}
endpoint struct {
method string
name string
url string
}
endpoints []endpoint
endpointsSet []endpoints
)
var (
// etagCache is an etag cache. Stores the retrieved etag for some url
etagCache = xsync.NewMapOf[etagPair]()
pixieNonce = func() string {
nonce := env.String("WEBHOOK_REQUEST_NONCE", "")
if nonce == "" {
const pixieNonceAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
src := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, 32)
for i := range b {
b[i] = pixieNonceAlphabet[src.Intn(len(pixieNonceAlphabet))]
}
nonce = string(b)
}
zlog.Info().Str("nonce", nonce).Msg("X-Pixie-Nonce value")
return nonce
}()
maxWebhookTime = env.Duration("WEBHOOK_MAX_DURATION", time.Second*30)
)
func Do(ctx context.Context, log zerolog.Logger, url, verb string, ip net.IP, requestBody io.Reader) (responseBody io.ReadCloser, responseType string, loaded bool, err error) {
req, err := http.NewRequestWithContext(ctx, verb, url, requestBody)
if err != nil {
err = fmt.Errorf("cannot create request [%v] '%v'", verb, url)
return
}
req.Header.Set("User-Agent", "Pixie/CupsProxy")
req.Header.Set("Accept", "application/json, image/*")
req.Header.Set("X-Pixie-Nonce", pixieNonce)
if ip != nil {
req.Header.Set("X-Forwarded-For", ip.String())
}
// Check for, and add, cached etag values
etag, etagLoaded := etagCache.Load(url)
if etagLoaded {
req.Header.Add("If-None-Match", etag.Key)
req.Header.Add("If-Modified-Since", etag.Date)
}
resp, err := http.DefaultClient.Do(req)
var statusCode int
if resp != nil {
statusCode = resp.StatusCode
}
log.Err(err).Int("status", statusCode).Msg("called hook")
if err != nil {
err = fmt.Errorf("cannot create request [%v] '%v'", verb, url)
return
}
if resp.StatusCode == http.StatusNotModified {
// Nothing to do, not loaded, nor an error is thrown. Close body just in case and return
err = resp.Body.Close()
log.Err(err).Msg("status not changed, continuing")
return
}
responseBody, responseType, loaded = resp.Body, resp.Header.Get("content-type"), true
if key, date := resp.Header.Get("ETag"), resp.Header.Get("Date"); key != "" || date != "" {
etagCache.Store(url, etagPair{Key: key, Date: date})
}
if resp.StatusCode/100 != 2 {
// Only continue on success
log.Warn().Msg("status not succesfull, not continuing to next hook")
err = fmt.Errorf("non-successfull status code received (%v)", resp.StatusCode)
}
return
}
func (ep endpoint) executeRequest(ctx context.Context, log zerolog.Logger, data *Props) (io.ReadCloser, string, bool, error) {
u, params := replaceParameters(ep.url, data, ep.name)
log.Debug().Str("original", ep.url).Object("relevant-data", params).Str("result", u).Msg("replaced url")
var reqBody io.Reader
if ep.method != http.MethodGet {
reqBody = data.json(map[string]string{
"webhook_name": ep.name,
"webhook_method": ep.method,
"webhook_url": u,
})
}
ctx, cancel := context.WithTimeout(ctx, maxWebhookTime)
defer cancel()
return Do(ctx, log, u, ep.method, data.ip, reqBody)
}
func (ep endpoint) handleResponse(log zerolog.Logger, respBody io.ReadCloser, respType string, data *Props) (extra endpoints) {
extra = make(endpoints, 0, 3)
respType = strings.Split(respType, ";")[0]
log.Info().Str("response_type", respType).Msg("handling response")
switch respType {
case "image/jpeg", "image/png", "image/gif":
// Store in file and
fn := downloadTo + "/" + data.ip.String() + "." + respType[6:]
f, err := os.OpenFile(fn, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755)
log.Debug().Err(err).Str("filename", fn).Msg("storing image")
if err != nil {
log.Warn().Err(err).Str("filename", fn).Msg("could not open imagefile")
break
}
numbts, err := io.Copy(f, respBody)
log.Debug().Err(err).Int64("num-bytes", numbts).Str("filename", fn).Msg("written image")
if err != nil {
log.Warn().Str("filename", fn).Msg("image writing failed, not storing the path")
break
}
data.Store(imageKey, fn)
_ = f.Close()
case "application/json":
var jsonData map[string]interface{}
err := json.NewDecoder(respBody).Decode(&jsonData)
if err != nil {
log.Err(err).Msg("could not decode json")
break
}
var str string
var ok bool
for k, v := range jsonData {
if str, ok = interfaceToString(v); !ok {
continue
}
// TODO fix the imageKey handling, this is currently incorrect!
if k == imageKey {
// Check for valid url
_, err := url.Parse(str)
log.Debug().Err(err).Str("url", str).Str("key", k).Msg("url found to call, will call if valid")
if err == nil {
extra = append(extra, endpoint{
method: http.MethodGet,
name: imageKey,
url: str,
})
}
} else if keyTemplate != "" {
data.Store("webhook_key", k)
// Replace they key with the contents of the template
orig := k
var params mapWriter
k, params = replaceParameters(keyTemplate, data, ep.name)
log.Debug().Str("original", orig).Str("template", keyTemplate).Object("relevant-data", params).Str("result", k).Msg("filled template for key")
}
data.Store(k, str)
}
default:
log.Warn().Str("content-type", respType).Msg("unsupported content type encountered, ignored")
}
return
}
func (ep endpoints) handle(c chan e, log zerolog.Logger, data *Props) func() {
return func() {
defer func(c chan e) { c <- empty }(c)
eps := slices.Clone(ep)
// Any endpoint called can have
for len(eps) > 0 {
newEps := make(endpoints, 0, len(ep))
for _, end := range eps {
log = log.With().Str("verb", end.method).Str("url", end.url).Bool("with-ip", data.ip != nil).Logger()
respBody, respType, loaded, err := end.executeRequest(lifecycle.Context(), log, data)
log.Err(err).Bool("new data", loaded).Msg("request executed")
if err != nil {
return
}
// Nothing to do/update, continue to next in set
if !loaded {
log.Debug().Msg("no new data loaded, skipping response handling")
continue
}
data.latestData = time.Now()
newEps = append(newEps, end.handleResponse(log, respBody, respType, data)...)
}
eps = newEps
}
}
}
func interfaceToString(vi interface{}) (strVal string, ok bool) {
ok = true
switch v := vi.(type) {
case string:
strVal = v
case int:
strVal = strconv.Itoa(v)
case uint8:
strVal = strconv.Itoa(int(v))
case int8:
strVal = strconv.Itoa(int(v))
case uint16:
strVal = strconv.Itoa(int(v))
case int16:
strVal = strconv.Itoa(int(v))
case uint32:
strVal = strconv.Itoa(int(v))
case int32:
strVal = strconv.Itoa(int(v))
case uint64:
strVal = strconv.FormatUint(v, 10)
case int64:
strVal = strconv.FormatInt(v, 10)
case bool:
strVal = strconv.FormatBool(v)
case float64:
strVal = strconv.FormatFloat(v, 'f', 10, 64)
case float32:
strVal = strconv.FormatFloat(float64(v), 'f', 10, 32)
case []string:
strVal = strings.Join(v, ", ")
default:
ok = false
}
return
}