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

Commit

Permalink
feat: new arg "-D" Daemon mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sky96111 committed May 14, 2023
1 parent 5c5918d commit c24ed51
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 27 deletions.
31 changes: 25 additions & 6 deletions utils/Config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package utils
import (
"github.com/spf13/viper"
"log"
"math/rand"
"time"
)

type Config struct {
Randomize bool
ChangeOnMinutes int
ChangeOnFlow float64
IntervalMinutes int
ChangeOnHours int
ChangeOnGB float64
}

type User struct {
Expand All @@ -28,8 +31,9 @@ func init() {
func CreateConfig() error {
runtimeViper.Set("config", Config{
Randomize: false,
ChangeOnMinutes: 0,
ChangeOnFlow: 0.0,
IntervalMinutes: 5,
ChangeOnHours: 0,
ChangeOnGB: 0.0,
})
runtimeViper.Set("user", []User{
{
Expand All @@ -54,7 +58,9 @@ func CreateConfig() error {
return nil
}

func LoadConfig() (Config, error) {
func loadConfig() (Config, error) {
log.Println("parsing config...")

var config Config

err := runtimeViper.ReadInConfig()
Expand All @@ -70,7 +76,8 @@ func LoadConfig() (Config, error) {
return config, nil
}

func LoadUsers() ([]User, error) {
func loadUsers() ([]User, error) {
log.Println("parsing users...")
err := runtimeViper.ReadInConfig()
if err != nil {
return nil, err
Expand All @@ -91,3 +98,15 @@ func LoadUsers() ([]User, error) {

return enabledUsers, nil
}

func randomizeUserList(users []User) []User {
log.Println("randomize user list...")

source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
r.Shuffle(len(users), func(i, j int) {
users[i], users[j] = users[j], users[i]
})

return users
}
100 changes: 100 additions & 0 deletions utils/Daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package utils

import (
"errors"
"log"
"math/rand"
"net/http"
"time"
)

func Daemon(client *http.Client) error {
log.Println("daemon mode")

ForceReLogin = true
config, err := loadConfig()
users, err := loadUsers()
if err != nil {
return err
}

if !(config.IntervalMinutes > 0) {
return errors.New("IntervalMinutes must be greater than 0")
}

log.Println("###################")
log.Println("IntervalMinutes:", config.IntervalMinutes)

if config.ChangeOnHours <= 0 {
log.Println("ChangeOnHours: disabled")
} else {
log.Println("ChangeOnHours:", config.ChangeOnHours)
}

if config.ChangeOnGB <= 0 {
log.Println("ChangeOnGB: disabled")
} else {
log.Println("ChangeOnGB:", config.ChangeOnGB)
}

log.Println("###################")
log.Println("daemon mode started")

if config.Randomize {
users = randomizeUserList(users)
}

err = LoginUserList(client, users)
if err != nil {
log.Println(err)
}

ticker := time.NewTicker(time.Duration(config.IntervalMinutes) * time.Minute)
for range ticker.C {

minutes, flow, err := GetAccount(client)
if err != nil {
err := LoginUserList(client, users)
if err != nil {
log.Println(err)
}
continue
}

log.Println("hours:", minutes/60)
log.Println("flow:", flow/1024/1024/1024, "GB")

if minutes >= config.ChangeOnHours*60 && config.ChangeOnHours > 0 {
log.Println("change user for time limit reached!")

if config.Randomize {
users = randomizeUserList(users)
}

err := LoginUserList(client, users)
if err != nil {
log.Println(err)
}
} else if flow/1024/1024/1024 >= config.ChangeOnGB && config.ChangeOnGB > 0 {
log.Println("change user for flow limit reached!")

if config.Randomize {
log.Println("randomize user list...")

source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
r.Shuffle(len(users), func(i, j int) {
users[i], users[j] = users[j], users[i]
})
}

err := LoginUserList(client, users)
if err != nil {
log.Println(err)
}
}

}

return nil
}
38 changes: 17 additions & 21 deletions utils/Login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"errors"
"io"
"log"
"math/rand"
"net/http"
"strings"
"time"
)

func Login(client *http.Client, username string, password string) error {
Expand Down Expand Up @@ -52,18 +50,11 @@ func Login(client *http.Client, username string, password string) 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")
return errors.New("login failed")
}
}

func LoginFromConfig(client *http.Client) error {
log.Println("username and password not provided")
log.Println("try parsing from config file...")

users, err := LoadUsers()
if err != nil {
return err
}
func LoginUserList(client *http.Client, users []User) error {

if len(users) == 0 {
return errors.New("no user found")
Expand All @@ -72,21 +63,26 @@ func LoginFromConfig(client *http.Client) error {
log.Println("found", len(users), "user(s)")

if Randomize {
log.Println("randomize user list")

source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
r.Shuffle(len(users), func(i, j int) {
users[i], users[j] = users[j], users[i]
})
users = randomizeUserList(users)
}

for _, user := range users {
if err := Login(client, user.Username, user.Password); err == nil {
return nil
if err := Login(client, user.Username, user.Password); err != nil {
log.Println(err)
continue
}
log.Println("login failed")
return nil
}

return errors.New("all login attempts failed")
}

func LoginFromConfig(client *http.Client) error {
log.Println("username and password not provided")

users, err := loadUsers()
if err != nil {
return err
}
return LoginUserList(client, users)
}
6 changes: 6 additions & 0 deletions zwu.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
optLogout bool
optStatus bool
optCreateConfig bool
optDaemon bool
)

func init() {
Expand All @@ -26,6 +27,7 @@ func init() {
flag.BoolVar(&optLogout, "L", false, "Perform Logout operation")
flag.BoolVar(&optStatus, "S", false, "Perform GetStatus operation")
flag.BoolVar(&optCreateConfig, "C", false, "Create ./zwu.toml template")
flag.BoolVar(&optDaemon, "D", false, "Run as daemon")

flag.Parse()
}
Expand All @@ -51,6 +53,10 @@ func run() error {
return utils.CreateConfig()
}

if optDaemon {
return utils.Daemon(client)
}

if username == "" || password == "" {
return utils.LoginFromConfig(client)
}
Expand Down

0 comments on commit c24ed51

Please sign in to comment.