-
Notifications
You must be signed in to change notification settings - Fork 1
/
tftp_server.go
110 lines (95 loc) · 2.75 KB
/
tftp_server.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
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/c2h5oh/datasize"
magic "github.com/hosom/gomagic"
"github.com/pin/tftp"
"github.com/spf13/viper"
)
func tftpReadHandler(filename string, rf io.ReaderFrom) error {
cleanedPath := CleanPath(filename)
fullPath := filepath.Join(viper.GetString("upload.dataDir"), cleanedPath)
file, err := os.Open(fullPath)
if err != nil {
log.Printf("TFTP: failed to open file for reading: %v", err)
return err
}
defer file.Close()
n, err := rf.ReadFrom(file)
if err != nil {
log.Printf("TFTP: failed to read file: %v", err)
return err
}
log.Printf("TFTP: %d bytes sent of %v", n, filename)
return nil
}
func tftpWriteHandler(filename string, wf io.WriterTo) error {
if !strings.HasPrefix(filename, viper.GetString("tftp.writePrefix")) {
log.Printf("TFTP: forbidden filename: %v", filename)
return fmt.Errorf("forbidden filename, must start with %v", viper.GetString("tftp.writePrefix"))
}
cleanedPath := CleanPath(filename)
fullPath := filepath.Join(viper.GetString("upload.dataDir"), cleanedPath)
if err := validateUploadFilename(cleanedPath); err != nil {
log.Printf("TFTP: invalid filename %v: %v", cleanedPath, err)
return err
}
dirPath := filepath.Dir(fullPath)
os.MkdirAll(dirPath, 0777)
os.Remove(fullPath + "._infocache")
os.Remove(fullPath + "._infolock")
f, err := os.Create(fullPath)
if err != nil {
log.Printf("TFTP: failed to create file %v: %v", cleanedPath, err)
return err
}
defer f.Close()
n, err := wf.WriteTo(f)
if err != nil {
log.Printf("TFTP: failed to write file %v: %v", cleanedPath, err)
return err
}
log.Printf("TFTP: %d bytes received of %v", n, filename)
fileType := ""
m, err := magic.Open(magic.MAGIC_NONE)
if err != nil {
fileType = fmt.Sprintf("error while opening magic database: %v", err)
} else {
fileType, err = m.File(fullPath)
if err != nil {
fileType = fmt.Sprintf("error determining file type: %v", err)
}
}
data := map[string]interface{}{
"url": viper.GetString("http.url") + "/" + cleanedPath,
"sizeExact": n,
"size": datasize.ByteSize(n).HR(),
"type": fileType,
"message": fmt.Sprintf("File %v uploaded!", cleanedPath),
}
// notify all waiting listeners
notifyFileListeners(cleanedPath)
data["uploadedAt"] = time.Now()
data["uploaderLocation"] = "TFTP"
data["name"] = cleanedPath
addToRecents(data)
return nil
}
func runTftpServer() {
if !viper.GetBool("tftp.enabled") {
return
}
s := tftp.NewServer(tftpReadHandler, tftpWriteHandler)
s.SetTimeout(5 * time.Second) // optional
log.Printf("TFTP: started on port 69")
err := s.ListenAndServe(":69") // blocks until s.Shutdown() is called
if err != nil {
log.Fatalf("TFTP: failed to start server: %v", err)
}
}