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

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/HXSecurity/DongTai-agent-go/model/request"
     7  	"github.com/HXSecurity/DongTai-agent-go/model/response"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  /*	AgentRegister
    12   	Agent 启动时,向 Server 端注册的接口
    13  	req: api.AgentRegisterReq
    14  */
    15  func AgentRegister(req request.AgentRegisterReq) (AgentId int, err error) {
    16  	fmt.Println("探针注册开始等待返回")
    17  	resp, body, errs := POST("/api/v1/agent/register", req).End()
    18  	fmt.Println("探针注册返回:", string(body))
    19  	if len(errs) > 0 {
    20  		for _, v := range errs {
    21  			fmt.Println(v)
    22  		}
    23  		err = errs[0]
    24  		return 0, err
    25  	}
    26  	if resp.StatusCode == 200 {
    27  		var res response.AgentRegisterRes
    28  		err := json.Unmarshal([]byte(body), &res)
    29  		if err != nil {
    30  			return 0, err
    31  		}
    32  		if res.Status == 201 {
    33  			AgentId = res.Data.Id
    34  			fmt.Println("注册成功,探针ID为", res.Data.Id)
    35  			return AgentId, nil
    36  		}
    37  		return 0, errors.New("注册失败,失败原因" + res.Msg)
    38  
    39  	}
    40  	return 0, errors.New("状态码为" + resp.Status)
    41  }
    42  
    43  func Limit() map[string]string {
    44  	var limit = make(map[string]string)
    45  	resp, body, errs := GET("/api/v1/agent/limit", nil).End()
    46  	if len(errs) > 0 {
    47  		for _, v := range errs {
    48  			fmt.Println(v)
    49  		}
    50  		return limit
    51  	}
    52  	if resp.StatusCode == 200 {
    53  		var res response.LimitRes
    54  		err := json.Unmarshal([]byte(body), &res)
    55  		if err != nil {
    56  			fmt.Println(err)
    57  		}
    58  		for _, item := range res.Data {
    59  			limit[item.Key] = item.Value
    60  		}
    61  	}
    62  	return limit
    63  }
    64  
    65  /*	Profiles
    66  	Engine 运行时,从 OpenAPI 服务获取Hook规则
    67  */
    68  func Profiles(req request.HookRuleReq) {
    69  	resp, body, errs := GET("/api/v1/profiles", req).End()
    70  	if len(errs) > 0 {
    71  		for _, v := range errs {
    72  			fmt.Println(v)
    73  		}
    74  		return
    75  	}
    76  	if resp.StatusCode == 200 {
    77  		var res response.AgentRegisterRes
    78  		err := json.Unmarshal([]byte(body), &res)
    79  		if err != nil {
    80  			fmt.Println(err)
    81  			return
    82  		}
    83  	}
    84  }
    85  
    86  func ReportUpload(req request.UploadReq) {
    87  	resp, body, errs := POST("/api/v1/report/upload", req).End()
    88  	if len(errs) > 0 {
    89  		for _, v := range errs {
    90  			fmt.Println(v)
    91  		}
    92  		return
    93  	}
    94  	if resp.StatusCode == 200 {
    95  		var res response.ResBase
    96  		err := json.Unmarshal([]byte(body), &res)
    97  		if res.Status == 201 {
    98  			if req.Type != 1 {
    99  				fmt.Println(res.Msg)
   100  			}
   101  		} else {
   102  			if req.Type != 1 {
   103  				fmt.Println(res.Msg)
   104  			}
   105  		}
   106  		if err != nil {
   107  			fmt.Println(err)
   108  			return
   109  		}
   110  	}
   111  }