Pull request: all: add stub binary for new api

Merge in DNS/adguard-home from new-api to master

Squashed commit of the following:

commit 83f4418c253b9abc5131d9e2acc2a4a96e4122c4
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 26 19:09:34 2022 +0300

    all: fix build

commit 1fbb53fdf779bde79fab72f9c8eb929e08bb044c
Merge: 73a55197 1c89394a
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 26 18:37:27 2022 +0300

    Merge branch 'master' into new-api

commit 73a5519723f662979bdeb5192bc15835e7f03512
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 26 18:36:50 2022 +0300

    v1: imp names, docs

commit d3fbc2f2082612b8ba438c8216c6c74421cc2df5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Apr 22 17:55:42 2022 +0300

    cmd: imp docs

commit c2a73aa364a848e8066d1132d4b53bbc3e22db2d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Apr 22 16:19:14 2022 +0300

    all: add stub binary for new api
This commit is contained in:
Ainar Garipov 2022-04-26 20:50:09 +03:00
parent 1c89394aef
commit ed449c6186
14 changed files with 5437 additions and 2 deletions

View File

@ -34,6 +34,8 @@ YARN_INSTALL_FLAGS = $(YARN_FLAGS) --network-timeout 120000 --silent\
--ignore-engines --ignore-optional --ignore-platform\
--ignore-scripts
V1API = 0
# Macros for the build-release target. If FRONTEND_PREBUILT is 0, the
# default, the macro $(BUILD_RELEASE_DEPS_$(FRONTEND_PREBUILT)) expands
# into BUILD_RELEASE_DEPS_0, and so both frontend and backend
@ -61,6 +63,7 @@ ENV = env\
PATH="$${PWD}/bin:$$( "$(GO.MACRO)" env GOPATH )/bin:$${PATH}"\
RACE='$(RACE)'\
SIGN='$(SIGN)'\
V1API='$(V1API)'\
VERBOSE='$(VERBOSE)'\
VERSION='$(VERSION)'\

View File

@ -175,3 +175,13 @@ func RootDirFS() (fsys fs.FS) {
// behavior is undocumented but it currently works.
return os.DirFS("")
}
// NotifyShutdownSignal notifies c on receiving shutdown signals.
func NotifyShutdownSignal(c chan<- os.Signal) {
notifyShutdownSignal(c)
}
// IsShutdownSignal returns true if sig is a shutdown signal.
func IsShutdownSignal(sig os.Signal) (ok bool) {
return isShutdownSignal(sig)
}

27
internal/aghos/os_unix.go Normal file
View File

@ -0,0 +1,27 @@
//go:build darwin || freebsd || linux || openbsd
// +build darwin freebsd linux openbsd
package aghos
import (
"os"
"os/signal"
"golang.org/x/sys/unix"
)
func notifyShutdownSignal(c chan<- os.Signal) {
signal.Notify(c, unix.SIGINT, unix.SIGQUIT, unix.SIGTERM)
}
func isShutdownSignal(sig os.Signal) (ok bool) {
switch sig {
case
unix.SIGINT,
unix.SIGQUIT,
unix.SIGTERM:
return true
default:
return false
}
}

View File

