Skip to content

Commit de94df1

Browse files
author
piexlmax
committed
add log and fixed trace bug
1 parent 05f851b commit de94df1

File tree

11 files changed

+64
-59
lines changed

11 files changed

+64
-59
lines changed

api/base_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"github.com/HXSecurity/DongTai-agent-go/global"
66
"github.com/HXSecurity/DongTai-agent-go/utils"
77
"github.com/parnurzeal/gorequest"
8+
"time"
89
)
910

1011
// NewRequest
1112
// 空白的请求工具 预留做后续的统一参数处理
1213
func NewRequest() *gorequest.SuperAgent {
1314
request := gorequest.New()
15+
request.Timeout(30 * time.Second)
1416
return request
1517
}
1618

api/engine.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
req: api.AgentRegisterReq
1414
*/
1515
func AgentRegister(req request.AgentRegisterReq) (AgentId int, err error) {
16+
fmt.Println("探针注册开始等待返回")
1617
resp, body, errs := POST("/api/v1/agent/register", req).End()
18+
fmt.Println("探针注册返回:", string(body))
1719
if len(errs) > 0 {
1820
for _, v := range errs {
1921
fmt.Println(v)
@@ -31,7 +33,7 @@ func AgentRegister(req request.AgentRegisterReq) (AgentId int, err error) {
3133
AgentId = res.Data.Id
3234
fmt.Println("注册成功,探针ID为", res.Data.Id)
3335
return AgentId, nil
34-
}
36+
}
3537
return 0, errors.New("注册失败,失败原因" + res.Msg)
3638

3739
}

core/base/execCmdRun/install.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

core/base/execCmdStart/install.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package execCmdStart
2+
3+
import (
4+
"fmt"
5+
"github.com/HXSecurity/DongTai-agent-go/model"
6+
"github.com/brahma-adshonor/gohook"
7+
"os/exec"
8+
)
9+
10+
func init() {
11+
model.HookMap["execCmdStart"] = new(ExecCmdStart)
12+
}
13+
14+
type ExecCmdStart struct {
15+
}
16+
17+
func (h *ExecCmdStart) Hook() {
18+
var cmd *exec.Cmd
19+
err := gohook.HookMethod(cmd, "Start", Start, StartT)
20+
if err != nil {
21+
fmt.Println(err, "execCmdStart")
22+
} else {
23+
fmt.Println("execCmdStart")
24+
}
25+
}
26+
27+
func (h *ExecCmdStart) UnHook() {
28+
gohook.UnHook(exec.Command)
29+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package execCmdRun
1+
package execCmdStart
22

33
import (
44
"github.com/HXSecurity/DongTai-agent-go/model/request"
55
"os/exec"
66
"reflect"
77
)
88

9-
func Run(cmd *exec.Cmd) error {
10-
e := RunT(cmd)
9+
func Start(cmd *exec.Cmd) error {
10+
e := StartT(cmd)
1111
//
1212
var u uintptr
1313
value := reflect.ValueOf(cmd)
@@ -18,12 +18,12 @@ func Run(cmd *exec.Cmd) error {
1818
NeedHook: request.Collect(u),
1919
Source: false,
2020
OriginClassName: "exec.(*Cmd)",
21-
MethodName: "Run",
21+
MethodName: "Start",
2222
ClassName: "exec.(*Cmd)",
2323
})
2424
return e
2525
}
2626

27-
func RunT(cmd *exec.Cmd) error {
27+
func StartT(cmd *exec.Cmd) error {
2828
return nil
2929
}

core/grpc/clientConn/replacement.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func Invoke(cl *grpc.ClientConn, ctx context.Context, method string, args, reply
6262
OriginClassName: "grpc.(*ClientConn)",
6363
MethodName: "Invoke",
6464
ClassName: "grpc.(*ClientConn)",
65+
TraceId: tranceid,
6566
})
6667
return err
6768
}

core/grpc/newServer/replacement.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"google.golang.org/grpc"
1212
"google.golang.org/grpc/metadata"
1313
"strconv"
14-
"strings"
1514
)
1615

