Skip to content

Commit 80e5f92

Browse files
committed
feat: Add TFTP and HTTP ports to metadata so that frontend no longer has to guess download links
1 parent 8a4f79e commit 80e5f92

File tree

8 files changed

+108
-37
lines changed

8 files changed

+108
-37
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ depend:
5353
# Setup CLIs
5454
GO111MODULE=on go get github.com/golang/protobuf/protoc-gen-go@latest
5555
GO111MODULE=on go get github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
56+
GO111MODULE=on go get github.com/golang/protobuf/protoc-gen-go@latest
5657
# Generate bindings
5758
go generate ./...

api/proto/v1/metadata.proto

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ service MetadataService {
1010
rpc GetMetadata(google.protobuf.Empty) returns (MetadataMessage);
1111
}
1212

13-
message MetadataMessage { string AdvertisedIP = 1; }
13+
message MetadataMessage {
14+
string AdvertisedIP = 1;
15+
int32 TFTPPort = 2;
16+
int32 HTTPPort = 3;
17+
}

cmd/bofied-backend/main.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package main
22

33
import (
44
"log"
5+
"net"
56
"os"
67
"path/filepath"
8+
"strconv"
79

810
"github.com/pojntfx/bofied/pkg/config"
911
"github.com/pojntfx/bofied/pkg/constants"
@@ -63,6 +65,27 @@ For more information, please visit https://github.com/pojntfx/bofied.`,
6365
}
6466
}
6567

68+
// Parse flags
69+
_, tftpPortRaw, err := net.SplitHostPort(viper.GetString(tftpListenAddressKey))
70+
if err != nil {
71+
return err
72+
}
73+
74+
tftpPort, err := strconv.Atoi(tftpPortRaw)
75+
if err != nil {
76+
return err
77+
}
78+
79+
_, httpPortRaw, err := net.SplitHostPort(viper.GetString(webDAVAndHTTPListenAddressKey))
80+
if err != nil {
81+
return err
82+
}
83+
84+
httpPort, err := strconv.Atoi(httpPortRaw)
85+
if err != nil {
86+
return err
87+
}
88+
6689
// Create eventing utilities
6790
eventsHandler := eventing.NewEventHandler()
6891

@@ -75,7 +98,12 @@ For more information, please visit https://github.com/pojntfx/bofied.`,
7598

7699
// Create services
77100
eventsService := services.NewEventsService(eventsHandler, contextValidator)
78-
metadataService := services.NewMetadataService(viper.GetString(advertisedIPKey), contextValidator)
101+
metadataService := services.NewMetadataService(
102+
viper.GetString(advertisedIPKey),
103+
int32(tftpPort),
104+
int32(httpPort),
105+
contextValidator,
106+
)
79107

80108
// Create servers
81109
dhcpServer := servers.NewDHCPServer(
@@ -146,7 +174,7 @@ For more information, please visit https://github.com/pojntfx/bofied.`,
146174

147175
cmd.PersistentFlags().String(dhcpListenAddressKey, ":67", "Listen address for DHCP server")
148176
cmd.PersistentFlags().String(proxyDHCPListenAddressKey, ":4011", "Listen address for proxyDHCP server")
149-
cmd.PersistentFlags().String(tftpListenAddressKey, ":"+constants.TFTPPort, "Listen address for TFTP server")
177+
cmd.PersistentFlags().String(tftpListenAddressKey, ":69", "Listen address for TFTP server")
150178
cmd.PersistentFlags().String(webDAVAndHTTPListenAddressKey, ":15256", "Listen address for WebDAV, HTTP and gRPC-Web server")
151179
cmd.PersistentFlags().String(grpcListenAddressKey, ":15257", "Listen address for gRPC server")
152180

pkg/api/proto/v1/events.pb.go

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/proto/v1/metadata.pb.go

Lines changed: 40 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/constants/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ package constants
22

33
const (
44
BootConfigFileName = "config.go"
5-
TFTPPort = "69"
65
)

pkg/providers/data_provider.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"os"
88
"path"
99
"path/filepath"
10+
"strconv"
11+
"strings"
1012
"time"
1113

1214
"github.com/maxence-charriere/go-app/v9/pkg/app"
@@ -123,6 +125,8 @@ type DataProvider struct {
123125
eventsErr error
124126

125127
advertisedIP string
128+
tftpPort int32
129+
httpPort int32
126130

127131
useAdvertisedIP bool
128132
useAdvertisedIPForWebDAV bool
@@ -159,7 +163,7 @@ func (c *DataProvider) Render() app.UI {
159163
u := c.httpShareLink
160164

161165
if c.useAdvertisedIP {
162-
u.Host = net.JoinHostPort(c.advertisedIP, u.Port())
166+
u.Host = net.JoinHostPort(c.advertisedIP, strconv.Itoa(int(c.httpPort)))
163167
}
164168

165169
if c.useHTTPS {
@@ -195,7 +199,7 @@ func (c *DataProvider) Render() app.UI {
195199
u := address
196200

197201
if c.useAdvertisedIPForWebDAV {
198-
u.Host = net.JoinHostPort(c.advertisedIP, u.Port())
202+
u.Host = net.JoinHostPort(c.advertisedIP, strconv.Itoa(int(c.httpPort)))
199203
}
200204

201205
if c.useDavs {
@@ -426,8 +430,10 @@ func (c *DataProvider) sharePath(path string) {
426430
// Set HTTP share link
427431
c.httpShareLink = *u
428432

433+
// Transform to TFTP URL
429434
u.Scheme = "tftp"
430-
u.Host = u.Hostname() + ":" + constants.TFTPPort
435+
u.Host = net.JoinHostPort(u.Hostname(), strconv.Itoa(int(c.tftpPort)))
436+
u.Path = strings.TrimPrefix(u.Path, servers.HTTPPrefix)
431437

432438
// Set TFTP share link
433439
c.tftpShareLink = *u
@@ -624,6 +630,8 @@ func (c *DataProvider) getMetadata(ctx app.Context) {
624630
// We have to use `Context.Emit` here as this runs from a separate Goroutine
625631
ctx.Emit(func() {
626632
c.advertisedIP = metadata.GetAdvertisedIP()
633+
c.tftpPort = metadata.GetTFTPPort()
634+
c.httpPort = metadata.GetHTTPPort()
627635
})
628636
}
629637

0 commit comments

Comments
 (0)