Skip to content

Instantly share code, notes, and snippets.

@ndenev
Created March 28, 2017 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndenev/2a565cde2df958e3552b2943dab92b2d to your computer and use it in GitHub Desktop.
Save ndenev/2a565cde2df958e3552b2943dab92b2d to your computer and use it in GitHub Desktop.
GoTftpd + Shoelaces = <3
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"time"
"github.com/pin/tftp"
)
var allowedFiles = [...]string{"undionly.kpxe"}
func checkAllowed(filename string) bool {
for _, file := range allowedFiles {
if file == filename {
return true
}
}
return false
}
func readHandler(filename string, rf io.ReaderFrom) error {
if !checkAllowed(filename) {
return errors.New("Not allowed")
}
raddr := rf.(tftp.OutgoingTransfer).RemoteAddr()
log.Println("RRQ from", raddr.String())
file, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return err
}
n, err := rf.ReadFrom(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return err
}
fmt.Printf("%d bytes sent\n", n)
return nil
}
func main() {
s := tftp.NewServer(readHandler, nil)
s.SetTimeout(5 * time.Second)
err := s.ListenAndServe("192.168.56.1:69")
if err != nil {
fmt.Fprintf(os.Stdout, "server: %v\n", err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment