Skip to content

Commit

Permalink
Merge pull request #12 from pojntfx/add-ports-to-metadata
Browse files Browse the repository at this point in the history
Add TFTP and HTTP ports to metadata
  • Loading branch information
pojntfx authored Jun 13, 2021
2 parents 8a4f79e + 80e5f92 commit fd05caf
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 37 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ depend:
# Setup CLIs
GO111MODULE=on go get github.com/golang/protobuf/protoc-gen-go@latest
GO111MODULE=on go get github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
GO111MODULE=on go get github.com/golang/protobuf/protoc-gen-go@latest
# Generate bindings
go generate ./...
6 changes: 5 additions & 1 deletion api/proto/v1/metadata.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ service MetadataService {
rpc GetMetadata(google.protobuf.Empty) returns (MetadataMessage);
}

message MetadataMessage { string AdvertisedIP = 1; }
message MetadataMessage {
string AdvertisedIP = 1;
int32 TFTPPort = 2;
int32 HTTPPort = 3;
}
32 changes: 30 additions & 2 deletions cmd/bofied-backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"log"
"net"
"os"
"path/filepath"
"strconv"

"github.com/pojntfx/bofied/pkg/config"
"github.com/pojntfx/bofied/pkg/constants"
Expand Down Expand Up @@ -63,6 +65,27 @@ For more information, please visit https://github.com/pojntfx/bofied.`,
}
}

// Parse flags
_, tftpPortRaw, err := net.SplitHostPort(viper.GetString(tftpListenAddressKey))
if err != nil {
return err
}

tftpPort, err := strconv.Atoi(tftpPortRaw)
if err != nil {
return err
}

_, httpPortRaw, err := net.SplitHostPort(viper.GetString(webDAVAndHTTPListenAddressKey))
if err != nil {
return err
}

httpPort, err := strconv.Atoi(httpPortRaw)
if err != nil {
return err
}

// Create eventing utilities
eventsHandler := eventing.NewEventHandler()

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

// Create services
eventsService := services.NewEventsService(eventsHandler, contextValidator)
metadataService := services.NewMetadataService(viper.GetString(advertisedIPKey), contextValidator)
metadataService := services.NewMetadataService(
viper.GetString(advertisedIPKey),
int32(tftpPort),
int32(httpPort),
contextValidator,
)

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

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

Expand Down
18 changes: 9 additions & 9 deletions pkg/api/proto/v1/events.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 40 additions & 20 deletions pkg/api/proto/v1/metadata.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pkg/constants/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ package constants

const (
BootConfigFileName = "config.go"
TFTPPort = "69"
)
14 changes: 11 additions & 3 deletions pkg/providers/data_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"

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

advertisedIP string
tftpPort int32
httpPort int32

useAdvertisedIP bool
useAdvertisedIPForWebDAV bool
Expand Down Expand Up @@ -159,7 +163,7 @@ func (c *DataProvider) Render() app.UI {
u := c.httpShareLink

if c.useAdvertisedIP {
u.Host = net.JoinHostPort(c.advertisedIP, u.Port())
u.Host = net.JoinHostPort(c.advertisedIP, strconv.Itoa(int(c.httpPort)))
}

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

if c.useAdvertisedIPForWebDAV {
u.Host = net.JoinHostPort(c.advertisedIP, u.Port())
u.Host = net.JoinHostPort(c.advertisedIP, strconv.Itoa(int(c.httpPort)))
}

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

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

// Set TFTP share link
c.tftpShareLink = *u
Expand Down Expand Up @@ -624,6 +630,8 @@ func (c *DataProvider) getMetadata(ctx app.Context) {
// We have to use `Context.Emit` here as this runs from a separate Goroutine
ctx.Emit(func() {
c.advertisedIP = metadata.GetAdvertisedIP()
c.tftpPort = metadata.GetTFTPPort()
c.httpPort = metadata.GetHTTPPort()
})
}

Expand Down
Loading

0 comments on commit fd05caf

Please sign in to comment.