Skip to content

Commit

Permalink
Add auto setup support for DD-WRT
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Jan 2, 2020
1 parent 937fe45 commit 47b2116
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
28 changes: 28 additions & 0 deletions install.sh
Expand Up @@ -172,6 +172,8 @@ detect_os() {
ASUSWRT-Merlin)
echo "asuswrt-merlin"; return 0
;;
DD-WRT)
echo "ddwrt"; return 0
esac
;;
Darwin)
Expand Down Expand Up @@ -328,6 +330,29 @@ uninstall_openwrt() {
opkg remove nextdns
}

install_ddwrt() {
if [ "$(nvram get enable_jffs2)" = "0" ]; then
log_error "JFFS support not enabled"
log_info "To enabled JFFS:"
log_info " 1. On the router web page click on Administration."
log_info " 2. Scroll down until you see JFFS2 Support section."
log_info " 3. Click Enable JFFS."
log_info " 4. Click Save."
log_info " 5. Wait couple seconds, then click Apply."
log_info " 6. Wait again. Go back to the Enable JFFS section, and enable Clean JFFS."
log_info " 7. Do not click Save. Click Apply instead."
log_info " 8. Wait till you get the web-GUI back, then disable Clean JFFS again."
log_info " 9. Click Save."
log_info "10. Relaunch this installer."
exit 1
fi
install_bin
}

uninstall_ddwrt() {
uninstall_bin
}

install_brew() {
silent_exec brew install nextdns/tap/nextdns
}
Expand Down Expand Up @@ -381,6 +406,9 @@ install_type() {
asuswrt-merlin|edgeos)
echo "bin"
;;
ddwrt)
echo "ddwrt"
;;
darwin)
if [ -x /usr/local/bin/brew ]; then
echo "brew"
Expand Down
106 changes: 106 additions & 0 deletions router/ddwrt/setup.go
@@ -0,0 +1,106 @@
package ddwrt

import (
"bytes"
"fmt"
"os/exec"
"strings"
"text/template"

"github.com/nextdns/nextdns/config"
"github.com/nextdns/nextdns/router/internal"
)

type Router struct {
ListenPort string
ClientReporting bool
savedParams []string
}

func New() (*Router, bool) {
if b, err := exec.Command("uname", "-o").Output(); err != nil ||
!strings.HasPrefix(string(b), "DD-WRT") {
return nil, false
}
return &Router{
ListenPort: "5342",
}, true
}

func (r *Router) Configure(c *config.Config) {
c.Listen = "127.0.0.1:" + r.ListenPort
r.ClientReporting = c.ReportClientInfo
}

func (r *Router) Setup() error {
t, err := template.New("").Parse(tmpl)
if err != nil {
return err
}

var buf bytes.Buffer
if err = t.Execute(&buf, r); err != nil {
return err
}

// Save nvram values so we can restore them.
if r.savedParams, err = internal.NVRAM(
"dns_dnsmasq",
"dnsmasq_options",
"dns_crypt",
"dnssec",
"dnsmasq_no_dns_rebind",
"dnsmasq_add_mac"); err != nil {
return err
}

// Configure the firmware:
// * Add dnsmasq options to route queries to nextdns
// * DNS rebinding is disabled, as DNS blocking uses 0.0.0.0 to block domains.
// The rebinding protection can be setup and enforced at NextDNS level.
// * DNSCrypt is disabled as it would conflict.
// * DNSSEC validation is disabled as when a DNSSEC supported domain is blocked,
// the validation will fail as blocking alters the response. NextDNS takes care
// of DNS validation for non blocked queries.
// * DNS over TLS is disabled so stubby does not run for nothing.
if err := internal.SetNVRAM(
"dns_dnsmasq=1",
"dnsmasq_options="+buf.String(),
"dns_crypt=0",
"dnssec=0",
"dnsmasq_no_dns_rebind=0",
"dnsmasq_add_mac=0"); err != nil {
return err
}

// Restart dnsmasq service to apply changes.
return restartDNSMasq()
}

func (r *Router) Restore() error {
// Restore previous settings.
if err := internal.SetNVRAM(r.savedParams...); err != nil {
return err
}
// Restart dnsmasq service to apply changes.
return restartDNSMasq()
}

func restartDNSMasq() error {
if err := exec.Command("stopservice", "dnsmasq").Run(); err != nil {
return fmt.Errorf("stopservice dnsmasq: %v", err)
}
if err := exec.Command("startservice", "dnsmasq").Run(); err != nil {
return fmt.Errorf("startservice dnsmasq: %v", err)
}
return nil
}

var tmpl = `# Configuration generated by NextDNS
no-resolv
server=127.0.0.1#{{.ListenPort}}
{{- if .ClientReporting}}
add-mac
add-subnet=32,128
{{- end}}
`

0 comments on commit 47b2116

Please sign in to comment.