Skip to content

Commit

Permalink
fix: respect rootDir for tftp server
Browse files Browse the repository at this point in the history
  • Loading branch information
teodor-pripoae committed Dec 3, 2024
1 parent 702a031 commit 14db857
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func server(cmd *cobra.Command, args []string) {
quit(err.Error())
}

tftp := tftp.New()
tftp := tftp.New(tftp.WithRootDir(config.GetRootDir()))
server, err := ipxe.New(
ipxe.WithIP(config.HTTP.IP),
ipxe.WithPort(config.HTTP.Port),
Expand Down
5 changes: 4 additions & 1 deletion pkg/tftp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ load("@rules_go//go:def.bzl", "go_library")

go_library(
name = "tftp",
srcs = ["server.go"],
srcs = [
"options.go",
"server.go",
],
importpath = "toni.systems/goipxeboot/pkg/tftp",
visibility = ["//visibility:public"],
deps = [
Expand Down
9 changes: 9 additions & 0 deletions pkg/tftp/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tftp

type Option func(*server)

func WithRootDir(dir string) Option {
return func(s *server) {
s.root = dir
}
}
12 changes: 10 additions & 2 deletions pkg/tftp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"io"
"os"
"path/filepath"
"time"

"github.com/pin/tftp/v3"
Expand All @@ -29,9 +30,10 @@ type Server interface {
type server struct {
tftp *tftp.Server
filename string
root string
}

func New() Server {
func New(options ...Option) Server {
s := &server{
filename: "./ipxe/ipxe.efi",
}
Expand All @@ -40,6 +42,10 @@ func New() Server {
t.SetTimeout(timeout)
s.tftp = t

for _, option := range options {
option(s)
}

return s
}

Expand All @@ -55,7 +61,9 @@ func (s *server) ReadHandler(filename string, rf io.ReaderFrom) error {
return ErrInvalidFilename
}

file, err := os.Open(s.filename)
path := filepath.Join(s.root, filename)

file, err := os.Open(path)
if err != nil {
return errors.Join(err, ErrFailedToOpenFile)
}
Expand Down

0 comments on commit 14db857

Please sign in to comment.