1716
const (
@@ -36,17 +35,6 @@ func interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInf
3635
if len(dt) != 0 {
3736
Traceid = dt[0]
3837
}
39-
four := strconv.Itoa(int(worker.GetId()))
40-
tranceids := strings.Split(Traceid, ".")
41-
tranceids[OnlyKey] = four
42-
newId := ""
43-
for i := 0; i < len(tranceids); i++ {
44-
if i == OnlyKey {
45-
newId += tranceids[i]
46-
} else {
47-
newId += tranceids[i] + "."
48-
}
49-
}
5038

5139
id := utils.CatGoroutineID()
5240
request.FmtHookPool(request.PoolReq{
@@ -56,13 +44,14 @@ func interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInf
5644
OriginClassName: "grpc",
5745
MethodName: "NewServer",
5846
ClassName: "grpc",
47+
TraceId: Traceid,
5948
})
6049
// 获取metadata
6150
res, err := handler(ctx, req)
6251
go func() {
6352
worker, _ := utils.NewWorker(global.AgentId)
6453
onlyKey := int(worker.GetId())
65-
header := base64.StdEncoding.EncodeToString([]byte("dt-traceid:" + newId))
54+
header := base64.StdEncoding.EncodeToString([]byte("dt-traceid:" + Traceid))
6655
HookGroup := &request.UploadReq{
6756
Type: 36,
6857
InvokeId: onlyKey,
@@ -81,7 +70,7 @@ func interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInf
8170
ReqBody: "",
8271
QueryString: "",
8372
Pool: []request.Pool{},
84-
TraceId: newId,
73+
TraceId: Traceid,
8574
},
8675
},
8776
}

hook/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func (b *Base) GetHook() []string {
1212
"jsonNewDecoder",
1313
"runtimeConcatstrings",
1414
"execCommand",
15-
"execCmdRun",
15+
"execCmdStart",
1616
"bufioWriterWrite",
1717
"bufioWriterWriteString",
1818
"runtimesSringtoslicebyte",

model/request/engine.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ type PoolReq struct {
121121
NeedHook []interface{}
122122
NeedCatch []interface{}
123123
ArgsStr string
124+
TraceId string
124125
}
125126

126127
type Log struct {
@@ -161,9 +162,9 @@ func (p *PoolTree) IsThisBegin(GoroutineID string) bool {
161162

162163
func (p *PoolTree) FMT(pools *[]Pool, w *utils.Worker, goroutineIDs map[string]bool, TraceId string) {
163164
p.Pool.InvokeId = int(w.GetId())
164-
if(p.Pool.ClassName == "grpc.(*ClientConn)"){
165-
p.Pool.TraceId = TraceId
166-
}
165+
//if(p.Pool.ClassName == "grpc.(*ClientConn)"){
166+
// p.Pool.TraceId = TraceId
167+
//}
167168
*pools = append(*pools, *p.Pool)
168169
goroutineIDs[p.GoroutineID] = true
169170
fmt.Println(p.Pool.ClassName, p.Pool.MethodName)
@@ -249,6 +250,7 @@ func FmtHookPool(p PoolReq) Pool {
249250
CallerMethod: callerMethod,
250251
RetClassName: RetClassNames,
251252
Args: ArgsStr,
253+
TraceId: p.TraceId,
252254
}
253255

254256
poolTree := PoolTree{

run/base/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package base
33
import (
44
_ "github.com/HXSecurity/DongTai-agent-go/core/base/bufioWriterWrite"
55
_ "github.com/HXSecurity/DongTai-agent-go/core/base/bufioWriterWriteString"
6-
_ "github.com/HXSecurity/DongTai-agent-go/core/base/execCmdRun"
6+
_ "github.com/HXSecurity/DongTai-agent-go/core/base/execCmdStart"
77
_ "github.com/HXSecurity/DongTai-agent-go/core/base/execCommand"
88
_ "github.com/HXSecurity/DongTai-agent-go/core/base/fmtSprintf"
99
_ "github.com/HXSecurity/DongTai-agent-go/core/base/htmlTemplateExecuteTemplate"

0 commit comments

Comments
 (0)