-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathctx.go
61 lines (53 loc) · 1.29 KB
/
ctx.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
package proxy
import (
"fmt"
"github.com/google/uuid"
"net"
"net/http"
"strings"
)
type Context struct {
Id string
ClientAddr string
RemoteIp string
RemoteHost string
RemotePort string
ServName string
Request *http.Request
Response *http.Response
IsTLS bool
Protocol string
ThirdCtx interface{}
}
func NewContext() *Context {
id := strings.ReplaceAll(uuid.New().String(), "-", "")[:16]
return &Context{
Id: id,
IsTLS: false,
Protocol: "TCP",
}
}
func (c *Context) Preffix(args ...interface{}) (preffix string) {
preffixSB := strings.Builder{}
if c.Id != "" {
preffixSB.WriteString("[" + c.Id + "]")
}
if len(args) > 0 {
if args[0].(bool) {
preffixSB.WriteString(" " + fmt.Sprintf("[%s => %s]", net.JoinHostPort(c.RemoteHost, c.RemotePort), c.ClientAddr) + " [Response]")
} else {
preffixSB.WriteString(" " + fmt.Sprintf("[%s => %s]", c.ClientAddr, net.JoinHostPort(c.RemoteHost, c.RemotePort)) + " [Request]")
}
} else {
if c.ClientAddr != "" {
preffixSB.WriteString(" " + "[" + c.ClientAddr + "]")
}
if c.RemoteHost != "" {
preffixSB.WriteString(" " + "[" + net.JoinHostPort(c.RemoteHost, c.RemotePort) + "]")
}
}
if c.Protocol != "" {
preffixSB.WriteString(" " + "[" + c.Protocol + "]")
}
return preffixSB.String()
}