-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
409 lines (353 loc) · 11.7 KB
/
main.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Package main boots the UseWebhook CLI tool.
package main
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// Global variables that can be set via LDFLAGS during build time
var (
// Version is set during release
Version = "dev"
APIURL = "https://usewebhook.com/api/webhooks/"
BaseURL = "https://usewebhook.com"
SettingsFilename = ".usewebhook"
)
// WebhookRequest represents a single webhook request
type WebhookRequest struct {
RequestID string `json:"request_id"`
Timestamp string `json:"timestamp"`
IP string `json:"ip"`
Method string `json:"method"`
Query string `json:"query"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}
// WebhookResponse represents the response from the webhook API
type WebhookResponse struct {
Requests []WebhookRequest `json:"requests"`
}
// Config represents the user's configuration
type Config struct {
WebhookHistory []string `json:"webhook_history"`
LastUsed string `json:"last_used"`
}
// AppConfig holds the configuration for the current run of the application
type AppConfig struct {
FullLog bool
ForwardTo string
WebhookID string
RequestID string
PollSleep time.Duration
InitialSleep time.Duration
}
// fetchWebhookData retrieves webhook data from the API
func fetchWebhookData(webhookID string, params url.Values) (*WebhookResponse, error) {
requestURL := APIURL + webhookID
if len(params) > 0 {
requestURL += "?" + params.Encode()
}
resp, err := http.Get(requestURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unable to fetch webhook data (status code %d)", resp.StatusCode)
}
var webhookResp WebhookResponse
if err := json.NewDecoder(resp.Body).Decode(&webhookResp); err != nil {
return nil, err
}
return &webhookResp, nil
}
// getValueOrEmpty returns a string representation of the value or "(empty)" if it's nil or an empty string
func getValueOrEmpty(value interface{}) string {
if value == nil || value == "" {
return "(empty)"
}
switch v := value.(type) {
case string:
return v
case []byte:
return string(v)
default:
return fmt.Sprintf("%v", v)
}
}
// logRequest logs the details of a webhook request
func logRequest(request WebhookRequest, fullLog bool) {
if fullLog {
color.Yellow("\n=== Start of Request ID: %s ===\n", request.RequestID)
color.Cyan("Timestamp: %s", color.HiBlackString(request.Timestamp))
color.Cyan("Source IP (anonymized): %s", color.HiBlackString(getValueOrEmpty(request.IP)))
color.Cyan("Method: %s", color.HiBlackString(getValueOrEmpty(request.Method)))
color.Cyan("Query: %s", color.HiBlackString(getValueOrEmpty(request.Query)))
color.Cyan("Headers: %s", color.HiBlackString(getValueOrEmpty(prettyJSON(request.Headers))))
color.Cyan("Body: %s", color.HiBlackString(getValueOrEmpty(request.Body)))
color.Yellow("=== End of Request ID: %s ===\n", request.RequestID)
} else {
// Log in one line
color.Yellow("[INCOMING] %s%s %s%s %s%s %s%s",
color.HiBlackString("timestamp="), getValueOrEmpty(request.Timestamp),
color.HiBlackString("ip="), getValueOrEmpty(request.IP),
color.HiBlackString("method="), getValueOrEmpty(request.Method),
color.HiBlackString("request_id="), getValueOrEmpty(request.RequestID),
)
}
}
// prettyJSON returns a formatted JSON string
func prettyJSON(v interface{}) string {
jsonData, err := json.MarshalIndent(v, "", " ")
if err != nil {
return fmt.Sprintf("%v", v)
}
return string(jsonData)
}
// forwardRequest forwards the webhook request to the specified URL
func forwardRequest(request WebhookRequest, forwardTo string) {
client := &http.Client{}
reqURL := forwardTo
if request.Query != "" {
reqURL += "?" + request.Query
}
req, err := http.NewRequest(request.Method, reqURL, strings.NewReader(request.Body))
if err != nil {
color.Red("Error creating forward request: %v", err)
return
}
// Set headers
for k, v := range request.Headers {
req.Header.Set(k, v)
}
// Handle Base64 content-type logic
if req.Header.Get("Content-Type") == "application/base64" {
decodedBody, originalContentType, err := decodeBase64Body(request.Body)
if err != nil {
color.Red("Error decoding Base64 body: %v", err)
return
}
req.Body = io.NopCloser(strings.NewReader(decodedBody)) // Set decoded body
req.Header.Set("Content-Type", originalContentType) // Set original content-type
req.Header.Del("X-Original-Content-Type")
}
parsedURL, err := url.Parse(forwardTo)
if err != nil {
color.Red("Error parsing forward URL: %v", err)
return
}
req.Header.Set("Host", parsedURL.Host)
start := time.Now()
resp, err := client.Do(req)
if err != nil {
color.Red("Error forwarding request to %s: %v", reqURL, err)
return
}
defer resp.Body.Close()
duration := time.Since(start)
color.Blue("[FORWARDED] %s%d %s%dms %s%s", color.HiBlackString("status="), resp.StatusCode, color.HiBlackString("time="), duration.Milliseconds(), color.HiBlackString("destination="), reqURL)
}
// decodeBase64Body decodes a Base64 encoded body and extracts the original content-type
func decodeBase64Body(encodedBody string) (string, string, error) {
decoded, err := base64.StdEncoding.DecodeString(encodedBody)
if err != nil {
return "", "", err
}
// Extract the original content-type from the decoded body
originalContentType := http.DetectContentType(decoded)
return string(decoded), originalContentType, nil
}
// pollWebhook continuously polls the webhook API for new requests
func pollWebhook(config AppConfig) {
lastPollTime := time.Now().UTC()
for {
params := url.Values{}
if config.RequestID != "" {
params.Set("request_id", config.RequestID)
} else {
params.Set("since", lastPollTime.Format(time.RFC3339))
}
webhookData, err := fetchWebhookData(config.WebhookID, params)
if err != nil {
color.Red("Error fetching webhook data: %v", err)
time.Sleep(config.InitialSleep)
continue
}
for _, request := range webhookData.Requests {
logRequest(request, config.FullLog)
if config.ForwardTo != "" {
forwardRequest(request, config.ForwardTo)
}
}
// if single request mode, exit after the first request
if config.RequestID != "" {
if len(webhookData.Requests) <= 0 {
color.Red("No requests found for request ID: %s", config.RequestID)
os.Exit(1)
}
os.Exit(0)
}
lastPollTime = time.Now().UTC()
time.Sleep(config.PollSleep)
}
}
// extractIdsFromURLOrArgs extracts the webhook ID and optionally the request ID from various URL patterns
func extractIdsFromURLOrArgs(webhookURL string) (string, string, error) {
re := regexp.MustCompile(`^(?:https?://)?(?:usewebhook\.com/)?([0-9a-fA-F]{32})(?:\?.*)?$`)
if matches := re.FindStringSubmatch(webhookURL); matches != nil {
return matches[1], "", nil
}
parsedURL, err := url.Parse(webhookURL)
if err != nil {
return "", "", fmt.Errorf("invalid URL format")
}
queryParams := parsedURL.Query()
webhookID := queryParams.Get("id")
requestID := queryParams.Get("req")
if webhookID == "" {
return "", "", fmt.Errorf("invalid webhook ID")
}
return webhookID, requestID, nil
}
// getConfigFilePath returns the path to the config file
func getConfigFilePath() string {
homeDir, err := os.UserHomeDir()
if err != nil {
color.Red("Error getting user home directory: %v", err)
return ""
}
return filepath.Join(homeDir, SettingsFilename)
}
// loadConfig loads the user's configuration from the config file
func loadConfig() (*Config, error) {
configPath := getConfigFilePath()
if configPath == "" {
return nil, fmt.Errorf("unable to determine config file path")
}
data, err := os.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {
return &Config{}, nil
}
return nil, err
}
var config Config
if err := json.Unmarshal(data, &config); err != nil {
return nil, err
}
return &config, nil
}
// saveConfig saves the user's configuration to the config file
func saveConfig(config *Config) error {
configPath := getConfigFilePath()
if configPath == "" {
return fmt.Errorf("unable to determine config file path")
}
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return os.WriteFile(configPath, data, 0600)
}
// createRootCommand creates and returns the root command for the CLI
func createRootCommand() *cobra.Command {
appConfig := AppConfig{
PollSleep: 3 * time.Second,
InitialSleep: 1 * time.Second,
}
rootCmd := &cobra.Command{
Use: "usewebhook <webhook ID or URL>",
Short: "Capture and inspect webhooks from your browser. Replay them on localhost.",
Version: Version,
Example: "usewebhook # creates a new webhook\nusewebhook <webhook ID>\nusewebhook <webhook ID> --log-details\nusewebhook <webhook ID> --request-id <request ID>\nusewebhook <webhook ID> --forward-to http://localhost:3000/your-endpoint",
Run: func(cmd *cobra.Command, args []string) {
runRootCommand(cmd, args, &appConfig)
},
}
rootCmd.Flags().StringVarP(&appConfig.RequestID, "request-id", "r", "", "the request ID to fetch (optional)")
rootCmd.Flags().StringVarP(&appConfig.ForwardTo, "forward-to", "f", "", "forward incoming requests to the provided URL (optional)")
rootCmd.Flags().BoolVarP(&appConfig.FullLog, "log-details", "l", false, "log full request details (default: false)")
return rootCmd
}
// runRootCommand executes the main logic of the CLI
func runRootCommand(cmd *cobra.Command, args []string, appConfig *AppConfig) {
config, err := loadConfig()
if err != nil {
color.Red("Error loading config: %v", err)
os.Exit(1)
}
if len(args) == 0 {
// If no webhook ID or URL is provided, use the last used webhook ID
appConfig.WebhookID = config.LastUsed
if appConfig.WebhookID == "" {
// If still no webhook ID, create a new one
randomBytes := make([]byte, 16)
_, err := rand.Read(randomBytes)
if err != nil {
color.Red("Error generating random webhook ID: %v", err)
os.Exit(1)
}
appConfig.WebhookID = hex.EncodeToString(randomBytes)
color.HiBlack("No webhook ID or URL provided, creating a new one...")
}
} else {
// If a webhook ID or URL is provided, extract the webhook ID and optionally the request ID
webhookID, requestID, err := extractIdsFromURLOrArgs(args[0])
if err != nil {
color.Red("Error parsing webhook URL: %v", err)
os.Exit(1)
}
appConfig.WebhookID = webhookID
if requestID != "" {
appConfig.RequestID = requestID
}
}
// Update config
config.LastUsed = appConfig.WebhookID
if !contains(config.WebhookHistory, appConfig.WebhookID) {
config.WebhookHistory = append(config.WebhookHistory, appConfig.WebhookID)
}
if err := saveConfig(config); err != nil {
color.Yellow("Warning: Unable to save config: %v", err)
}
if appConfig.RequestID != "" {
color.Green("Single request mode. Retrieving webhook=%s request=%s\n\n", appConfig.WebhookID, appConfig.RequestID)
} else {
color.Green("Dashboard: %s/?id=%s", BaseURL, appConfig.WebhookID)
color.Green("Webhook URL: %s/%s", BaseURL, appConfig.WebhookID)
if appConfig.ForwardTo != "" {
color.Green("Forwarding to: %s", appConfig.ForwardTo)
}
color.HiBlack("\nPress Ctrl+C to stop\n\n")
}
pollWebhook(*appConfig)
}
// contains checks if a slice contains a specific item
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
// main is the entry point of the application
func main() {
rootCmd := createRootCommand()
if err := rootCmd.Execute(); err != nil {
color.Red("Error: %v", err)
os.Exit(1)
}
}