github.com/HXSecurity/DongTai-agent-go@v0.4.2/model/request/engine.go (about)

     1  package request
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strconv"
     7  
     8  	"github.com/HXSecurity/DongTai-agent-go/global"
     9  	"github.com/HXSecurity/DongTai-agent-go/utils"
    10  )
    11  
    12  type AgentRegisterReq struct {
    13  	AutoCreateProject bool   `json:"autoCreateProject"`
    14  	Name              string `json:"name"`
    15  	Language          string `json:"language"`
    16  	Version           string `json:"version"`
    17  	ProjectName       string `json:"projectName"`
    18  	Hostname          string `json:"hostname"`
    19  	Network           string `json:"network"`
    20  	ContainerName     string `json:"containerName"`
    21  	ContainerVersion  string `json:"containerVersion"`
    22  	ServerAddr        string `json:"serverAddr"`
    23  	ServerPort        string `json:"serverPort"`
    24  	ServerPath        string `json:"serverPath"`
    25  	ServerEnv         string `json:"serverEnv"`
    26  	Pid               string `json:"pid"`
    27  	ProjectVersion    string `json:"projectVersion"`
    28  }
    29  
    30  type HookRuleReq struct {
    31  	Language string `json:"language"`
    32  }
    33  
    34  // UploadReq
    35  /* Type
    36  数据类型,可选择:
    37  1 - Agent心跳数据
    38  17 - 依赖组件数据
    39  36 - 方法调用数据
    40  */
    41  type UploadReq struct {
    42  	Type     int    `json:"type"`
    43  	Detail   Detail `json:"detail"`
    44  	InvokeId int    `json:"invokeId"`
    45  }
    46  
    47  type Detail struct {
    48  	AgentId int `json:"agentId"`
    49  	Pant
    50  	Component
    51  	Components
    52  	Function
    53  	Pool
    54  	Log
    55  	Api
    56  }
    57  
    58  type Pant struct {
    59  	Disk            string `json:"disk"`
    60  	Memory          string `json:"memory"`
    61  	Cpu             string `json:"cpu"`
    62  	MethodQueue     int    `json:"methodQueue"`
    63  	ReplayQueue     int    `json:"replayQueue"`
    64  	ReqCount        int    `json:"reqCount"`
    65  	ReportQueue     int    `json:"reportQueue"`
    66  	IsCoreInstalled int    `json:"isCoreInstalled"`
    67  	IsCoreRunning   int    `json:"isCoreRunning"`
    68  }
    69  
    70  type Component struct {
    71  	PackagePath      string `json:"packagePath"`
    72  	PackageVersion   string `json:"packageVersion"`
    73  	PackageSignature string `json:"packageSignature"`
    74  	PackageName      string `json:"packageName"`
    75  	PackageAlgorithm string `json:"packageAlgorithm"`
    76  }
    77  
    78  type Components struct {
    79  	Packages []Component `json:"packages"`
    80  }
    81  
    82  type Function struct {
    83  	Uri           string `json:"uri"`
    84  	Url           string `json:"url"`
    85  	Protocol      string `json:"protocol"`
    86  	ContextPath   string `json:"contextPath"`
    87  	Pool          []Pool `json:"pool"`
    88  	Language      string `json:"language"`
    89  	ClientIp      string `json:"clientIp"`
    90  	Secure        bool   `json:"secure"`
    91  	QueryString   string `json:"queryString"`
    92  	ReplayRequest bool   `json:"replayRequest"`
    93  	Method        string `json:"method"`
    94  	ReqHeader     string `json:"reqHeader"`
    95  	ReqBody       string `json:"reqBody"`
    96  	ResBody       string `json:"resBody"`
    97  	Scheme        string `json:"scheme"`
    98  	ResHeader     string `json:"resHeader"`
    99  	TraceId       string `json:"traceId"`
   100  }
   101  
   102  type Pool struct {
   103  	InvokeId         int           `json:"invokeId"`
   104  	Interfaces       []interface{} `json:"interfaces"`
   105  	TargetHash       []string      `json:"targetHash"`
   106  	TargetValues     string        `json:"targetValues"`
   107  	Signature        string        `json:"signature"`
   108  	OriginClassName  string        `json:"originClassName"`
   109  	SourceValues     string        `json:"sourceValues"`
   110  	MethodName       string        `json:"methodName"`
   111  	ClassName        string        `json:"className"`
   112  	Source           bool          `json:"source"`
   113  	CallerLineNumber int           `json:"callerLineNumber"`
   114  	CallerClass      string        `json:"callerClass"`
   115  	Args             string        `json:"args"`
   116  	CallerMethod     string        `json:"callerMethod"`
   117  	SourceHash       []string      `json:"sourceHash"`
   118  	RetClassName     string        `json:"retClassName"`
   119  	TraceId          string        `json:"traceId"`
   120  	Plugin           string        `json:"plugin"`
   121  }
   122  
   123  type PoolReq struct {
   124  	Source          bool
   125  	OriginClassName string
   126  	MethodName      string
   127  	ClassName       string
   128  	Args            []interface{}
   129  	Reqs            []interface{}
   130  	NeedHook        []interface{}
   131  	NeedCatch       []interface{}
   132  	ArgsStr         string
   133  	TraceId         string
   134  	Plugin          string
   135  }
   136  
   137  type Log struct {
   138  	Log string `json:"log"`
   139  }
   140  
   141  type Api struct {
   142  	ApiData []ApiData `json:"apiData"`
   143  }
   144  
   145  type ApiData struct {
   146  	Uri         string      `json:"uri"`
   147  	Method      []string    `json:"method"`
   148  	Class       string      `json:"class"`
   149  	Parameters  []Parameter `json:"parameters"`
   150  	ReturnType  string      `json:"returnType"`
   151  	File        string      `json:"file"`
   152  	Controller  string      `json:"controller"`
   153  	Description string      `json:"description"`
   154  }
   155  
   156  type Parameter struct {
   157  	Name       string `json:"name"`
   158  	Type       string `json:"type"`
   159  	Annotation string `json:"annotation"`
   160  }
   161  
   162  type PoolTree struct {
   163  	*Pool
   164  	Begin       bool
   165  	GoroutineID string
   166  	Children    []*PoolTree
   167  }
   168  
   169  func (p *PoolTree) IsThisBegin(GoroutineID string) bool {
   170  	return GoroutineID == p.GoroutineID && p.Begin
   171  }
   172  
   173  func (p *PoolTree) FMT(pools *[]Pool, w *utils.Worker, goroutineIDs map[string]bool, TraceId string) {
   174  	p.Pool.InvokeId = int(w.GetId())
   175  	// if(p.Pool.ClassName == "grpc.(*ClientConn)"){
   176  	//	p.Pool.TraceId = TraceId
   177  	// }
   178  	*pools = append(*pools, *p.Pool)
   179  	goroutineIDs[p.GoroutineID] = true
   180  	fmt.Println(p.Pool.ClassName, p.Pool.MethodName)
   181  	fmt.Println(p.Pool.SourceValues, "====>", p.Pool.TargetValues)
   182  	fmt.Println(p.Pool.SourceHash, "===>", p.Pool.TargetHash)
   183  	for k, v := range p.Children {
   184  		global.PoolTreeMap.Delete(&v.Pool.TargetHash)
   185  		p.Children[k].FMT(pools, w, goroutineIDs, TraceId)
   186  	}
   187  }
   188  
   189  func Collect(args ...interface{}) []interface{} {
   190  	return args
   191  }
   192  
   193  func FmtHookPool(p PoolReq) Pool {
   194  	signature, callerClass, callerMethod, callerLineNumber := utils.FmtStack()
   195  	var sourceHash global.HashKeys = make(global.HashKeys, 0)
   196  	var SourceValues string = ""
   197  	if len(p.NeedHook) == 0 {
   198  		utils.RangeSource(p.Args, &p.NeedHook)
   199  	}
   200  	for _, v := range p.NeedHook {
   201  		switch v.(type) {
   202  		case string:
   203  			sourceHash = append(sourceHash, utils.GetSource(v))
   204  			SourceValues = utils.StringAdd(SourceValues, v.(string), " ")
   205  			break
   206  		case uintptr:
   207  			sourceHash = append(sourceHash, strconv.Itoa(int(v.(uintptr))))
   208  			SourceValues = utils.StringAdd(SourceValues, strconv.Itoa(int(v.(uintptr))), " ")
   209  			break
   210  		default:
   211  			t := reflect.TypeOf(v)
   212  			value := reflect.ValueOf(v)
   213  			if t.Kind() == reflect.String {
   214  				sourceHash = append(sourceHash, utils.GetSource(value.String()))
   215  				SourceValues = utils.StringAdd(SourceValues, value.String(), " ")
   216  			}
   217  			break
   218  		}
   219  	}
   220  	var targetHash global.HashKeys = make(global.HashKeys, 0)
   221  	var targetValues string = ""
   222  	var RetClassNames string = ""
   223  	if len(p.NeedCatch) == 0 {
   224  		utils.RangeSource(p.Reqs, &p.NeedCatch)
   225  	}
   226  	for _, v := range p.Reqs {
   227  		if reflect.ValueOf(v).IsValid() {
   228  			RetClassNames = utils.StringAdd(RetClassNames, reflect.ValueOf(v).Type().String(), " ")
   229  		}
   230  	}
   231  	for _, v := range p.NeedCatch {
   232  		switch v.(type) {
   233  		case string:
   234  			targetHash = append(targetHash, utils.GetSource(v))
   235  			targetValues = utils.StringAdd(targetValues, v.(string), " ")
   236  		case uintptr:
   237  			targetHash = append(targetHash, strconv.Itoa(int(v.(uintptr))))
   238  			targetValues = utils.StringAdd(targetValues, strconv.Itoa(int(v.(uintptr))), " ")
   239  		}
   240  	}
   241  	var ArgsStr string
   242  	if p.ArgsStr != "" {
   243  		ArgsStr = p.ArgsStr
   244  	} else {
   245  		ArgsStr = utils.Strval(p.Args)
   246  	}
   247  	var pool = Pool{
   248  		Source:           p.Source,
   249  		Interfaces:       []interface{}{},
   250  		SourceValues:     SourceValues,
   251  		SourceHash:       sourceHash,
   252  		TargetValues:     targetValues,
   253  		TargetHash:       targetHash,
   254  		Signature:        signature,
   255  		OriginClassName:  p.OriginClassName,
   256  		MethodName:       p.MethodName,
   257  		ClassName:        p.ClassName,
   258  		CallerLineNumber: callerLineNumber,
   259  		CallerClass:      callerClass,
   260  		CallerMethod:     callerMethod,
   261  		RetClassName:     RetClassNames,
   262  		Args:             ArgsStr,
   263  		TraceId:          p.TraceId,
   264  		Plugin:           p.Plugin,
   265  	}
   266  
   267  	poolTree := PoolTree{
   268  		Begin:       p.Source,
   269  		Pool:        &pool,
   270  		Children:    []*PoolTree{},
   271  		GoroutineID: utils.CatGoroutineID(),
   272  	}
   273  
   274  	if !p.Source {
   275  		global.PoolTreeMap.Range(func(key, value interface{}) bool {
   276  			if key.(*global.HashKeys).Some(sourceHash) {
   277  				poolTree.GoroutineID = value.(*PoolTree).GoroutineID
   278  				value.(*PoolTree).Children = append(value.(*PoolTree).Children, &poolTree)
   279  				global.PoolTreeMap.Store(&targetHash, &poolTree)
   280  			}
   281  			return true
   282  		})
   283  	} else {
   284  		global.PoolTreeMap.Store(&targetHash, &poolTree)
   285  	}
   286  	return pool
   287  }
   288  
   289  func RunMapGCbYGoroutineID(goroutineIDs map[string]bool) {
   290  	global.PoolTreeMap.Range(func(key, value interface{}) bool {
   291  		for id, _ := range goroutineIDs {
   292  			if value.(*PoolTree).GoroutineID == id {
   293  				global.PoolTreeMap.Delete(key)
   294  			}
   295  		}
   296  		return true
   297  	})
   298  }