From c74419785b4bcf81d264b5b9357183b524cc609e Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Thu, 7 Nov 2024 16:57:00 -0700 Subject: [PATCH 1/4] Select the IP for use based on a give interface name: This allows just specifying an interface and letting the first IP on that interface be used for all CLI flag defaults. Signed-off-by: Jacob Weinstock --- cmd/smee/flag.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/cmd/smee/flag.go b/cmd/smee/flag.go index 2167b5e6..3102e4f3 100644 --- a/cmd/smee/flag.go +++ b/cmd/smee/flag.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "net" + "os" "regexp" "sort" "strings" @@ -175,7 +176,38 @@ func newCLI(cfg *config, fs *flag.FlagSet) *ffcli.Command { } } +// ipByInterface returns the first IPv4 address on the named network interface. +func ipByInterface(name string) string { + iface, err := net.InterfaceByName(name) + if err != nil { + return "" + } + + addrs, err := iface.Addrs() + if err != nil { + return "" + } + + for _, addr := range addrs { + ipNet, ok := addr.(*net.IPNet) + if !ok { + continue + } + + if ipNet.IP.To4() != nil { + return ipNet.IP.String() + } + } + + return "" +} + func detectPublicIPv4() string { + if netint := os.Getenv("SMEE_IP_DETECT_INTERFACE"); netint != "" { + if ip := ipByInterface(netint); ip != "" { + return ip + } + } ipDgw, err := autoDetectPublicIpv4WithDefaultGateway() if err == nil { return ipDgw.String() From 5562f82e17d53fcac7f7b1ebdad903128aaf9fbe Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Fri, 8 Nov 2024 11:56:42 -0700 Subject: [PATCH 2/4] Fix linting warnings by cleaning up lint config: Signed-off-by: Jacob Weinstock --- .golangci.yml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index c6c08ab9..10a1a810 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -91,12 +91,6 @@ linters-settings: - name: unconditional-recursion - name: waitgroup-by-value - staticcheck: - go: "1.20" - - unused: - go: "1.20" - output: sort-results: true @@ -113,7 +107,7 @@ linters: - errname - errorlint - exhaustive - - exportloopref + - copyloopvar - forcetypeassert - gocognit - goconst From 9bfbb22de7882c5abc60e660a2bf43348acbe692 Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Fri, 8 Nov 2024 11:57:41 -0700 Subject: [PATCH 3/4] Change env name for detecting the public ip Signed-off-by: Jacob Weinstock --- cmd/smee/flag.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/smee/flag.go b/cmd/smee/flag.go index 3102e4f3..b723d893 100644 --- a/cmd/smee/flag.go +++ b/cmd/smee/flag.go @@ -203,7 +203,7 @@ func ipByInterface(name string) string { } func detectPublicIPv4() string { - if netint := os.Getenv("SMEE_IP_DETECT_INTERFACE"); netint != "" { + if netint := os.Getenv("SMEE_PUBLIC_IP_INTERFACE"); netint != "" { if ip := ipByInterface(netint); ip != "" { return ip } From 21acec3457714f02ba783428fc8e2c1e892a17d4 Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Fri, 8 Nov 2024 12:17:18 -0700 Subject: [PATCH 4/4] Update README: Add section on CLI and env vars and the one off note about `SMEE_PUBLIC_IP_INTERFACE`. Signed-off-by: Jacob Weinstock --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 79a3d94b..ae610801 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,16 @@ It is not recommended, but it is possible for Smee to be run in `reservation` mo 1. All DHCP servers are configured to serve the same IP address and network boot details as Smee. In this scenario the DHCP functionality of Smee is redundant. It would be recommended to run Smee with the DHCP server functionality disabled (`-dhcp=false`). See the [doc](./docs/DHCP.md) on using your existing DHCP service for more details. +### Environment Variables and CLI Flags + +It's important to note that CLI flags take precedence over environment variables. All CLI flags can be set as environment variables. Environment variable names are the same as the flag names with some modifications. For example, the flag `-dhcp-addr` has the environment variable of `SMEE_DHCP_ADDR`. The modifications of CLI flags to environment variables are as follows: + +- prefixed with `SMEE_` +- all uppercase +- hyphens (`-`) are replaced with underscores (`_`) + +There is one environment variable that does not have a corresponding CLI flag. The environment variable is `SMEE_PUBLIC_IP_INTERFACE`. This environment variable takes a local network interface name and uses it to auto detect the IP address to use as the default in all other CLI flags that require an IP address. This is useful when the machine running Smee has multiple network interfaces and you want the default detected IP to be from this specified interface. + ### Local Setup Running the Tests