-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (206 loc) · 6.6 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
// Copyright 2021 冯立强 fenglq@tingyun.com. All rights reserved.
// +build linux
// +build amd64 arm64
// +build cgo
package irisframe
import (
"fmt"
"net"
"reflect"
"runtime"
"github.com/goinline/agent/libs/tystring"
"github.com/goinline/agent"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/core/router"
"github.com/kataras/iris/v12/mvc"
"github.com/kataras/iris/v12/websocket"
"github.com/kataras/neffos"
)
const (
StorageIndexIris = tingyun3.StorageIndexIris
)
type controllerInfo struct {
name string
method string
}
//go:noinline
func irisCreateRoutes(api *router.APIBuilder, methods []string, relativePath string, handlers ...context.Handler) []*router.Route {
fmt.Println(api, methods, relativePath, handlers)
return nil
}
func wrapHandler(handler context.Handler, path string) context.Handler {
info := controllerInfo{}
if i := tingyun3.LocalGet(StorageIndexIris); i != nil {
controller := i.(controllerInfo)
info.name, info.method = controller.name, controller.method
}
if len(info.method) == 0 {
handlerPC := reflect.ValueOf(handler).Pointer()
info.method = runtime.FuncForPC(handlerPC).Name()
}
token := "github.com/goinline/agent"
if tystring.SubString(info.method, 0, len(token)) == token {
return handler
}
return func(ctx *context.Context) {
if tingyun3.LocalGet(StorageIndexIris) == nil {
action := tingyun3.GetAction()
if action != nil {
if len(info.name) == 0 && len(info.method) > 0 {
action.SetName("ROUTE", info.method)
} else if len(info.name) > 0 {
action.SetName("Method", info.method)
action.SetName("CLASS", info.name)
}
if len(info.name) == 0 && len(info.method) == 0 {
action.SetName("URI", path)
}
}
tingyun3.LocalSet(StorageIndexIris, 1)
defer tingyun3.LocalDelete(StorageIndexIris)
}
handler(ctx)
}
}
//go:noinline
func WrapirisCreateRoutes(api *router.APIBuilder, methods []string, relativePath string, handlers ...context.Handler) []*router.Route {
for i := 0; i < len(handlers); i++ {
handlers[i] = wrapHandler(handlers[i], api.GetRelPath()+relativePath)
}
return irisCreateRoutes(api, methods, relativePath, handlers...)
}
//go:noinline
func irishandleMany(c *mvc.ControllerActivator, method, path, funcName string, override bool, middleware ...context.Handler) []*router.Route {
fmt.Println(c, method, path, funcName, override, middleware)
return nil
}
//go:noinline
func WrapirishandleMany(c *mvc.ControllerActivator, method, path, funcName string, override bool, middleware ...context.Handler) []*router.Route {
info := controllerInfo{
name: c.Name(),
method: funcName,
}
tingyun3.LocalSet(StorageIndexIris, info)
defer tingyun3.LocalDelete(StorageIndexIris)
return irishandleMany(c, method, path, funcName, override, middleware...)
}
//go:noinline
func irisHandleDir(api *router.APIBuilder, requestPath string, fsOrDir interface{}, opts ...router.DirOptions) (routes []*router.Route) {
fmt.Println(api, requestPath, fsOrDir, opts)
return nil
}
//go:noinline
func WrapirisHandleDir(api *router.APIBuilder, requestPath string, fsOrDir interface{}, opts ...router.DirOptions) (routes []*router.Route) {
return irisHandleDir(api, requestPath, fsOrDir, opts...)
}
//github.com/kataras/neffos.(*Conn).handleMessage
//go:noinline
func neffosConnhandleMessage(c *neffos.Conn, msg neffos.Message) error {
fmt.Println(c, msg)
return nil
}
func readBoolean(name string, defaultValue bool) bool {
retvalue := defaultValue
if value, found := tingyun3.ConfigRead(name); found {
if v, ok := value.(bool); ok {
retvalue = v
}
}
return retvalue
}
//go:noinline
func WrapneffosConnhandleMessage(c *neffos.Conn, msg neffos.Message) error {
action := tingyun3.GetAction()
preaction := action
if preaction == nil {
r := c.Socket().Request()
if readBoolean("websocket_enabled", false) {
if action, _ = tingyun3.CreateAction("URI", r.URL.Path); action != nil {
tingyun3.SetAction(action)
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
action.SetName("CLIENTIP", host)
}
}
}
}
defer func() {
if preaction == nil && action != nil {
action.Finish()
tingyun3.LocalClear()
}
}()
return neffosConnhandleMessage(c, msg)
}
//go:noinline
func websocketUpgrade(ctx *context.Context, idGen websocket.IDGenerator, s *neffos.Server) *neffos.Conn {
fmt.Println(ctx, idGen, s)
return nil
}
//go:noinline
func WrapwebsocketUpgrade(ctx *context.Context, idGen websocket.IDGenerator, s *neffos.Server) *neffos.Conn {
action := tingyun3.GetAction()
if action != nil {
action.SetName("websocket", "Upgrade")
}
r := websocketUpgrade(ctx, idGen, s)
return r
}
//go:noinline
func neffosfireEvent(e neffos.Events, c *neffos.NSConn, msg neffos.Message) error {
fmt.Println(e, c, msg)
return nil
}
//go:noinline
func WrapneffosfireEvent(e neffos.Events, c *neffos.NSConn, msg neffos.Message) error {
action := tingyun3.GetAction()
if action != nil {
if h, found := e[msg.Event]; found {
handlerPC := reflect.ValueOf(h).Pointer()
methodName := runtime.FuncForPC(handlerPC).Name()
if len(methodName) > 0 {
action.SetName("Method", methodName)
action.SetName("Websocket", methodName)
}
}
}
return neffosfireEvent(e, c, msg)
}
//go:noinline
func neffosmakeEventFromMethod(v reflect.Value, method reflect.Method, eventMatcher neffos.EventMatcherFunc) (eventName string, cb neffos.MessageHandlerFunc) {
fmt.Println(v, method, eventMatcher)
return "", nil
}
//go:noinline
func WrapneffosmakeEventFromMethod(v reflect.Value, method reflect.Method, eventMatcher neffos.EventMatcherFunc) (eventName string, cb neffos.MessageHandlerFunc) {
name, handler := neffosmakeEventFromMethod(v, method, eventMatcher)
if handler == nil {
return name, handler
}
className := v.Type().String()
methodName := method.Name
if tystring.SubString(className, 0, 1) == "*" {
className = tystring.SubString(className, 1, len(className))
}
eventName = name
cb = func(nsconn *neffos.NSConn, msg neffos.Message) error {
action := tingyun3.GetAction()
if action != nil {
if len(name) > 0 {
action.SetName("Method", methodName)
}
if len(className) > 0 {
action.SetName("CLASS", className)
}
}
return handler(nsconn, msg)
}
return
}
func init() {
tingyun3.Register(reflect.ValueOf(WrapirisCreateRoutes).Pointer())
tingyun3.Register(reflect.ValueOf(WrapwebsocketUpgrade).Pointer())
tingyun3.Register(reflect.ValueOf(WrapneffosfireEvent).Pointer())
tingyun3.Register(reflect.ValueOf(WrapirishandleMany).Pointer())
tingyun3.Register(reflect.ValueOf(WrapneffosConnhandleMessage).Pointer())
tingyun3.Register(reflect.ValueOf(WrapneffosmakeEventFromMethod).Pointer())
}