mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-04 01:07:49 +02:00
internal: record correct interfaces for queries from router on Firewalla
This commit is contained in:
committed by
Cuong Manh Le
parent
12c8ab696f
commit
59dc74ffbb
@@ -11,11 +11,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/Control-D-Inc/ctrld"
|
"github.com/fsnotify/fsnotify"
|
||||||
"tailscale.com/net/interfaces"
|
"tailscale.com/net/interfaces"
|
||||||
"tailscale.com/util/lineread"
|
"tailscale.com/util/lineread"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/Control-D-Inc/ctrld"
|
||||||
|
"github.com/Control-D-Inc/ctrld/internal/router"
|
||||||
)
|
)
|
||||||
|
|
||||||
type dhcp struct {
|
type dhcp struct {
|
||||||
@@ -279,4 +280,22 @@ func (d *dhcp) addSelf() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
for _, netIface := range router.SelfInterfaces() {
|
||||||
|
mac := netIface.HardwareAddr.String()
|
||||||
|
if mac == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d.mac2name.Store(mac, hostname)
|
||||||
|
addrs, _ := netIface.Addrs()
|
||||||
|
for _, addr := range addrs {
|
||||||
|
ipNet, ok := addr.(*net.IPNet)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ip := ipNet.IP
|
||||||
|
d.mac.LoadOrStore(ip.String(), mac)
|
||||||
|
d.ip.LoadOrStore(mac, ip.String())
|
||||||
|
d.ip2name.Store(ip.String(), hostname)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
const ConfigContentTmpl = `# GENERATED BY ctrld - DO NOT MODIFY
|
const ConfigContentTmpl = `# GENERATED BY ctrld - DO NOT MODIFY
|
||||||
no-resolv
|
no-resolv
|
||||||
{{- range .Upstreams}}
|
{{- range .Upstreams}}
|
||||||
server={{ .Ip }}#{{ .Port }}
|
server={{ .IP }}#{{ .Port }}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- if .SendClientInfo}}
|
{{- if .SendClientInfo}}
|
||||||
add-mac
|
add-mac
|
||||||
@@ -36,7 +36,7 @@ if [ -n "$pid" ] && [ -f "/proc/${pid}/cmdline" ]; then
|
|||||||
# use ctrld as upstream
|
# use ctrld as upstream
|
||||||
pc_delete "server=" "$config_file"
|
pc_delete "server=" "$config_file"
|
||||||
{{- range .Upstreams}}
|
{{- range .Upstreams}}
|
||||||
pc_append "server={{ .Ip }}#{{ .Port }}" "$config_file"
|
pc_append "server={{ .IP }}#{{ .Port }}" "$config_file"
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- if .SendClientInfo}}
|
{{- if .SendClientInfo}}
|
||||||
pc_append "add-mac" "$config_file" # add client mac
|
pc_append "add-mac" "$config_file" # add client mac
|
||||||
@@ -56,7 +56,7 @@ fi
|
|||||||
`
|
`
|
||||||
|
|
||||||
type Upstream struct {
|
type Upstream struct {
|
||||||
Ip string
|
IP string
|
||||||
Port int
|
Port int
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ func ConfTmpl(tmplText string, cfg *ctrld.Config) (string, error) {
|
|||||||
if ip == "0.0.0.0" || ip == "::" || ip == "" {
|
if ip == "0.0.0.0" || ip == "::" || ip == "" {
|
||||||
ip = "127.0.0.1"
|
ip = "127.0.0.1"
|
||||||
}
|
}
|
||||||
upstreams := []Upstream{{Ip: ip, Port: listener.Port}}
|
upstreams := []Upstream{{IP: ip, Port: listener.Port}}
|
||||||
return confTmpl(tmplText, upstreams, cfg.HasUpstreamSendClientInfo())
|
return confTmpl(tmplText, upstreams, cfg.HasUpstreamSendClientInfo())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,25 +97,35 @@ func confTmpl(tmplText string, upstreams []Upstream, sendClientInfo bool) (strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func firewallaUpstreams(port int) []Upstream {
|
func firewallaUpstreams(port int) []Upstream {
|
||||||
matches, err := filepath.Glob("/home/pi/firerouter/etc/dnsmasq.dns.*.conf")
|
ifaces := FirewallaSelfInterfaces()
|
||||||
if err != nil {
|
upstreams := make([]Upstream, 0, len(ifaces))
|
||||||
return nil
|
for _, netIface := range ifaces {
|
||||||
}
|
addrs, _ := netIface.Addrs()
|
||||||
upstreams := make([]Upstream, 0, len(matches))
|
for _, addr := range addrs {
|
||||||
for _, match := range matches {
|
if netIP, ok := addr.(*net.IPNet); ok && netIP.IP.To4() != nil {
|
||||||
// Trim prefix and suffix to get the iface name only.
|
upstreams = append(upstreams, Upstream{
|
||||||
ifaceName := strings.TrimSuffix(strings.TrimPrefix(match, "/home/pi/firerouter/etc/dnsmasq.dns."), ".conf")
|
IP: netIP.IP.To4().String(),
|
||||||
if netIface, _ := net.InterfaceByName(ifaceName); netIface != nil {
|
Port: port,
|
||||||
addrs, _ := netIface.Addrs()
|
})
|
||||||
for _, addr := range addrs {
|
|
||||||
if netIP, ok := addr.(*net.IPNet); ok && netIP.IP.To4() != nil {
|
|
||||||
upstreams = append(upstreams, Upstream{
|
|
||||||
Ip: netIP.IP.To4().String(),
|
|
||||||
Port: port,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return upstreams
|
return upstreams
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FirewallaSelfInterfaces returns list of interfaces that will be configured with default dnsmasq setup on Firewalla.
|
||||||
|
func FirewallaSelfInterfaces() []*net.Interface {
|
||||||
|
matches, err := filepath.Glob("/home/pi/firerouter/etc/dnsmasq.dns.*.conf")
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ifaces := make([]*net.Interface, 0, len(matches))
|
||||||
|
for _, match := range matches {
|
||||||
|
// Trim prefix and suffix to get the iface name only.
|
||||||
|
ifaceName := strings.TrimSuffix(strings.TrimPrefix(match, "/home/pi/firerouter/etc/dnsmasq.dns."), ".conf")
|
||||||
|
if netIface, _ := net.InterfaceByName(ifaceName); netIface != nil {
|
||||||
|
ifaces = append(ifaces, netIface)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ifaces
|
||||||
|
}
|
||||||
|
|||||||
@@ -188,6 +188,16 @@ func ServiceDependencies() []string {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SelfInterfaces return list of *net.Interface that will be source of requests from router itself.
|
||||||
|
func SelfInterfaces() []*net.Interface {
|
||||||
|
switch Name() {
|
||||||
|
case firewalla.Name:
|
||||||
|
return dnsmasq.FirewallaSelfInterfaces()
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func distroName() string {
|
func distroName() string {
|
||||||
switch {
|
switch {
|
||||||
case bytes.HasPrefix(unameO(), []byte("DD-WRT")):
|
case bytes.HasPrefix(unameO(), []byte("DD-WRT")):
|
||||||
|
|||||||
Reference in New Issue
Block a user