Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying an interface to bind to: #362

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ FLAGS
-dhcp-enabled [dhcp] enable DHCP server (default "true")
-dhcp-http-ipxe-binary-url [dhcp] HTTP ipxe binaries URL to use in DHCP packets (default "http://172.17.0.2:8080/ipxe/")
-dhcp-http-ipxe-script-url [dhcp] HTTP ipxe script URL to use in DHCP packets (default "http://172.17.0.2/auto.ipxe")
-dhcp-iface [dhcp] interface to bind to for DHCP requests
-dhcp-ip-for-packet [dhcp] ip address to use in DHCP packets (opt 54, etc) (default "172.17.0.2")
-dhcp-syslog-ip [dhcp] syslog server IP address to use in DHCP packets (opt 7) (default "172.17.0.2")
-dhcp-tftp-ip [dhcp] tftp server IP address to use in DHCP packets (opt 66, etc) (default "172.17.0.2:69")
Expand Down
64 changes: 0 additions & 64 deletions cmd/smee/env.go

This file was deleted.

59 changes: 59 additions & 0 deletions cmd/smee/flag.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"errors"
"flag"
"fmt"
"net"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -107,6 +109,7 @@ func ipxeHTTPScriptFlags(c *config, fs *flag.FlagSet) {
func dhcpFlags(c *config, fs *flag.FlagSet) {
fs.BoolVar(&c.dhcp.enabled, "dhcp-enabled", true, "[dhcp] enable DHCP server")
fs.StringVar(&c.dhcp.bindAddr, "dhcp-addr", "0.0.0.0:67", "[dhcp] local IP:Port to listen on for DHCP requests")
fs.StringVar(&c.dhcp.bindInterface, "dhcp-iface", "", "[dhcp] interface to bind to for DHCP requests")
fs.StringVar(&c.dhcp.ipForPacket, "dhcp-ip-for-packet", detectPublicIPv4(""), "[dhcp] IP address to use in DHCP packets (opt 54, etc)")
fs.StringVar(&c.dhcp.syslogIP, "dhcp-syslog-ip", detectPublicIPv4(""), "[dhcp] syslog server IP address to use in DHCP packets (opt 7)")
fs.StringVar(&c.dhcp.tftpIP, "dhcp-tftp-ip", detectPublicIPv4(":69"), "[dhcp] tftp server IP address to use in DHCP packets (opt 66, etc)")
Expand Down Expand Up @@ -143,3 +146,59 @@ func newCLI(cfg *config, fs *flag.FlagSet) *ffcli.Command {
UsageFunc: customUsageFunc,
}
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why these 3 functions were added, they are not being used anywhere else in the PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i moved them in from another file that didn't need to exist. Sorry, it was just a clean up item!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, pr LGTM, once merged ill work on moving from boots 0.8.0 to the latest smee version then!

func detectPublicIPv4(extra string) string {
ip, err := autoDetectPublicIPv4()
if err != nil {
return ""
}

return fmt.Sprintf("%v%v", ip.String(), extra)
}

func autoDetectPublicIPv4() (net.IP, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, fmt.Errorf("unable to auto-detect public IPv4: %w", err)
}
for _, addr := range addrs {
ip, ok := addr.(*net.IPNet)
if !ok {
continue
}
v4 := ip.IP.To4()
if v4 == nil || !v4.IsGlobalUnicast() {
continue
}

return v4, nil
}

return nil, errors.New("unable to auto-detect public IPv4")
}

func parseTrustedProxies(trustedProxies string) (result []string) {
for _, cidr := range strings.Split(trustedProxies, ",") {
cidr = strings.TrimSpace(cidr)
if cidr == "" {
continue
}
_, _, err := net.ParseCIDR(cidr)
if err != nil {
// Its not a cidr, but maybe its an IP
if ip := net.ParseIP(cidr); ip != nil {
if ip.To4() != nil {
cidr += "/32"
} else {
cidr += "/128"
}
} else {
// not an IP, panic
panic("invalid ip cidr in TRUSTED_PROXIES cidr=" + cidr)
}
}
result = append(result, cidr)
}

return result
}
1 change: 1 addition & 0 deletions cmd/smee/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ FLAGS
-dhcp-enabled [dhcp] enable DHCP server (default "true")
-dhcp-http-ipxe-binary-url [dhcp] HTTP ipxe binaries URL to use in DHCP packets (default "http://%[1]v:8080/ipxe/")
-dhcp-http-ipxe-script-url [dhcp] HTTP ipxe script URL to use in DHCP packets (default "http://%[1]v/auto.ipxe")
-dhcp-iface [dhcp] interface to bind to for DHCP requests
-dhcp-ip-for-packet [dhcp] IP address to use in DHCP packets (opt 54, etc) (default "%[1]v")
-dhcp-syslog-ip [dhcp] syslog server IP address to use in DHCP packets (opt 7) (default "%[1]v")
-dhcp-tftp-ip [dhcp] tftp server IP address to use in DHCP packets (opt 66, etc) (default "%[1]v:69")
Expand Down
3 changes: 2 additions & 1 deletion cmd/smee/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type ipxeHTTPScript struct {
type dhcpConfig struct {
enabled bool
bindAddr string
bindInterface string
ipForPacket string
syslogIP string
tftpIP string
Expand Down Expand Up @@ -219,7 +220,7 @@ func main() {
if err != nil {
panic(fmt.Errorf("invalid tftp address for DHCP server: %w", err))
}
conn, err := server4.NewIPv4UDPConn("", net.UDPAddrFromAddrPort(bindAddr))
conn, err := server4.NewIPv4UDPConn(cfg.dhcp.bindInterface, net.UDPAddrFromAddrPort(bindAddr))
if err != nil {
panic(err)
}
Expand Down
Loading