Skip to content

Commit

Permalink
Try webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
subdavis committed May 6, 2020
1 parent f250fc1 commit 9615c97
Show file tree
Hide file tree
Showing 22 changed files with 149 additions and 31 deletions.
5 changes: 3 additions & 2 deletions dhcpd/dhcp_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/AdguardTeam/AdGuardHome/event"
"github.com/AdguardTeam/AdGuardHome/util"

"github.com/AdguardTeam/golibs/log"
Expand Down Expand Up @@ -92,7 +93,7 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
httpError(r, w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)
return
}
s.conf.ConfigModified()
s.conf.ConfigModified(event.DHCP)

if newconfig.Enabled {
staticIP, err := HasStaticIP(newconfig.InterfaceName)
Expand Down Expand Up @@ -314,7 +315,7 @@ func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) {
s.conf.HTTPRegister = oldconf.HTTPRegister
s.conf.ConfigModified = oldconf.ConfigModified
s.conf.DBFilePath = oldconf.DBFilePath
s.conf.ConfigModified()
s.conf.ConfigModified(event.DHCP)
}

func (s *Server) registerHandlers() {
Expand Down
2 changes: 1 addition & 1 deletion dhcpd/dhcpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ServerConfig struct {
DBFilePath string `json:"-" yaml:"-"` // path to DB file

// Called when the configuration is changed by HTTP request
ConfigModified func() `json:"-" yaml:"-"`
ConfigModified func(string) `json:"-" yaml:"-"`

// Register an HTTP handler
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request)) `json:"-" yaml:"-"`
Expand Down
3 changes: 2 additions & 1 deletion dnsfilter/blocked_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/AdguardTeam/AdGuardHome/event"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/urlfilter/rules"
)
Expand Down Expand Up @@ -233,7 +234,7 @@ func (d *Dnsfilter) handleBlockedServicesSet(w http.ResponseWriter, r *http.Requ

log.Debug("Updated blocked services list: %d", len(list))

d.ConfigModified()
d.ConfigModified(event.BlockedServices)
}

// registerBlockedServicesHandlers - register HTTP handlers
Expand Down
2 changes: 1 addition & 1 deletion dnsfilter/dnsfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Config struct {
AutoHosts *util.AutoHosts `yaml:"-"`

// Called when the configuration is changed by HTTP request
ConfigModified func() `yaml:"-"`
ConfigModified func(string) `yaml:"-"`

// Register an HTTP handler
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request)) `yaml:"-"`
Expand Down
5 changes: 3 additions & 2 deletions dnsfilter/rewrites.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sort"
"strings"

"github.com/AdguardTeam/AdGuardHome/event"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)
Expand Down Expand Up @@ -180,7 +181,7 @@ func (d *Dnsfilter) handleRewriteAdd(w http.ResponseWriter, r *http.Request) {
log.Debug("Rewrites: added element: %s -> %s [%d]",
ent.Domain, ent.Answer, len(d.Config.Rewrites))

d.Config.ConfigModified()
d.Config.ConfigModified(event.DNSRewrite)
}

func (d *Dnsfilter) handleRewriteDelete(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -208,7 +209,7 @@ func (d *Dnsfilter) handleRewriteDelete(w http.ResponseWriter, r *http.Request)
d.Config.Rewrites = arr
d.confLock.Unlock()

d.Config.ConfigModified()
d.Config.ConfigModified(event.DNSRewrite)
}

