/
exec.go
234 lines (203 loc) · 5.07 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package yaklib
import (
"context"
"errors"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/utils"
"os/exec"
"runtime"
"syscall"
"time"
"github.com/google/shlex"
)
func _execStringToCommand(ctx context.Context, s string) (*exec.Cmd, error) {
cmds, err := shlex.Split(s)
if err != nil {
return nil, utils.Errorf("parse string to cmd args failed: %s, reason: %v", s, err)
}
if cmds == nil {
return nil, utils.Errorf("error system cmd: %v", s)
}
if len(cmds) > 1 {
return exec.CommandContext(ctx, cmds[0], cmds[1:]...), nil
} else {
return exec.CommandContext(ctx, cmds[0]), nil
}
}
// 执行系统命令
func _execSystem(ctx context.Context, i string) ([]byte, error) {
s, err := _execStringToCommand(ctx, i)
if err != nil {
return nil, err
}
raw, err := s.CombinedOutput()
if err != nil {
return nil, utils.Errorf("system[%v] failed: %v", i, err)
}
return raw, err
}
func _execSystemBatch(i string, opts ...poolOpt) {
config := &_execPoolConfig{
concurrent: 20,
}
for _, opt := range opts {
opt(config)
}
if config.concurrent <= 0 {
config.concurrent = 20
}
swg := utils.NewSizedWaitGroup(config.concurrent)
defer swg.Wait()
for _, cmdRaw := range _fuzz(i) {
cmdRaw := cmdRaw
swg.Add()
go func() {
defer swg.Done()
log.Infof("start exec: %v", cmdRaw)
ctx := context.Background()
if config.timeout > 0 {
ctx = utils.TimeoutContext(config.timeout)
}
raw, err := _execSystem(ctx, cmdRaw)
if err != nil {
log.Infof("exec[%v] failed: %v", cmdRaw, err)
return
}
if config.callback != nil {
config.callback(cmdRaw, raw)
}
}()
}
}
func _checkExecCrash(c *exec.Cmd) (bool, error) {
sysType := runtime.GOOS
if sysType == "windows" {
return true, errors.New("Unspport Windows now")
}
processState := c.ProcessState
status := processState.Sys().(syscall.WaitStatus)
if status.Signaled() {
signal := status.Signal()
if signal == syscall.SIGSEGV || signal == syscall.SIGABRT {
return true, nil
}
}
if status.Stopped() {
signal := status.StopSignal()
if signal == syscall.SIGSEGV || signal == syscall.SIGABRT {
return true, nil
}
}
return false, nil
}
// ////////////////////////////////////////////////////
type _execPoolConfig struct {
concurrent int
timeout time.Duration
callback func(cmd string, results []byte)
}
type poolOpt func(c *_execPoolConfig)
func _execConcurrent(i int) poolOpt {
return func(c *_execPoolConfig) {
c.concurrent = i
}
}
func _execTimeout(i float64) poolOpt {
return func(c *_execPoolConfig) {
c.timeout = utils.FloatSecondDuration(i)
}
}
func _execSetCallback(f func(string, []byte)) poolOpt {
return func(c *_execPoolConfig) {
c.callback = f
}
}
func _execWatchStdout(i string, timeout float64, f func(raw []byte) bool) error {
defer func() {
if err := recover(); err != nil {
log.Error(err)
}
}()
rootCtx, cancel := context.WithCancel(utils.TimeoutContext(utils.FloatSecondDuration(timeout)))
cmd, err := _execStringToCommand(rootCtx, i)
if err != nil {
return utils.Errorf("create system command[%v] failed: %v", i, err)
}
reader, err := cmd.StdoutPipe()
if err != nil {
return utils.Errorf("create[%v] stdout pipe failed: %s", i, err)
}
go func() {
defer func() {
if err := recover(); err != nil {
log.Error(err)
}
}()
defer cancel()
utils.ReadWithContextTickCallback(
context.Background(), reader, f, time.Second,
)
}()
err = cmd.Run()
if err != nil {
return utils.Errorf("exec %v failed: %s", i, err.Error())
}
return nil
}
func _execWatchStderr(i string, timeout float64, f func(raw []byte) bool) error {
defer func() {
if err := recover(); err != nil {
log.Error(err)
}
}()
rootCtx, cancel := context.WithCancel(utils.TimeoutContext(utils.FloatSecondDuration(timeout)))
_ = cancel
cmd, err := _execStringToCommand(rootCtx, i)
if err != nil {
return utils.Errorf("create system command[%v] failed: %v", i, err)
}
reader, err := cmd.StderrPipe()
if err != nil {
return utils.Errorf("create[%v] stderr pipe failed: %s", i, err)
}
go func() {
defer func() {
if err := recover(); err != nil {
log.Error(err)
}
}()
defer cancel()
utils.ReadWithContextTickCallback(
context.Background(), reader, f, time.Second,
)
}()
err = cmd.Run()
if err != nil {
return utils.Errorf("exec %v failed: %s", i, err.Error())
}
return nil
}
// 系统命令执行导出接口
var ExecExports = map[string]interface{}{
"CommandContext": _execStringToCommand,
"Command": func(i string) (*exec.Cmd, error) {
return _execStringToCommand(context.Background(), i)
},
//检查是否crash
"CheckCrash": _checkExecCrash,
// 批量命令执行
"SystemBatch": _execSystemBatch,
// 带上下文的基础命令执行
"SystemContext": _execSystem,
// 基础命令执行
"System": func(i string) ([]byte, error) {
return _execSystem(context.Background(), i)
},
// 监控输出
"WatchStdout": _execWatchStdout,
"WatchOutput": _execWatchStdout,
"WatchStderr": _execWatchStderr,
"timeout": _execTimeout,
"callback": _execSetCallback,
"concurrent": _execConcurrent,
}