Skip to content

Commit

Permalink
fix(info): adapt to new page dom structure (#51)
Browse files Browse the repository at this point in the history
also:
1. prevent adding duplicated account
2. show the current plan stage
  • Loading branch information
ShiMaRing authored Oct 8, 2022
1 parent 69899c0 commit d6a85cd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ var (
password := ctx.String("password")
if password != "" {
if err = account.SetPassword(ctx.String("password"), []byte(ctx.String("secret"))); err != nil {
return fmt.Errorf("fail to set account:\n\t'%s' not found", username)
return fmt.Errorf("fail to set password:\n\t'%v'", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (i *infoPrinter) PrintDevices() {
return
}
for _, device := range devices {
console.InfoF("\tNo.%d\t%s\t%s\t%s\n", device.ID, device.IP, device.StartTime, device.SID)
console.InfoF("\tNo.%d\t%s\t%s\t%8s\t%s\n", device.ID, device.IP, device.StartTime, device.SID, device.Stage)
}
}

Expand All @@ -154,7 +154,7 @@ func (i *infoPrinter) PrintLog(page int) {
return
}
for _, record := range records {
console.InfoF("\t%s - %s\t%s\t%s\n", record.StartTime, record.EndTime, record.IP, record.Traffic)
console.InfoF("\t%s - %s\t%s\t%10s\t%s\n", record.StartTime, record.EndTime, record.IP, record.Traffic, record.UsedDuration)
}
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/handler/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (d *DashboardHandler) GetPackage() (*Package, error) {
return nil, err
}

infos, _ := utils.MatchMultiple(regexp.MustCompile(`<td data-col-seq="3">(.+?)</td><td data-col-seq="4">(.+?)</td><td data-col-seq="6">(.+?)</td><td data-col-seq="7">(.+?)</td></tr>`), body)
infos, _ := utils.MatchMultiple(regexp.MustCompile(`<td data-col-seq="3">(.+?)</td><td data-col-seq="4">(.+?)</td><td data-col-seq="6">(.+?)</td><td data-col-seq="7">(.+?)</td>`), body)
if len(infos) < 1 {
return nil, errors.New("fail to get package info")
}
Expand All @@ -140,6 +140,7 @@ type Device struct {
ID int
IP string
StartTime string
Stage string
SID string
}

Expand All @@ -148,10 +149,10 @@ func (d *DashboardHandler) GetDevice() ([]Device, error) {
if err != nil {
return []Device{}, err
}
ds, _ := utils.MatchMultiple(regexp.MustCompile(`<tr data-key="(\d+)"><td data-col-seq="0">\d+</td><td data-col-seq="1">(.+?)</td><td data-col-seq="3">(.+?)</td><td data-col-seq="9">.+?</td>`), body)
ds, _ := utils.MatchMultiple(regexp.MustCompile(`<tr data-key="(\d+)"><td data-col-seq="0">\d+</td><td data-col-seq="1">(.+?)</td><td data-col-seq="3">(.+?)</td><td data-col-seq="7">(.+?)</td><td data-col-seq="9">.+?</td>`), body)
result := make([]Device, len(ds))
for i, device := range ds {
result[i] = Device{i, device[2], device[3], device[1]}
result[i] = Device{i, device[2], device[3], device[4], device[1]}
}
return result, nil
}
Expand Down Expand Up @@ -207,7 +208,7 @@ func (d *DashboardHandler) GetUsageRecords(page int) ([]UsageRecord, error) {
return []UsageRecord{}, errors.New("error occurs when parsing usage log page")
}

hs, _ := utils.MatchMultiple(regexp.MustCompile(`<td data-col-seq="1">(.+?)</td><td data-col-seq="2">(.+?)</td><td data-col-seq="5">(.+?)</td><td data-col-seq="10">.+?</td><td data-col-seq="12">.+?</td><td style="display: none;" data-col-seq="14">.+?</td><td data-col-seq="15">(.+?)</td><td style="display: none;" data-col-seq="16">.+?</td><td data-col-seq="17">(.+?)</td><td data-col-seq="18">.+?</td></tr>`), body)
hs, _ := utils.MatchMultiple(regexp.MustCompile(`<td data-col-seq="0">.+?</td><td data-col-seq="1">(.+?)</td><td data-col-seq="2">(.+?)</td><td data-col-seq="5">(.+?)</td><td data-col-seq="10">.+?</td><td data-col-seq="12">.+?</td><td style="display: none;" data-col-seq="16">.+?</td><td data-col-seq="17">(.+?)</td><td style="display: none;" data-col-seq="18">.+?</td><td data-col-seq="19">(.+?)</td><td data-col-seq="20">.+?</td>`), body)
result := make([]UsageRecord, len(hs))
for i, h := range hs {
result[i] = UsageRecord{h[1], h[2], h[3], h[4], h[5]}
Expand Down
10 changes: 9 additions & 1 deletion pkg/model/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package model

import "github.com/neucn/ipgw/pkg/utils"
import (
"fmt"
"github.com/neucn/ipgw/pkg/utils"
)

type Config struct {
DefaultAccount string `json:"default_account"`
Accounts []*Account `json:"accounts"`
}

func (c *Config) AddAccount(username, password, secret string) error {
for _, account := range c.Accounts {
if account.Username == username {
return fmt.Errorf("account %s already exists \n", username)
}
}
encryptedPassword, err := utils.Encrypt([]byte(password), []byte(secret))
if err != nil {
return err
Expand Down

0 comments on commit d6a85cd

Please sign in to comment.