-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmain.go
361 lines (313 loc) · 7.45 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package main
import (
"akile_monitor/client/model"
"bytes"
"compress/gzip"
"context"
"fmt"
"github.com/cloudwego/hertz/pkg/common/json"
"io"
"log"
"regexp"
"sort"
"strconv"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/glebarez/sqlite"
"github.com/hertz-contrib/cors"
"github.com/hertz-contrib/websocket"
"gorm.io/gorm"
)
type Data struct {
Name string `gorm:"primaryKey"`
Data string
}
type M struct {
Host *model.Host
State *model.HostState
TimeStamp int64
}
var db *gorm.DB
var filedb *gorm.DB
func initDb() {
var dbfile = "file::memory:?cache=shared"
Db, err := gorm.Open(sqlite.Open(dbfile), &gorm.Config{})
if err != nil {
log.Panic(err)
}
Db.AutoMigrate(&Data{})
db = Db
}
func initFileDb() {
var dbfile = "ak_monitor.db"
Db, err := gorm.Open(sqlite.Open(dbfile), &gorm.Config{})
if err != nil {
log.Panic(err)
}
Db.AutoMigrate(&Host{})
filedb = Db
}
type Host struct {
Name string `json:"name" gorm:"primaryKey"`
DueTime int64 `json:"due_time"` // 到期时间
BuyUrl string `json:"buy_url"` // 购买链接
Seller string `json:"seller"` // 卖家
Price string `json:"price"` // 价格
}
var upgrader = websocket.HertzUpgrader{
CheckOrigin: func(r *app.RequestContext) bool {
return true // 允许所有跨域请求
},
EnableCompression: true,
} // use default options
func monitor(_ context.Context, c *app.RequestContext) {
err := upgrader.Upgrade(c, func(conn *websocket.Conn) {
var authed bool
for {
mt, message, err := conn.ReadMessage()
if err != nil {
log.Printf("client: %s,read: %s\n", c.ClientIP(), err.Error())
break
}
if !authed {
if string(message) != cfg.AuthSecret {
log.Printf("client: %s,auth failed\n", c.ClientIP())
break
}
authed = true
err = conn.WriteMessage(mt, []byte("auth success"))
if err != nil {
log.Printf("client: %s,write: %s\n", c.ClientIP(), err.Error())
break
}
continue
}
//gzip解压
var buf bytes.Buffer
buf.Write(message)
r, _ := gzip.NewReader(&buf)
message, _ = io.ReadAll(r)
r.Close()
var d M
err = json.Unmarshal(message, &d)
if err != nil {
log.Printf("client: %s,unmarshal: %s\n", c.ClientIP(), err.Error())
break
}
var data Data
db.Model(&Data{}).Where("name = ?", d.Host.Name).First(&data)
if data.Name == "" {
db.Create(&Data{Name: d.Host.Name, Data: string(message)})
} else {
db.Model(&Data{}).Where("name = ?", d.Host.Name).Update("data", string(message))
}
}
})
if err != nil {
log.Printf("client: %s,upgrade: %s\n", c.ClientIP(), err.Error())
return
}
}
var offline = make(map[string]bool)
func main() {
LoadConfig()
initDb()
initFileDb()
if cfg.EnableTG {
go startbot()
}
if cfg.TgChatID != 0 {
go func() {
for {
var mm []M
data := fetchData()
json.Unmarshal(data, &mm)
for _, v := range mm {
// 30秒内离线
if v.TimeStamp < time.Now().Unix()-60 {
if !offline[v.Host.Name] {
offline[v.Host.Name] = true
msg := fmt.Sprintf("❌ %s 离线了", v.Host.Name)
SendTGMessage(msg)
}
} else {
if offline[v.Host.Name] {
offline[v.Host.Name] = false
msg := fmt.Sprintf("✅ %s 上线了", v.Host.Name)
SendTGMessage(msg)
}
}
}
time.Sleep(time.Second * 20)
}
}()
}
h := server.Default(server.WithHostPorts(cfg.Listen))
config := cors.DefaultConfig()
config.AllowAllOrigins = true
h.Use(cors.New(config))
h.NoHijackConnPool = true
h.GET("/info", Info)
h.POST("/info", UpdateInfo)
h.GET(cfg.UpdateUri, monitor)
h.GET(cfg.WebUri, ws)
h.GET(cfg.HookUri, Hook)
h.POST("/delete", DeleteHost)
h.Spin()
}
func Hook(_ context.Context, c *app.RequestContext) {
token := c.Query("token")
if token != cfg.HookToken {
c.JSON(401, "auth failed")
return
}
data := fetchData()
c.JSON(200, data)
}
func Info(_ context.Context, c *app.RequestContext) {
var ret []*Host
err := filedb.Model(&Host{}).Find(&ret).Error
if err != nil {
log.Println(err)
c.JSON(200, "[]")
return
}
c.JSON(200, ret)
}
type UpdateRequest struct {
AuthSecret string `json:"auth_secret"`
Host
}
func UpdateInfo(_ context.Context, c *app.RequestContext) {
var ret UpdateRequest
err := c.BindJSON(&ret)
if err != nil {
c.JSON(400, "bad request")
return
}
if ret.AuthSecret != cfg.AuthSecret {
c.JSON(401, "auth failed")
return
}
var h Host
filedb.Model(&Host{}).Where("name = ?", ret.Name).First(&h)
if h.Name == "" {
h = ret.Host
filedb.Model(&Host{}).Create(&h)
} else {
h = ret.Host
filedb.Model(&Host{}).Where("name = ?", ret.Name).Save(&h)
}
c.JSON(200, "ok")
}
type DeleteHostRequest struct {
AuthSecret string `json:"auth_secret"`
Name string `json:"name"`
}
func DeleteHost(_ context.Context, c *app.RequestContext) {
var req DeleteHostRequest
err := c.BindJSON(&req)
if err != nil {
c.JSON(400, "bad request")
return
}
if req.AuthSecret != cfg.AuthSecret {
c.JSON(401, "auth failed")
return
}
var data Data
db.Model(&Data{}).Where("name = ?", req.Name).First(&data)
if data.Name == "" {
c.JSON(404, "not found")
return
}
db.Delete(&Data{}, "name = ?", req.Name)
c.JSON(200, "ok")
}
func ws(_ context.Context, c *app.RequestContext) {
err := upgrader.Upgrade(c, func(conn *websocket.Conn) {
for {
_, _, err := conn.ReadMessage()
if err != nil {
log.Printf("client: %s,read: %s\n", c.ClientIP(), err.Error())
break
}
data := fetchData()
err = conn.WriteMessage(websocket.TextMessage, append([]byte("data: "), data...))
if err != nil {
log.Printf("client: %s,write: %s\n", c.ClientIP(), err.Error())
break
}
}
})
if err != nil {
log.Printf("client: %s,upgrade: %s\n", c.ClientIP(), err.Error())
return
}
}
func fetchData() []byte {
// 模拟数据获取
var ret []Data
db.Model(&Data{}).Find(&ret)
var mm []M
//排序根据Name 10在9后面
sort.Slice(ret, func(i, j int) bool {
return compareStrings(ret[i].Name, ret[j].Name) < 0
})
//var jsonData string
for _, v := range ret {
var m M
json.Unmarshal([]byte(v.Data), &m)
mm = append(mm, m)
}
jsonData, _ := json.Marshal(mm)
return jsonData
}
// 定义一个函数来比较两个带字母和数字的字符串
func compareStrings(str1, str2 string) int {
//先去除空格
str1 = regexp.MustCompile(`\s+`).ReplaceAllString(str1, "")
str2 = regexp.MustCompile(`\s+`).ReplaceAllString(str2, "")
// 使用正则表达式提取字母和数字部分
re := regexp.MustCompile(`([a-zA-Z]+)(\d*)`)
matches1 := re.FindStringSubmatch(str1)
matches2 := re.FindStringSubmatch(str2)
if len(matches1) != 3 || len(matches2) != 3 {
return 0 // 格式不匹配
}
// 提取字母部分
letter1 := matches1[1]
letter2 := matches2[1]
// 提取并转换数字部分
num1 := 0
num2 := 0
if len(matches1[2]) > 0 {
num1, _ = strconv.Atoi(matches1[2])
}
if len(matches2[2]) > 0 {
num2, _ = strconv.Atoi(matches2[2])
}
// 先比较字母部分,逐个字符比较
for i := 0; i < len(letter1) && i < len(letter2); i++ {
if letter1[i] < letter2[i] {
return -1
} else if letter1[i] > letter2[i] {
return 1
}
}
// 如果字母部分相同,长度不等时,短的字母部分小
if len(letter1) < len(letter2) {
return -1
} else if len(letter1) > len(letter2) {
return 1
}
// 字母相同,比较数字部分
if num1 < num2 {
return -1
} else if num1 > num2 {
return 1
}
// 如果字母和数字都相同
return 0
}