all: add support for Synology router

This commit is contained in:
Cuong Manh Le
2023-05-18 23:03:03 +07:00
committed by Cuong Manh Le
parent 7ac5555a84
commit d2fc530316
5 changed files with 85 additions and 18 deletions

View File

@@ -49,7 +49,7 @@ func dnsMasqConf() (string, error) {
var sb strings.Builder
var tmplText string
switch Name() {
case DDWrt, OpenWrt, Ubios:
case DDWrt, OpenWrt, Ubios, Synology:
tmplText = dnsMasqConfigContentTmpl
case Merlin:
tmplText = merlinDNSMasqPostConfTmpl

View File

@@ -28,7 +28,6 @@ func setupOpenWrt() error {
if _, err := uci("delete", "dhcp.@dnsmasq[0].port"); err != nil && !errors.Is(err, errUCIEntryNotFound) {
return err
}
// Disable dnsmasq as DNS server.
dnsMasqConfigContent, err := dnsMasqConf()
if err != nil {
return err

View File

@@ -19,10 +19,11 @@ import (
)
const (
OpenWrt = "openwrt"
DDWrt = "ddwrt"
Merlin = "merlin"
Ubios = "ubios"
OpenWrt = "openwrt"
DDWrt = "ddwrt"
Merlin = "merlin"
Ubios = "ubios"
Synology = "synology"
)
// ErrNotSupported reports the current router is not supported error.
@@ -39,21 +40,22 @@ type router struct {
// SupportedPlatforms return all platforms that can be configured to run with ctrld.
func SupportedPlatforms() []string {
return []string{DDWrt, Merlin, OpenWrt, Ubios}
return []string{DDWrt, Merlin, OpenWrt, Ubios, Synology}
}
var configureFunc = map[string]func() error{
DDWrt: setupDDWrt,
Merlin: setupMerlin,
OpenWrt: setupOpenWrt,
Ubios: setupUbiOS,
DDWrt: setupDDWrt,
Merlin: setupMerlin,
OpenWrt: setupOpenWrt,
Ubios: setupUbiOS,
Synology: setupSynology,
}
// Configure configures things for running ctrld on the router.
func Configure(c *ctrld.Config) error {
name := Name()
switch name {
case DDWrt, Merlin, OpenWrt, Ubios:
case DDWrt, Merlin, OpenWrt, Ubios, Synology:
if c.HasUpstreamSendClientInfo() {
r := routerPlatform.Load()
r.sendClientInfo = true
@@ -88,7 +90,7 @@ func ConfigureService(sc *service.Config) error {
}
case OpenWrt:
sc.Option["SysvScript"] = openWrtScript
case Merlin, Ubios:
case Merlin, Ubios, Synology:
}
return nil
}
@@ -151,6 +153,8 @@ func PostInstall() error {
return postInstallOpenWrt()
case Ubios:
return postInstallUbiOS()
case Synology:
return postInstallSynology()
}
return nil
}
@@ -167,6 +171,8 @@ func Cleanup() error {
return cleanupOpenWrt()
case Ubios:
return cleanupUbiOS()
case Synology:
return cleanupSynology()
}
return nil
}
@@ -175,7 +181,7 @@ func Cleanup() error {
func ListenAddress() string {
name := Name()
switch name {
case DDWrt, Merlin, OpenWrt, Ubios:
case DDWrt, Merlin, OpenWrt, Ubios, Synology:
return "127.0.0.1:5354"
}
return ""
@@ -194,14 +200,16 @@ func Name() string {
func distroName() string {
switch {
case bytes.HasPrefix(uname(), []byte("DD-WRT")):
case bytes.HasPrefix(unameO(), []byte("DD-WRT")):
return DDWrt
case bytes.HasPrefix(uname(), []byte("ASUSWRT-Merlin")):
case bytes.HasPrefix(unameO(), []byte("ASUSWRT-Merlin")):
return Merlin
case haveFile("/etc/openwrt_version"):
return OpenWrt
case haveDir("/data/unifi"):
return Ubios
case bytes.HasPrefix(unameU(), []byte("synology")):
return Synology
}
return ""
}
@@ -216,7 +224,12 @@ func haveDir(dir string) bool {
return fi != nil && fi.IsDir()
}
func uname() []byte {
func unameO() []byte {
out, _ := exec.Command("uname", "-o").Output()
return out
}
func unameU() []byte {
out, _ := exec.Command("uname", "-u").Output()
return out
}

View File

@@ -0,0 +1,55 @@
package router
import (
"fmt"
"os"
"os/exec"
)
const (
synologyDNSMasqConfigPath = "/etc/dhcpd/dhcpd-zzz-ctrld.conf"
synologyDhcpdInfoPath = "/etc/dhcpd/dhcpd-zzz-ctrld.info"
)
func setupSynology() error {
dnsMasqConfigContent, err := dnsMasqConf()
if err != nil {
return err
}
if err := os.WriteFile(synologyDNSMasqConfigPath, []byte(dnsMasqConfigContent), 0600); err != nil {
return err
}
if err := os.WriteFile(synologyDhcpdInfoPath, []byte(`enable="yes"`), 0600); err != nil {
return err
}
if err := synologyRestartDNSMasq(); err != nil {
return err
}
return nil
}
func cleanupSynology() error {
// Remove the custom config files.
for _, f := range []string{synologyDNSMasqConfigPath, synologyDhcpdInfoPath} {
if err := os.Remove(f); err != nil {
return err
}
}
// Restart dnsmasq service.
if err := synologyRestartDNSMasq(); err != nil {
return err
}
return nil
}
func postInstallSynology() error {
return nil
}
func synologyRestartDNSMasq() error {
if out, err := exec.Command("/etc/rc.network", "nat-restart-dhcp").CombinedOutput(); err != nil {
return fmt.Errorf("synologyRestartDNSMasq: %s - %w", string(out), err)
}
return nil
}