-
Notifications
You must be signed in to change notification settings - Fork 34
/
addAgent.go
74 lines (63 loc) · 1.81 KB
/
addAgent.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
package agents
import (
"github.com/TeaWeb/code/teaconfigs/agents"
"github.com/TeaWeb/code/teaweb/actions/default/agents/agentutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/utils/string"
)
type AddAgentAction actions.Action
// 添加代理
func (this *AddAgentAction) Run(params struct{}) {
this.Data["defaultGroupName"] = agents.SharedGroupList().FindDefaultGroup().Name
this.Data["groups"] = agents.SharedGroupList().Groups
this.Show()
}
// 提价保存
func (this *AddAgentAction) RunPost(params struct {
Name string
Host string
GroupId string
AllowAllIP bool
IPs []string `alias:"ips"`
On bool
CheckDisconnections bool
AutoUpdates bool
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入主机名").
Field("host", params.Host).
Require("请输入主机地址")
agentList, err := agents.SharedAgentList()
if err != nil {
this.Fail("保存失败:" + err.Error())
}
agent := agents.NewAgentConfig()
agent.On = params.On
agent.Name = params.Name
agent.Host = params.Host
if len(params.GroupId) > 0 {
agent.AddGroup(params.GroupId)
}
agent.AllowAll = params.AllowAllIP
agent.Allow = params.IPs
agent.Key = stringutil.Rand(32)
agent.CheckDisconnections = params.CheckDisconnections
agent.AutoUpdates = params.AutoUpdates
agent.AddDefaultApps()
err = agent.Save()
if err != nil {
this.Fail("保存失败:" + err.Error())
}
agentList.AddAgent(agent.Filename())
err = agentList.Save()
if err != nil {
this.Fail("保存失败:" + err.Error())
}
this.Data["agentId"] = agent.Id
// 通知更新
agentutils.PostAgentEvent(agent.Id, agentutils.NewAgentEvent("ADD_AGENT", maps.Map{}))
this.Success()
}