Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vouv committed Nov 2, 2020
1 parent 5131396 commit d3f69b1
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 129 deletions.
21 changes: 7 additions & 14 deletions cmd/srun/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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>")

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions cmd/srun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
)

const Version = "v0.1.26"
const Version = "v1.0.0"

var loginCmd = &cobra.Command{
Use: "login",
Expand Down Expand Up @@ -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)
},
}

Expand Down
21 changes: 9 additions & 12 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
34 changes: 34 additions & 0 deletions model/request.go
Original file line number Diff line number Diff line change
@@ -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},
}
}
86 changes: 17 additions & 69 deletions model/model.go → model/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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))
Expand All @@ -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"`
}
14 changes: 2 additions & 12 deletions resp/resp.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand Down
8 changes: 0 additions & 8 deletions store/errors.go

This file was deleted.

16 changes: 8 additions & 8 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package store
import (
"encoding/base64"
"encoding/json"
"errors"
log "github.com/sirupsen/logrus"
"github.com/vouv/srun/model"
"io/ioutil"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit d3f69b1

Please sign in to comment.