github.com/HXSecurity/DongTai-agent-go@v0.4.2/core/gin/ginEngineServerHTTP/replacement.go (about)

     1  package ginEngineServerHTTP
     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/gin-gonic/gin"
    12  	"net/http"
    13  	"reflect"
    14  	"strconv"
    15  	"strings"
    16  )
    17  
    18  func MyServer(server *gin.Engine, w http.ResponseWriter, r *http.Request) {
    19  	MyServerTemp(server, w, r)
    20  	id := utils.CatGoroutineID()
    21  	worker, _ := utils.NewWorker(global.AgentId)
    22  	go func() {
    23  		t := reflect.ValueOf(r.Body)
    24  		var headerBase string
    25  		body := ""
    26  		for k, v := range r.Header {
    27  			headerBase += k + ": " + strings.Join(v, ",") + "\n"
    28  		}
    29  
    30  		TraceId := global.TraceId + "-" + strconv.Itoa(int(worker.GetId()))
    31  		global.TargetTraceId = TraceId
    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  		HookGroup := &request.UploadReq{
    61  			Type:     36,
    62  			InvokeId: onlyKey,
    63  			Detail: request.Detail{
    64  				AgentId: global.AgentId,
    65  				Function: request.Function{
    66  					Method:        r.Method,
    67  					Url:           scheme + "://" + r.Host + r.RequestURI,
    68  					Uri:           r.URL.Path,
    69  					Protocol:      r.Proto,
    70  					ClientIp:      r.RemoteAddr,
    71  					Language:      "GO",
    72  					ReplayRequest: false,
    73  					ReqHeader:     header,
    74  					ReqBody:       body,
    75  					QueryString:   r.URL.RawQuery,
    76  					Pool:          []request.Pool{},
    77  					TraceId:       tranceID,
    78  				},
    79  			},
    80  		}
    81  		var resBody string
    82  		res, ok := global.ResponseMap.Load(id)
    83  		if ok {
    84  			global.ResponseMap.Delete(id)
    85  			resBody = res.(string)
    86  		}
    87  
    88  		var resH string
    89  		value2, ok1 := global.ResponseHeaderMap.Load(id)
    90  		if ok1 {
    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 *gin.Engine, w http.ResponseWriter, r *http.Request) {
   116  	for i := 0; i < 100; i++ {
   117  
   118  	}
   119  	return
   120  }