-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
176 lines (138 loc) · 3.89 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"gopkg.in/ini.v1"
)
func checkResponse(httpResponse *http.Response, err error) {
if httpResponse != nil {
if httpResponse.StatusCode >= 400 {
body, respErr := ioutil.ReadAll(httpResponse.Body)
if respErr != nil {
log.Println("Error reading response body:", respErr)
}
log.Println("Response HTTP Status code: ", httpResponse.StatusCode)
log.Println("Response HTTP Body: ", string(body))
}
}
if err != nil {
log.Printf("Something went wrong sending your message: %s\n", err)
}
}
func main() {
opts := processArgs()
if opts.version {
fmt.Printf("Emissary version: %s\n", VERSION)
os.Exit(0)
}
if len(os.Args) <= 1 {
flag.Usage()
os.Exit(0)
}
var cfg *ini.File
if len(opts.channel) != 0 || opts.email {
User, err := user.Current()
if err != nil {
log.Fatal("Something went wrong trying to figure out your home directory", err)
}
configPath := filepath.FromSlash(User.HomeDir + "/.config/emissary.ini")
cfg, err = ini.Load(configPath)
if err != nil {
log.Fatal("Fail to read configuration file: ", err)
}
}
if len(opts.message) > 0 && opts.stdin {
fmt.Println("[-] Please take input from either STDIN or cli argument, not both.")
os.Exit(1)
}
if opts.message == "" && !opts.stdin {
fmt.Println("[-] You forgot to set message...")
os.Exit(1)
}
if len(opts.inline.hooks) > 0 {
for _, val := range opts.inline.hooks {
if val.webhook == "" {
fmt.Println("[-] Inline webhook does not contain webhook...")
os.Exit(1)
}
if val.textField == "" {
val.textField = "text"
}
json, err := PreparePayload(opts.message, val.textField, val.data)
if err != nil {
log.Printf("Could not prepare payload for webhook %s", val.webhook)
continue
}
resp, err := SendRequest(val.webhook, json)
checkResponse(resp, err)
}
}
messages := []string{opts.header + "\n--------"}
if opts.stdin {
count := 0
sc := bufio.NewScanner(os.Stdin)
msg := ""
for sc.Scan() {
msg = sc.Text()
if opts.rows == 0 {
messages = append(messages, msg)
} else {
if count < opts.rows {
messages = append(messages, msg)
} else {
break
}
}
count++
}
messages = append(messages, fmt.Sprintf("--------\nSent %d lines", count))
opts.message = strings.Join(messages[:], "\n")
}
if len(opts.channel) != 0 {
for _, ch := range opts.channel {
webhook := cfg.Section(ch).Key("webhook").String()
textField := cfg.Section(ch).Key("textField").MustString("text")
additionalData := cfg.Section(ch).Key("data").String()
if webhook == "" {
log.Printf("[-] Channel %s does not contain a webhook...", ch)
continue
}
// MS Teams hack for properly showing rows
if strings.HasPrefix(webhook, "https://outlook.office.com") {
split := strings.Split(opts.message, "\n")
newMessage := ""
for _, v := range split {
newMessage += v + "\n\n"
}
opts.message = newMessage
}
json, err := PreparePayload(opts.message, textField, additionalData)
if err != nil {
log.Printf("Could not prepare payload for channel %s", ch)
continue
}
resp, err := SendRequest(webhook, json)
checkResponse(resp, err)
}
}
if opts.email {
emailUsername := cfg.Section("Email").Key("username").String()
emailPassword := cfg.Section("Email").Key("password").String()
emailRecipient := cfg.Section("Email").Key("recipient").String()
emailPort := cfg.Section("Email").Key("port").String()
emailServer := cfg.Section("Email").Key("server").String()
emailSubject := cfg.Section("Email").Key("subject").String()
emailConfig := EmailConfig{username: emailUsername, password: emailPassword,
recipient: emailRecipient, port: emailPort, server: emailServer, subject: emailSubject,
message: opts.message}
checkResponse(nil, Email(emailConfig))
}
}