@ -4,6 +4,10 @@
package aghos
import (
"os"
"os/signal"
"syscall"
"golang.org/x/sys/windows"
)
@ -35,3 +39,20 @@ func haveAdminRights() (bool, error) {
func isOpenWrt() (ok bool) {
return false
}
func notifyShutdownSignal(c chan<- os.Signal) {
// syscall.SIGTERM is processed automatically. See go doc os/signal,
// section Windows.
signal.Notify(c, os.Interrupt)
}
func isShutdownSignal(sig os.Signal) (ok bool) {
switch sig {
case
os.Interrupt,
syscall.SIGTERM:
return true
default:
return false
}
}

33
internal/v1/agh/agh.go Normal file
View File

@ -0,0 +1,33 @@
// Package agh contains common entities and interfaces of AdGuard Home.
//
// TODO(a.garipov): Move to the upper-level internal/.
package agh
import "context"
// Service is the interface for API servers.
//
// TODO(a.garipov): Consider adding a context to Start.
//
// TODO(a.garipov): Consider adding a Wait method or making an extension
// interface for that.
type Service interface {
// Start starts the service. It does not block.
Start() (err error)
// Shutdown gracefully stops the service. ctx is used to determine
// a timeout before trying to stop the service less gracefully.
Shutdown(ctx context.Context) (err error)
}
// type check
var _ Service = EmptyService{}
// EmptyService is a Service that does nothing.
type EmptyService struct{}
// Start implements the Service interface for EmptyService.
func (EmptyService) Start() (err error) { return nil }
// Shutdown implements the Service interface for EmptyService.
func (EmptyService) Shutdown(_ context.Context) (err error) { return nil }

69
internal/v1/cmd/cmd.go Normal file
View File

@ -0,0 +1,69 @@
// Package cmd is the AdGuard Home entry point. It contains the on-disk
// configuration file utilities, signal processing logic, and so on.
//
// TODO(a.garipov): Move to the upper-level internal/.
package cmd
import (
"context"
"io/fs"
"math/rand"
"net"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/v1/websvc"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
)
// Main is the entry point of application.
func Main(clientBuildFS fs.FS) {
// # Initial Configuration
rand.Seed(time.Now().UnixNano())
// TODO(a.garipov): Set up logging.
// # Web Service
// TODO(a.garipov): Use in the Web service.
_ = clientBuildFS
// TODO(a.garipov): Make configurable.
web := websvc.New(&websvc.Config{
Addresses: []*netutil.IPPort{{
IP: net.IP{127, 0, 0, 1},
Port: 3001,
}},
Timeout: 60 * time.Second,
})
err := web.Start()
fatalOnError(err)
sigHdlr := newSignalHandler(
web,
)
go sigHdlr.handle()
select {}
}
// defaultTimeout is the timeout used for some operations where another timeout
// hasn't been defined yet.
const defaultTimeout = 15 * time.Second
// ctxWithDefaultTimeout is a helper function that returns a context with
// timeout set to defaultTimeout.
func ctxWithDefaultTimeout() (ctx context.Context, cancel context.CancelFunc) {
return context.WithTimeout(context.Background(), defaultTimeout)
}
// fatalOnError is a helper that exits the program with an error code if err is
// not nil. It must only be used within Main.
func fatalOnError(err error) {
if err != nil {
log.Fatal(err)
}
}

70
internal/v1/cmd/signal.go Normal file
View File

@ -0,0 +1,70 @@
package cmd
import (
"os"
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/AdGuardHome/internal/v1/agh"
"github.com/AdguardTeam/golibs/log"
)
// signalHandler processes incoming signals and shuts services down.
type signalHandler struct {
signal chan os.Signal
// services are the services that are shut down before application
// exiting.
services []agh.Service
}
// handle processes OS signals.
func (h *signalHandler) handle() {
defer log.OnPanic("signalProcessor.handle")
for sig := range h.signal {
log.Info("sigproc: received signal %q", sig)
if aghos.IsShutdownSignal(sig) {
h.shutdown()
}
}
}
// Exit status constants.
const (
statusSuccess = 0
statusError = 1
)
// shutdown gracefully shuts down all services.
func (h *signalHandler) shutdown() {
ctx, cancel := ctxWithDefaultTimeout()
defer cancel()
status := statusSuccess
log.Info("sigproc: shutting down services")
for i, service := range h.services {
err := service.Shutdown(ctx)
if err != nil {
log.Error("sigproc: shutting down service at index %d: %s", i, err)
status = statusError
}
}
log.Info("sigproc: shutting down adguard home")
os.Exit(status)
}
// newSignalHandler returns a new signalHandler that shuts down svcs.
func newSignalHandler(svcs ...agh.Service) (h *signalHandler) {
h = &signalHandler{
signal: make(chan os.Signal, 1),
services: svcs,
}
aghos.NotifyShutdownSignal(h.signal)
return h
}

View File

@ -0,0 +1,185 @@
// Package websvc contains the AdGuard Home web service.
//
// TODO(a.garipov): Add tests.
package websvc
import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"sync"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/v1/agh"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
)
// Config is the AdGuard Home web service configuration structure.
type Config struct {
// TLS is the optional TLS configuration. If TLS is not nil,
// SecureAddresses must not be empty.
TLS *tls.Config
// Addresses are the addresses on which to serve the plain HTTP API.
Addresses []*netutil.IPPort
// SecureAddresses are the addresses on which to serve the HTTPS API. If
// SecureAddresses is not empty, TLS must not be nil.
SecureAddresses []*netutil.IPPort
// Timeout is the timeout for all server operations.
Timeout time.Duration
}
// Service is the AdGuard Home web service. A nil *Service is a valid service
// that does nothing.
type Service struct {
tls *tls.Config
servers []*http.Server
timeout time.Duration
}
// New returns a new properly initialized *Service. If c is nil, svc is a nil
// *Service that does nothing.
func New(c *Config) (svc *Service) {
if c == nil {
return nil
}
svc = &Service{
tls: c.TLS,
timeout: c.Timeout,
}
mux := http.NewServeMux()
mux.HandleFunc("/health-check", svc.handleGetHealthCheck)
for _, a := range c.Addresses {
addr := a.String()
errLog := log.StdLog("websvc: http: "+addr, log.ERROR)
svc.servers = append(svc.servers, &http.Server{
Addr: addr,
Handler: mux,
ErrorLog: errLog,
ReadTimeout: c.Timeout,
WriteTimeout: c.Timeout,
IdleTimeout: c.Timeout,
ReadHeaderTimeout: c.Timeout,
})
}
for _, a := range c.SecureAddresses {
addr := a.String()
errLog := log.StdLog("websvc: https: "+addr, log.ERROR)
svc.servers = append(svc.servers, &http.Server{
Addr: addr,
Handler: mux,
TLSConfig: c.TLS,
ErrorLog: errLog,
ReadTimeout: c.Timeout,
WriteTimeout: c.Timeout,
IdleTimeout: c.Timeout,
ReadHeaderTimeout: c.Timeout,
})
}
return svc
}
// Addrs returns all addresses on which this server serves the HTTP API. Addrs
// must not be called until Start returns.
func (svc *Service) Addrs() (addrs []string) {
addrs = make([]string, 0, len(svc.servers))
for _, srv := range svc.servers {
addrs = append(addrs, srv.Addr)
}
return addrs
}
// handleGetHealthCheck is the handler for the GET /health-check HTTP API.
func (svc *Service) handleGetHealthCheck(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "OK")
}
// unit is a convenient alias for struct{}.
type unit = struct{}
// type check
var _ agh.Service = (*Service)(nil)
// Start implements the agh.Service interface for *Service. svc may be nil.
// After Start exits, all HTTP servers have tried to start, possibly failing and
// writing error messages to the log.
func (svc *Service) Start() (err error) {
if svc == nil {
return nil
}
srvs := svc.servers
wg := &sync.WaitGroup{}
wg.Add(len(srvs))
for _, srv := range srvs {
go serve(srv, wg)
}
wg.Wait()
return nil
}
// serve starts and runs srv and writes all errors into its log.
func serve(srv *http.Server, wg *sync.WaitGroup) {
addr := srv.Addr
defer log.OnPanic(addr)
var l net.Listener
var err error
if srv.TLSConfig == nil {
l, err = net.Listen("tcp", addr)
} else {
l, err = tls.Listen("tcp", addr, srv.TLSConfig)
}
if err != nil {
srv.ErrorLog.Printf("starting srv %s: binding: %s", addr, err)
}
// Update the server's address in case the address had the port zero, which
// would mean that a random available port was automatically chosen.
srv.Addr = l.Addr().String()
log.Info("websvc: starting srv http://%s", srv.Addr)
wg.Done()
err = srv.Serve(l)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
srv.ErrorLog.Printf("starting srv %s: %s", addr, err)
}
}
// Shutdown implements the agh.Service interface for *Service. svc may be nil.
func (svc *Service) Shutdown(ctx context.Context) (err error) {
if svc == nil {
return nil
}
var errs []error
for _, srv := range svc.servers {
serr := srv.Shutdown(ctx)
if serr != nil {
errs = append(errs, fmt.Errorf("shutting down srv %s: %w", srv.Addr, serr))
}
}
if len(errs) > 0 {
return errors.List("shutting down")
}
return nil
}

