perf: ws模块telnet分开处理避免类型指针错误导致panic程序崩溃

This commit is contained in:
TsMask
2024-08-07 19:34:27 +08:00
parent 0f98508169
commit a5363b1ce1
5 changed files with 81 additions and 71 deletions

View File

@@ -50,10 +50,12 @@ func (c *ConnTelnet) NewClient() (*ConnTelnet, error) {
// fmt.Fprintln(client, c.User)
// fmt.Fprintln(client, c.Password)
c.Client = &client
// 调整窗口大小 (120 列 x 128 行)
requestPty(c.Client, 120, 128)
// 需要确保接收方理解并正确处理发送窗口大小设置命令
client.Write([]byte{255, 251, 31})
client.Write([]byte{255, 250, 31, byte(120 >> 8), byte(120 & 0xFF), byte(128 >> 8), byte(128 & 0xFF), 255, 240})
c.Client = &client
// 排空连接登录的信息
c.RunCMD("")
@@ -111,20 +113,9 @@ func (c *ConnTelnet) NewClientSession(cols, rows int) (*TelnetClientSession, err
if c.Client == nil {
return nil, fmt.Errorf("telnet client not connected")
}
requestPty(c.Client, cols, rows)
return &TelnetClientSession{
s := &TelnetClientSession{
Client: *c.Client,
}, nil
}
// requestPty 调整终端窗口大小
func requestPty(client *net.Conn, cols, rows int) error {
if client == nil {
return fmt.Errorf("telnet client not connected")
}
conn := *client
// 需要确保接收方理解并正确处理发送窗口大小设置命令
conn.Write([]byte{255, 251, 31})
conn.Write([]byte{255, 250, 31, byte(cols >> 8), byte(cols & 0xFF), byte(rows >> 8), byte(rows & 0xFF), 255, 240})
return nil
s.WindowChange(cols, rows)
return s, nil
}

View File

@@ -19,6 +19,17 @@ func (s *TelnetClientSession) Close() {
}
}
// WindowChange informs the remote host about a terminal window dimension change to h rows and w columns.
func (s *TelnetClientSession) WindowChange(h, w int) error {
if s.Client == nil {
return fmt.Errorf("client is nil to content write failed")
}
// 需要确保接收方理解并正确处理发送窗口大小设置命令
s.Client.Write([]byte{255, 251, 31})
s.Client.Write([]byte{255, 250, 31, byte(w >> 8), byte(w & 0xFF), byte(h >> 8), byte(h & 0xFF), 255, 240})
return nil
}
// Write 写入命令 不带回车(\n)也会执行根据客户端情况
func (s *TelnetClientSession) Write(cmd string) (int, error) {
if s.Client == nil {