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

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