Compare commits

...

3 Commits

Author SHA1 Message Date
bakito
1986e87518 correct log fields 2021-04-03 21:04:50 +02:00
bakito
cf0a381f80 log fields 2021-04-03 20:38:41 +02:00
bakito
5e591e04c3 simplifiy
cleanup code
skip arm for darwin and windows
2021-04-03 20:19:34 +02:00
6 changed files with 104 additions and 36 deletions

View File

@@ -1,29 +1,38 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
builds:
- env:
- CGO_ENABLED=0
ldflags:
- -s -w -X github.com/bakito/adguardhome-sync/version.Version={{.Version}}
goos:
- linux
- windows
- darwin
goarch:
- 386
- amd64
- arm
- arm64
goarm:
- 5
- 6
- 7
hooks:
post: upx {{ .Path }}
- env:
- CGO_ENABLED=0
ldflags:
- -s -w -X github.com/bakito/adguardhome-sync/version.Version={{.Version}}
goos:
- linux
- windows
- darwin
goarch:
- 386
- amd64
- arm
- arm64
goarm:
- 5
- 6
- 7
ignore:
- goos: darwin
goarch: arm
- goos: darwin
goarch: arm64
- goos: windows
goarch: arm
- goos: windows
goarch: arm64
hooks:
post: upx {{ .Path }}
archives:
- replacements:
386: i386
amd64: x86_64
- replacements:
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
@@ -32,5 +41,5 @@ changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
- '^docs:'
- '^test:'

View File

