Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
refactor: spilt code and make it more elegant
Browse files Browse the repository at this point in the history
  • Loading branch information
sky96111 committed May 2, 2023
1 parent 2c4b25b commit c644706
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 166 deletions.
17 changes: 17 additions & 0 deletions utils/Decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

import (
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"log"
)

func GbkToUtf8(resp io.ReadCloser) string {
gbkBodyText := transform.NewReader(resp, simplifiedchinese.GBK.NewDecoder())
bodyText, err := io.ReadAll(gbkBodyText)
if err != nil {
log.Panicln(bodyText)
}
return string(bodyText)
}
40 changes: 40 additions & 0 deletions utils/GetAccount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package utils

import (
"errors"
"io"
"log"
"net/http"
"strconv"
"strings"
)

func GetAccount(client *http.Client) (int, float64, error) {
req, _ := http.NewRequest("GET", "http://192.168.255.19/", nil)
resp, err := client.Do(req)
if err != nil {
return 0, 0, err
}

defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println(err)
}
}(resp.Body)
bodyText := GbkToUtf8(resp.Body)

time := strings.Split(strings.Split(bodyText, "time='")[1], "';")[0]
flow := strings.Split(strings.Split(bodyText, "flow='")[1], "';")[0]
time = strings.Trim(time, " ")
flow = strings.Trim(flow, " ")

timeInt, _ := strconv.Atoi(time)
flowFloat64, _ := strconv.ParseFloat(flow, 64)

if strings.Contains(bodyText, `location.href="0.htm"`) {
return 0, 0, errors.New("device is not authed")
} else {
return timeInt, flowFloat64, nil
}
}
46 changes: 46 additions & 0 deletions utils/Login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package utils

import (
"errors"
"io"
"log"
"net/http"
"strings"
)

func Login(client *http.Client, username string, password string) error {
log.Println("login as", username, "...")

var data = strings.NewReader(`DDDDD=` +
username +
`&upass=` +
password +
`&0MKKey=%B5%C7%C2%BC+Login`)
req, _ := http.NewRequest("POST", "http://192.168.255.19/0.htm", data)
resp, err := client.Do(req)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println(err)
}
}(resp.Body)
bodyText := GbkToUtf8(resp.Body)

switch {
case strings.Contains(bodyText, "You have successfully logged into our system."):
return nil
case strings.Contains(bodyText, "msga='userid error1'"):
return errors.New("username " + username + " not exist")
case strings.Contains(bodyText, "msga='userid error2'"):
return errors.New("this account is banned")
case strings.Contains(bodyText, "msga='ldap auth error'"):
return errors.New("username or password error")
case strings.Contains(bodyText, "msga='[02], 本帐号只能在指定 IP 段使用'"):
return errors.New("this account only available in specified IP range")
default:
return errors.New("unknown error")
}
}
32 changes: 32 additions & 0 deletions utils/Logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

import (
"errors"
"io"
"log"
"net/http"
"strings"
)

func Logout(client *http.Client) error {
log.Println("logout...")

req, _ := http.NewRequest("GET", "http://192.168.255.19/F.htm", nil)
resp, err := client.Do(req)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println(err)
}
}(resp.Body)
bodyText := GbkToUtf8(resp.Body)

if strings.Contains(bodyText, "Msg=14") {
return nil
} else {
return errors.New("unknown error")
}
}
32 changes: 32 additions & 0 deletions utils/Status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

import (
"log"
"net/http"
)

func Status(client *http.Client) error {
time, flow, err := GetAccount(client)
if err != nil {
return err
}

if time < 60 {
log.Println("time:", time, "Min")
} else if time < 60*24 {
log.Println("time:", time/60, "Hour")
} else {
log.Println("time:", time/60/24, "Day")
}

flow /= 1024
if flow < 1024 {
log.Println("flow:", flow, "MB")
} else if flow < 1024*1024 {
log.Println("flow:", flow/1024, "GB")
} else {
log.Println("flow:", flow/1024/1024, "TB")
}

return nil
}
Loading

0 comments on commit c644706

Please sign in to comment.