forked from gaulliath/operative-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
145 lines (124 loc) · 3.47 KB
/
cron.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
// AUTHOR : TRISTAN GRANIER
// RESUME : Search matching element to pastebin
// TIME : Every 24 hours
package pastebin_cron
import (
"bytes"
"encoding/json"
"log"
"net/http"
"strings"
"time"
"github.com/Oni-kuki/operative-framework/session"
"github.com/joho/godotenv"
"github.com/pkg/errors"
)
type Service struct {
session.CronJob
session *session.Session
}
func GetNewService(sess *session.Session) *Service {
return &Service{
session: sess,
}
}
// Service as been started every 24 hours
func (service *Service) GetHibernate() time.Duration {
return 24 * time.Hour
}
// Service name as been set here
func (service *Service) Name() string {
return "pastebin.cron"
}
// Define if service need configuration file
func (service *Service) HasConfiguration() bool {
return true
}
// Get configuration
func (service *Service) GetConfiguration() map[string]string {
configuration := make(map[string]string)
configuration["MATCH"] = "example@gmail.com,example2@gmail.com"
configuration["TO_SERVER"] = "false"
configuration["SERVER_URI"] = "http://example.com/api/v1.0/insert"
configuration["VERBOSE"] = "true"
return configuration
}
// Get required fields in configuration file
func (service *Service) GetRequired() []string {
return []string{
"MATCH",
"TO_SERVER",
"VERBOSE",
}
}
// Fetching matching to pastebin
func (service *Service) Fetch(configuration map[string]string, match string) (bool, error) {
module, err := service.session.SearchModule("pastebin.search")
if err != nil {
return false, err
}
targetId, err := service.session.AddTarget("text", match)
if err != nil {
if !strings.Contains(err.Error(), "already exist") || targetId == "" {
return false, err
}
}
_, err = module.SetParameter("TARGET", targetId)
if err != nil {
return false, err
}
_, err = module.SetParameter("LIMIT", "10")
if err != nil {
return false, err
}
module.Start()
target, err := service.session.GetTarget(targetId)
if err != nil {
return false, err
}
results, err := target.GetFormatedResults("pastebin.search")
js, err := json.Marshal(&results)
if err != nil {
return false, err
}
if strings.ToLower(configuration["TO_SERVER"]) == "true" {
if strings.ToLower(configuration["VERBOSE"]) == "true" {
log.Println("Prepare request '" + match + "' at '" + configuration["SERVER_URI"] + "'")
}
req, err := http.NewRequest("POST", configuration["SERVER_URI"], bytes.NewBuffer(js))
if err != nil {
return false, errors.New("Can't make request to '" + configuration["SERVER_URI"] + "'")
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
if strings.ToLower(configuration["VERBOSE"]) == "true" {
log.Println("Request '" + match + "' as been sent at '" + configuration["SERVER_URI"] + "'")
}
}
return true, nil
}
// Service Execution with opf routine
func (service *Service) Run() (bool, error) {
configuration, _ := godotenv.Read(service.session.Config.Common.ConfigurationJobs + service.Name() + "/cron.conf")
service.session.Stream.Verbose = false
if strings.Contains(configuration["MATCH"], ",") {
matches := strings.Split(configuration["MATCH"], ",")
for _, match := range matches {
ret, err := service.Fetch(configuration, match)
if err != nil {
return ret, err
}
}
} else {
ret, err := service.Fetch(configuration, configuration["MATCH"])
if err != nil {
return ret, err
}
}
return true, nil
}