Skip to content

Commit

Permalink
调整代码
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeEirc committed Dec 2, 2020
1 parent d06be0e commit 13fca1f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions _example/telnet_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func init() {

func main() {
flag.Parse()
conf := tclientlib.ClientConfig{
User: username,
conf := tclientlib.Config{
Username: username,
Password: password,
Timeout: 30 * time.Second,
}
Expand Down
14 changes: 7 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const defaultTimeout = time.Second * 15

type Client struct {
conf *ClientConfig
conf *Config
sock net.Conn
enableWindows bool
autoLogin bool
Expand Down Expand Up @@ -48,7 +48,7 @@ func (c *Client) loginAuthentication() error {

func (c *Client) handleLoginData(data []byte) AuthStatus {
if c.conf.UsernameRegex.Match(data) {
_, _ = c.sock.Write([]byte(c.conf.User + "\r\n"))
_, _ = c.sock.Write([]byte(c.conf.Username + "\r\n"))
log.Printf("Username pattern match: %s \n", bytes.TrimSpace(data))
return AuthPartial
} else if c.conf.PasswordRegex.Match(data) {
Expand Down Expand Up @@ -132,11 +132,11 @@ func (c *Client) handleOptionPacket(p OptionPacket) OptionPacket {
case 1:
switch p.CommandCode {
case OLD_ENVIRON, NEW_ENVIRON:
if c.conf.User != "" {
if c.conf.Username != "" {
replyPacket.Parameters = append(replyPacket.Parameters, 0)
replyPacket.Parameters = append(replyPacket.Parameters, []byte("USER")...)
replyPacket.Parameters = append(replyPacket.Parameters, 1)
replyPacket.Parameters = append(replyPacket.Parameters, []byte(c.conf.User)...)
replyPacket.Parameters = append(replyPacket.Parameters, []byte(c.conf.Username)...)
}
case TSPEED:
replyPacket.Parameters = append(replyPacket.Parameters, 0)
Expand Down Expand Up @@ -215,19 +215,19 @@ func (c *Client) WindowChange(w, h int) error {

}

func Dial(network, addr string, config *ClientConfig) (*Client, error) {
func Dial(network, addr string, config *Config) (*Client, error) {
conn, err := net.DialTimeout(network, addr, config.Timeout)
if err != nil {
return nil, err
}
return NewClientConn(conn, config)
}

func NewClientConn(conn net.Conn, config *ClientConfig) (*Client, error) {
func NewClientConn(conn net.Conn, config *Config) (*Client, error) {
fullConf := *config
fullConf.SetDefaults()
var autoLogin bool
if config.User != "" && config.Password != "" {
if config.Username != "" && config.Password != "" {
autoLogin = true
}
client := &Client{
Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func defaultTerminalOptions() TerminalOptions {
}
}

type ClientConfig struct {
User string
type Config struct {
Username string
Password string
Timeout time.Duration
TTYOptions *TerminalOptions
Expand All @@ -31,7 +31,7 @@ type ClientConfig struct {
LoginSuccessRegex *regexp.Regexp
}

func (conf *ClientConfig) SetDefaults() {
func (conf *Config) SetDefaults() {
if conf.Timeout == 0 || conf.Timeout < defaultTimeout {
conf.Timeout = defaultTimeout
}
Expand Down

0 comments on commit 13fca1f

Please sign in to comment.