-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
321 lines (272 loc) · 8.06 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"database/sql"
"fmt"
"html"
"log"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
var versionString = "v1.2.0"
var db *sql.DB
var dbErr error
var myConfig conf
var minerList = make(map[string]map[string]mineRpc)
var progStartTime = time.Now()
var mutex = &sync.Mutex{}
var totalHashG = make(map[string]string)
var currentPricePerDMO = 0.0
func main() {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
store := cookie.NewStore([]byte("secret"))
store.Options(sessions.Options{MaxAge: 60 * 60 * 24 * 7}) // expire in 7 days
router.Use(sessions.Sessions("mysession", store))
router.Use(sessionMgr())
myConfig.getConf()
fmt.Printf("Access stats at http://localhost:%s/stats\n", myConfig.ServerPort)
db, dbErr = sql.Open("mysql", myConfig.DBUser+":"+myConfig.DBPass+"@tcp("+myConfig.DBIP+":"+myConfig.DBPort+")/"+myConfig.DBName)
// Truly a fatal error.
if dbErr != nil {
log.Printf("Unable to connect to DB: %s", dbErr.Error())
os.Exit(1)
}
defer db.Close()
fmt.Printf("Connected to DB: %s\n", myConfig.DBName)
getAllUserInfo()
if myConfig.MinerLateTime < 20 {
myConfig.MinerLateTime = 20
}
if myConfig.DailyStatDays < 2 {
myConfig.DailyStatDays = 3
}
if myConfig.DailyStatDays > 21 {
myConfig.DailyStatDays = 21
}
if myConfig.AutoRefreshSeconds < 10 && myConfig.AutoRefreshSeconds > 0 {
myConfig.AutoRefreshSeconds = 10
}
getCoinGeckoDMOPrice()
txStats()
updateMinerStatus()
go func() {
for {
getCoinGeckoDMOPrice()
time.Sleep(240 * time.Second)
}
}()
go func() {
for {
txStats()
time.Sleep(60 * time.Second)
}
}()
go func() {
for {
updateMinerStatus()
time.Sleep(10 * time.Second)
}
}()
router.Static("/static", "./static")
router.LoadHTMLGlob("templates/*.html")
router.GET("/", addPageVars(), landingPage)
router.GET("/dmowrapversioncheck", getDMOWrapVersion)
router.GET("/wrapminer", addPageVars(), wrapMiner)
router.GET("/faq", addPageVars(), faqPage)
router.GET("/stats", checkLoggedIn(), addPageVars(), statsPage)
router.GET("/account", checkLoggedIn(), addPageVars(), accountPage)
router.GET("/admin", checkLoggedIn(), addPageVars(), adminPage)
router.POST("/changepass", checkLoggedIn(), addPageVars(), doChangePass)
router.POST("/minerstats", checkBearer(), getMinerStatsRPC)
router.POST("/removeminer", checkLoggedIn(), addPageVars(), removeLateMiner)
router.GET("/login", addPageVars(), loginPage)
router.POST("/login", addPageVars(), doLogin)
router.POST("/register", addPageVars(), doRegister)
router.POST("/doupdatetelegramid", checkLoggedIn(), addPageVars(), doUpdateTelegramID)
router.POST("/doupdateaddrs", checkLoggedIn(), addPageVars(), doUpdateAddrs)
router.GET("/logout", checkLoggedIn(), addPageVars(), doLogout)
log.Printf("Starting server!\n")
router.Run(":" + myConfig.ServerPort)
}
func checkBearer() gin.HandlerFunc {
return func(c *gin.Context) {
auth := c.Request.Header.Get("Authorization")
if auth == "" {
log.Printf("Attempt to access Bearer auth path with no token\n")
c.String(http.StatusForbidden, "No Authorization header provided")
c.Abort()
return
} else {
token := strings.TrimPrefix(auth, "Bearer ")
_, ok := cloudKeyList[token]
if !ok {
log.Printf("Bearer token provided is not authorized: %s\n", token)
c.String(http.StatusForbidden, "Invalid Token")
c.Abort()
return
}
c.Set("cloudKey", token)
}
c.Next()
}
}
func sessionMgr() gin.HandlerFunc {
return func(c *gin.Context) {
// Get a new cookie or set one if it does not exist:
session := sessions.Default(c)
id := session.Get("ID")
guest := session.Get("guest")
if id == nil {
session.Set("ID", 0)
}
if guest == nil {
session.Set("guest", true)
}
session.Save()
c.Next()
}
}
func addPageVars() gin.HandlerFunc {
return func(c *gin.Context) {
var pVars pageVars
session := sessions.Default(c)
userID := session.Get("ID").(int)
pVars.UserID = userID
pVars.UserName = userIDList[userID].UserName
pVars.CloudKey = html.EscapeString(userIDList[userID].CloudKey)
pVars.TelegramUserID = userIDList[userID].TelegramUserId
pVars.Guest = session.Get("guest").(bool)
pVars.Admin = userIDList[userID].Admin
pVars.Paid = userIDList[userID].Paid
c.Set("pVars", pVars)
c.Next()
}
}
func checkLoggedIn() gin.HandlerFunc {
return func(c *gin.Context) {
// Get a new cookie or set one if it does not exist:
session := sessions.Default(c)
id := session.Get("ID")
guest := session.Get("guest")
if id != nil && id.(int) != 0 {
if guest != nil && !guest.(bool) {
c.Next()
}
}
var userID = id.(int)
mutex.Lock()
lastActive[userID] = time.Now().Unix()
mutex.Unlock()
updateUserLastActive(userID)
c.Redirect(http.StatusTemporaryRedirect, "/")
}
}
var hourStrings = map[int]string{
0: "12 AM",
1: "1 AM",
2: "2 AM",
3: "3 AM",
4: "4 AM",
5: "5 AM",
6: "6 AM",
7: "7 AM",
8: "8 AM",
9: "9 AM",
10: "10 AM",
11: "11 AM",
12: "12 PM",
13: "1 PM",
14: "2 PM",
15: "3 PM",
16: "4 PM",
17: "5 PM",
18: "6 PM",
19: "7 PM",
20: "8 PM",
21: "9 PM",
22: "10 PM",
23: "11 PM",
}
func updateMinerStatus() {
for userID, stats := range addrStats {
var thisInfo OverallInfoTX
nDays := len(stats.DailyStats)
thisInfo.CurrentCoinsPerDay = stats.DailyStats[nDays-2].Coins
thisInfo.Projection = fmt.Sprintf("%.1f", stats.ProjectedCoinsToday)
thisInfo.NetHash = formatHashNum(int(stats.NetHash))
// Daily Average is the Average of the average of all days except today...
tmpCoins := 0.0
tmpPerc := 0.0
for i := 0; i < nDays; i++ {
var thisDay DayStatTX
thisDay.Day = stats.DailyStats[i].Day
thisDay.CoinCount = stats.DailyStats[i].Coins
thisDay.WinPercent = stats.DailyStats[i].WinPercent
thisDay.CoinsPerHour = thisDay.CoinCount / 24.0
if i < (nDays - 1) {
tmpCoins += stats.DailyStats[i].Coins
tmpPerc += stats.DailyStats[i].WinPercent
} else {
thisDay.CoinsPerHour = stats.ProjectedCoinsToday / 24.0
}
thisInfo.DayStats = append(thisInfo.DayStats, thisDay)
}
thisInfo.DailyAverage = tmpCoins / (float64(nDays) - 1.0)
thisInfo.HourlyAverage = thisInfo.DailyAverage / 24.0
thisInfo.WinPercent = tmpPerc / (float64(nDays) - 1.0)
nHours := len(stats.HourlyStats)
thisMinute := float64(time.Now().Minute() + 1)
for j := 0; j < nHours; j++ {
var thisHour HourStatTX
thisHour.Hour = stats.HourlyStats[j].Hour
thisHour.HourStr = hourStrings[thisHour.Hour]
thisHour.CoinCount = float64(stats.HourlyStats[j].Coins)
if j < (nHours - 1) {
thisHour.CoinsPerMinute = float64(stats.HourlyStats[j].Coins) * (1.0 / 60.0)
} else {
thisHour.CoinsPerMinute = float64(stats.HourlyStats[j].Coins) * (1.0 / thisMinute)
}
thisInfo.HourStats = append(thisInfo.HourStats, thisHour)
}
mutex.Lock()
overallInfoTX[userID] = thisInfo
mutex.Unlock()
}
mutex.Lock() // For now... lock for the whole time we are reading and writing back to minerList...
if len(minerList) > 0 {
for cloudKey, myMinerList := range minerList {
totalHash := 0
totalActiveMiners := 0
for minerID, stats := range myMinerList {
howLong := time.Since(stats.LastReport).Round(time.Second)
stats.HowLate = howLong.String()
myMinerList[minerID] = stats
if howLong.Seconds() > myConfig.MinerLateTime && !stats.Late {
stats.Late = true
myMinerList[minerID] = stats
if len(cloudKeyList[cloudKey].TelegramUserId) > 0 {
sendOfflineNotificationToTelegram(stats.Name, cloudKeyList[cloudKey].TelegramUserId)
}
} else if howLong.Seconds() <= myConfig.MinerLateTime {
stats.Late = false
totalActiveMiners += 1
myMinerList[minerID] = stats
totalHash += stats.Hashrate
}
}
userID := cloudKeyList[cloudKey].ID
var myOverallInfoTX OverallInfoTX = overallInfoTX[userID]
myOverallInfoTX.TotalActiveMiners = totalActiveMiners
overallInfoTX[userID] = myOverallInfoTX
totalHashG[cloudKey] = formatHashNum(totalHash) // TODO: should be by cloudkey...
}
}
mutex.Unlock()
}