-
Notifications
You must be signed in to change notification settings - Fork 31
/
action_script.go
68 lines (58 loc) · 1.68 KB
/
action_script.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
package iplibrary
import (
"errors"
"fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
"path/filepath"
"time"
)
// ScriptAction 脚本命令动作
type ScriptAction struct {
BaseAction
config *firewallconfigs.FirewallActionScriptConfig
}
func NewScriptAction() *ScriptAction {
return &ScriptAction{}
}
func (this *ScriptAction) Init(config *firewallconfigs.FirewallActionConfig) error {
this.config = &firewallconfigs.FirewallActionScriptConfig{}
err := this.convertParams(config.Params, this.config)
if err != nil {
return err
}
if len(this.config.Path) == 0 {
return NewFataError("'path' should not be empty")
}
return nil
}
func (this *ScriptAction) AddItem(listType IPListType, item *pb.IPItem) error {
return this.runAction("addItem", listType, item)
}
func (this *ScriptAction) DeleteItem(listType IPListType, item *pb.IPItem) error {
return this.runAction("deleteItem", listType, item)
}
func (this *ScriptAction) runAction(action string, listType IPListType, item *pb.IPItem) error {
// TODO 智能支持 .sh 脚本文件
var cmd = executils.NewTimeoutCmd(30*time.Second, this.config.Path)
cmd.WithEnv([]string{
"ACTION=" + action,
"TYPE=" + item.Type,
"IP_FROM=" + item.IpFrom,
"IP_TO=" + item.IpTo,
"EXPIRED_AT=" + fmt.Sprintf("%d", item.ExpiredAt),
"LIST_TYPE=" + listType,
})
if len(this.config.Cwd) > 0 {
cmd.WithDir(this.config.Cwd)
} else {
cmd.WithDir(filepath.Dir(this.config.Path))
}
cmd.WithStderr()
err := cmd.Run()
if err != nil {
return errors.New(err.Error() + ", output: " + cmd.Stderr())
}
return nil
}