Skip to content

Commit

Permalink
add logout and sudawifi support
Browse files Browse the repository at this point in the history
  • Loading branch information
LionTao committed Aug 13, 2018
1 parent 6dca9a6 commit 2abd1ef
Showing 1 changed file with 165 additions and 27 deletions.
192 changes: 165 additions & 27 deletions suda_wg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/base64"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/urfave/cli"
Expand All @@ -10,6 +11,7 @@ import (
"os/exec"
"runtime"
"strings"
"time"
)

func main() {
Expand All @@ -27,41 +29,137 @@ func main() {
Value: "",
Usage: "Password of your account",
},
cli.StringFlag{
Name: "portal,P",
Value: "wg",
Usage: "Short name of the portal",
},
}

app.Action = func(ctx *cli.Context) {
var username, password string
if ctx.String("username") != "" {
//fmt.Println("here")
username = ctx.String("username")
} else {
fmt.Print("Username:")
fmt.Scanln(&username)
}

if ctx.String("password") != "" {
//fmt.Println("here")
username = ctx.String("password")
} else {
fmt.Print("Password:")
fmt.Scanln(&password)
}
ev, vs := get_wg_param()
wg_login(ev, vs, username, password)
if NetWorkStatus() {
fmt.Println("Login success")
} else {
fmt.Println("[WARNING] Connection test Failed")
}
app.Commands = []cli.Command{
{
Name: "login",
Aliases: []string{"l"},
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",
},
},
Action: func(ctx *cli.Context) error {
login(ctx)
return nil
},
},
{
Name: "logout",
Aliases: []string{"l"},
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",
},
},
Action: func(ctx *cli.Context) error {
logout(ctx)
return nil
},
},
}

//app.Action = func(ctx *cli.Context) error{
// login(ctx)
// return nil
//}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}

}

func logout(ctx *cli.Context) {
wifiLogout()
time.Sleep(2)
if NetWorkStatus() {
username, passsword := getAccount(ctx)
wgLogout(username, passsword)
}
log.Println("Offine now")
}

func getAccount(ctx *cli.Context) (uname, pwd string) {
var username, password string
if ctx.String("username") != "" {
//fmt.Println("here")
username = ctx.String("username")
} else {
fmt.Print("Username:")
fmt.Scanln(&username)
}

if ctx.String("password") != "" {
//fmt.Println("here")
username = ctx.String("password")
} else {
fmt.Print("Password:")
fmt.Scanln(&password)
}
return username, password
}

func login(ctx *cli.Context) {
username, password := getAccount(ctx)

//default is wg
if ctx.String("portal") == "wifi" {
log.Println("Logging into sudawifi")
wifiLogin(username, password)
} else {
log.Println(ctx.String("portal"))
log.Println("Logging into sudawg")
wgLogin(username, password)
}

if NetWorkStatus() {
fmt.Println("Login success")
} else {
fmt.Println("[WARNING] Connection test Failed")
}
}

func wgLogout(username string, password string) {
ev, vs := get_wg_param()
wg_POST(ev, vs, username, password, "退出网关")
}
func wgLogin(username string, password string) {

ev, vs := get_wg_param()
wg_POST(ev, vs, username, password, "登陆网关")
}

func get_wg_param() (eventvalidation, viewstate string) {
url := "http://wg.suda.edu.cn/indexn.aspx"
res, err := http.Get(url)
Expand Down Expand Up @@ -106,7 +204,7 @@ func get_wg_param() (eventvalidation, viewstate string) {
return EVENTVALIDATION, VIEWSTATE
}

func wg_login(EVENTVALIDATION string, VIEWSTATE string, username string, password string) {
func wg_POST(EVENTVALIDATION string, VIEWSTATE string, username string, password string, action string) {
if EVENTVALIDATION == "" || VIEWSTATE == "" {
log.Fatal("Oh no! no param was given")
}
Expand All @@ -123,7 +221,11 @@ func wg_login(EVENTVALIDATION string, VIEWSTATE string, username string, passwor
r.Form.Add("TextBox2", password)
r.Form.Add("nw", "RadioButton2")
r.Form.Add("tm", "RadioButton8")
r.Form.Add("Button1", "登陆网关")
if action == "登陆网关" {
r.Form.Add("Button1", action)
} else if action == "退出网关" {
r.Form.Add("Button4", action)
}

bodystr := strings.TrimSpace(r.Form.Encode())
request, err := http.NewRequest("POST", url, strings.NewReader(bodystr))
Expand Down Expand Up @@ -161,3 +263,39 @@ func NetWorkStatus() bool {
}
return true
}

func wifiLogin(username string, password string) {
url := "http://a.suda.edu.cn/index.php/index/login"

var r http.Request
r.ParseForm()
r.Form.Add("username", username)
r.Form.Add("password", base64.StdEncoding.EncodeToString([]byte(password)))

bodystr := strings.TrimSpace(r.Form.Encode())
request, err := http.NewRequest("POST", url, strings.NewReader(bodystr))
if err != nil {
log.Fatal("POST Failed")
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("Connection", "Keep-Alive")

resp, err := http.DefaultClient.Do(request)
if err != nil || resp.StatusCode != 200 {
log.Println("[WARNING] Wifi Login Failed")
fmt.Println(resp.StatusCode)
fmt.Println(err)
log.Fatal("[ERROR] An error occurred in sending request")
}
}

func wifiLogout() {
url := "http://a.suda.edu.cn/index.php/index/logout"
resp, err := http.Get(url)
if err != nil || resp.StatusCode != 200 {
log.Println("[WARNING] Wifi Logout Failed")
fmt.Println(resp.StatusCode)
fmt.Println(err)
log.Fatal("[ERROR] An error occurred in sending request")
}
}

0 comments on commit 2abd1ef

Please sign in to comment.