-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
64 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package protocol | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/netip" | ||
|
||
"github.com/favonia/cloudflare-ddns/internal/ipnet" | ||
"github.com/favonia/cloudflare-ddns/internal/pp" | ||
) | ||
|
||
// LocalWithInterface detects the IP address by choosing the first "good" IP | ||
// address assigned to a network interface. | ||
type LocalWithInterface struct { | ||
// Name of the detection protocol. | ||
ProviderName string | ||
|
||
// The name of the network interface | ||
InterfaceName string | ||
} | ||
|
||
// Name of the detection protocol. | ||
func (p LocalWithInterface) Name() string { | ||
return p.ProviderName | ||
} | ||
|
||
// GetIP detects the IP address by pretending to send an UDP packet. | ||
// (No actual UDP packets will be sent out.) | ||
func (p LocalWithInterface) GetIP(_ context.Context, ppfmt pp.PP, ipNet ipnet.Type) (netip.Addr, Method, bool) { | ||
iface, err := net.InterfaceByName(p.InterfaceName) | ||
if err != nil { | ||
ppfmt.Noticef(pp.EmojiUserError, "Failed to find an interface named %q: %v", p.InterfaceName, err) | ||
return netip.Addr{}, MethodUnspecified, false | ||
} | ||
|
||
addrs, err := iface.Addrs() | ||
if err != nil { | ||
ppfmt.Noticef(pp.EmojiUserError, "Failed to list addresses of %q: %v", p.InterfaceName, err) | ||
return netip.Addr{}, MethodUnspecified, false | ||
} | ||
|
||
for _, addr := range addrs { | ||
ip, err := netip.ParseAddr(addr.String()) | ||
if err != nil { | ||
ppfmt.Noticef(pp.EmojiUserError, "Failed to parse address %q of %q: %v", addr.String(), p.InterfaceName, err) | ||
return netip.Addr{}, MethodUnspecified, false | ||
} | ||
|
||
if ipNet.Matches(ip) && ip.IsGlobalUnicast() { | ||
return ip, MethodUnspecified, true | ||
} | ||
} | ||
|
||
return netip.Addr{}, MethodUnspecified, false | ||
} |