-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (101 loc) · 2.12 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
package main
import (
"crypto/sha1"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
)
type LoginResponse struct {
Errorcode int `json:"errorcode"`
}
type LoginError struct {
StatusCode int
Body string
}
func (e LoginError) Error() string {
return fmt.Sprintf("%#v", e)
}
func main() {
const (
baseURL = "http://swt.passengerwifi.com/cws/"
reqTimeout = 5 * time.Second
reqRetry = 5 * time.Second
)
if len(os.Args) != 3 {
ExitWithUsage()
}
username, password := os.Args[1], os.Args[2]
if username == "" || password == "" {
ExitWithUsage()
}
req, err := LoginRequest(username, password, baseURL)
if err != nil {
log.Fatalln(err)
}
client := &http.Client{
Transport: &http.Transport{
ResponseHeaderTimeout: reqTimeout,
},
}
for {
err := Login(req, client)
if err == nil {
log.Println("login successful!")
break
}
log.Println("login failed:", err)
log.Println("sleeping for:", reqRetry)
time.Sleep(reqRetry)
}
}
func ExitWithUsage() {
fmt.Fprintf(os.Stderr, "Usage: %s <username> <password>\n", os.Args[0])
os.Exit(2)
}
func LoginRequest(username, password string, baseURL string) (*http.Request, error) {
loginURL, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
passwordHash := fmt.Sprintf("%x", sha1.Sum([]byte(password)))
params := url.Values{}
params.Set("rq", "login")
params.Set("username", username)
params.Set("password", passwordHash)
loginURL.RawQuery = params.Encode()
return http.NewRequest("GET", loginURL.String(), nil)
}
func Login(req *http.Request, client *http.Client) error {
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return LoginError{
StatusCode: resp.StatusCode,
Body: fmt.Sprintf("%s", raw),
}
}
var message LoginResponse
err = json.Unmarshal(raw, &message)
if err != nil {
return err
}
if message.Errorcode != 0 {
return LoginError{
StatusCode: resp.StatusCode,
Body: fmt.Sprintf("%s", raw),
}
}
return nil
}