Skip to content

Commit 5ceef6f

Browse files
bytes
1 parent 79ef076 commit 5ceef6f

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

xdcc/xdcc.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"log"
77
"os"
88
"os/exec"
9+
910
"github.com/gorilla/mux"
11+
"github.com/gosimple/slug"
12+
"golang.org/x/exp/maps"
1013
"gopkg.in/ini.v1"
11-
"golang.org/x/exp/maps"
12-
"github.com/gosimple/slug"
13-
14-
1514

15+
"crypto/tls"
1616
"encoding/binary"
1717
"fmt"
1818
"io"
@@ -22,7 +22,7 @@ import (
2222
"strings"
2323
"sync"
2424
"unicode/utf8"
25-
"crypto/tls"
25+
2626
"github.com/kiwiirc/webircgateway/pkg/irc"
2727
"github.com/kiwiirc/webircgateway/pkg/webircgateway"
2828
"golang.org/x/net/html/charset"
@@ -119,6 +119,19 @@ func ensureUtf8(s string, fromEncoding string) string {
119119
return s2
120120
}
121121

122+
type WriteCounter struct {
123+
Total uint64
124+
connection *net.Conn
125+
}
126+
127+
func (wc *WriteCounter) Write(p []byte) (int, error) {
128+
n := len(p)
129+
wc.Total += uint64(n)
130+
binary.Write((*wc.connection), binary.BigEndian, wc.Total)
131+
return n, nil
132+
}
133+
134+
122135
func serveFile(parts ParsedParts, w http.ResponseWriter, r *http.Request) (work bool) {
123136

124137
ipPort := fmt.Sprintf("%s:%d", parts.ip.String(), parts.port)
@@ -131,14 +144,19 @@ func serveFile(parts ParsedParts, w http.ResponseWriter, r *http.Request) (work
131144
return false
132145
}
133146
conn, err := net.Dial("tcp", ipPort)
147+
134148
if err != nil {
135149
w.WriteHeader(http.StatusBadGateway)
136150
w.Write([]byte(err.Error()))
137151
return false
138152
}
139153

140154
pr, pw := io.Pipe()
141-
155+
counter := &WriteCounter{
156+
connection :&conn,
157+
Total: 0,
158+
}
159+
142160

143161
contentDisposition := fmt.Sprintf("attachment; filename=%s", parts.file)
144162
w.Header().Set("Content-Disposition", contentDisposition)
@@ -148,7 +166,8 @@ func serveFile(parts ParsedParts, w http.ResponseWriter, r *http.Request) (work
148166
panic("overflows!")
149167
}
150168
w.Header().Set("Content-Length", strconv.Itoa(intLength) /*r.Header.Get("Content-Length")*/)
151-
go io.Copy(pw, conn)
169+
170+
go io.Copy(pw, io.TeeReader( conn,counter))
152171
io.Copy(w, pr)
153172
//stream the body to the client without fully loading it into memory
154173
// pbw := bufio.NewWriter(conn)
@@ -176,6 +195,9 @@ func serveFile(parts ParsedParts, w http.ResponseWriter, r *http.Request) (work
176195
}
177196
func DCCSend(hook *webircgateway.HookIrcLine) {
178197

198+
199+
200+
//TODO DCC Send To Server
179201
if hook.Halt || hook.ToServer {
180202
return
181203
}

0 commit comments

Comments
 (0)