diff --git a/cmd/srun/cli.go b/cmd/srun/cli.go index 3ec42af..8ba5780 100644 --- a/cmd/srun/cli.go +++ b/cmd/srun/cli.go @@ -21,19 +21,15 @@ func LoginE(cmd *cobra.Command, args []string) error { } log.Info("尝试登录...") - //username = model.AddSuffix(username, server) - info, err := core.Login(&account) - if err != nil { + if err = core.Login(&account); err != nil { return err } log.Info("登录成功!") - err = store.SetInfo(info.AccessToken, info.Acid) + err = store.SetInfo(account.AccessToken, account.Acid) if err != nil { return err } - - //GetInfo(cmd, params...) return nil } @@ -51,12 +47,11 @@ func LogoutE(cmd *cobra.Command, args []string) error { } func InfoE(cmd *cobra.Command, args []string) error { - res, err := core.Info() + info, err := core.Info() if err != nil { - log.Error(err) - os.Exit(1) + return err } - fmt.Println(res.String()) + fmt.Println(info.String()) return nil } @@ -70,8 +65,7 @@ func ConfigE(cmd *cobra.Command, args []string) error { fd, _ := term.GetFdInfo(in) oldState, err := term.SaveState(fd) if err != nil { - log.Error(err) - os.Exit(1) + return err } fmt.Print("设置校园网密码:\n>") @@ -87,8 +81,7 @@ func ConfigE(cmd *cobra.Command, args []string) error { pwd = strings.TrimSpace(pwd) if err := store.SetAccount(username, pwd); err != nil { - log.Error(err) - os.Exit(1) + return err } log.Info("账号密码已被保存") return nil diff --git a/cmd/srun/main.go b/cmd/srun/main.go index c96d8fe..73a1c1a 100755 --- a/cmd/srun/main.go +++ b/cmd/srun/main.go @@ -6,7 +6,7 @@ import ( "os" ) -const Version = "v0.1.26" +const Version = "v1.0.0" var loginCmd = &cobra.Command{ Use: "login", @@ -36,10 +36,12 @@ var rootCmd = &cobra.Command{ Use: "srun [command]", Short: "A efficient client for BIT campus network", RunE: func(cmd *cobra.Command, args []string) error { + return LoginE(cmd, args) + }, + PersistentPreRun: func(cmd *cobra.Command, args []string) { if debugMode { log.SetLevel(log.DebugLevel) } - return LoginE(cmd, args) }, } diff --git a/core/core.go b/core/core.go index 3597f9e..8a8841d 100644 --- a/core/core.go +++ b/core/core.go @@ -41,7 +41,7 @@ func Prepare() (int, error) { // step 1: prepare & get acid // step 2: get challenge // step 3: do login -func Login(account *model.Account) (result model.QInfo, err error) { +func Login(account *model.Account) (err error) { log.Debug("Username: ", account.Username) // 先获取acid @@ -51,7 +51,8 @@ func Login(account *model.Account) (result model.QInfo, err error) { return } - username := account.GenUsername() + username := account.Username + // 创建登录表单 formLogin := model.Login(username, account.Password, acid) @@ -71,7 +72,7 @@ func Login(account *model.Account) (result model.QInfo, err error) { formLogin.Set("chksum", hash.Checksum(formLogin, token)) // response - ra := resp.RAction{} + ra := resp.ActionResp{} if err = utils.GetJson(baseAddr+portalUrl, formLogin, &ra); err != nil { log.Debug("request error", err) @@ -88,17 +89,13 @@ func Login(account *model.Account) (result model.QInfo, err error) { return } - result = model.QInfo{ - Acid: acid, - Username: username, - ClientIp: rc.ClientIp, - AccessToken: rc.Challenge, - } + account.AccessToken = token + account.Acid = acid return } // api info -func Info() (info *model.RInfo, err error) { +func Info() (info *model.InfoResp, err error) { // 余量查询 err = utils.GetJson(baseAddr+succeedUrl, url.Values{}, &info) @@ -111,7 +108,7 @@ func Info() (info *model.RInfo, err error) { // api logout func Logout(username string) (err error) { q := model.Logout(username) - ra := resp.RAction{} + ra := resp.ActionResp{} if err = utils.GetJson(baseAddr+portalUrl, q, &ra); err != nil { log.Debug(err) err = ErrRequest @@ -124,7 +121,7 @@ func Logout(username string) (err error) { return } -func getChallenge(username string) (res resp.Challenge, err error) { +func getChallenge(username string) (res resp.ChallengeResp, err error) { qc := model.Challenge(username) err = utils.GetJson(baseAddr+challengeUrl, qc, &res) return diff --git a/model/account.go b/model/account.go index e0ae5b5..22b0652 100644 --- a/model/account.go +++ b/model/account.go @@ -28,7 +28,3 @@ func (a *Account) JSONBytes() (jsonData []byte, err error) { func (a *Account) String() string { return fmt.Sprintln("用户名:", a.Username) } - -func (a *Account) GenUsername() string { - return a.Username -} diff --git a/model/request.go b/model/request.go new file mode 100644 index 0000000..dab9519 --- /dev/null +++ b/model/request.go @@ -0,0 +1,34 @@ +package model + +import ( + "fmt" + "net/url" +) + +func Challenge(username string) url.Values { + return url.Values{ + "username": {username}, + "ip": {""}, + } +} + +func Login(username, password string, acid int) url.Values { + return url.Values{ + "action": {"login"}, + "username": {username}, + "password": {password}, + "ac_id": {fmt.Sprint(acid)}, + "ip": {""}, + "info": {}, + "chksum": {}, + "n": {"200"}, + "type": {"1"}, + } +} + +func Logout(username string) url.Values { + return url.Values{ + "action": {"logout"}, + "username": {username}, + } +} diff --git a/model/model.go b/model/response.go similarity index 52% rename from model/model.go rename to model/response.go index ff77c30..84016fa 100644 --- a/model/model.go +++ b/model/response.go @@ -3,38 +3,23 @@ package model import ( "fmt" "github.com/vouv/srun/utils" - "net/url" "strings" ) -// query challenge -type QChallenge struct { - Username string `json:"username"` - Ip string `json:"ip"` +type ChallengeResp struct { + Challenge string `json:"challenge"` + ClientIp string `json:"client_ip"` } -// query login -type QLogin struct { - Action string `json:"action"` - Username string `json:"username"` - Password string `json:"password"` - Acid int `json:"ac_id"` - Ip string `json:"ip"` - Info string `json:"info"` - Chksum string `json:"chksum"` - N int `json:"n"` - Type int `json:"type"` +type ActionResp struct { + Res string `json:"res"` + Error string `json:"error"` + Ecode interface{} `json:"ecode"` + ErrorMsg string `json:"error_msg"` + ClientIp string `json:"client_ip"` } -// query info -type QInfo struct { - Acid int `json:"ac_id"` - Username string `json:"username"` - ClientIp string `json:"client_ip"` - AccessToken string `json:"access_token"` -} - -type RInfo struct { +type InfoResp struct { ServerFlag int64 `json:"ServerFlag"` AddTime int64 `json:"add_time"` AllBytes int64 `json:"all_bytes"` @@ -59,7 +44,7 @@ type RInfo struct { WalletBalance float64 `json:"wallet_balance"` } -func (r *RInfo) String() string { +func (r *InfoResp) String() string { sb := strings.Builder{} sb.WriteString(fmt.Sprintf("用户名: %s\n", r.UserName)) sb.WriteString(fmt.Sprintf("IP地址: %s\n", r.OnlineIp)) @@ -70,47 +55,10 @@ func (r *RInfo) String() string { return sb.String() } -// query logout -type QLogout struct { - Action string `json:"action"` - Username string `json:"username"` - Acid int `json:"ac_id"` - Ip string `json:"ip"` -} - -func Challenge(username string) url.Values { - return url.Values{ - "username": {username}, - "ip": {""}, - } -} - -func Info(acid int, username, clientIp, accessToken string) url.Values { - return url.Values{ - "ac_id": {fmt.Sprint(acid)}, - "username": {username}, - "client_ip": {clientIp}, - "access_token": {accessToken}, - } -} - -func Login(username, password string, acid int) url.Values { - return url.Values{ - "action": {"login"}, - "username": {username}, - "password": {password}, - "ac_id": {fmt.Sprint(acid)}, - "ip": {""}, - "info": {}, - "chksum": {}, - "n": {"200"}, - "type": {"1"}, - } -} - -func Logout(username string) url.Values { - return url.Values{ - "action": {"logout"}, - "username": {username}, - } +// query info +type InfoResult struct { + Acid int `json:"ac_id"` + Username string `json:"username"` + ClientIp string `json:"client_ip"` + AccessToken string `json:"access_token"` } diff --git a/resp/resp.go b/resp/resp.go index 2cc2159..30ea924 100644 --- a/resp/resp.go +++ b/resp/resp.go @@ -1,21 +1,11 @@ package resp -// response challenge -type Challenge struct { +type ChallengeResp struct { Challenge string `json:"challenge"` ClientIp string `json:"client_ip"` } -// example -// map[res:login_error -// srun_ver:SRunCGIAuthIntfSvr V1.01 B20180306 -// st:1.543044956e+09 -// client_ip:10.62.44.153 -// ecode:E2616 -// error:login_error -// error_msg:E2616: Average users. -// online_ip:10.62.44.153] -type RAction struct { +type ActionResp struct { Res string `json:"res"` Error string `json:"error"` Ecode interface{} `json:"ecode"` diff --git a/store/errors.go b/store/errors.go deleted file mode 100644 index f1edc88..0000000 --- a/store/errors.go +++ /dev/null @@ -1,8 +0,0 @@ -package store - -import "errors" - -// 读取账号文件错误 -var ErrReadFile = errors.New("读取账号文件错误") -var ErrWriteFile = errors.New("写入账号文件错误") -var ErrParse = errors.New("序列化账号错误") diff --git a/store/store.go b/store/store.go index 20168bb..11630d7 100644 --- a/store/store.go +++ b/store/store.go @@ -3,6 +3,7 @@ package store import ( "encoding/base64" "encoding/json" + "errors" log "github.com/sirupsen/logrus" "github.com/vouv/srun/model" "io/ioutil" @@ -13,12 +14,16 @@ import ( const accountFileName = "account.json" +// 读取账号文件错误 +var ErrReadFile = errors.New("读取账号文件错误") +var ErrWriteFile = errors.New("写入账号文件错误") +var ErrParse = errors.New("序列化账号错误") + var RootPath string // 写入账号信息到文件 // 统一错误 func SetAccount(username, password string) (err error) { - //write to local dir account, err := ReadAccount() if err != nil { @@ -66,7 +71,7 @@ func ReadAccount() (account model.Account, err error) { err = ErrReadFile return } - + // decode base64 decoded, err := base64.StdEncoding.DecodeString(string(readed)) if err != nil { log.Debugf("解析账号文件错误, %s", err) @@ -103,7 +108,7 @@ func WriteAccount(account model.Account) (err error) { err = ErrParse return } - // b64 encode + // encode base64 str := base64.StdEncoding.EncodeToString(jBytes) _, wErr := file.WriteString(str) if wErr != nil { @@ -114,11 +119,6 @@ func WriteAccount(account model.Account) (err error) { return } -// 初始化账号信息 -func InitAccount() error { - return WriteAccount(model.Account{}) -} - func getAccountFilename() (fileSrc string, err error) { storageDir := filepath.Join(RootPath, ".srun") if _, sErr := os.Stat(storageDir); sErr != nil {