-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpdump.go
107 lines (96 loc) · 2.87 KB
/
httpdump.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
package main
import (
"flag"
"fmt"
"github.com/gofiber/fiber/v2"
"httpdump/pkg/log"
"os"
"os/signal"
"syscall"
)
func main() {
port := flag.String("p", "8080", "http port")
respType := flag.String("t", "html", "response Content-Type: html, json, txt")
logPath := flag.String("l", "", "log path")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
}
flag.Parse()
// init logger
log.Init(*logPath, "trace")
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
DisableDefaultDate: true,
DisableHeaderNormalizing: true,
DisableDefaultContentType: true,
})
// Match any request
app.Use(func(c *fiber.Ctx) error {
if c.Path() != "/favicon.ico" {
log.Trace(c.IP(), c.Request().String())
}
err := c.Next()
// print response
// log.Debug(c.Response().String())
return err
})
app.Get("/", func(c *fiber.Ctx) error {
switch *respType {
case "json":
return c.JSON(fiber.Map{"hello": "world"})
case "txt":
c.Set("Content-Type", "text/plain; charset=utf-8")
return c.SendString("hello world")
default:
c.Set("Content-Type", "text/html; charset=utf-8")
return c.SendString(html)
}
})
// fmt.Println(*port)
go log.Fatal(app.Listen(fmt.Sprintf(":%s", *port)))
watchSignal()
}
func watchSignal() {
log.Info("Server pid:", os.Getpid())
sigs := make(chan os.Signal, 1)
// https://pkg.go.dev/os/signal
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
for {
// 没有信号就阻塞,从而避免主协程退出
sig := <-sigs
log.Info("Get signal:", sig)
switch sig {
default:
log.Info("Stop")
return
}
}
}
var html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>console</title>
<script>
window.onload = function() {
console.log("navigator.webdriver:", navigator.webdriver);
console.log("navigator.userAgent:", navigator.userAgent);
console.log("navigator.plugins:", navigator.plugins);
console.log("navigator.languages:", navigator.languages);
var canvas = document.createElement('canvas');
var gl = canvas.getContext('webgl');
var debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
var vendor = gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
var renderer = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
console.log("webgl vendor:", vendor);
console.log("webgl renderer:", renderer);
};
</script>
</head>
<body>
hello world
</body>
</html>`