-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
154 lines (136 loc) · 3.56 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
// Copyright 2023 冯立强 fenglq@tingyun.com. All rights reserved.
// +build linux
// +build amd64 arm64
// +build cgo
package ginframe
import (
"io"
"reflect"
"strconv"
"time"
"github.com/gorilla/websocket"
"github.com/TingYunGo/goagent"
"github.com/TingYunGo/goagent/libs/tystring"
)
const (
StorageGorillaWS = tingyun3.StorageGorillaWS
)
func matchMethod(method, matcher string) bool {
return tystring.SubString(method, 0, len(matcher)) == matcher
}
func isNativeMethod(method string) bool {
if matchMethod(method, "io.") {
return true
}
if matchMethod(method, "io/") {
return true
}
if matchMethod(method, "github.com/gorilla/websocket") {
return true
}
return false
}
//go:noinline
func getCallName(skip int) (callerName string) {
skip++
callerName = tingyun3.GetCallerName(skip)
for isNativeMethod(callerName) {
skip++
callerName = tingyun3.GetCallerName(skip)
}
return
}
func onWebsocketTaskEnd() {
action := tingyun3.GetAction()
if action == nil {
return
}
if component := tingyun3.GetComponent(); component != nil {
component.Finish()
}
igonre_duration := tingyun3.ReadLocalConfigInteger(tingyun3.ConfigLocalIntegerWebsocketIgnore, 0)
if action.Duration() < time.Duration(igonre_duration)*time.Millisecond {
action.Ignore()
} else {
action.SetStatusCode(1)
action.Finish()
}
tingyun3.LocalClear()
}
func getWebsockType(messageType int) string {
if messageType == 1 {
return "websocket.text"
}
if messageType == 2 {
return "websocket.bin"
}
return "websocket." + strconv.Itoa(messageType)
}
func (r *messageReader) onWebsocketTaskBegin() {
methodName := getCallName(3)
action, _ := tingyun3.CreateAction(getWebsockType(r.messageType), methodName)
if action == nil {
return
}
action.SetName("CLIENTIP", r.addr)
if len(r.url) > 0 {
action.SetURL(r.url)
}
component := action.CreateComponent(methodName)
tingyun3.SetAction(action)
tingyun3.SetComponent(component)
}
type messageReader struct {
r io.Reader
size int64
messageType int
addr string
url string
}
func (r *messageReader) Read(b []byte) (int, error) {
size, err := r.r.Read(b)
if size > 0 {
r.size += int64(size)
}
if err == io.EOF {
r.onWebsocketTaskBegin()
}
return size, err
}
func (r *messageReader) Close() error {
return r.r.(io.Closer).Close()
}
//go:noinline
func ConnNextReader(c *websocket.Conn) (messageType int, r io.Reader, err error) {
trampoline.arg1 = *trampoline.idpointer + trampoline.idindex + trampoline.arg1 + trampoline.arg2 + trampoline.arg3 + trampoline.arg4 + trampoline.arg5 + trampoline.arg6 + trampoline.arg7 +
trampoline.arg8 + trampoline.arg9 + trampoline.arg10 + trampoline.arg11 + trampoline.arg12 + trampoline.arg13 + trampoline.arg14 + trampoline.arg15 + trampoline.arg16 +
trampoline.arg17 + trampoline.arg18 + trampoline.arg19 + trampoline.arg20
return -1, nil, nil
}
//go:noinline
func WrapConnNextReader(c *websocket.Conn) (int, io.Reader, error) {
onWebsocketTaskEnd()
msgType, r, err := ConnNextReader(c)
if r != nil && msgType != 8 {
remoteIP := ""
tystring.SplitMapString(c.RemoteAddr().String(), func(t byte) bool {
return t == ':'
}, func(ip, _ string) {
remoteIP = ip
})
url := ""
raw := c.UnderlyingConn()
if wrpper_conn, ok := raw.(*tingyun3.NetConnWrapper); ok {
url = wrpper_conn.Url()
if len(url) > 0 {
url = "ws" + url
}
}
r = &messageReader{r, 0, msgType, remoteIP, url}
}
return msgType, r, err
}
func init() {
tingyun3.Register(reflect.ValueOf(WrapConnNextReader).Pointer())
tingyun3.Register(reflect.ValueOf(initTrampoline).Pointer())
}