Skip to content

Instantly share code, notes, and snippets.

@inkedawn inkedawn/main.go
Created Oct 22, 2018

Embed
What would you like to do?
ccuNetLogin
package main
import (
"fmt"
"strings"
"errors"
"net/http"
"net/url"
"encoding/json"
"os"
"io/ioutil"
"log"
"os/exec"
)
type accountInfo struct {
Account string
Password string
IsRemember bool
}
type loginResult struct {
Success bool
Msg string
Action string
Pop int
UserName string
Location string
}
func scanAccountInfo(ac *accountInfo) error {
fmt.Print("输入帐号:")
fmt.Scanln(&ac.Account)
fmt.Print("输入密码:")
fmt.Scanln(&ac.Password)
if ac.Account == "" || ac.Password == "" {
return errors.New("帐号与密码不能为空!")
}
var strIsRemember string
fmt.Print("是否记住帐号(Yes/No):")
fmt.Scanln(&strIsRemember)
strIsRemember = strings.ToLower(strIsRemember)
ac.IsRemember = (strIsRemember!="n") && (strIsRemember!="no")
if ac.IsRemember {
strIsRemember = "Yes"
} else {
strIsRemember = "No"
}
fmt.Println(strIsRemember)
return nil
}
const SAVE_FILE_NAME = "account.dat"
func readAccountInfo(ac *accountInfo) error {
log.Println("readAccountInfo")
file, err := os.OpenFile(SAVE_FILE_NAME, os.O_RDONLY, 0755)
if err != nil {
return err
}
defer file.Close()
jsonBytes, err := ioutil.ReadAll(file)
if err != nil {
log.Println("file.Read failed:", err)
return err
}
log.Println("Read account info from file: ", string(jsonBytes))
err = json.Unmarshal(jsonBytes, ac)
if err != nil {
log.Println("json.Unmarshal failed:", err)
return err
}
log.Println("readAccountInfo complete")
return nil
}
func saveAccountInfo(ac *accountInfo) error {
log.Println("saveAccountInfo")
file, err := os.OpenFile(SAVE_FILE_NAME, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer file.Close()
jsonBytes , err := json.Marshal(*ac)
if err != nil {
log.Println("json.Marshal failed:", err)
return err
}
_, err = file.Write(jsonBytes)
if err != nil {
log.Println("file.Write failed:", err)
return err
}
log.Println("saveAccountInfo complete")
return nil
}
func logoutNet() (loginResult, error) {
var result loginResult
var client = &http.Client{}
const LOGIN_URL string = "http://1.1.1.2/ac_portal/login.php"
uri := LOGIN_URL
postValues := url.Values{}
postValues.Add("opr", "logout")
_, err := client.PostForm(uri, postValues)
if err != nil {
panic(err)
return result, err
}
return result, nil
// TODO: implement resp parse and return Result
}
func loginNet(ac accountInfo) (loginResult, error) {
defer func(){
if err:=recover(); err!=nil{
fmt.Println("登录异常")
fmt.Println(err.(error).Error())
}
}()
var result loginResult
var client = &http.Client{}
const LOGIN_URL string = "http://1.1.1.2/ac_portal/login.php"
uri := LOGIN_URL
postValues := url.Values{}
postValues.Add("opr", "pwdLogin")
postValues.Add("userName", ac.Account)
postValues.Add("pwd", ac.Password)
postValues.Add("rememberPwd", "0")
resp, err := client.PostForm(uri, postValues)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
panic(err)
return result, err
}
if resp.StatusCode == 200 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
return result, err
}
err = json.Unmarshal([]byte(strings.Replace(string(body), `'`, `"`, -1)), &result)
//fmt.Println(string(body))
if err != nil {
fmt.Println(string(body))
fmt.Println(string(err.Error()))
panic(err)
return result, err
}
//fmt.Println(string(body))
}
return result, nil
}
func init() {
log.SetOutput(ioutil.Discard)
}
func main() {
var ac accountInfo
err := readAccountInfo(&ac)
if err != nil {
err:= scanAccountInfo(&ac)
if err != nil {
fmt.Println(err.Error())
return
}
}
if (ac.Account == "logout") {
logoutNet()
return
}
fmt.Println("登录中...")
resp, err := loginNet(ac)
if err != nil {
fmt.Println(err.Error())
return
}
if resp.Success {
fmt.Println("登录成功!")
if err:= saveAccountInfo(&ac); ac.IsRemember && err != nil {
fmt.Println(err.Error())
}
exec.Command("cmd.exe", "/c start http://www.baidu.com/")
} else {
//fmt.Print(resp)
//http://1.1.1.2/ac_portal/proxy.html?type=logout
fmt.Println("登录失败,原因是:" + resp.Msg)
if ac.IsRemember {
var isdeletestr string
var isdelete bool
fmt.Print("是否清除已保存的帐号信息?(Yes/No):")
fmt.Scanln(&isdeletestr)
isdeletestr = strings.ToLower(isdeletestr)
isdelete = (isdeletestr=="y") || (isdeletestr!="yes")
if isdelete {
isdeletestr = "Yes"
} else {
isdeletestr = "No"
}
fmt.Println(isdeletestr)
if isdelete {
os.Remove(SAVE_FILE_NAME)
fmt.Println("账号信息已清除")
}
}
}
fmt.Println("现在可以关闭窗口")
fmt.Scan()
select{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.