-
Notifications
You must be signed in to change notification settings - Fork 0
/
telnet_client.go
235 lines (194 loc) · 4.85 KB
/
telnet_client.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"net"
"os"
"syscall"
"time"
)
var xtrn string
var from string
const defaultBufferSize = 4096
// TelnetClient represents a TCP client which is responsible for writing input data and printing response.
type TelnetClient struct {
destination *net.TCPAddr
responseTimeout time.Duration
}
// NewTelnetClient method creates new instance of TCP client.
func NewTelnetClient(options Options) *TelnetClient {
tcpAddr := createTCPAddr(options)
resolved := resolveTCPAddr(tcpAddr)
return &TelnetClient{
destination: resolved,
responseTimeout: options.Timeout(),
}
}
func createTCPAddr(options Options) string {
// dropFileData()
var buffer bytes.Buffer
buffer.WriteString(options.Host())
buffer.WriteByte(':')
buffer.WriteString(fmt.Sprintf("%d", options.Port()))
return buffer.String()
}
func resolveTCPAddr(addr string) *net.TCPAddr {
resolved, error := net.ResolveTCPAddr("tcp", addr)
if nil != error {
log.Fatalf("Error occured while resolving TCP address \"%v\": %v\n", addr, error)
}
return resolved
}
// ProcessData method processes data: reads from input and writes to output.
func (t *TelnetClient) ProcessData(inputData io.Reader, outputData io.Writer) {
connection, error := net.DialTCP("tcp", nil, t.destination)
if nil != error {
log.Fatalf("Error occured while connecting to address \"%v\": %v\n", t.destination.String(), error)
}
defer connection.Close()
requestDataChannel := make(chan []byte)
doneChannel := make(chan bool)
responseDataChannel := make(chan []byte)
go t.readInputData(inputData, requestDataChannel, doneChannel)
go t.readServerData(connection, responseDataChannel)
var afterEOFResponseTicker = new(time.Ticker)
var afterEOFMode bool
var somethingRead bool
for {
select {
case request := <-requestDataChannel:
if _, error := connection.Write(request); nil != error {
log.Fatalf("Error occured while writing to TCP socket: %v\n", error)
}
case <-doneChannel:
afterEOFMode = true
afterEOFResponseTicker = time.NewTicker(t.responseTimeout)
case response := <-responseDataChannel:
outputData.Write([]byte(fmt.Sprintf("%v", string(response))))
somethingRead = true
if afterEOFMode {
afterEOFResponseTicker.Stop()
afterEOFResponseTicker = time.NewTicker(t.responseTimeout)
}
case <-afterEOFResponseTicker.C:
if !somethingRead {
log.Println("Nothing read. Maybe connection timeout.")
}
return
}
}
}
func (t *TelnetClient) readInputData(inputData io.Reader, toSent chan<- []byte, doneChannel chan<- bool) {
buffer := make([]byte, defaultBufferSize)
var error error
var n int
reader := bufio.NewReader(inputData)
for nil == error {
n, error = reader.Read(buffer)
toSent <- buffer[:n]
}
t.assertEOF(error)
doneChannel <- true
}
func (t *TelnetClient) readServerData(connection *net.TCPConn, received chan<- []byte) {
buffer := make([]byte, defaultBufferSize)
var error error
var n int
assignArgs()
// Send data to Rlogin
fmt.Fprintf(connection, "\x00%s\x00%s\x00%s\x00", "", from, xtrn)
for nil == error {
n, error = connection.Read(buffer)
received <- buffer[:n]
}
go startPolling1(connection)
t.assertEOF(error)
}
func (t *TelnetClient) assertEOF(error error) {
if "EOF" != error.Error() {
log.Fatalf("Error occured while operating on TCP socket: %v\n", error)
}
}
func assignArgs() {
from = os.Args[4]
xtrn = os.Args[3]
}
// func dropFileData() {
// from = os.Args[5]
// xtrn = os.Args[4]
// drop := os.Args[3]
// file, err := os.Open(drop)
// if err != nil {
// log.Fatal(err)
// }
// scanner := bufio.NewScanner(file)
// scanner.Split(bufio.ScanLines)
// var text []string
// for scanner.Scan() {
// text = append(text, scanner.Text())
// }
// file.Close()
// count := 0
// for _, eachLn := range text {
// if count == 35 {
// name = eachLn
// }
// if count == 51 {
// break
// }
// count++
// }
// if err := scanner.Err(); err != nil {
// log.Fatal(err)
// }
// return
// }
func connCheck(conn net.Conn) error {
var sysErr error = nil
rc, err := conn.(syscall.Conn).SyscallConn()
if err != nil {
conn.Close()
os.Exit(1)
return err
}
err = rc.Read(func(fd uintptr) bool {
var buf []byte = []byte{0}
n, _, err := syscall.Recvfrom(int(fd), buf, syscall.MSG_PEEK|syscall.MSG_DONTWAIT)
switch {
case n == 0 && err == nil:
sysErr = io.EOF
log.Println(sysErr)
conn.Close()
os.Exit(1)
case err == syscall.EAGAIN || err == syscall.EWOULDBLOCK:
sysErr = nil
conn.Close()
os.Exit(1)
log.Println(sysErr)
default:
sysErr = err
conn.Close()
os.Exit(1)
log.Println(err)
}
return true
})
if err != nil {
log.Println(err)
conn.Close()
os.Exit(1)
return err
}
conn.Close()
os.Exit(1)
return sysErr
}
func startPolling1(conn net.Conn) {
for {
time.Sleep(500 * time.Millisecond)
go connCheck(conn)
}
}