-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(provider): specify interface name (#941)
- Loading branch information
Showing
17 changed files
with
604 additions
and
163 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,19 +1,12 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/favonia/cloudflare-ddns/internal/ipnet" | ||
"github.com/favonia/cloudflare-ddns/internal/provider/protocol" | ||
) | ||
import "github.com/favonia/cloudflare-ddns/internal/provider/protocol" | ||
|
||
// NewLocal creates a specialized Local provider that uses Cloudflare as the remote server. | ||
// (No actual UDP packets will be sent to Cloudflare.) | ||
func NewLocal() Provider { | ||
return protocol.Local{ | ||
ProviderName: "local", | ||
RemoteUDPAddr: map[ipnet.Type]string{ | ||
// 1.0.0.1 is used in case 1.1.1.1 is hijacked by the router | ||
ipnet.IP4: "1.0.0.1:443", | ||
ipnet.IP6: "[2606:4700:4700::1111]:443", | ||
}, | ||
return protocol.LocalAuto{ | ||
ProviderName: "local", | ||
RemoteUDPAddr: "api.cloudflare.com:443", | ||
} | ||
} |
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,11 @@ | ||
package provider | ||
|
||
import "github.com/favonia/cloudflare-ddns/internal/provider/protocol" | ||
|
||
// NewLocalWithInterface creates a protocol.LocalWithInterface provider. | ||
func NewLocalWithInterface(iface string) Provider { | ||
return protocol.LocalWithInterface{ | ||
ProviderName: "local:" + iface, | ||
InterfaceName: iface, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,63 @@ | ||
package protocol | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/netip" | ||
|
||
"github.com/favonia/cloudflare-ddns/internal/ipnet" | ||
"github.com/favonia/cloudflare-ddns/internal/pp" | ||
) | ||
|
||
// LocalAuto detects the IP address by pretending to send out an UDP packet | ||
// and using the source IP address assigned by the system. In most cases | ||
// it will detect the IP address of the network interface toward the internet. | ||
// (No actual UDP packets will be sent out.) | ||
type LocalAuto struct { | ||
// Name of the detection protocol. | ||
ProviderName string | ||
|
||
// The target of the hypothetical UDP packet to be sent. | ||
RemoteUDPAddr string | ||
} | ||
|
||
// Name of the detection protocol. | ||
func (p LocalAuto) Name() string { | ||
return p.ProviderName | ||
} | ||
|
||
// ExtractUDPAddr converts an address from [net.Interface.Addrs] to [netip.Addr]. | ||
// The address will be unmapped. | ||
func ExtractUDPAddr(ppfmt pp.PP, addr net.Addr) (netip.Addr, bool) { | ||
switch v := addr.(type) { | ||
case *net.UDPAddr: | ||
ip := v.AddrPort().Addr().Unmap() | ||
if !ip.IsValid() { | ||
ppfmt.Noticef(pp.EmojiImpossible, "Failed to parse UDP source address %q", v.IP.String()) | ||
return netip.Addr{}, false | ||
} | ||
return ip, ip.IsValid() | ||
default: | ||
ppfmt.Noticef(pp.EmojiImpossible, "Unexpected UDP source address data %q of type %T", addr.String(), addr) | ||
return netip.Addr{}, false | ||
} | ||
} | ||
|
||
// GetIP detects the IP address by pretending to send an UDP packet. | ||
// (No actual UDP packets will be sent out.) | ||
func (p LocalAuto) GetIP(_ context.Context, ppfmt pp.PP, ipNet ipnet.Type) (netip.Addr, Method, bool) { | ||
conn, err := net.Dial(ipNet.UDPNetwork(), p.RemoteUDPAddr) | ||
if err != nil { | ||
ppfmt.Noticef(pp.EmojiError, "Failed to detect a local %s address: %v", ipNet.Describe(), err) | ||
return netip.Addr{}, MethodUnspecified, false | ||
} | ||
defer conn.Close() | ||
|
||
ip, ok := ExtractUDPAddr(ppfmt, conn.LocalAddr()) | ||
if !ok { | ||
return netip.Addr{}, MethodUnspecified, false | ||
} | ||
|
||
normalizedIP, ok := ipNet.NormalizeDetectedIP(ppfmt, ip) | ||
return normalizedIP, MethodPrimary, ok | ||
} |
Oops, something went wrong.