View File

@ -0,0 +1,69 @@
package websvc_test
import (
"context"
"io"
"net"
"net/http"
"net/url"
"testing"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/v1/websvc"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testTimeout = 1 * time.Second
func TestService_Start_getHealthCheck(t *testing.T) {
c := &websvc.Config{
TLS: nil,
Addresses: []*netutil.IPPort{{
IP: net.IP{127, 0, 0, 1},
Port: 0,
}},
SecureAddresses: nil,
Timeout: testTimeout,
}
svc := websvc.New(c)
err := svc.Start()
require.NoError(t, err)
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
t.Cleanup(cancel)
err = svc.Shutdown(ctx)
require.NoError(t, err)
})
addrs := svc.Addrs()
require.Len(t, addrs, 1)
u := &url.URL{
Scheme: "http",
Host: addrs[0],
Path: "/health-check",
}
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
require.NoError(t, err)
httpCli := &http.Client{
Timeout: testTimeout,
}
resp, err := httpCli.Do(req)
require.NoError(t, err)
testutil.CleanupAndRequireSuccess(t, resp.Body.Close)
assert.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, []byte("OK"), body)
}

View File

@ -1,3 +1,6 @@
//go:build !v1
// +build !v1
package main
import (

21
main_v1.go Normal file
View File

@ -0,0 +1,21 @@
//go:build v1
// +build v1
package main
import (
"embed"
"github.com/AdguardTeam/AdGuardHome/internal/v1/cmd"
)
// Embed the prebuilt client here since we strive to keep .go files inside the
// internal directory and the embed package is unable to embed files located
// outside of the same or underlying directory.
//go:embed build2
var clientBuildFS embed.FS
func main() {
cmd.Main(clientBuildFS)
}

4913
openapi/v1.yaml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -123,4 +123,14 @@ CGO_ENABLED="$cgo_enabled"
GO111MODULE='on'
export CGO_ENABLED GO111MODULE
"$go" build --ldflags "$ldflags" "$race_flags" --trimpath "$o_flags" "$v_flags" "$x_flags"
# Build the new binary if requested.
if [ "${V1API:-0}" -eq '0' ]
then
tags_flags='--tags='
else
tags_flags='--tags=v1'
fi
readonly tags_flags
"$go" build --ldflags "$ldflags" "$race_flags" "$tags_flags" --trimpath "$o_flags" "$v_flags"\
"$x_flags"

View File

@ -140,6 +140,7 @@ underscores() {
-e '_others.go'\
-e '_test.go'\
-e '_unix.go'\
-e '_v1.go'\
-e '_windows.go' \
-v\
| sed -e 's/./\t\0/'
@ -222,7 +223,7 @@ gocyclo --over 17 ./internal/dhcpd/ ./internal/dnsforward/\
# Apply stricter standards to new or somewhat refactored code.
gocyclo --over 10 ./internal/aghio/ ./internal/aghnet/ ./internal/aghos/\
./internal/aghtest/ ./internal/stats/ ./internal/tools/\
./internal/updater/ ./internal/version/ ./main.go
./internal/updater/ ./internal/v1/ ./internal/version/ ./main.go\
ineffassign ./...