This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtelnet.go
139 lines (126 loc) · 3.7 KB
/
telnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (C) 2017,2019 Steve Merrony
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"bufio"
"fmt"
"log"
"net"
"strconv"
"time"
)
const (
telnetCmdSE = 240
telnetCmdNOP = 241
telnetCmdDM = 242
telnetCmdBRK = 243
telnetCmdIP = 244
telnetCmdAO = 245
telnetCmdAYT = 246
telnetCmdEC = 247
telnetCmdEL = 248
telnetCmdGA = 249
telnetCmdSB = 250
telnetCmdWILL = 251
telnetCmdWONT = 252
telnetCmdDO = 253
telnetCmdDONT = 254
telnetCmdIAC = 255
telnetOptBIN = 0
telnetOptECHO = 1
telnetOptRECON = 2
telnetOptSGA = 3
telnetOptSTATUS = 5
telnetOptCOLS = 8
telnetOptROWS = 9
telnetOptEASCII = 17
telnetOptLOGOUT = 18
telnetOptTTYPE = 24
telnetOptNAWS = 31 // window size
telnetOptTSPEED = 32
telnetOptXDISP = 35
telnetOptNEWENV = 39
dialTimeout = time.Second * 10
)
type telnetSessionT struct {
conn net.Conn
stopTelnetWriterChan chan bool
}
func newTelnetSession() *telnetSessionT {
sess := new(telnetSessionT)
sess.stopTelnetWriterChan = make(chan bool)
return sess
}
func (sess *telnetSessionT) openTelnetConn(hostName string, portNum int) bool {
var err error
hostString := hostName + ":" + strconv.Itoa(portNum)
sess.conn, err = net.DialTimeout("tcp", hostString, dialTimeout)
if err != nil {
return false
}
go sess.telnetReader(fromHostChan)
go sess.telnetWriter(keyboardChan)
terminal.rwMutex.Lock()
terminal.connectionType = telnetConnected
terminal.remoteHost = hostName
terminal.remotePort = strconv.Itoa(portNum)
terminal.rwMutex.Unlock()
return true
}
func (sess *telnetSessionT) closeTelnetConn() {
sess.conn.Close()
select {
case sess.stopTelnetWriterChan <- true:
default:
}
terminal.rwMutex.Lock()
terminal.connectionType = disconnected
terminal.rwMutex.Unlock()
}
func (sess *telnetSessionT) telnetReader(hostChan chan<- []byte) {
fmt.Println("INFO: telnetReader starting")
for {
hostBytes := make([]byte, hostBuffSize)
n, err := sess.conn.Read(hostBytes)
if n == 0 {
//log.Fatalf("telnet got zero-byte message from host")
fmt.Println("INFO: telnetReader got zero length message, stopping")
telnetClose()
return
}
if err != nil {
log.Fatal("telnetReader got errror reading from host ", err.Error())
}
//fmt.Printf("telentReader got <%s> from host\n", hostBytes)
hostChan <- hostBytes[:n]
}
}
func (sess *telnetSessionT) telnetWriter(kbdChan <-chan byte) {
fmt.Println("INFO: telnetWriter starting")
writer := bufio.NewWriter(sess.conn)
for {
select {
case k := <-kbdChan:
writer.Write([]byte{k})
writer.Flush()
//fmt.Printf("Wrote <%d> to host\n", k)
case <-sess.stopTelnetWriterChan:
fmt.Println("INFO: telnetWriter stopping")
return
}
}
}