-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
88 lines (75 loc) · 1.79 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
package main
import (
"fmt"
"os"
"time"
)
func main() {
configuration, err := loadConfiguration()
if err != nil || configuration.Username == "" || configuration.Password == "" {
createEmptyConfigurationFile()
fmt.Println("Please, fill", CONFIG_PATH, "configuration file.")
return
}
token, err := loadToken()
if err == nil {
fmt.Println("Loaded token:", token)
fmt.Println()
}
isRunning := true
go func() {
isNetworkReachable := false
for isRunning {
newNetworkStatus, err := checkNetwork()
if err != nil {
fmt.Println("Network connection check has failed.")
fmt.Println(err)
fmt.Println()
isNetworkReachable = newNetworkStatus
time.Sleep(2 * time.Second)
continue
}
if newNetworkStatus {
// if notwork state changed
if !isNetworkReachable {
fmt.Println("Network is reachable.")
fmt.Println("You will be automatically reconnected after bmstu_lb timeout.")
fmt.Println()
fmt.Print("Press enter to ")
if token == NullToken {
fmt.Print("exit the program")
} else {
fmt.Print("logout")
}
fmt.Println("...")
fmt.Println()
}
isNetworkReachable = newNetworkStatus
time.Sleep(15 * time.Second)
continue
}
fmt.Println("Network is unreachable.")
token = NullToken
fmt.Println("Authorizing in bmstu_lb...")
token, err = logIn(configuration.Username, configuration.Password)
if err != nil {
fmt.Println("Can not authorize:", err)
fmt.Println()
time.Sleep(10 * time.Second)
continue
}
fmt.Println("Done. Your token:", token)
saveToken(token)
fmt.Println()
time.Sleep(5 * time.Second)
}
}()
fmt.Scanln()
isRunning = false
if token != NullToken {
logOut(token)
fmt.Println("You have been disconnected.")
}
deleteToken()
os.Exit(0)
}