Skip to content

Commit

Permalink
Feedback from @koenbollen
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnoud Vermeer committed Feb 28, 2019
1 parent 8112d88 commit b56dc83
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module tftp-proxy

require github.com/pin/tftp v2.1.0+incompatible
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/pin/tftp v2.1.0+incompatible h1:Yng4J7jv6lOc6IF4XoB5mnd3P7ZrF60XQq+my3FAMus=
github.com/pin/tftp v2.1.0+incompatible/go.mod h1:xVpZOMCXTy+A5QMjEVN0Glwa1sUvaJhFXbr/aAxuxGY=
13 changes: 6 additions & 7 deletions tftp.go → main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"time"
"path"
"io/ioutil"

"github.com/pin/tftp"
)
Expand All @@ -18,8 +19,8 @@ var dir string
// readHandler is called when client starts file download from server
func readHandler(filename string, rf io.ReaderFrom) error {

if _, err := os.Stat(filename); err == nil {
file, err := os.Open(filename)
if _, err := os.Stat(path.Join(dir, filename)); err == nil {
file, err := os.Open(path.Join(dir, filename))
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return err
Expand Down Expand Up @@ -49,7 +50,8 @@ func readHandler(filename string, rf io.ReaderFrom) error {
defer resp.Body.Close()

if resp.StatusCode != 200 {
return errors.New(fmt.Sprintf("Received status code: %d", resp.StatusCode))
io.Copy(ioutil.Discard, resp.Body)
return fmt.Errorf("Received status code: %d", resp.StatusCode)
}

rf.(tftp.OutgoingTransfer).SetSize(resp.ContentLength)
Expand All @@ -70,9 +72,6 @@ func main() {
flag.StringVar(&url, "url", "http://example.com", "The URL to proxy requests to. For example http://example.com")
flag.Parse()

// Change dir to the default tftp directory
os.Chdir(dir)

// use nil in place of handler to disable read or write operations
s := tftp.NewServer(readHandler, nil)
s.SetTimeout(5 * time.Second) // optional
Expand Down

0 comments on commit b56dc83

Please sign in to comment.