diff --git a/Makefile b/Makefile index ec92f90..4489c06 100644 --- a/Makefile +++ b/Makefile @@ -32,5 +32,5 @@ endif mockgen: ifeq (, $(shell which mockgen)) - $(shell go install github.com/golang/mock/mockgen@v1.5) + $(shell go install github.com/golang/mock/mockgen@v1.6.0) endif \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index f64e112..75bb26b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,6 +20,7 @@ const ( configAPIPort = "api.port" configAPIUsername = "api.username" configAPIPassword = "api.password" + configAPIDarkMode = "api.darkMode" configFeatureDHCPServerConfig = "features.dhcp.serverConfig" configFeatureDHCPStaticLeases = "features.dhcp.staticLeases" diff --git a/cmd/run.go b/cmd/run.go index 1ef6467..7a052ba 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -36,6 +36,8 @@ func init() { _ = viper.BindPFlag(configAPIUsername, doCmd.PersistentFlags().Lookup("api-username")) doCmd.PersistentFlags().String("api-password", "", "Sync API password") _ = viper.BindPFlag(configAPIPassword, doCmd.PersistentFlags().Lookup("api-password")) + doCmd.PersistentFlags().String("api-darkMode", "", "API UI in dark mode") + _ = viper.BindPFlag(configAPIDarkMode, doCmd.PersistentFlags().Lookup("api-darkMode")) doCmd.PersistentFlags().Bool("feature-dhcp-server-config", true, "Enable DHCP server config feature") _ = viper.BindPFlag(configFeatureDHCPServerConfig, doCmd.PersistentFlags().Lookup("feature-dhcp-server-config")) diff --git a/go.mod b/go.mod index 8681872..1c18210 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/bakito/adguardhome-sync go 1.17 require ( + github.com/gin-gonic/gin v1.7.7 github.com/go-resty/resty/v2 v2.7.0 github.com/golang/mock v1.6.0 github.com/google/uuid v1.3.0 @@ -18,21 +19,34 @@ require ( require ( github.com/fsnotify/fsnotify v1.5.1 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.13.0 // indirect + github.com/go-playground/universal-translator v0.17.0 // indirect + github.com/go-playground/validator/v10 v10.4.1 // indirect + github.com/golang/protobuf v1.5.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/leodido/go-urn v1.2.0 // indirect github.com/magiconair/properties v1.8.5 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect github.com/mitchellh/mapstructure v1.4.3 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml v1.9.4 // indirect github.com/spf13/afero v1.6.0 // indirect github.com/spf13/cast v1.4.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.2.0 // indirect + github.com/ugorji/go/codec v1.1.7 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect + golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect golang.org/x/text v0.3.7 // indirect + google.golang.org/protobuf v1.27.1 // indirect gopkg.in/ini.v1 v1.66.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 82c0688..d0763dc 100644 --- a/go.sum +++ b/go.sum @@ -112,6 +112,10 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4 github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs= +github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -119,6 +123,14 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2 github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= +github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= @@ -173,6 +185,7 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= @@ -244,6 +257,7 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= @@ -259,6 +273,8 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w= github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= @@ -272,6 +288,7 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= @@ -286,9 +303,11 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= @@ -367,6 +386,10 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= +github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= +github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -401,6 +424,7 @@ golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -659,6 +683,7 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= diff --git a/pkg/client/client.go b/pkg/client/client.go index aa465c8..7df3f60 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -57,13 +57,11 @@ func New(config types.AdGuardInstance) (Client, error) { // Client AdguardHome API client interface type Client interface { Host() string - Status() (*types.Status, error) ToggleProtection(enable bool) error RewriteList() (*types.RewriteEntries, error) AddRewriteEntries(e ...types.RewriteEntry) error DeleteRewriteEntries(e ...types.RewriteEntry) error - Filtering() (*types.FilteringStatus, error) ToggleFiltering(enabled bool, interval float64) error AddFilters(whitelist bool, e ...types.Filter) error @@ -71,34 +69,27 @@ type Client interface { UpdateFilters(whitelist bool, e ...types.Filter) error RefreshFilters(whitelist bool) error SetCustomRules(rules types.UserRules) error - SafeBrowsing() (bool, error) ToggleSafeBrowsing(enable bool) error Parental() (bool, error) ToggleParental(enable bool) error SafeSearch() (bool, error) ToggleSafeSearch(enable bool) error - Services() (types.Services, error) SetServices(services types.Services) error - Clients() (*types.Clients, error) AddClients(client ...types.Client) error UpdateClients(client ...types.Client) error DeleteClients(client ...types.Client) error - QueryLogConfig() (*types.QueryLogConfig, error) SetQueryLogConfig(enabled bool, interval float64, anonymizeClientIP bool) error StatsConfig() (*types.IntervalConfig, error) SetStatsConfig(interval float64) error Setup() error - AccessList() (*types.AccessList, error) SetAccessList(*types.AccessList) error - DNSConfig() (*types.DNSConfig, error) SetDNSConfig(*types.DNSConfig) error - DHCPServerConfig() (*types.DHCPServerConfig, error) SetDHCPServerConfig(*types.DHCPServerConfig) error AddDHCPStaticLeases(leases ...types.Lease) error diff --git a/pkg/sync/http.go b/pkg/sync/http.go index 98bfc5b..2a27278 100644 --- a/pkg/sync/http.go +++ b/pkg/sync/http.go @@ -7,6 +7,7 @@ import ( _ "embed" "errors" "fmt" + "html/template" "net" "net/http" "os" @@ -16,6 +17,8 @@ import ( "time" "github.com/bakito/adguardhome-sync/pkg/log" + "github.com/bakito/adguardhome-sync/version" + "github.com/gin-gonic/gin" ) var ( @@ -25,77 +28,50 @@ var ( favicon []byte ) -func (w *worker) handleSync(rw http.ResponseWriter, req *http.Request) { - switch req.Method { - case http.MethodPost: - l.With("remote-addr", req.RemoteAddr).Info("Starting sync from API") - w.sync() - default: - http.Error(rw, "only POST allowed", http.StatusBadRequest) - } +func (w *worker) handleSync(c *gin.Context) { + l.With("remote-addr", c.Request.RemoteAddr).Info("Starting sync from API") + w.sync() } -func (w *worker) handleRoot(rw http.ResponseWriter, _ *http.Request) { - rw.Header().Set("Content-Type", "text/html") - _, _ = rw.Write(index) +func (w *worker) handleRoot(c *gin.Context) { + c.HTML(http.StatusOK, "index.html", map[string]interface{}{ + "DarkMode": w.cfg.API.DarkMode, + "Version": version.Version, + "Build": version.Build, + }, + ) } -func (w *worker) handleFavicon(rw http.ResponseWriter, _ *http.Request) { - rw.Header().Set("Content-Type", "image/x-icon") - _, _ = rw.Write(favicon) +func (w *worker) handleFavicon(c *gin.Context) { + c.Data(http.StatusOK, "image/x-icon", favicon) } -func (w *worker) handleLogs(rw http.ResponseWriter, _ *http.Request) { - _, _ = rw.Write([]byte(strings.Join(log.Logs(), ""))) -} - -func (w *worker) basicAuth(h http.HandlerFunc) http.HandlerFunc { - return func(rw http.ResponseWriter, r *http.Request) { - rw.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) - - username, password, authOK := r.BasicAuth() - if !authOK { - http.Error(rw, "Not authorized", 401) - return - } - - if username != w.cfg.API.Username || password != w.cfg.API.Password { - http.Error(rw, "Not authorized", 401) - return - } - - h.ServeHTTP(rw, r) - } -} - -func use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc { - for _, m := range middleware { - h = m(h) - } - - return h +func (w *worker) handleLogs(c *gin.Context) { + c.Data(http.StatusOK, "text/plain", []byte(strings.Join(log.Logs(), ""))) } func (w *worker) listenAndServe() { l.With("port", w.cfg.API.Port).Info("Starting API server") ctx, cancel := context.WithCancel(context.Background()) - mux := http.NewServeMux() + + gin.SetMode(gin.ReleaseMode) + r := gin.New() + r.Use(gin.Recovery()) + if w.cfg.API.Username != "" && w.cfg.API.Password != "" { + r.Use(gin.BasicAuth(map[string]string{w.cfg.API.Username: w.cfg.API.Password})) + } httpServer := &http.Server{ Addr: fmt.Sprintf(":%d", w.cfg.API.Port), - Handler: mux, + Handler: r, BaseContext: func(_ net.Listener) context.Context { return ctx }, } - var mw []func(http.HandlerFunc) http.HandlerFunc - if w.cfg.API.Username != "" && w.cfg.API.Password != "" { - mw = append(mw, w.basicAuth) - } - - mux.HandleFunc("/api/v1/sync", use(w.handleSync, mw...)) - mux.HandleFunc("/api/v1/logs", use(w.handleLogs, mw...)) - mux.HandleFunc("/favicon.ico", use(w.handleFavicon, mw...)) - mux.HandleFunc("/", use(w.handleRoot, mw...)) + r.SetHTMLTemplate(template.Must(template.New("index.html").Parse(string(index)))) + r.POST("/api/v1/sync", w.handleSync) + r.GET("/api/v1/logs", w.handleLogs) + r.GET("/favicon.ico", w.handleFavicon) + r.GET("/", w.handleRoot) go func() { if err := httpServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { diff --git a/pkg/sync/index.html b/pkg/sync/index.html index 3832eeb..709226c 100644 --- a/pkg/sync/index.html +++ b/pkg/sync/index.html @@ -1,10 +1,16 @@ AdGuardHome sync - + {{- if .DarkMode }} + + {{- else }} + + {{- end }}