@@ -16,6 +16,7 @@ var (
l = log.GetLogger("client")
)
// New create a new client
func New(config types.AdGuardInstance) (Client, error) {
var apiURL string
@@ -46,6 +47,7 @@ func New(config types.AdGuardInstance) (Client, error) {
}, nil
}
// Client AdGuard Home API client interface
type Client interface {
Host() string

View File

@@ -29,26 +29,38 @@ func init() {
ErrorOutputPaths: []string{"stderr"},
}
opt := zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return zapcore.NewTee(c, &logList{enc: zapcore.NewConsoleEncoder(cfg.EncoderConfig)})
return zapcore.NewTee(c, &logList{
enc: zapcore.NewConsoleEncoder(cfg.EncoderConfig),
LevelEnabler: cfg.Level,
})
})
rootLogger, _ = cfg.Build(opt)
}
type logList struct {
zapcore.LevelEnabler
enc zapcore.Encoder
}
func (l *logList) Enabled(_ zapcore.Level) bool {
return true
func (l *logList) clone() *logList {
return &logList{
LevelEnabler: l.LevelEnabler,
enc: l.enc.Clone(),
}
}
func (l *logList) With(_ []zapcore.Field) zapcore.Core {
return l
func (l *logList) With(fields []zapcore.Field) zapcore.Core {
clone := l.clone()
addFields(clone.enc, fields)
return clone
}
func (l *logList) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
return ce.AddCore(ent, l)
if l.Enabled(ent.Level) {
return ce.AddCore(ent, l)
}
return ce
}
func (l *logList) Write(ent zapcore.Entry, fields []zapcore.Field) error {
@@ -68,10 +80,13 @@ func (l *logList) Sync() error {
return nil
}
// Logs get the current logs
func Logs() []string {
var list []string
for _, l := range logs {
list = append(list, l)
}
return list
return logs
}
func addFields(enc zapcore.ObjectEncoder, fields []zapcore.Field) {
for i := range fields {
fields[i].AddTo(enc)
}
}

View File

@@ -3,7 +3,6 @@ package sync
import (
"context"
"fmt"
"github.com/bakito/adguardhome-sync/pkg/log"
"net"
"net/http"
"os"
@@ -11,6 +10,8 @@ import (
"strings"
"syscall"
"time"
"github.com/bakito/adguardhome-sync/pkg/log"
)
func (w *worker) handleSync(rw http.ResponseWriter, req *http.Request) {

View File

@@ -152,6 +152,12 @@ func (w *worker) syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardIn
return
}
err = w.syncConfigs(o, rs, rc)
if err != nil {
l.With("error", err).Error("Error syncing configs")
return
}
err = w.syncRewrites(o.rewrites, rc)
if err != nil {
l.With("error", err).Error("Error syncing rewrites")
@@ -305,7 +311,10 @@ func (w *worker) syncGeneralSettings(o *origin, rs *types.Status, replica client
return err
}
}
return nil
}
func (w *worker) syncConfigs(o *origin, rs *types.Status, replica client.Client) error {
qlc, err := replica.QueryLogConfig()
if err != nil {
return err

View File

@@ -8,6 +8,7 @@ import (
"time"
)
// Config application configuration struct
type Config struct {
Origin AdGuardInstance `json:"origin" yaml:"origin"`
Replica *AdGuardInstance `json:"replica,omitempty" yaml:"replica,omitempty"`
@@ -16,12 +17,14 @@ type Config struct {
API API `json:"api,omitempty" yaml:"api,omitempty"`
}
// API configuration
type API struct {
Port int `json:"port,omitempty" yaml:"port,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
}
// UniqueReplicas get unique replication instances
func (cfg *Config) UniqueReplicas() []AdGuardInstance {
dedup := make(map[string]AdGuardInstance)
if cfg.Replica != nil {
@@ -38,6 +41,7 @@ func (cfg *Config) UniqueReplicas() []AdGuardInstance {
return r
}
// AdGuardInstance adguard home config instance
type AdGuardInstance struct {
URL string `json:"url" yaml:"url"`
APIPath string `json:"apiPath,omitempty" yaml:"apiPath,omitempty"`
@@ -46,14 +50,17 @@ type AdGuardInstance struct {
InsecureSkipVerify bool `json:"insecureSkipVerify" yaml:"insecureSkipVerify"`
}
// Key AdGuardInstance key
func (i *AdGuardInstance) Key() string {
return fmt.Sprintf("%s%s", i.URL, i.APIPath)
}
// Protection API struct
type Protection struct {
ProtectionEnabled bool `json:"protection_enabled"`
}
// Status API struct
type Status struct {
Protection
DNSAddresses []string `json:"dns_addresses"`
@@ -65,8 +72,10 @@ type Status struct {
Language string `json:"language"`
}
// RewriteEntries list of RewriteEntry
type RewriteEntries []RewriteEntry
// Merge RewriteEntries
func (rwe *RewriteEntries) Merge(other *RewriteEntries) (RewriteEntries, RewriteEntries) {
current := make(map[string]RewriteEntry)
@@ -91,17 +100,21 @@ func (rwe *RewriteEntries) Merge(other *RewriteEntries) (RewriteEntries, Rewrite
return adds, removes
}
// RewriteEntry API struct
type RewriteEntry struct {
Domain string `json:"domain"`
Answer string `json:"answer"`
}
// Key RewriteEntry key
func (re *RewriteEntry) Key() string {
return fmt.Sprintf("%s#%s", re.Domain, re.Answer)
}
// Filters list of Filter
type Filters []Filter
// Filter API struct
type Filter struct {
ID int `json:"id"`
Enabled bool `json:"enabled"`
@@ -112,6 +125,7 @@ type Filter struct {
Whitelist bool `json:"whitelist"` // needed for add
}
// FilteringStatus API struct
type FilteringStatus struct {
FilteringConfig
Filters Filters `json:"filters"`
@@ -119,39 +133,48 @@ type FilteringStatus struct {
UserRules UserRules `json:"user_rules"`
}
// UserRules API struct
type UserRules []string
// String toString of Users
func (ur UserRules) String() string {
return strings.Join(ur, "\n")
}
// EnableConfig API struct
type EnableConfig struct {
Enabled bool `json:"enabled"`
}
// IntervalConfig API struct
type IntervalConfig struct {
Interval int `json:"interval"`
}
// FilteringConfig API struct
type FilteringConfig struct {
EnableConfig
IntervalConfig
}
// QueryLogConfig API struct
type QueryLogConfig struct {
EnableConfig
IntervalConfig
AnonymizeClientIP bool `json:"anonymize_client_ip"`
}
// Equals QueryLogConfig equal check
func (qlc *QueryLogConfig) Equals(o *QueryLogConfig) bool {
return qlc.Enabled == o.Enabled && qlc.AnonymizeClientIP == o.AnonymizeClientIP && qlc.Interval == o.Interval
}
// RefreshFilter API struct
type RefreshFilter struct {
Whitelist bool `json:"whitelist"`
}
// Merge merge RefreshFilters
func (fs *Filters) Merge(other Filters) (Filters, Filters) {
current := make(map[string]Filter)
@@ -176,18 +199,22 @@ func (fs *Filters) Merge(other Filters) (Filters, Filters) {
return adds, removes
}
// Services API struct
type Services []string
// Sort sort Services
func (s Services) Sort() {
sort.Strings(s)
}
// Equals Services equal check
func (s *Services) Equals(o *Services) bool {
s.Sort()
o.Sort()
return equals(*s, *o)
}
// Clients API struct
type Clients struct {
Clients []Client `json:"clients"`
AutoClients []struct {
@@ -200,6 +227,7 @@ type Clients struct {
SupportedTags []string `json:"supported_tags"`
}
// Client API struct
type Client struct {
Ids []string `json:"ids"`
Tags []string `json:"tags"`
@@ -217,6 +245,7 @@ type Client struct {
DisallowedRule string `json:"disallowed_rule"`
}
// Sort sort clients
func (cl *Client) Sort() {
sort.Strings(cl.Ids)
sort.Strings(cl.Tags)
@@ -224,6 +253,7 @@ func (cl *Client) Sort() {
sort.Strings(cl.Upstreams)
}
// Equal Clients equal check
func (cl *Client) Equal(o *Client) bool {
cl.Sort()
o.Sort()
@@ -233,6 +263,7 @@ func (cl *Client) Equal(o *Client) bool {
return string(a) == string(b)
}
// Merge merge Clients
func (clients *Clients) Merge(other *Clients) ([]Client, []Client, []Client) {
current := make(map[string]Client)
for _, client := range clients.Clients {
@@ -266,6 +297,7 @@ func (clients *Clients) Merge(other *Clients) ([]Client, []Client, []Client) {
return adds, updates, removes
}
// ClientUpdate API struct
type ClientUpdate struct {
Name string `json:"name"`
Data Client `json:"data"`