-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (105 loc) · 2.49 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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
type Auth struct {
Username string `json:"username"`
Password string `json:"password"`
}
var client *http.Client
func readConf() (*Auth, error) {
b, err := ioutil.ReadFile("config.json")
if err != nil {
log.Println(err)
return nil, err
}
auth := new(Auth)
err = json.Unmarshal(b, auth)
return auth, err
}
func connect(auth *Auth) error {
postForm := fmt.Sprintf(
"buttonClicked=4&redirect_url=&err_flag=0&username=%s&password=%s",
auth.Username,
auth.Password,
)
postForm = strings.Replace(postForm, " ", "%20", -1)
reader := strings.NewReader(postForm)
req, err := http.NewRequest("POST", "https://2.2.2.2/login.html", reader)
if err != nil {
return err
}
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
req.Header.Set("Accept-Encoding", "gzip, deflate")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Host", "2.2.2.2")
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) Gecko/20100101 Firefox/41.0")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
return err
}
data, _ := ioutil.ReadAll(resp.Body)
idx := strings.Index(string(data), "Login Successful")
if idx < 0 {
return fmt.Errorf("login failed")
}
return nil
}
func Check() bool {
req, err := http.NewRequest("GET", "https://baidu.com", nil)
if err != nil {
return false
}
resp, err := client.Do(req)
if err != nil {
return false
}
data, _ := ioutil.ReadAll(resp.Body)
idx := strings.Index(string(data), "百度一下,你就知道")
if idx < 0 {
return false
}
return true
}
func main() {
auth, err := readConf()
if err != nil {
log.Println("读取配置文件出错:", err)
os.Exit(1)
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{Transport: tr,
Timeout: 20 * time.Second,
}
if err := connect(auth); err != nil {
log.Println("登陆失败:", err)
} else {
log.Println("登陆成功")
}
for {
if !Check() {
log.Println("重新连接 Wi-Fi")
if err := connect(auth); err != nil {
log.Println("登陆失败:", err)
} else {
log.Println("登陆成功")
}
} else {
log.Println("Wi-Fi 连接正常")
}
time.Sleep(10 * time.Second)
}
os.Exit(0)
}