Skip to content

Commit

Permalink
Add retry logic for factory.New.Handle() in run function
Browse files Browse the repository at this point in the history
Implemented a retry mechanism with a maximum of 10 attempts in the `run` function to handle failures from `factory.New.Handle()`. Removed unnecessary randomness and updated the `factory` package by simplifying the `CheckLoginAuth` and `FactoryMode` commands.
  • Loading branch information
Septrum101 committed Jul 25, 2024
1 parent 70c8658 commit 99908e2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
19 changes: 8 additions & 11 deletions app/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"math/rand"
"net/url"
"strconv"
"strings"
Expand All @@ -15,8 +14,6 @@ import (
"github.com/thank243/zteOnu/utils"
)

var randTime int

func New(user string, passwd string, ip string, port int, newMode bool) *Factory {
return &Factory{
user: user,
Expand All @@ -30,6 +27,11 @@ func New(user string, passwd string, ip string, port int, newMode bool) *Factory
}

func (f *Factory) reset() error {
// active onu web service first, increase the chances of success
if _, err := f.cli.R().Get("/"); err != nil {
return err
}

resp, err := f.cli.R().SetBody("SendSq.gch").Post("webFac")
if err != nil {
return err
Expand Down Expand Up @@ -80,9 +82,7 @@ func (f *Factory) sendSq() (uint8, error) {
func (f *Factory) checkLoginAuth() error {
command := fmt.Sprintf("CheckLoginAuth.gch?version50&user=%s&pass=%s", f.user, f.passwd)
if f.newMode {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randTime = r.Intn(1000)
command = fmt.Sprintf("CheckLoginAuth.gch?time%d&version61&user=%s&pass=%s", randTime, f.user, f.passwd)
command = fmt.Sprintf("CheckLoginAuth.gch?&version61&user=%s&pass=%s", f.user, f.passwd)
}
payload, err := utils.ECBEncrypt(
[]byte(command), f.key)
Expand Down Expand Up @@ -111,6 +111,7 @@ func (f *Factory) checkLoginAuth() error {

func (f *Factory) sendInfo() error {
command := []byte("SendInfo.gch?info=6|")

if f.newMode {
command = []byte("SendInfo.gch?info=12|")
magicBytes, err := base64.StdEncoding.DecodeString(magicBytesBase64)
Expand Down Expand Up @@ -143,11 +144,7 @@ func (f *Factory) sendInfo() error {

func (f *Factory) factoryMode() (user string, pass string, err error) {
command := "FactoryMode.gch?mode=2&user=notused"
if f.newMode {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
rt := r.Intn(1000-randTime) + randTime
command = fmt.Sprintf("FactoryMode.gch?time%d&mode=2&user=fuckyou", rt)
}

payload, err := utils.ECBEncrypt([]byte(command), f.key)
if err != nil {
return
Expand Down
20 changes: 17 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,23 @@ func init() {
func run() error {
version.Show()

tlUser, tlPass, err := factory.New(user, passwd, ip, port, version61).Handle()
if err != nil {
return err
var (
tlUser, tlPass string
err error
count uint8
)

for {
tlUser, tlPass, err = factory.New(user, passwd, ip, port, version61).Handle()
if err != nil {
count++
fmt.Println(err, fmt.Sprintf("Attempt retrying..(%d/10)", count))
if count > 10 {
return err
}
continue
}
break
}

if permTelnet {
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

var (
Version = "0.0.5"
Version = "0.0.6"
AppName = "ZteONU"
Intro = "github.com/thank243/zteOnu"
)
Expand Down

0 comments on commit 99908e2

Please sign in to comment.