This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: spilt code and make it more elegant
- Loading branch information
Showing
6 changed files
with
201 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.