2023-06-20 12:11:34 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-06-29 17:10:39 +01:00
|
|
|
"encoding"
|
2023-06-20 12:11:34 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-06-29 17:10:39 +01:00
|
|
|
"io/fs"
|
2023-06-20 12:11:34 +01:00
|
|
|
"net/netip"
|
|
|
|
"os"
|
2024-02-08 17:39:18 +00:00
|
|
|
"slices"
|
2023-06-20 12:11:34 +01:00
|
|
|
"strings"
|
|
|
|
|
2023-12-15 17:27:47 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/configmigrate"
|
2023-06-29 12:34:06 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
|
2023-06-20 12:11:34 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/version"
|
2023-06-29 17:10:39 +01:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2023-06-20 12:11:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// options contains all command-line options for the AdGuardHome(.exe) binary.
|
|
|
|
type options struct {
|
|
|
|
// confFile is the path to the configuration file.
|
|
|
|
confFile string
|
|
|
|
|
|
|
|
// logFile is the path to the log file. Special values:
|
|
|
|
//
|
|
|
|
// - "stdout": Write to stdout (the default).
|
|
|
|
// - "stderr": Write to stderr.
|
|
|
|
// - "syslog": Write to the system log.
|
|
|
|
logFile string
|
|
|
|
|
|
|
|
// pidFile is the path to the file where to store the PID.
|
|
|
|
pidFile string
|
|
|
|
|
|
|
|
// serviceAction is the service control action to perform:
|
|
|
|
//
|
|
|
|
// - "install": Installs AdGuard Home as a system service.
|
|
|
|
// - "uninstall": Uninstalls it.
|
|
|
|
// - "status": Prints the service status.
|
|
|
|
// - "start": Starts the previously installed service.
|
|
|
|
// - "stop": Stops the previously installed service.
|
|
|
|
// - "restart": Restarts the previously installed service.
|
|
|
|
// - "reload": Reloads the configuration.
|
|
|
|
// - "run": This is a special command that is not supposed to be used
|
|
|
|
// directly it is specified when we register a service, and it indicates
|
|
|
|
// to the app that it is being run as a service.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Use.
|
|
|
|
serviceAction string
|
|
|
|
|
|
|
|
// workDir is the path to the working directory. It is applied before all
|
|
|
|
// other configuration is read, so all relative paths are relative to it.
|
|
|
|
workDir string
|
|
|
|
|
2023-06-29 17:10:39 +01:00
|
|
|
// webAddr contains the address on which to serve the web UI.
|
|
|
|
webAddr netip.AddrPort
|
2023-06-20 12:11:34 +01:00
|
|
|
|
|
|
|
// checkConfig, if true, instructs AdGuard Home to check the configuration
|
2023-06-29 12:34:06 +01:00
|
|
|
// file, optionally print an error message to stdout, and exit with a
|
|
|
|
// corresponding exit code.
|
2023-06-20 12:11:34 +01:00
|
|
|
checkConfig bool
|
|
|
|
|
|
|
|
// disableUpdate, if true, prevents AdGuard Home from automatically checking
|
|
|
|
// for updates.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Use.
|
|
|
|
disableUpdate bool
|
|
|
|
|
|
|
|
// glinetMode enables the GL-Inet compatibility mode.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Use.
|
|
|
|
glinetMode bool
|
|
|
|
|
|
|
|
// help, if true, instructs AdGuard Home to print the command-line option
|
|
|
|
// help message and quit with a successful exit-code.
|
|
|
|
help bool
|
|
|
|
|
|
|
|
// localFrontend, if true, instructs AdGuard Home to use the local frontend
|
|
|
|
// directory instead of the files compiled into the binary.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Use.
|
|
|
|
localFrontend bool
|
|
|
|
|
|
|
|
// performUpdate, if true, instructs AdGuard Home to update the current
|
|
|
|
// binary and restart the service in case it's installed.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Use.
|
|
|
|
performUpdate bool
|
|
|
|
|
|
|
|
// verbose, if true, instructs AdGuard Home to enable verbose logging.
|
|
|
|
verbose bool
|
|
|
|
|
|
|
|
// version, if true, instructs AdGuard Home to print the version to stdout
|
|
|
|
// and quit with a successful exit-code. If verbose is also true, print a
|
|
|
|
// more detailed version description.
|
|
|
|
version bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indexes to help with the [commandLineOptions] initialization.
|
|
|
|
const (
|
|
|
|
confFileIdx = iota
|
|
|
|
logFileIdx
|
|
|
|
pidFileIdx
|
|
|
|
serviceActionIdx
|
|
|
|
workDirIdx
|
2023-06-29 17:10:39 +01:00
|
|
|
webAddrIdx
|
2023-06-20 12:11:34 +01:00
|
|
|
checkConfigIdx
|
|
|
|
disableUpdateIdx
|
|
|
|
glinetModeIdx
|
|
|
|
helpIdx
|
|
|
|
localFrontend
|
|
|
|
performUpdateIdx
|
|
|
|
verboseIdx
|
|
|
|
versionIdx
|
|
|
|
)
|
|
|
|
|
|
|
|
// commandLineOption contains information about a command-line option: its long
|
|
|
|
// and, if there is one, short forms, the value type, the description, and the
|
|
|
|
// default value.
|
|
|
|
type commandLineOption struct {
|
|
|
|
defaultValue any
|
|
|
|
description string
|
|
|
|
long string
|
|
|
|
short string
|
|
|
|
valueType string
|
|
|
|
}
|
|
|
|
|
|
|
|
// commandLineOptions are all command-line options currently supported by
|
|
|
|
// AdGuard Home.
|
|
|
|
var commandLineOptions = []*commandLineOption{
|
|
|
|
confFileIdx: {
|
2023-06-28 10:48:53 +01:00
|
|
|
// TODO(a.garipov): Remove the directory when the new code is ready.
|
|
|
|
defaultValue: "internal/next/AdGuardHome.yaml",
|
2023-06-20 12:11:34 +01:00
|
|
|
description: "Path to the config file.",
|
|
|
|
long: "config",
|
|
|
|
short: "c",
|
|
|
|
valueType: "path",
|
|
|
|
},
|
|
|
|
|
|
|
|
logFileIdx: {
|
|
|
|
defaultValue: "stdout",
|
|
|
|
description: `Path to log file. Special values include "stdout", "stderr", and "syslog".`,
|
|
|
|
long: "logfile",
|
|
|
|
short: "l",
|
|
|
|
valueType: "path",
|
|
|
|
},
|
|
|
|
|
|
|
|
pidFileIdx: {
|
|
|
|
defaultValue: "",
|
|
|
|
description: "Path to the file where to store the PID.",
|
|
|
|
long: "pidfile",
|
|
|
|
short: "",
|
|
|
|
valueType: "path",
|
|
|
|
},
|
|
|
|
|
|
|
|
serviceActionIdx: {
|
|
|
|
defaultValue: "",
|
|
|
|
description: `Service control action: "status", "install" (as a service), ` +
|
|
|
|
`"uninstall" (as a service), "start", "stop", "restart", "reload" (configuration).`,
|
|
|
|
long: "service",
|
|
|
|
short: "s",
|
|
|
|
valueType: "action",
|
|
|
|
},
|
|
|
|
|
|
|
|
workDirIdx: {
|
|
|
|
defaultValue: "",
|
|
|
|
description: `Path to the working directory. ` +
|
|
|
|
`It is applied before all other configuration is read, ` +
|
|
|
|
`so all relative paths are relative to it.`,
|
|
|
|
long: "work-dir",
|
|
|
|
short: "w",
|
|
|
|
valueType: "path",
|
|
|
|
},
|
|
|
|
|
2023-06-29 17:10:39 +01:00
|
|
|
webAddrIdx: {
|
|
|
|
defaultValue: netip.AddrPort{},
|
|
|
|
description: `Address to serve the web UI on, in the host:port format.`,
|
|
|
|
long: "web-addr",
|
|
|
|
short: "",
|
|
|
|
valueType: "host:port",
|
2023-06-20 12:11:34 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
checkConfigIdx: {
|
|
|
|
defaultValue: false,
|
2023-06-29 12:34:06 +01:00
|
|
|
description: "Check configuration, print errors to stdout, and quit.",
|
2023-06-20 12:11:34 +01:00
|
|
|
long: "check-config",
|
|
|
|
short: "",
|
|
|
|
valueType: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
disableUpdateIdx: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: "Disable automatic update checking.",
|
|
|
|
long: "no-check-update",
|
|
|
|
short: "",
|
|
|
|
valueType: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
glinetModeIdx: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: "Run in GL-Inet compatibility mode.",
|
|
|
|
long: "glinet",
|
|
|
|
short: "",
|
|
|
|
valueType: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
helpIdx: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: "Print this help message and quit.",
|
|
|
|
long: "help",
|
|
|
|
short: "h",
|
|
|
|
valueType: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
localFrontend: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: "Use local frontend directories.",
|
|
|
|
long: "local-frontend",
|
|
|
|
short: "",
|
|
|
|
valueType: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
performUpdateIdx: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: "Update the current binary and restart the service in case it's installed.",
|
|
|
|
long: "update",
|
|
|
|
short: "",
|
|
|
|
valueType: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
verboseIdx: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: "Enable verbose logging.",
|
|
|
|
long: "verbose",
|
|
|
|
short: "v",
|
|
|
|
valueType: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
versionIdx: {
|
|
|
|
defaultValue: false,
|
|
|
|
description: `Print the version to stdout and quit. ` +
|
|
|
|
`Print a more detailed version description with -v.`,
|
|
|
|
long: "version",
|
|
|
|
short: "",
|
|
|
|
valueType: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseOptions parses the command-line options for AdGuardHome.
|
|
|
|
func parseOptions(cmdName string, args []string) (opts *options, err error) {
|
|
|
|
flags := flag.NewFlagSet(cmdName, flag.ContinueOnError)
|
|
|
|
|
|
|
|
opts = &options{}
|
|
|
|
for i, fieldPtr := range []any{
|
|
|
|
confFileIdx: &opts.confFile,
|
|
|
|
logFileIdx: &opts.logFile,
|
|
|
|
pidFileIdx: &opts.pidFile,
|
|
|
|
serviceActionIdx: &opts.serviceAction,
|
|
|
|
workDirIdx: &opts.workDir,
|
2023-06-29 17:10:39 +01:00
|
|
|
webAddrIdx: &opts.webAddr,
|
2023-06-20 12:11:34 +01:00
|
|
|
checkConfigIdx: &opts.checkConfig,
|
|
|
|
disableUpdateIdx: &opts.disableUpdate,
|
|
|
|
glinetModeIdx: &opts.glinetMode,
|
|
|
|
helpIdx: &opts.help,
|
|
|
|
localFrontend: &opts.localFrontend,
|
|
|
|
performUpdateIdx: &opts.performUpdate,
|
|
|
|
verboseIdx: &opts.verbose,
|
|
|
|
versionIdx: &opts.version,
|
|
|
|
} {
|
|
|
|
addOption(flags, fieldPtr, commandLineOptions[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.Usage = func() { usage(cmdName, os.Stderr) }
|
|
|
|
|
|
|
|
err = flags.Parse(args)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addOption adds the command-line option described by o to flags using fieldPtr
|
|
|
|
// as the pointer to the value.
|
|
|
|
func addOption(flags *flag.FlagSet, fieldPtr any, o *commandLineOption) {
|
|
|
|
switch fieldPtr := fieldPtr.(type) {
|
|
|
|
case *string:
|
|
|
|
flags.StringVar(fieldPtr, o.long, o.defaultValue.(string), o.description)
|
|
|
|
if o.short != "" {
|
|
|
|
flags.StringVar(fieldPtr, o.short, o.defaultValue.(string), o.description)
|
|
|
|
}
|
|
|
|
case *bool:
|
|
|
|
flags.BoolVar(fieldPtr, o.long, o.defaultValue.(bool), o.description)
|
|
|
|
if o.short != "" {
|
|
|
|
flags.BoolVar(fieldPtr, o.short, o.defaultValue.(bool), o.description)
|
|
|
|
}
|
2023-06-29 17:10:39 +01:00
|
|
|
case encoding.TextUnmarshaler:
|
|
|
|
flags.TextVar(fieldPtr, o.long, o.defaultValue.(encoding.TextMarshaler), o.description)
|
|
|
|
if o.short != "" {
|
|
|
|
flags.TextVar(fieldPtr, o.short, o.defaultValue.(encoding.TextMarshaler), o.description)
|
|
|
|
}
|
2023-06-20 12:11:34 +01:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unexpected field pointer type %T", fieldPtr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// usage prints a usage message similar to the one printed by package flag but
|
|
|
|
// taking long vs. short versions into account as well as using more informative
|
|
|
|
// value hints.
|
|
|
|
func usage(cmdName string, output io.Writer) {
|
|
|
|
options := slices.Clone(commandLineOptions)
|
2023-08-10 18:00:17 +01:00
|
|
|
slices.SortStableFunc(options, func(a, b *commandLineOption) (res int) {
|
|
|
|
return strings.Compare(a.long, b.long)
|
2023-06-20 12:11:34 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
b := &strings.Builder{}
|
|
|
|
_, _ = fmt.Fprintf(b, "Usage of %s:\n", cmdName)
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
writeUsageLine(b, o)
|
|
|
|
|
|
|
|
// Use four spaces before the tab to trigger good alignment for both 4-
|
|
|
|
// and 8-space tab stops.
|
|
|
|
if shouldIncludeDefault(o.defaultValue) {
|
|
|
|
_, _ = fmt.Fprintf(b, " \t%s (Default value: %q)\n", o.description, o.defaultValue)
|
|
|
|
} else {
|
|
|
|
_, _ = fmt.Fprintf(b, " \t%s\n", o.description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = io.WriteString(output, b.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// shouldIncludeDefault returns true if this default value should be printed.
|
|
|
|
func shouldIncludeDefault(v any) (ok bool) {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case bool:
|
|
|
|
return v
|
|
|
|
case string:
|
|
|
|
return v != ""
|
|
|
|
default:
|
|
|
|
return v == nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeUsageLine writes the usage line for the provided command-line option.
|
|
|
|
func writeUsageLine(b *strings.Builder, o *commandLineOption) {
|
|
|
|
if o.short == "" {
|
|
|
|
if o.valueType == "" {
|
|
|
|
_, _ = fmt.Fprintf(b, " --%s\n", o.long)
|
|
|
|
} else {
|
|
|
|
_, _ = fmt.Fprintf(b, " --%s=%s\n", o.long, o.valueType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.valueType == "" {
|
|
|
|
_, _ = fmt.Fprintf(b, " --%s/-%s\n", o.long, o.short)
|
|
|
|
} else {
|
|
|
|
_, _ = fmt.Fprintf(b, " --%[1]s=%[3]s/-%[2]s %[3]s\n", o.long, o.short, o.valueType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processOptions decides if AdGuard Home should exit depending on the results
|
|
|
|
// of command-line option parsing.
|
|
|
|
func processOptions(
|
|
|
|
opts *options,
|
|
|
|
cmdName string,
|
|
|
|
parseErr error,
|
|
|
|
) (exitCode int, needExit bool) {
|
|
|
|
if parseErr != nil {
|
|
|
|
// Assume that usage has already been printed.
|
2023-06-29 17:10:39 +01:00
|
|
|
return statusArgumentError, true
|
2023-06-20 12:11:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if opts.help {
|
|
|
|
usage(cmdName, os.Stdout)
|
|
|
|
|
2023-06-29 17:10:39 +01:00
|
|
|
return statusSuccess, true
|
2023-06-20 12:11:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if opts.version {
|
|
|
|
if opts.verbose {
|
2023-12-15 17:27:47 +00:00
|
|
|
fmt.Print(version.Verbose(configmigrate.LastSchemaVersion))
|
2023-06-20 12:11:34 +01:00
|
|
|
} else {
|
|
|
|
fmt.Printf("AdGuard Home %s\n", version.Version())
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:10:39 +01:00
|
|
|
return statusSuccess, true
|
2023-06-20 12:11:34 +01:00
|
|
|
}
|
|
|
|
|
2023-06-29 12:34:06 +01:00
|
|
|
if opts.checkConfig {
|
|
|
|
err := configmgr.Validate(opts.confFile)
|
|
|
|
if err != nil {
|
|
|
|
_, _ = io.WriteString(os.Stdout, err.Error()+"\n")
|
|
|
|
|
2023-06-29 17:10:39 +01:00
|
|
|
return statusError, true
|
2023-06-29 12:34:06 +01:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:10:39 +01:00
|
|
|
return statusSuccess, true
|
2023-06-29 12:34:06 +01:00
|
|
|
}
|
|
|
|
|
2023-06-20 12:11:34 +01:00
|
|
|
return 0, false
|
|
|
|
}
|
2023-06-29 17:10:39 +01:00
|
|
|
|
|
|
|
// frontendFromOpts returns the frontend to use based on the options.
|
|
|
|
func frontendFromOpts(opts *options, embeddedFrontend fs.FS) (frontend fs.FS, err error) {
|
|
|
|
const frontendSubdir = "build/static"
|
|
|
|
|
|
|
|
if opts.localFrontend {
|
|
|
|
log.Info("warning: using local frontend files")
|
|
|
|
|
|
|
|
return os.DirFS(frontendSubdir), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.Sub(embeddedFrontend, frontendSubdir)
|
|
|
|
}
|