-
Notifications
You must be signed in to change notification settings - Fork 1
/
tftpHandleRRQ.go
executable file
·141 lines (112 loc) · 3.06 KB
/
tftpHandleRRQ.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
package main
import (
"fmt"
"io"
"log"
"net"
"net/http"
"strconv"
"time"
"github.com/a1comms/libgotftp/src"
)
func tftpHandleRRQ(res *tftp.RRQresponse) {
started := time.Now()
status, size := http.StatusNotFound, 0
defer func() {
tftpLogRequest(started, status, size, res)
}()
path := res.Request.Path
if len(path) >= 1 {
if path[0:1] == "/" {
path = path[1:]
}
} else {
status = http.StatusNotFound
res.WriteError(tftp.NOT_FOUND, "invalid path")
return
}
log.Printf(fmt.Sprintf(
"%s: blocksize %d from %s",
tftpLogRequestPrefix(res),
res.Request.Blocksize,
*res.Request.Addr,
))
defer res.End()
fileReader, fileSize, err := StorageGetFile(path)
if err != nil {
res.WriteError(tftp.NOT_FOUND, "storage error")
return
}
defer fileReader.Close()
size = int(fileSize)
if res.Request.TransferSize != -1 {
res.TransferSize = int(fileSize)
}
if err := res.WriteOACK(); err != nil {
status = http.StatusInternalServerError
log.Printf("%s: Failed to write OACK: %s", tftpLogRequestPrefix(res), err)
return
}
b := make([]byte, res.Request.Blocksize)
totalBytes := 0
for {
bytesRead, err := fileReader.Read(b)
totalBytes += bytesRead
if err == io.EOF {
if _, err := res.Write(b[:bytesRead]); err != nil {
status = http.StatusInternalServerError
log.Printf("%s: Failed to write last bytes of the reader: %s", tftpLogRequestPrefix(res), err)
return
}
break
} else if err != nil {
status = http.StatusInternalServerError
log.Printf("%s: Error while reading: %s", tftpLogRequestPrefix(res), err)
res.WriteError(tftp.UNKNOWN_ERROR, err.Error())
return
}
if _, err := res.Write(b[:bytesRead]); err != nil {
status = http.StatusInternalServerError
log.Printf("%s: Failed to write bytes: %s", tftpLogRequestPrefix(res), err)
return
}
}
took := time.Since(started)
speed := float64(totalBytes) / took.Seconds() / 1024 / 1024
status = http.StatusOK
log.Printf("%s: Sent %v bytes in %v %f MB/s\n", tftpLogRequestPrefix(res), totalBytes, took, speed)
}
func tftpLogRequestPrefix(res *tftp.RRQresponse) string {
req := res.Request
host, _, err := net.SplitHostPort((*req.Addr).String())
if err != nil {
host = (*req.Addr).String()
}
return fmt.Sprintf("TFTP: GET %s from %s", req.Path, host)
}
func tftpLogRequest(ts time.Time, status int, size int, res *tftp.RRQresponse) {
req := res.Request
username := "-"
host, _, err := net.SplitHostPort((*req.Addr).String())
if err != nil {
host = (*req.Addr).String()
}
uri := req.Path
buf := make([]byte, 0)
buf = append(buf, host...)
buf = append(buf, " - "...)
buf = append(buf, username...)
buf = append(buf, " ["...)
buf = append(buf, ts.Format("02/Jan/2006:15:04:05 -0700")...)
buf = append(buf, `] "`...)
buf = append(buf, `GET`...)
buf = append(buf, " "...)
buf = appendQuoted(buf, uri)
buf = append(buf, " "...)
buf = append(buf, `TFTP/0.0`...)
buf = append(buf, `" `...)
buf = append(buf, strconv.Itoa(status)...)
buf = append(buf, " "...)
buf = append(buf, strconv.Itoa(size)...)
log.Printf("%s\n", string(buf))
}