-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adguard home provider #3249
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package adguardhome | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"regexp" | ||
|
||
adguard "github.com/markussiebert/go-adguardhome-client/client" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"sigs.k8s.io/external-dns/endpoint" | ||
"sigs.k8s.io/external-dns/plan" | ||
"sigs.k8s.io/external-dns/provider" | ||
) | ||
|
||
// The regex hat should identify and parse all custom rules in adguard home that are used to handle dns records | ||
var regex = regexp.MustCompile(`(?P<dnsName>[a-zA-Z\*.-]+)\$dnstype=(?P<recordType>[A-Z]+),dnsrewrite=NOERROR;[A-Z]+;(?P<target>.*)`) | ||
|
||
// AdguardHomeProvider is an implementation of Provider for Vultr DNS. | ||
type AdguardHomeProvider struct { | ||
provider.BaseProvider | ||
client adguard.ClientWithResponses | ||
|
||
domainFilter endpoint.DomainFilter | ||
DryRun bool | ||
} | ||
|
||
// Prepare basicAuth with credentials | ||
func basicAuth(username, password string) string { | ||
auth := username + ":" + password | ||
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) | ||
} | ||
|
||
// NewAdguardHomeProvider initializes a new Vultr BNS based provider | ||
func NewAdguardHomeProvider(domainFilter endpoint.DomainFilter, dryRun bool) (*AdguardHomeProvider, error) { | ||
adguardHomeURL, adguardHomeUrlOk := os.LookupEnv("ADGUARD_HOME_URL") | ||
if !adguardHomeUrlOk { | ||
return nil, fmt.Errorf("no url was found in environment variable ADGUARD_HOME_URL") | ||
} | ||
|
||
adguardHomeUser, adguardHomeUserOk := os.LookupEnv("ADGUARD_HOME_USER") | ||
if !adguardHomeUserOk { | ||
return nil, fmt.Errorf("no user was found in environment variable ADGUARD_HOME_USER") | ||
} | ||
|
||
adguardHomePass, adguardHomePassOk := os.LookupEnv("ADGUARD_HOME_PASS") | ||
if !adguardHomePassOk { | ||
return nil, fmt.Errorf("no password was found in environment variable ADGUARD_HOME_PASS") | ||
} | ||
|
||
client, clientOk := adguard.NewClientWithResponses(adguardHomeURL, adguard.ClientOption(func(client *adguard.AdguardHomeClient) error { | ||
client.RequestEditors = append(client.RequestEditors, | ||
adguard.RequestEditorFn( | ||
func(ctx context.Context, req *http.Request) error { | ||
req.Header.Add("Authorization", basicAuth(adguardHomeUser, adguardHomePass)) | ||
return nil | ||
}, | ||
), | ||
adguard.RequestEditorFn( | ||
func(ctx context.Context, req *http.Request) error { | ||
req.Header.Add("User-Agent", "ExternalDNS") | ||
return nil | ||
}, | ||
), | ||
) | ||
return nil | ||
})) | ||
|
||
if clientOk != nil { | ||
return nil, fmt.Errorf("failed to create the adguard home api client") | ||
} | ||
|
||
p := &AdguardHomeProvider{ | ||
client: *client, | ||
domainFilter: domainFilter, | ||
DryRun: dryRun, | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
// ApplyChanges implements Provider, syncing desired state with the AdguardHome server Local DNS. | ||
func (p *AdguardHomeProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error { | ||
// Get current filter values | ||
resp, err := p.client.FilteringStatusWithResponse(ctx) | ||
if err != nil { | ||
log.Errorf("Error %s", err) | ||
return err | ||
} | ||
|
||
debugJ, _ := json.MarshalIndent(changes, "", " ") | ||
log.Debugf("changes, %s", string(debugJ)) | ||
userRules := *resp.JSON200.UserRules | ||
|
||
for _, createValue := range append(changes.Create, changes.UpdateNew...) { | ||
newRule := genAdguardCustomRuleRecord(createValue) | ||
log.Debugf("add custom rule %s", newRule) | ||
userRules = append(userRules, newRule) | ||
} | ||
|
||
for _, deleteValue := range append(changes.UpdateOld, changes.Delete...) { | ||
userRules = removeRule(userRules, genAdguardCustomRuleRecord(deleteValue), false) | ||
} | ||
|
||
apply, err := p.client.FilteringSetRulesWithResponse(ctx, adguard.SetRulesRequest{ | ||
Rules: &userRules, | ||
}) | ||
|
||
if apply.StatusCode() != 200 { | ||
log.Errorf("Failed to sync records %s", apply.Status()) | ||
} | ||
return err | ||
} | ||
|
||
func removeRule(rules []string, ruleToRemove string, finishAfterFirst bool) []string { | ||
userRules := rules | ||
for userRuleIndex, userRule := range rules { | ||
if userRule == ruleToRemove { | ||
log.Debugf("deleted rule %s", ruleToRemove) | ||
userRules = append(userRules[:userRuleIndex], userRules[userRuleIndex+1:]...) | ||
if finishAfterFirst { | ||
return userRules | ||
} | ||
} | ||
} | ||
return userRules | ||
} | ||
|
||
// Records implements Provider, populating a slice of endpoints from | ||
// AdguardHome local DNS. | ||
func (p *AdguardHomeProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, error) { | ||
resp, err := p.client.FilteringStatusWithResponse(ctx) | ||
if err != nil { | ||
log.Errorf("Error %s", err) | ||
} | ||
|
||
log.WithFields(log.Fields{ | ||
"func": "Records", | ||
"rules": resp.JSON200.UserRules, | ||
}).Debugf("retrieved rules") | ||
var ret []*endpoint.Endpoint | ||
for _, customRule := range *resp.JSON200.UserRules { | ||
parsed := parseAdguardCustomRuleRecord(customRule) | ||
if parsed != nil { | ||
ret = append(ret, parsed) | ||
} | ||
} | ||
|
||
return ret, nil | ||
} | ||
|
||
func genAdguardCustomRuleRecord(e *endpoint.Endpoint) string { | ||
return fmt.Sprintf("%s$dnstype=%s,dnsrewrite=NOERROR;%s;%s", e.DNSName, e.RecordType, e.RecordType, e.Targets.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest wrapping the DNSName in |%s| because otherwise a record for example.com will also match test.example.com or test.example.com.example.com Default matching mode in AGH appears to be "contains substring" As per https://github.com/AdguardTeam/AdGuardHome/wiki/Hosts-Blocklists#adblock-style i.e. "|%s|$dnstype=%s,dnsrewrite=NOERROR;%s;%s" |
||
} | ||
|
||
func parseAdguardCustomRuleRecord(s string) *endpoint.Endpoint { | ||
|
||
matched := regex.FindStringSubmatch(s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The regex for this will likely need to be altered for the above comment too. |
||
|
||
if matched == nil { | ||
return nil | ||
} | ||
|
||
log.Debugf("successfully parsed rule %s", s) | ||
|
||
endpoint := &endpoint.Endpoint{ | ||
DNSName: matched[1], | ||
RecordType: matched[2], | ||
Targets: endpoint.Targets{matched[3]}, | ||
} | ||
debugJ, _ := json.MarshalIndent(endpoint, "", " ") | ||
log.Debugf("returning endpoint %s", string(debugJ)) | ||
return endpoint | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to document that the AGH URL passed here needs to be suffixed with /control/ for a regular AGH deployment. Since that's where the APIs are under.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got confused trying to troubleshoot that with a very unhelpful error about a nil reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right - it took me some time to figure Out the right path...