mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
all: add support for Synology router
This commit is contained in:
@@ -43,7 +43,7 @@ func initRouterCLI() {
|
|||||||
platform = router.Name()
|
platform = router.Name()
|
||||||
}
|
}
|
||||||
switch platform {
|
switch platform {
|
||||||
case router.DDWrt, router.Merlin, router.OpenWrt, router.Ubios:
|
case router.DDWrt, router.Merlin, router.OpenWrt, router.Ubios, router.Synology:
|
||||||
default:
|
default:
|
||||||
unsupportedPlatformHelp(cmd)
|
unsupportedPlatformHelp(cmd)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func dnsMasqConf() (string, error) {
|
|||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
var tmplText string
|
var tmplText string
|
||||||
switch Name() {
|
switch Name() {
|
||||||
case DDWrt, OpenWrt, Ubios:
|
case DDWrt, OpenWrt, Ubios, Synology:
|
||||||
tmplText = dnsMasqConfigContentTmpl
|
tmplText = dnsMasqConfigContentTmpl
|
||||||
case Merlin:
|
case Merlin:
|
||||||
tmplText = merlinDNSMasqPostConfTmpl
|
tmplText = merlinDNSMasqPostConfTmpl
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ func setupOpenWrt() error {
|
|||||||
if _, err := uci("delete", "dhcp.@dnsmasq[0].port"); err != nil && !errors.Is(err, errUCIEntryNotFound) {
|
if _, err := uci("delete", "dhcp.@dnsmasq[0].port"); err != nil && !errors.Is(err, errUCIEntryNotFound) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Disable dnsmasq as DNS server.
|
|
||||||
dnsMasqConfigContent, err := dnsMasqConf()
|
dnsMasqConfigContent, err := dnsMasqConf()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
+28
-15
@@ -19,10 +19,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
OpenWrt = "openwrt"
|
OpenWrt = "openwrt"
|
||||||
DDWrt = "ddwrt"
|
DDWrt = "ddwrt"
|
||||||
Merlin = "merlin"
|
Merlin = "merlin"
|
||||||
Ubios = "ubios"
|
Ubios = "ubios"
|
||||||
|
Synology = "synology"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrNotSupported reports the current router is not supported error.
|
// 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.
|
// SupportedPlatforms return all platforms that can be configured to run with ctrld.
|
||||||
func SupportedPlatforms() []string {
|
func SupportedPlatforms() []string {
|
||||||
return []string{DDWrt, Merlin, OpenWrt, Ubios}
|
return []string{DDWrt, Merlin, OpenWrt, Ubios, Synology}
|
||||||
}
|
}
|
||||||
|
|
||||||
var configureFunc = map[string]func() error{
|
var configureFunc = map[string]func() error{
|
||||||
DDWrt: setupDDWrt,
|
DDWrt: setupDDWrt,
|
||||||
Merlin: setupMerlin,
|
Merlin: setupMerlin,
|
||||||
OpenWrt: setupOpenWrt,
|
OpenWrt: setupOpenWrt,
|
||||||
Ubios: setupUbiOS,
|
Ubios: setupUbiOS,
|
||||||
|
Synology: setupSynology,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure configures things for running ctrld on the router.
|
// Configure configures things for running ctrld on the router.
|
||||||
func Configure(c *ctrld.Config) error {
|
func Configure(c *ctrld.Config) error {
|
||||||
name := Name()
|
name := Name()
|
||||||
switch name {
|
switch name {
|
||||||
case DDWrt, Merlin, OpenWrt, Ubios:
|
case DDWrt, Merlin, OpenWrt, Ubios, Synology:
|
||||||
if c.HasUpstreamSendClientInfo() {
|
if c.HasUpstreamSendClientInfo() {
|
||||||
r := routerPlatform.Load()
|
r := routerPlatform.Load()
|
||||||
r.sendClientInfo = true
|
r.sendClientInfo = true
|
||||||
@@ -88,7 +90,7 @@ func ConfigureService(sc *service.Config) error {
|
|||||||
}
|
}
|
||||||
case OpenWrt:
|
case OpenWrt:
|
||||||
sc.Option["SysvScript"] = openWrtScript
|
sc.Option["SysvScript"] = openWrtScript
|
||||||
case Merlin, Ubios:
|
case Merlin, Ubios, Synology:
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -151,6 +153,8 @@ func PostInstall() error {
|
|||||||
return postInstallOpenWrt()
|
return postInstallOpenWrt()
|
||||||
case Ubios:
|
case Ubios:
|
||||||
return postInstallUbiOS()
|
return postInstallUbiOS()
|
||||||
|
case Synology:
|
||||||
|
return postInstallSynology()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -167,6 +171,8 @@ func Cleanup() error {
|
|||||||
return cleanupOpenWrt()
|
return cleanupOpenWrt()
|
||||||
case Ubios:
|
case Ubios:
|
||||||
return cleanupUbiOS()
|
return cleanupUbiOS()
|
||||||
|
case Synology:
|
||||||
|
return cleanupSynology()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -175,7 +181,7 @@ func Cleanup() error {
|
|||||||
func ListenAddress() string {
|
func ListenAddress() string {
|
||||||
name := Name()
|
name := Name()
|
||||||
switch name {
|
switch name {
|
||||||
case DDWrt, Merlin, OpenWrt, Ubios:
|
case DDWrt, Merlin, OpenWrt, Ubios, Synology:
|
||||||
return "127.0.0.1:5354"
|
return "127.0.0.1:5354"
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
@@ -194,14 +200,16 @@ func Name() string {
|
|||||||
|
|
||||||
func distroName() string {
|
func distroName() string {
|
||||||
switch {
|
switch {
|
||||||
case bytes.HasPrefix(uname(), []byte("DD-WRT")):
|
case bytes.HasPrefix(unameO(), []byte("DD-WRT")):
|
||||||
return DDWrt
|
return DDWrt
|
||||||
case bytes.HasPrefix(uname(), []byte("ASUSWRT-Merlin")):
|
case bytes.HasPrefix(unameO(), []byte("ASUSWRT-Merlin")):
|
||||||
return Merlin
|
return Merlin
|
||||||
case haveFile("/etc/openwrt_version"):
|
case haveFile("/etc/openwrt_version"):
|
||||||
return OpenWrt
|
return OpenWrt
|
||||||
case haveDir("/data/unifi"):
|
case haveDir("/data/unifi"):
|
||||||
return Ubios
|
return Ubios
|
||||||
|
case bytes.HasPrefix(unameU(), []byte("synology")):
|
||||||
|
return Synology
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -216,7 +224,12 @@ func haveDir(dir string) bool {
|
|||||||
return fi != nil && fi.IsDir()
|
return fi != nil && fi.IsDir()
|
||||||
}
|
}
|
||||||
|
|
||||||
func uname() []byte {
|
func unameO() []byte {
|
||||||
out, _ := exec.Command("uname", "-o").Output()
|
out, _ := exec.Command("uname", "-o").Output()
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unameU() []byte {
|
||||||
|
out, _ := exec.Command("uname", "-u").Output()
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user