|
| 1 | +package muxServeHTTP |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "encoding/base64" |
| 7 | + "github.com/HXSecurity/DongTai-agent-go/api" |
| 8 | + "github.com/HXSecurity/DongTai-agent-go/global" |
| 9 | + "github.com/HXSecurity/DongTai-agent-go/model/request" |
| 10 | + "github.com/HXSecurity/DongTai-agent-go/utils" |
| 11 | + "github.com/go-mux/mux" |
| 12 | + "net/http" |
| 13 | + "reflect" |
| 14 | + "strconv" |
| 15 | + "strings" |
| 16 | +) |
| 17 | + |
| 18 | +func MyServer(server *mux.Router, w http.ResponseWriter, r *http.Request) { |
| 19 | + worker, _ := utils.NewWorker(global.AgentId) |
| 20 | + |
| 21 | + TraceId := global.TraceId + "-" + strconv.Itoa(int(worker.GetId())) |
| 22 | + global.TargetTraceId = TraceId |
| 23 | + MyServerTemp(server, w, r) |
| 24 | + id := utils.CatGoroutineID() |
| 25 | + go func() { |
| 26 | + t := reflect.ValueOf(r.Body) |
| 27 | + var headerBase string |
| 28 | + body := "" |
| 29 | + for k, v := range r.Header { |
| 30 | + headerBase += k + ": " + strings.Join(v, ",") + "\n" |
| 31 | + } |
| 32 | + tranceID := TraceId + "." + strconv.Itoa(global.AgentId) + ".0.0.0" |
| 33 | + headerBase += "dt-traceid:" + tranceID |
| 34 | + if t.Kind() == reflect.Ptr { |
| 35 | + buf := t. |
| 36 | + Elem(). |
| 37 | + FieldByName("src"). |
| 38 | + Elem().Elem(). |
| 39 | + FieldByName("R"). |
| 40 | + Elem().Elem(). |
| 41 | + FieldByName("buf").Bytes() |
| 42 | + buf = buf[:bytes.IndexByte(buf, 0)] |
| 43 | + reader := bufio.NewReader(bytes.NewReader(buf)) |
| 44 | + var reqArr []string |
| 45 | + for { |
| 46 | + line, _, err := reader.ReadLine() |
| 47 | + if err != nil { |
| 48 | + break |
| 49 | + } |
| 50 | + reqArr = append(reqArr, string(line)) |
| 51 | + } |
| 52 | + body = reqArr[len(reqArr)-1] |
| 53 | + } |
| 54 | + header := base64.StdEncoding.EncodeToString([]byte(headerBase)) |
| 55 | + scheme := "http" |
| 56 | + if r.TLS != nil { |
| 57 | + scheme = "https" |
| 58 | + } |
| 59 | + onlyKey := int(worker.GetId()) |
| 60 | + |
| 61 | + HookGroup := &request.UploadReq{ |
| 62 | + Type: 36, |
| 63 | + InvokeId: onlyKey, |
| 64 | + Detail: request.Detail{ |
| 65 | + AgentId: global.AgentId, |
| 66 | + Function: request.Function{ |
| 67 | + Method: r.Method, |
| 68 | + Url: scheme + "://" + r.Host + r.RequestURI, |
| 69 | + Uri: r.URL.Path, |
| 70 | + Protocol: r.Proto, |
| 71 | + ClientIp: r.RemoteAddr, |
| 72 | + Language: "GO", |
| 73 | + ReplayRequest: false, |
| 74 | + ReqHeader: header, |
| 75 | + ReqBody: body, |
| 76 | + QueryString: r.URL.RawQuery, |
| 77 | + Pool: []request.Pool{}, |
| 78 | + TraceId: tranceID, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + var resBody string |
| 83 | + var resH string |
| 84 | + res, ok := global.ResponseMap.Load(id) |
| 85 | + if ok { |
| 86 | + global.ResponseMap.Delete(id) |
| 87 | + resBody = res.(string) |
| 88 | + } |
| 89 | + value2, ok2 := global.ResponseHeaderMap.Load(id) |
| 90 | + if ok2 { |
| 91 | + global.ResponseHeaderMap.Delete(id) |
| 92 | + resH = value2.(string) |
| 93 | + } |
| 94 | + for k, v := range w.Header() { |
| 95 | + resH += k + ": " + strings.Join(v, ",") + "\n" |
| 96 | + } |
| 97 | + resHeader := base64.StdEncoding.EncodeToString([]byte(resH)) |
| 98 | + HookGroup.Detail.ResHeader = resHeader |
| 99 | + HookGroup.Detail.ResBody = resBody |
| 100 | + goroutineIDs := make(map[string]bool) |
| 101 | + global.PoolTreeMap.Range(func(key, value interface{}) bool { |
| 102 | + if value.(*request.PoolTree).IsThisBegin(id) { |
| 103 | + global.PoolTreeMap.Delete(key) |
| 104 | + value.(*request.PoolTree).FMT(&HookGroup.Detail.Function.Pool, worker, goroutineIDs, HookGroup.Detail.Function.TraceId) |
| 105 | + return false |
| 106 | + } |
| 107 | + return true |
| 108 | + }) |
| 109 | + api.ReportUpload(*HookGroup) |
| 110 | + request.RunMapGCbYGoroutineID(goroutineIDs) |
| 111 | + }() |
| 112 | + return |
| 113 | +} |
| 114 | + |
| 115 | +func MyServerTemp(server *mux.Router, w http.ResponseWriter, r *http.Request) { |
| 116 | + for i := 0; i < 100; i++ { |
| 117 | + |
| 118 | + } |
| 119 | + return |
| 120 | +} |
0 commit comments