Skip to content

Commit

Permalink
Refactor Telnet connection handling
Browse files Browse the repository at this point in the history
Adjusted the Telnet connection process to better handle errors and improve safety. The connection is now established within the New() function and returned with proper error state. This mitigates the risk of working with null or improperly closed connections which existed in the earlier approach.
  • Loading branch information
Septrum101 committed Sep 22, 2023
1 parent 1ecbf70 commit b8c06b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
3 changes: 1 addition & 2 deletions app/telnet/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ const ctrl = "\r\n"
type Telnet struct {
user string
pass string
ip string
conn net.Conn
Conn net.Conn
}
31 changes: 11 additions & 20 deletions app/telnet/telnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@ import (
"fmt"
"net"
"strings"
"time"
)

func New(user string, pass string, ip string) *Telnet {
return &Telnet{
func New(user string, pass string, ip string) (*Telnet, error) {
conn, err := net.Dial("tcp", ip+":telnet")
if err != nil {
return nil, err
}

t := &Telnet{
user: user,
pass: pass,
ip: ip,
Conn: conn,
}

return t, nil
}

func (t *Telnet) PermTelnet() error {
conn, err := net.Dial("tcp", t.ip+":telnet")
if err != nil {
return err
}
defer conn.Close()
t.conn = conn

if err := t.loginTelnet(); err != nil {
return err
}
Expand All @@ -31,13 +30,6 @@ func (t *Telnet) PermTelnet() error {
return err
}

// reboot device
fmt.Println("wait reboot..")
time.Sleep(time.Second)
if err := t.Reboot(); err != nil {
return err
}

return nil
}

Expand All @@ -60,14 +52,13 @@ func (t *Telnet) modifyDB() error {
if err := t.sendCmd(lanEnable, tsLanUser, tsLanPwd, maxConn, initSecLvl, save); err != nil {
return err
}
fmt.Println("Permanent Telnet succeed\r\nuser: root, pass: Zte521")

return nil
}

func (t *Telnet) sendCmd(commands ...string) error {
cmd := []byte(strings.Join(commands, ctrl) + ctrl)
n, err := t.conn.Write(cmd)
n, err := t.Conn.Write(cmd)
if err != nil {
return err
}
Expand Down
22 changes: 21 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"time"

"github.com/thank243/zteOnu/app/factory"
"github.com/thank243/zteOnu/app/telnet"
Expand All @@ -28,9 +29,28 @@ func main() {
}

if *permTelnet {
t := telnet.New(tlUser, tlPass, *ip)
// create telnet conn
t, err := telnet.New(tlUser, tlPass, *ip)
if err != nil {
fmt.Println(err)
return
}
defer t.Conn.Close()

// handle permanent telnet
if err := t.PermTelnet(); err != nil {
fmt.Println(err)
return
} else {
fmt.Println("Permanent Telnet succeed\r\nuser: root, pass: Zte521")
}

// reboot device
fmt.Println("wait reboot..")
time.Sleep(time.Second)
if err := t.Reboot(); err != nil {
fmt.Println(err)
return
}
} else {
fmt.Printf("user: %s\npass: %s", tlUser, tlPass)
Expand Down

0 comments on commit b8c06b8

Please sign in to comment.