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
|
package monitor
import (
"crypto/tls"
"fmt"
"log"
"net"
"net/mail"
"net/smtp"
"strings"
"github.com/ethereum/go-ethereum/common"
)
const (
// FINED notes the node is fined
FINED uint = iota
// INSUFFICIENT_TAN notes the node key with balance < 100 TAN
INSUFFICIENT_TAN
// INSUFFICIENT_ETH notes the node key with balance < threshold
INSUFFICIENT_ETH
)
// Notifier is the interface for notifier interfaces.
type Notifier interface {
notify(node, string, uint, string)
}
// Email is the email notifier object.
type Email struct {
sender string
password string
smtpServer string
ccList string
skipList []string
}
// NewEmail is the Email constructor.
func NewEmail(sender, password, server, ccList string, skipList []string) Notifier {
e := Email{
sender: sender,
password: password,
smtpServer: server,
ccList: ccList,
skipList: skipList,
}
return &e
}
func (e *Email) notify(n node, network string, notifyType uint, threshold string) {
var subj string
var body string
for _, skip := range e.skipList {
if(skip == n.email) {
return
}
}
switch notifyType {
case FINED:
subj = "[Notification] Your Tangerine " + network + " Network Full Node is Fined"
body = e.finedBody(n.name)
go e.send(n.email, network, subj, body)
case INSUFFICIENT_TAN:
subj = "[Notification] Tangerine " + network + " Insufficient Balance in Node Key"
body = e.insufficientBalanceBody(n.name, n.nodeKeyAddress)
go e.send(n.email, network, subj, body)
case INSUFFICIENT_ETH:
subj = "[Notification] Tangerine " + network + " Insufficient ETH in Node Key"
body = e.insufficientETHBalanceBody(n.name,
n.nodeKeyAddress, threshold, network)
go e.send(n.email, network, subj, body)
}
}
func (e *Email) finedBody(name string) string {
body := `
Dear Node Operator:
Your full node "` + name + `" is fined.
Please pay the fine, and check your node status.
Also, please check https://tangerine-network.github.io/wiki/#/Rules-for-the-node-set?id=penalty for the penalty condition.
Thanks for your cooperation.
-- Tangerine Network
`
return body
}
func (e *Email) insufficientBalanceBody(name string, address common.Address) string {
body := `
Dear Node Operator:
There is no sufficient balacne in your full node "` + name + `" key address "` + address.Hex() + `".
Please transfer some tokens to the node key address and keep the balance at least 200 TAN.
Thanks for your cooperation.
-- Tangerine Network
`
return body
}
func (e *Email) insufficientETHBalanceBody(
name string,
address common.Address,
threshold string,
network string) string {
ethNet := "Rinkeby"
if network == "Mainnet" {
ethNet = "Mainnet"
}
body := `
Dear Node Operator:
There is no sufficient ETH in your full node "` + name + `" key address "` + address.Hex() + `".
Please transfer some ETH (` + ethNet + `) to the node key address and keep the balance at least ` + threshold + ` ETH.
Thanks for your cooperation.
-- Tangerine Network
`
return body
}
func (e *Email) send(toAddress, network, subj, body string) {
from := mail.Address{Name: "Tangerine Notifier", Address: e.sender}
to := mail.Address{Address: toAddress}
// Setup headers
headers := make(map[string]string)
headers["From"] = from.String()
headers["To"] = to.String()
headers["Subject"] = subj
if e.ccList != "" {
headers["Cc"] = strings.TrimSuffix(e.ccList, "\n")
}
// Setup message
message := ""
for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + body
// Connect to the SMTP Server
servername := e.smtpServer + ":465"
host, _, _ := net.SplitHostPort(servername)
auth := smtp.PlainAuth("", e.sender, e.password, host)
// TLS config
tlsconfig := &tls.Config{
InsecureSkipVerify: false,
ServerName: host,
}
// Here is the key, you need to call tls.Dial instead of smtp.Dial
// for smtp servers running on 465 that require an ssl connection
// from the very beginning (no starttls)
conn, err := tls.Dial("tcp", servername, tlsconfig)
if err != nil {
log.Panic(err)
}
c, err := smtp.NewClient(conn, host)
if err != nil {
log.Panic(err)
}
// Auth
if err = c.Auth(auth); err != nil {
log.Panic(err)
}
// To && From
if err = c.Mail(from.Address); err != nil {
log.Panic(err)
}
if err = c.Rcpt(to.Address); err != nil {
log.Panic(err)
}
if e.ccList != "" {
cc := strings.Split(e.ccList, ",")
for _, address := range cc {
address = strings.TrimSuffix(address, "\n")
if err = c.Rcpt(address); err != nil {
log.Println("add cc fail", address, err)
}
}
}
// Data
w, err := c.Data()
if err != nil {
log.Panic(err)
}
_, err = w.Write([]byte(message))
if err != nil {
log.Panic(err)
}
err = w.Close()
if err != nil {
log.Panic(err)
}
c.Quit()
}
|