-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudawgCli.go
205 lines (186 loc) · 3.85 KB
/
sudawgCli.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
package main
import (
"fmt"
"log"
"os"
"time"
"io/ioutil"
"github.com/BurntSushi/toml"
"github.com/urfave/cli"
)
//type tomlConfig struct {
// user account
//}
type account struct {
Username string
Password string
}
func main() {
// Create Cli app
app := cli.NewApp()
app.Name = "sudawg-agent"
app.Usage = "Login Program for wg.suda.edu.cn"
app.Version = "v0.3"
// Global options for login without login command
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "username,u",
Value: "",
Usage: "Your username for login",
},
cli.StringFlag{
Name: "password,p",
Value: "",
Usage: "Password of your account",
},
cli.StringFlag{
Name: "portal,P",
Value: "wg",
Usage: "Short name of the portal",
},
}
// Define cli commands with command options
app.Commands = []cli.Command{
{
Name: "login",
Aliases: []string{"l"},
Flags: []cli.Flag{
cli.StringFlag{
Name: "file,f",
Usage: "Path to config file",
},
cli.StringFlag{
Name: "username,u",
Value: "",
Usage: "Your username for login",
},
cli.StringFlag{
Name: "password,p",
Value: "",
Usage: "Password of your account",
},
cli.StringFlag{
Name: "portal,P",
Value: "wg",
Usage: "Short name of the portal",
},
},
Action: func(ctx *cli.Context) error {
login(ctx)
return nil
},
},
{
Name: "logout",
Aliases: []string{"l"},
Flags: []cli.Flag{
cli.StringFlag{
Name: "file,f",
Usage: "Path to config file",
},
cli.StringFlag{
Name: "username,u",
Value: "",
Usage: "Your username for login",
},
cli.StringFlag{
Name: "password,p",
Value: "",
Usage: "Password of your account",
},
cli.StringFlag{
Name: "portal,P",
Value: "wg",
Usage: "Short name of the portal",
},
},
Action: func(ctx *cli.Context) error {
logout(ctx)
return nil
},
},
}
//Default action if no commands were given
app.Action = func(ctx *cli.Context) error {
login(ctx)
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
// Master func of logout
func logout(ctx *cli.Context) {
wifiLogout()
time.Sleep(2)
if NetWorkStatus() {
user := getAccount(ctx)
wgLogout(user)
}
log.Println("Offine now")
}
// Master func of login
func login(ctx *cli.Context) {
var user account
user = getAccount(ctx)
// default portal is wg
if ctx.String("portal") == "wifi" {
//change portal to sudawifi if specified
log.Println("Logging into sudawifi")
wifiLogin(user)
} else {
log.Println("Logging into sudawg")
wgLogin(user)
}
// Connection test
if NetWorkStatus() {
fmt.Println("[SUCCESS] Login success")
} else {
fmt.Println("[WARNING] Connection test Failed")
}
}
// Utility func for acquiring user account
func getAccount(ctx *cli.Context) (user account) {
var err error
if ctx.String("file") != "" {
//var config tomlConfig
var (
fp *os.File
fcontent []byte
)
if fp, err = os.Open(ctx.String("file")); err != nil {
fmt.Println("open error ", err)
}
fmt.Println("Using " + ctx.String("file"))
if fcontent, err = ioutil.ReadAll(fp); err != nil {
fmt.Println("ReadAll error ", err)
}
//temp := new(account)
if _, err = toml.Decode(string(fcontent), &user); err != nil {
fmt.Println("toml.Unmarshal error ", err)
}
return user
}
if ctx.String("username") != "" {
user.Username = ctx.String("username")
} else {
fmt.Print("Username:")
_, err = fmt.Scanln(&user.Username)
for err != nil {
fmt.Print("Username:")
_, err = fmt.Scanln(&user.Username)
}
}
if ctx.String("password") != "" {
user.Password = ctx.String("password")
} else {
fmt.Print("Password:")
_, err = fmt.Scanln(&user.Password)
for err != nil {
fmt.Print("Password:")
_, err = fmt.Scanln(&user.Password)
}
}
return user
}