-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
198 lines (177 loc) · 4.6 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
)
var (
username string
password string
waitTime int
brakKey string
url string
)
func init() {
flag.StringVar(&username, "u", "", "username")
flag.StringVar(&password, "p", "", "password")
flag.IntVar(&waitTime, "w", 10, "wait time")
flag.StringVar(&brakKey, "b", "", "bark key")
flag.StringVar(&url, "url", "http://192.168.8.1", "5g cpe url")
}
func main() {
flag.Parse()
logout := make(chan int64)
go login(logout)
for {
t := <-logout
now := time.Now().UnixMilli()
log.Printf("上次登录时间: %s", time.UnixMilli(t).Format("2006/01/02 15:04:05"))
if (now-t) < 1000*60*5+1000*10 && (now-t) > 1000*60*4+1000*50 {
log.Printf("登录会话过期, 立即重新登录")
go login(logout)
continue
}
log.Printf("账号在其它地方登录, 等待%d分钟后重新登录", waitTime)
<-time.After(time.Duration(waitTime) * time.Minute)
go login(logout)
}
}
type userLogin struct {
Username string `json:"username"`
Password string `json:"password"`
}
func login(logout chan int64) {
user := userLogin{
Username: username,
Password: password,
}
loginInfo, err := requestPost(user, "/api/sign/DO_WEB_LOGIN", "DO_WEB_LOGIN")
if err != nil {
log.Print(err)
}
status := strings.Split(loginInfo, "|")[0]
if status == "1" {
panic("当前已有用户在别处登录,请稍后登录")
} else if status == "2" {
panic("您的连续错误登录次数已经达到3次, 请1分钟后再试")
} else if status == "3" {
panic("管理帐号已被禁用,请另选帐号登录")
} else if status == "4" {
panic("用户名或密码错误,请重试")
} else if status == "5" {
panic("未知错误")
} else if status == "0" {
log.Print("登录成功")
}
now := time.Now()
for {
islogin, err := requestGet("/api/tmp/IS_LOGGED_IN")
if err != nil {
log.Print(err)
}
log.Print("是否登录: ", strings.TrimSpace(islogin))
if strings.TrimSpace(islogin) == "0" {
logout <- now.UnixMilli()
return
}
hb, err := requestGet("/api/tmp/heartbeat")
if err != nil {
log.Print(err)
}
log.Print("心跳请求: ", strings.TrimSpace(hb))
if strings.TrimSpace(hb) != "true" {
logout <- now.UnixMilli()
return
}
watchSms()
<-time.After(3 * time.Second)
}
}
type NewSmsFlag struct {
NewSmsFlag string `json:"new_sms_flag"`
}
func watchSms() {
smsFlag, err := requestGet("/api/tmp/FHAPIS?ajaxmethod=get_new_sms")
if err != nil {
log.Print(err)
}
var data = new(NewSmsFlag)
err = json.Unmarshal([]byte(smsFlag), data)
if err != nil {
log.Print(err)
}
log.Print("是否有新短信: ", strings.TrimSpace(data.NewSmsFlag))
if strings.TrimSpace(data.NewSmsFlag) == "true" {
smsNotice()
}
}
func smsNotice() {
sms, err := requestPost(nil, "/api/tmp/FHAPIS", "get_sms_data")
if err != nil {
log.Print(err)
}
var msg map[string]interface{}
err = json.Unmarshal([]byte(sms), &msg)
if err != nil {
log.Print(err)
}
for _, v := range msg {
for _, vv := range v.(map[string]interface{}) {
if m, ok := vv.(map[string]interface{}); ok {
if m["rcvorsend"] == "recv" {
if m["isOpened"] == "0" {
log.Print("--------------------新短信--------------------")
log.Print("短信号码: ", v.(map[string]interface{})["session_phone"])
log.Print("短信内容: ", m["msg_content"])
log.Print("短信时间: ", m["time"])
log.Print("短信ID: ", m["childnode"])
log.Print("--------------------------------------------")
res, err := barkNotice(v.(map[string]interface{})["session_phone"].(string), m["msg_content"].(string))
if err != nil {
log.Print(err)
} else {
log.Print("bark通知: ", res)
}
readSms(m["childnode"].(string))
}
}
}
}
}
}
func readSms(id string) {
var data = map[string]interface{}{
"url": map[string]interface{}{
"smsIsopend" + id: "InternetGatewayDevice.X_FH_MobileNetwork.SMS_Recv.SMS_RecvMsg." + id + ".isOpened",
},
"value": map[string]interface{}{
"smsIsopend" + id: "1",
},
}
res, err := requestPost(data, "/api/tmp/FHAPIS", "set_value_by_xmlnode")
if err != nil {
log.Print(err)
}
log.Printf("短信[%s]已读: %s", id, res)
}
func barkNotice(title, content string) (string, error) {
if brakKey == "" {
return "", fmt.Errorf("bark key is empty")
}
url := fmt.Sprintf("https://api.day.app/%s/%s/%s?level=timeSensitive", brakKey, title, content)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}