-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.go
129 lines (105 loc) · 3.01 KB
/
conn.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
// Copyright © 2019 Free Chess Club <hi@freechess.club>
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package icsgo
import (
"bytes"
"fmt"
"log"
"time"
"github.com/ziutek/telnet"
)
const (
timesealHello = "TIMESEAL2|freeseal|icsgo|"
)
// Conn represents a connection to the ICS server
type Conn struct {
// whether timeseal is enabled on the connection
timeseal bool
// whether debug/verbose logging is enabled
debug bool
// the underlying telnet connection
conn *telnet.Conn
}
// Dial creates a new connection
func Dial(addr string, retries int, timeout time.Duration, timeseal, debug bool) (*Conn, error) {
connected := false
var conn *telnet.Conn
var err error
for attempts := 1; attempts <= retries && connected != true; attempts++ {
log.Printf("connecting to ICS server %s (attempt %d of %d)...", addr, attempts, retries)
conn, err = telnet.DialTimeout("tcp", addr, timeout)
if err != nil {
timeout = time.Duration(float64(timeout) * 1.5)
continue
}
connected = true
}
if err != nil || connected == false {
return nil, fmt.Errorf("connecting to server %s: %v", addr, err)
}
log.Printf("connected to ICS server %s! (timeseal: %t)", addr, timeseal)
c := &Conn{
timeseal: timeseal,
debug: debug,
conn: conn,
}
if timeseal {
c.Write([]byte(timesealHello))
}
return c, nil
}
// ReadUntilTimeout reads messages from the connection until the given prompt is encountered
// or until the given timeout duration has surpassed
func (c *Conn) ReadUntilTimeout(prompt string, timeout time.Duration) ([]byte, error) {
c.conn.SetReadDeadline(time.Now().Add(timeout))
bs, err := c.conn.ReadUntil(prompt)
if err != nil {
return nil, err
}
if c.debug {
log.Printf("< %s", string(bs))
}
if c.timeseal {
for {
i := bytes.Index(bs, []byte{'[', 'G', ']', 0x00})
if i == -1 {
break
}
c.Write([]byte{0x02, 0x39})
bs = append(bs[:i], bs[i+4:]...)
}
}
bs = bytes.Replace(bs, []byte("\u0007"), []byte{}, -1)
bs = bytes.Replace(bs, []byte("\x00"), []byte{}, -1)
bs = bytes.Replace(bs, []byte("\\ "), []byte{}, -1)
bs = bytes.Replace(bs, []byte("\r"), []byte{}, -1)
bs = bytes.Replace(bs, []byte(prompt), []byte{}, -1)
bs = bytes.TrimSpace(bs)
return bs, nil
}
// ReadUntil reads messages from the connection until the given prompt is encountered
func (c *Conn) ReadUntil(prompt string) ([]byte, error) {
return c.ReadUntilTimeout(prompt, 3600*time.Second)
}
// Write writes the given message on the open connection
func (c *Conn) Write(msg []byte) error {
if c.debug {
log.Printf("> %s", string(msg))
}
if c.timeseal {
msg = encode(msg, len(msg))
}
return c.RawWrite(msg)
}
// RawWrite writes the given message on the open connection without timeseal encoding
func (c *Conn) RawWrite(msg []byte) error {
c.conn.SetWriteDeadline(time.Now().Add(20 * time.Second))
_, err := c.conn.Conn.Write(msg)
return err
}
// Close closes the connection to the ICS server
func (c *Conn) Close() {
c.conn.Close()
}