-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
166 lines (151 loc) · 3.6 KB
/
router.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
package Mikrotik
import (
"crypto/md5"
"encoding/hex"
"fmt"
"log"
"net"
"strings"
"time"
)
type Router struct {
connection net.Conn
response map[string]chan Sentence
defaultresponse chan Sentence
idList chan string
}
func New() *Router {
maxId := 100
res := &Router{
nil,
make(map[string]chan Sentence),
make(chan Sentence),
make(chan string, maxId),
}
for k := 0; k < maxId; k++ {
res.idList <- fmt.Sprint(k)
}
return res
}
func (r *Router) Connect(addr string) error {
var err error
var d net.Dialer
d.Timeout = time.Second * 10
r.connection, err = d.Dial("tcp", addr)
if err == nil {
go func() {
for {
//Get length, maximal 5 bytes
sentence := &Sentence{}
var id string = ""
sentenceDone := false
for !sentenceDone {
var lengthbytes []byte = make([]byte, 5)
var length int
for k := 0; k < 5; k++ {
_, err := r.connection.Read(lengthbytes[k : k+1])
if err != nil {
//Panic??
//How do we report this error?
log.Println(err)
return
}
if length, _, err = decodelength(lengthbytes[:k+1]); err == nil {
lengthbytes = lengthbytes[:k+1]
break
}
}
if length == 0 {
sentenceDone = true
continue
}
var wordbytes []byte = make([]byte, length)
for length > 0 {
var n int
n, err = r.connection.Read(wordbytes[len(wordbytes)-length:])
length -= n
}
if err != nil {
//Panic??
//How do we report this error?
panic(err)
}
word, bl, err := Decode(append(lengthbytes, wordbytes...))
if (err != nil) || (bl != (len(lengthbytes) + len(wordbytes))) {
//Panic??
//How do we report this error?
panic(err)
}
if strings.Index(string(word), ".tag=") == 0 {
id = string(word[len(".tag="):])
}
sentence.Add(word)
}
if len(*sentence) == 0 {
log.Println("len(*sentence) == 0 ")
continue
}
if id != "" {
r.response[id] <- *sentence
} else {
}
if (sentence.Contains(Word("!done")) /*|| sentence.Contains(Word("!trap"))*/) && (id != "") {
close(r.response[id])
r.idList <- id
}
}
}()
}
return err
}
func (r *Router) Login(user, pass string) error {
login := make(Sentence, 0)
login.Add(Command("/login"))
c, _, err := r.SendSentence(login)
if err != nil {
return err
}
chalSen := <-c
if !chalSen.Contains(Word("=ret=")) {
return fmt.Errorf("Response did not have challenge %v", []Word(chalSen))
}
retW := chalSen[chalSen.Index("=ret=")]
chal, err := hex.DecodeString(strings.Split(string(retW[1:]), "=")[1])
if err != nil {
return err
}
Sum := md5.New()
hashBytes := append([]byte{0}, []byte(pass)...)
hashBytes = append(hashBytes, []byte(chal)...)
_, err = Sum.Write(hashBytes)
if err != nil {
return err
}
resp := Sum.Sum(nil)
login.Add(Attribute("name", user))
login.Add(Attribute("response", "00"+hex.EncodeToString(resp)))
c, _, err = r.SendSentence(login)
if err != nil {
return err
}
chalSen = <-c
if chalSen.Contains(Word("!trap")) {
return fmt.Errorf("%v", []Word(chalSen))
}
return nil
}
func (r *Router) SendSentence(mess Sentence) (response chan Sentence, tag string, err error) {
if r.connection == nil {
return nil, "", fmt.Errorf("Router not connected")
}
tag = <-r.idList
response = make(chan Sentence)
mess.Add(APIAttribute("tag", tag))
r.response[tag] = response
r.connection.SetWriteDeadline(time.Now().Add(time.Second * 10))
_, err = r.connection.Write(mess.Encode())
return response, tag, err
}
func (r *Router) Close() error {
return r.connection.Close()
}