func (d *Dnsfilter) registerRewritesHandlers() {
Expand Down
13 changes: 7 additions & 6 deletions dnsfilter/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"
"time"

"github.com/AdguardTeam/AdGuardHome/event"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/cache"
"github.com/AdguardTeam/golibs/log"
Expand Down Expand Up @@ -296,12 +297,12 @@ func httpError(r *http.Request, w http.ResponseWriter, code int, format string,

func (d *Dnsfilter) handleSafeBrowsingEnable(w http.ResponseWriter, r *http.Request) {
d.Config.SafeBrowsingEnabled = true
d.Config.ConfigModified()
d.Config.ConfigModified(event.DNSSafeBrowsing)
}

func (d *Dnsfilter) handleSafeBrowsingDisable(w http.ResponseWriter, r *http.Request) {
d.Config.SafeBrowsingEnabled = false
d.Config.ConfigModified()
d.Config.ConfigModified(event.DNSSafeBrowsing)
}

func (d *Dnsfilter) handleSafeBrowsingStatus(w http.ResponseWriter, r *http.Request) {
Expand All @@ -323,12 +324,12 @@ func (d *Dnsfilter) handleSafeBrowsingStatus(w http.ResponseWriter, r *http.Requ

func (d *Dnsfilter) handleParentalEnable(w http.ResponseWriter, r *http.Request) {
d.Config.ParentalEnabled = true
d.Config.ConfigModified()
d.Config.ConfigModified(event.DNSParental)
}

func (d *Dnsfilter) handleParentalDisable(w http.ResponseWriter, r *http.Request) {
d.Config.ParentalEnabled = false
d.Config.ConfigModified()
d.Config.ConfigModified(event.DNSParental)
}

func (d *Dnsfilter) handleParentalStatus(w http.ResponseWriter, r *http.Request) {
Expand All @@ -351,12 +352,12 @@ func (d *Dnsfilter) handleParentalStatus(w http.ResponseWriter, r *http.Request)

func (d *Dnsfilter) handleSafeSearchEnable(w http.ResponseWriter, r *http.Request) {
d.Config.SafeSearchEnabled = true
d.Config.ConfigModified()
d.Config.ConfigModified(event.DNSSafeSearch)
}

func (d *Dnsfilter) handleSafeSearchDisable(w http.ResponseWriter, r *http.Request) {
d.Config.SafeSearchEnabled = false
d.Config.ConfigModified()
d.Config.ConfigModified(event.DNSSafeSearch)
}

func (d *Dnsfilter) handleSafeSearchStatus(w http.ResponseWriter, r *http.Request) {
Expand Down
3 changes: 2 additions & 1 deletion dnsforward/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"sync"

"github.com/AdguardTeam/AdGuardHome/event"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/urlfilter"
"github.com/AdguardTeam/urlfilter/filterlist"
Expand Down Expand Up @@ -196,7 +197,7 @@ func (s *Server) handleAccessSet(w http.ResponseWriter, r *http.Request) {
s.conf.BlockedHosts = j.BlockedHosts
s.access = a
s.Unlock()
s.conf.ConfigModified()
s.conf.ConfigModified(event.DNSAccess)

log.Debug("Access: updated lists: %d, %d, %d",
len(j.AllowedClients), len(j.DisallowedClients), len(j.BlockedHosts))
Expand Down
2 changes: 1 addition & 1 deletion dnsforward/dnsforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ type ServerConfig struct {
TLSCiphers []uint16 // list of TLS ciphers to use

// Called when the configuration is changed by HTTP request
ConfigModified func()
ConfigModified func(string)

// Register an HTTP handler
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request))
Expand Down
3 changes: 2 additions & 1 deletion dnsforward/dnsforward_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"

"github.com/AdguardTeam/AdGuardHome/event"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/jsonutil"
"github.com/AdguardTeam/golibs/log"
Expand Down Expand Up @@ -178,7 +179,7 @@ func (s *Server) handleSetConfig(w http.ResponseWriter, r *http.Request) {
}

s.Unlock()
s.conf.ConfigModified()
s.conf.ConfigModified(event.DNSConfig)

if restart {
err = s.Reconfigure(nil)
Expand Down
34 changes: 34 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package event

const (
// DNSConfig event when dns config changes
DNSConfig = "dns_config"
// DNSRewrite event when rewrite rules change
DNSRewrite = "dns_rewrite"
// DNSSafeBrowsing event when safe browsing status changes
DNSSafeBrowsing = "dns_safe_browsing"
// DNSAccess event when ...
DNSAccess = "dns_access"
// DNSParental event when parental controls change
DNSParental = "dns_parental"
// DNSSafeSearch event when safe serach status changes
DNSSafeSearch = "dns_safe_search"
// BlockedServices event when blocked services change
BlockedServices = "blocked_services"
// DHCP event when dhcp settings change
DHCP = "dpcp"
// Stats event when stats config is modified
Stats = "stats"
// QueryLog event when query log config is modified
QueryLog = "query_log"
// Filter event when filtering lists and blacklist/whitelist change
Filter = "filter"
// FilterRule event when filter rules change
FilterRule = "filter_rule"
// I18N event when i18n settings change
I18N = "i19n"
// Client event when clients are added, removed, or modified
Client = "client"
// TLS event when tls settings change
TLS = "tls"
)
8 changes: 5 additions & 3 deletions home/clients_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io/ioutil"
"net/http"

"github.com/AdguardTeam/AdGuardHome/event"
)

type clientJSON struct {
Expand Down Expand Up @@ -173,7 +175,7 @@ func (clients *clientsContainer) handleAddClient(w http.ResponseWriter, r *http.
return
}

onConfigModified()
onConfigModified(event.Client)
}

// Remove client
Expand All @@ -196,7 +198,7 @@ func (clients *clientsContainer) handleDelClient(w http.ResponseWriter, r *http.
return
}

onConfigModified()
onConfigModified(event.Client)
}

type updateJSON struct {
Expand Down Expand Up @@ -235,7 +237,7 @@ func (clients *clientsContainer) handleUpdateClient(w http.ResponseWriter, r *ht
return
}

onConfigModified()
onConfigModified(event.Client)
}

// Get the list of clients by IP address list
Expand Down
2 changes: 2 additions & 0 deletions home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type configuration struct {

DHCP dhcpd.ServerConfig `yaml:"dhcp"`

Webhooks []Webhook `yaml:"webhooks"`

// Note: this array is filled only before file read/write and then it's cleared
Clients []clientObject `yaml:"clients"`

Expand Down
11 changes: 6 additions & 5 deletions home/control_filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"time"

"github.com/AdguardTeam/AdGuardHome/event"
"github.com/AdguardTeam/AdGuardHome/util"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
Expand Down Expand Up @@ -85,7 +86,7 @@ func (f *Filtering) handleFilteringAddURL(w http.ResponseWriter, r *http.Request
return
}

onConfigModified()
onConfigModified(event.Filter)
enableFilters(true)

_, err = fmt.Fprintf(w, "OK %d rules\n", filt.RulesCount)
Expand Down Expand Up @@ -128,7 +129,7 @@ func (f *Filtering) handleFilteringRemoveURL(w http.ResponseWriter, r *http.Requ
*filters = newFilters
config.Unlock()

onConfigModified()
onConfigModified(event.Filter)
enableFilters(true)

// Note: the old files "filter.txt.old" aren't deleted - it's not really necessary,
Expand Down Expand Up @@ -175,7 +176,7 @@ func (f *Filtering) handleFilteringSetURL(w http.ResponseWriter, r *http.Request
return
}

onConfigModified()
onConfigModified(event.Filter)
restart := false
if (status & statusEnabledChanged) != 0 {
// we must add or remove filter rules
Expand Down Expand Up @@ -208,7 +209,7 @@ func (f *Filtering) handleFilteringSetRules(w http.ResponseWriter, r *http.Reque
}

config.UserRules = strings.Split(string(body), "\n")
onConfigModified()
onConfigModified(event.FilterRule)
enableFilters(true)
}

Expand Down Expand Up @@ -328,7 +329,7 @@ func (f *Filtering) handleFilteringConfig(w http.ResponseWriter, r *http.Request

config.DNS.FilteringEnabled = req.Enabled
config.DNS.FiltersUpdateIntervalHours = req.Interval
onConfigModified()
onConfigModified(event.Filter)
enableFilters(true)
}

Expand Down
3 changes: 2 additions & 1 deletion home/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
)

// Called by other modules when configuration is changed
func onConfigModified() {
func onConfigModified(e string) {
go webhookHandleEvent(e)
_ = config.write()
}

Expand Down
3 changes: 2 additions & 1 deletion home/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strings"

"github.com/AdguardTeam/AdGuardHome/event"
"github.com/AdguardTeam/golibs/log"
)

Expand Down Expand Up @@ -85,6 +86,6 @@ func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
}

config.Language = language
onConfigModified()
onConfigModified(event.I18N)
returnOK(w)
}
3 changes: 2 additions & 1 deletion home/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sync"
"time"

"github.com/AdguardTeam/AdGuardHome/event"
"github.com/AdguardTeam/golibs/log"
"github.com/joomcode/errorx"
)
Expand Down Expand Up @@ -268,7 +269,7 @@ func (t *TLSMod) handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
t.status = status
t.confLock.Unlock()
t.setCertFileTime()
onConfigModified()
onConfigModified(event.TLS)
err = reconfigureDNSServer()
if err != nil {
httpError(w, http.StatusInternalServerError, "%s", err)
Expand Down
Loading

0 comments on commit 9615c97

Please sign in to comment.