-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.go
262 lines (221 loc) · 6.23 KB
/
job.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*!*
* Copyright (c) 2022-2025 Hangzhou Guanwaii Technology Co,.Ltd.
*
* This source code is licensed under the MIT License,
* which is located in the LICENSE file in the source tree's root directory.
*
* File: job.go
* Author: mingcheng ([email protected])
* File Created: 2022-07-22 23:37:43
*
* Modified By: mingcheng ([email protected])
* Last Modified: 2025-02-28 10:46:31
*/
package simplyddns
import (
"context"
"fmt"
"github.com/go-resty/resty/v2"
"net"
"net/http"
"strings"
"time"
)
// SourceFunc to define the source function
type SourceConfig struct {
Interval uint `yaml:"interval,omitempty"`
Type string `yaml:"type,omitempty"`
Path string `yaml:"path"`
Content string `yaml:"content"`
}
// TargetFunc to define the target function
type TargetConfig struct {
Type string `yaml:"type,omitempty"`
Key string `yaml:"key,omitempty"`
Token string `yaml:"token"`
Proxy string `yaml:"proxy"`
Domains []string `yaml:"domains,omitempty"`
}
// WebHook to define the webhook
type WebHook struct {
Url string `yaml:"url" mapstructure:"url"`
Token string `yaml:"token" mapstructure:"token"`
UserName string `yaml:"token" mapstructure:"username"`
Password string `yaml:"token" mapstructure:"password"`
}
// JobConfig to define the job configure
type JobConfig struct {
WebHook WebHook `yaml:"webhook" mapstructure:"webhook"`
Source SourceConfig `yaml:"source,omitempty" mapstructure:"source"`
Target TargetConfig `yaml:"target,omitempty" mapstructure:"target"`
}
// Job to define the job
type Job struct {
Config *JobConfig
SourceFunc []SourceFunc
TargetFunc TargetFunc
ticker *time.Ticker
done chan bool
lastIP *net.IP
}
// RunWebhook to run the webhook when ip address has updated
func (j *Job) RunWebhook(ctx context.Context, addr string, domains []string) (err error) {
client := resty.New()
request := client.R().
SetContext(ctx).
SetHeader("Address", addr).
SetHeader("Domains", strings.Join(domains, ",")).
SetBody(map[string]interface{}{
"address": addr,
"domains": strings.Join(domains, ","),
"now": time.Now(),
}).
SetError(&err)
if token := j.Config.WebHook.Token; token != "" {
request.SetAuthToken(token)
}
if username := j.Config.WebHook.UserName; username != "" {
request.SetBasicAuth(username, j.Config.WebHook.Password)
}
var resp *resty.Response
resp, err = request.Post(j.Config.WebHook.Url)
if resp.StatusCode() != http.StatusOK {
err = fmt.Errorf("%v", resp.Status())
}
return err
}
// Start to start a job
func (j *Job) Start(ctx context.Context) {
go func() {
var (
err error
addr *net.IP
job = j
)
for ; true; <-job.ticker.C {
var config = job.Config
// check configure
if err = ValidateConfig(config); err != nil {
log.Errorf("validate job configure is fail, %v", err)
continue
}
// run source function
if addr, err = job.Source(ctx, &config.Source); err != nil || addr == nil || addr.String() == "" {
log.Error(err)
continue
}
// markup the source func result
log.Debugf("get address from source fun %s, value is %s", config.Source.Type, addr.String())
// ignore the same ip address
if job.lastIP != nil && job.lastIP.Equal(*addr) {
log.Warnf("ignore the cached address %s", addr.String())
continue
}
domains := config.Target.Domains
if len(domains) > 0 {
if err = ValidateRecords(domains, addr); err == nil {
log.Errorf("valdate dns record without error, maybe already setted %s", addr.String())
continue
}
}
// run the target func
err = job.TargetFunc(ctx, addr, &job.Config.Target)
if err != nil {
log.Warn(err)
continue
}
log.Infof("run target function is successful, please check")
// cache the last ip address
job.lastIP = addr
// trigger the webhook if configured
if config.WebHook.Url != "" {
log.Tracef("the webhook url is %s", config.WebHook.Url)
if err = job.RunWebhook(ctx, addr.String(), domains); err != nil {
log.Warnf("run webhook with error %s", err.Error())
} else {
log.Infof("run webhook %s is finished", config.WebHook.Url)
}
}
}
}()
select {
case <-j.done:
case <-ctx.Done():
j.ticker.Stop()
return
}
}
// Stop to stop a job
func (j *Job) Stop() {
log.Debug("stopping job")
j.done <- true
}
// Source to execute multi-source function
func (j Job) Source(ctx context.Context, config *SourceConfig) (*net.IP, error) {
if j.SourceFunc == nil || len(j.SourceFunc) == 0 {
return nil, fmt.Errorf("source functions is empty")
}
var (
err error
errTimes int
lastAddr *net.IP
)
for _, v := range j.SourceFunc {
var addr *net.IP
addr, err = v(ctx, config)
if err != nil {
log.Error(err, errTimes)
errTimes = errTimes + 1
}
if addr != nil {
if lastAddr != nil && !addr.Equal(*lastAddr) {
return nil, fmt.Errorf("fetch address is not the same, %v vs %v", lastAddr, addr)
}
lastAddr = addr
}
}
if errTimes > 0 && len(sourceFuncs) > 3 && errTimes >= len(j.SourceFunc)/2 {
return nil, fmt.Errorf("max error times reached(%d), so the result is not right", errTimes)
}
return lastAddr, nil
}
// NewJob for instance a new ddns job
func NewJob(config JobConfig) (job *Job, err error) {
// check the configure
if config.Source.Type == "" || config.Target.Type == "" {
err = fmt.Errorf("source or target type can not be empty")
return
}
if config.Source.Interval <= 0 {
err = fmt.Errorf("source check interval can not below zero or empty")
return
}
// split fn types as array
types := strings.Split(config.Source.Type, ",")
if len(types) == 0 {
err = fmt.Errorf("load source %s is empty", config.Source.Type)
return
}
// notice: the source functions is an array
var sourceFuncs []SourceFunc
for _, v := range types {
var fn SourceFunc
fn, err = SourceFuncByName(strings.ToLower(v))
if err != nil {
return
}
// add func to source functions
sourceFuncs = append(sourceFuncs, fn)
}
fnTarget, err := TargetFuncByName(config.Target.Type)
if err != nil {
return nil, err
}
return &Job{
SourceFunc: sourceFuncs,
TargetFunc: fnTarget,
Config: &config,
ticker: time.NewTicker(time.Second * time.Duration(config.Source.Interval)),
done: make(chan bool),
}, nil
}