-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpl_utils.go
More file actions
151 lines (128 loc) · 3.83 KB
/
pl_utils.go
File metadata and controls
151 lines (128 loc) · 3.83 KB
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
package main
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
)
const (
HookPre = 0
HookPost = 1
)
var NotifyEventTypes = []string{
"agent.new",
"agent.activate",
"agent.terminate",
"agent.remove",
"listener.start",
"listener.stop",
"task.complete",
"download.finish",
"tunnel.start",
"tunnel.stop",
"credentials.add",
"target.add",
}
const BatchThreshold = 3
type EventConfig struct {
Enabled bool `yaml:"enabled" json:"enabled"`
Template string `yaml:"template" json:"template"`
}
type TelegramService struct {
Enabled bool `yaml:"enabled" json:"enabled"`
BotToken string `yaml:"bot_token" json:"bot_token"`
ChatIDs []string `yaml:"chat_ids" json:"chat_ids"`
}
type SlackService struct {
Enabled bool `yaml:"enabled" json:"enabled"`
WebhookURL string `yaml:"webhook_url" json:"webhook_url"`
}
type DiscordService struct {
Enabled bool `yaml:"enabled" json:"enabled"`
WebhookURL string `yaml:"webhook_url" json:"webhook_url"`
}
type RocketChatService struct {
Enabled bool `yaml:"enabled" json:"enabled"`
WebhookURL string `yaml:"webhook_url" json:"webhook_url"`
}
type WebhookService struct {
Enabled bool `yaml:"enabled" json:"enabled"`
URL string `yaml:"url" json:"url"`
Method string `yaml:"method" json:"method"`
Headers map[string]string `yaml:"headers" json:"headers"`
Data string `yaml:"data" json:"data"`
}
type ServicesConfig struct {
Telegram TelegramService `yaml:"telegram" json:"telegram"`
Slack SlackService `yaml:"slack" json:"slack"`
Discord DiscordService `yaml:"discord" json:"discord"`
RocketChat RocketChatService `yaml:"rocketchat" json:"rocketchat"`
Webhook WebhookService `yaml:"webhook" json:"webhook"`
}
type NotifierConfig struct {
RestoreDatabase bool `yaml:"restore_database" json:"restore_database"`
Enabled bool `yaml:"enabled" json:"enabled"`
Services ServicesConfig `yaml:"services" json:"services"`
Events map[string]EventConfig `yaml:"events" json:"events"`
}
type ConfigResponse struct {
Action string `json:"action"`
Enabled bool `json:"enabled"`
Services ServicesConfig `json:"services"`
Events map[string]EventConfig `json:"events"`
}
type ResultResponse struct {
Action string `json:"action"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
type Teamserver interface {
TsEventHookRegister(eventType string, name string, phase int, priority int, handler func(event any) error) string
TsEventHookUnregisterByName(name string) int
TsServiceSendDataAll(service string, data string)
TsServiceSendDataClient(operator string, service string, data string)
TsExtenderDataSave(extenderName string, key string, value []byte) error
TsExtenderDataLoad(extenderName string, key string) ([]byte, error)
}
func loadConfig(serviceConfig string) error {
if serviceConfig == "" {
return fmt.Errorf("empty service config")
}
err := yaml.Unmarshal([]byte(serviceConfig), &Config)
if err != nil {
return err
}
if Config.RestoreDatabase {
data, err := Ts.TsExtenderDataLoad("notifier", "config")
if err == nil && data != nil {
var dbConfig NotifierConfig
if json.Unmarshal(data, &dbConfig) == nil {
Config = dbConfig
fmt.Println("[Notifier] Config restored from database")
return nil
}
}
fmt.Println("[Notifier] No saved config in database, using service config")
}
return nil
}
func saveConfig() error {
data, err := json.Marshal(&Config)
if err != nil {
return err
}
return Ts.TsExtenderDataSave("notifier", "config", data)
}
//////////
func maskString(s string) string {
if len(s) == 0 {
return "*"
}
if len(s) <= 2 {
return s
}
rs := []rune(s)
for i := 1; i < len(rs)-1; i++ {
rs[i] = '*'
}
return string(rs)
}