-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtftp.go
50 lines (45 loc) · 1.15 KB
/
tftp.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type tftpStuff struct {
username string
password string
role string
}
func (t tftpStuff) readHandler(filename string, rf io.ReaderFrom) error {
log.Infof("Got request for %s", filename)
someString := fmt.Sprintf("username %s password 0 %s role %s\n", t.username, t.password, t.role)
myReader := strings.NewReader(someString)
_, err := rf.ReadFrom(myReader)
if err != nil {
log.Errorf("%v", err)
return err
}
return nil
}
func (t tftpStuff) writeHandler(filenameRaw string, wt io.WriterTo) error {
log.Infof("Got request for %s", filenameRaw)
_, filename := filepath.Split(filenameRaw)
if filename == "" {
log.Infof("empty filename after cleanup: %s", filenameRaw)
return fmt.Errorf("empty filename after cleanup: %s", filenameRaw)
}
filename = fmt.Sprintf("%s_%s", filename, randomString(5))
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
if err != nil {
log.Errorf("%v", err)
return err
}
n, err := wt.WriteTo(file)
if err != nil {
log.Errorf("%v", err)
return err
}
log.Infof("%s: %d bytes received", filename, n)
return nil
}