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

Set the default CLI IP for use based on a give interface name: #544

Merged
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
8 changes: 1 addition & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -113,7 +107,7 @@ linters:
- errname
- errorlint
- exhaustive
- exportloopref
- copyloopvar
- forcetypeassert
- gocognit
- goconst
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions cmd/smee/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"flag"
"fmt"
"net"
"os"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -175,7 +176,38 @@
}
}

// 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 ""
}

Check warning on line 184 in cmd/smee/flag.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/flag.go#L180-L184

Added lines #L180 - L184 were not covered by tests

addrs, err := iface.Addrs()
if err != nil {
return ""
}

Check warning on line 189 in cmd/smee/flag.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/flag.go#L186-L189

Added lines #L186 - L189 were not covered by tests

for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok {
continue

Check warning on line 194 in cmd/smee/flag.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/flag.go#L191-L194

Added lines #L191 - L194 were not covered by tests
}

if ipNet.IP.To4() != nil {
return ipNet.IP.String()
}

Check warning on line 199 in cmd/smee/flag.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/flag.go#L197-L199

Added lines #L197 - L199 were not covered by tests
}

return ""

Check warning on line 202 in cmd/smee/flag.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/flag.go#L202

Added line #L202 was not covered by tests
}

func detectPublicIPv4() string {
if netint := os.Getenv("SMEE_PUBLIC_IP_INTERFACE"); netint != "" {
if ip := ipByInterface(netint); ip != "" {
return ip
}

Check warning on line 209 in cmd/smee/flag.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/flag.go#L207-L209

Added lines #L207 - L209 were not covered by tests
}
ipDgw, err := autoDetectPublicIpv4WithDefaultGateway()
if err == nil {
return ipDgw.String()
Expand Down
Loading