-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdcc.go
221 lines (190 loc) · 5.37 KB
/
xdcc.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
package main
import "net/url"
import "path"
import "net"
import "fmt"
import "time"
//import "golang.org/x/net/proxy"
import "regexp"
import "os"
import "strconv"
import "encoding/binary"
import "log"
type XDCCDownloadMessage struct {
Progress float32
Message string
Filename string
Err string
Speed int64
}
type XDCC struct {
IRCConn *IRC
Conf *Config
Bot string
Channel string
Package string
Filename string
Quit chan bool
}
func (i *XDCC) awaitFeedbackAfterRequest(ch chan PrivMsg) (string, bool) {
select {
case feedback := <-ch:
return feedback.Content, true
case <-time.After(30 * time.Second):
return "", false
}
}
type SendReq struct {
Filename string
IP string
Port string
Size int64
}
func (i *XDCC) ParseSend(feedback string) *SendReq {
r := regexp.MustCompile(`DCC SEND ((?:"[^"]+")|(?:[^ ]+)) ([0-9]*) ([0-9]*) ([0-9]*)`)
if !r.MatchString(feedback) {
return nil
}
s := r.FindStringSubmatch(feedback)
filename, ip, port, size := s[1], s[2], s[3], s[4]
fmt.Printf("File: %s, ip: %s, port: %s, size: %s", filename, ip, port, size)
a, _ := strconv.ParseInt(ip, 10, 64)
fmt.Println(a)
ipstr := fmt.Sprintf("%d.%d.%d.%d", byte(a>>24), byte(a>>16), byte(a>>8), byte(a))
var sizeI int64
sizeI, _ = strconv.ParseInt(size, 10, 64)
return &SendReq{Filename: filename, IP:ipstr, Port: port, Size: sizeI}
}
func (i *XDCC) quitSignalRecv(prog chan XDCCDownloadMessage) bool {
select {
case <-i.Quit:
prog<-XDCCDownloadMessage{Err: "quit"}
return true
default:
return false
}
}
func (i *XDCC) Download(prog chan XDCCDownloadMessage, tempdir string) bool {
OfferMatchesDesired := func (offer string) bool {
parsed := i.ParseSend(offer)
if parsed == nil {
return false
}
fmt.Println("got a dcc send")
return (parsed.Filename == i.Filename)
}
if (!i.IRCConn.JoinChannel(i.Channel)) {
prog<- XDCCDownloadMessage{Err: "Joining channel failed"}
return false
}
awaitFeedback := make(chan PrivMsg)
fmt.Println("Nick: ", i.IRCConn.Nick)
i.IRCConn.SubscriptionCh <- PrivMsgSubscription{To: i.IRCConn.Nick, Backchannel: awaitFeedback, Once: true}
var feedback string
recv := false
CMAXTRIES := 2
feedback, recv = i.awaitFeedbackAfterRequest(awaitFeedback)
a := 0
for a = 0; a < CMAXTRIES && !OfferMatchesDesired(feedback); a++ {
prog<- XDCCDownloadMessage{Message: "Try: " + strconv.Itoa(a)}
i.IRCConn.CommandCh <- fmt.Sprintf("PRIVMSG %s :xdcc send %s", i.Bot, i.Package)
feedback, recv = i.awaitFeedbackAfterRequest(awaitFeedback)
if recv {
prog<- XDCCDownloadMessage{Message: feedback}
}
if i.quitSignalRecv(prog) {
return false
}
}
if (a >=CMAXTRIES) {
prog<- XDCCDownloadMessage{Err: "No dcc send from bot"}
i.IRCConn.CommandCh <- fmt.Sprintf("PRIVMSG %s :xdcc remove", i.Bot) // we might be on some queue...
prog<- XDCCDownloadMessage{Err: "no dcc send"}
return false
}
fmt.Print("got feedback")
fmt.Print(feedback)
offer := i.ParseSend(feedback)
var conn net.Conn
var err error
if proxyDial := i.Conf.GetProxyDial(); proxyDial != nil {
conn, err = (*proxyDial).Dial("tcp", offer.IP+":"+offer.Port)
} else {
conn, err = net.Dial("tcp", offer.IP+":"+offer.Port)
}
if err != nil {
prog<- XDCCDownloadMessage{Err: string(err.Error())}
return false
}
var recvBytes uint64
recvBytes = 0
mirc64 := false
if offer.Size>>32 > 0 {
mirc64 = true
log.Print("Mirc64")
} else {
log.Print("Mirc32")
}
recoveredErr := false
recvBuf := make([]byte, 4096)
pathToFile := path.Join(tempdir, url.PathEscape(offer.Filename))
f, err := os.Create(pathToFile)
defer f.Close()
timeLastRecv := time.Now()
var samplingN int64
G:
for {
conn.SetReadDeadline(time.Now().Add(20*time.Second))
if int64(recvBytes) == (offer.Size) { // maybe EOF => done
fmt.Println("Received file.")
break G
}
n, err2 := conn.Read(recvBuf[:]) // recv data
if err2 != nil {
// try recovering, some bots need this...
if (mirc64) {
bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, uint64(recvBytes))
conn.Write(bs)
} else {
bs := make([]byte, 4)
binary.BigEndian.PutUint32(bs, uint32(recvBytes))
conn.Write(bs)
}
log.Print("sending recv bytes...")
if (recoveredErr) { // if this didnt work, we exit
prog<-XDCCDownloadMessage{Err: err2.Error()}
return false
}
recoveredErr = true
} else {
recoveredErr = false // we weither recovered or the error never happened
}
if (i.Conf.SpeedLimit != 0 && samplingN > int64(i.Conf.SpeedLimit)*int64(1024)) {
elapsed := time.Since(timeLastRecv)
if elapsed < time.Duration(time.Second) {
time.Sleep(time.Duration(time.Second)-elapsed)
}
elapsed = time.Since(timeLastRecv)
timeLastRecv = time.Now()
speed := int64(float64(samplingN)/elapsed.Seconds()/1024)
prog<-XDCCDownloadMessage{Speed: speed}
samplingN = 0
} else {
samplingN += int64(n)
}
recvBytes = recvBytes + uint64(n)
//recvBytesSinceLastAck += int64(n)
// send ack
f.Write(recvBuf[:n])
prog<-XDCCDownloadMessage{Progress: float32(recvBytes)/float32(offer.Size)}
if i.quitSignalRecv(prog) {
return false
// todo: cleanup
}
}
f.Sync()
f.Close()
prog<-XDCCDownloadMessage{Filename: pathToFile}
return int64(recvBytes) == (offer.Size)
}