2020-09-07 11:10:56 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-20 17:27:53 +03:00
|
|
|
"net"
|
2020-09-07 11:10:56 +03:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2021-01-13 16:18:51 +03:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/version"
|
2022-04-06 19:56:39 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-09-07 11:10:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// options passed from command-line arguments
|
|
|
|
type options struct {
|
|
|
|
verbose bool // is verbose logging enabled
|
|
|
|
configFilename string // path to the config file
|
|
|
|
workDir string // path to the working directory where we will store the filters data and the querylog
|
2021-01-20 17:27:53 +03:00
|
|
|
bindHost net.IP // host address to bind HTTP server on
|
2020-09-07 11:10:56 +03:00
|
|
|
bindPort int // port to serve HTTP pages on
|
|
|
|
logFile string // Path to the log file. If empty, write to stdout. If "syslog", writes to syslog
|
|
|
|
pidFile string // File name to save PID to
|
|
|
|
checkConfig bool // Check configuration and exit
|
|
|
|
disableUpdate bool // If set, don't check for updates
|
|
|
|
|
|
|
|
// service control action (see service.ControlAction array + "status" command)
|
|
|
|
serviceControlAction string
|
|
|
|
|
|
|
|
// runningAsService flag is set to true when options are passed from the service runner
|
|
|
|
runningAsService bool
|
|
|
|
|
2020-09-11 13:19:37 +03:00
|
|
|
glinetMode bool // Activate GL-Inet compatibility mode
|
2021-04-12 18:31:45 +03:00
|
|
|
|
|
|
|
// noEtcHosts flag should be provided when /etc/hosts file shouldn't be
|
|
|
|
// used.
|
|
|
|
noEtcHosts bool
|
2021-05-21 14:55:42 +03:00
|
|
|
|
|
|
|
// localFrontend forces AdGuard Home to use the frontend files from disk
|
|
|
|
// rather than the ones that have been compiled into the binary.
|
|
|
|
localFrontend bool
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// functions used for their side-effects
|
|
|
|
type effect func() error
|
|
|
|
|
|
|
|
type arg struct {
|
|
|
|
description string // a short, English description of the argument
|
|
|
|
longName string // the name of the argument used after '--'
|
|
|
|
shortName string // the name of the argument used after '-'
|
|
|
|
|
|
|
|
// only one of updateWithValue, updateNoValue, and effect should be present
|
|
|
|
|
|
|
|
updateWithValue func(o options, v string) (options, error) // the mutator for arguments with parameters
|
|
|
|
updateNoValue func(o options) (options, error) // the mutator for arguments without parameters
|
|
|
|
effect func(o options, exec string) (f effect, err error) // the side-effect closure generator
|
2020-09-07 11:53:16 +03:00
|
|
|
|
|
|
|
serialize func(o options) []string // the re-serialization function back to arguments (return nil for omit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// {type}SliceOrNil functions check their parameter of type {type}
|
|
|
|
// against its zero value and return nil if the parameter value is
|
|
|
|
// zero otherwise they return a string slice of the parameter
|
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
func ipSliceOrNil(ip net.IP) []string {
|
|
|
|
if ip == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{ip.String()}
|
|
|
|
}
|
|
|
|
|
2020-09-07 11:53:16 +03:00
|
|
|
func stringSliceOrNil(s string) []string {
|
|
|
|
if s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-20 17:27:53 +03:00
|
|
|
|
2020-09-07 11:53:16 +03:00
|
|
|
return []string{s}
|
|
|
|
}
|
|
|
|
|
|
|
|
func intSliceOrNil(i int) []string {
|
|
|
|
if i == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-20 17:27:53 +03:00
|
|
|
|
2020-09-07 11:53:16 +03:00
|
|
|
return []string{strconv.Itoa(i)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func boolSliceOrNil(b bool) []string {
|
|
|
|
if b {
|
|
|
|
return []string{}
|
|
|
|
}
|
2021-01-20 17:27:53 +03:00
|
|
|
|
2020-09-07 11:53:16 +03:00
|
|
|
return nil
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var args []arg
|
|
|
|
|
|
|
|
var configArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Path to the config file.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"config", "c",
|
|
|
|
func(o options, v string) (options, error) { o.configFilename = v; return o, nil },
|
|
|
|
nil,
|
|
|
|
nil,
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return stringSliceOrNil(o.configFilename) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var workDirArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Path to the working directory.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"work-dir", "w",
|
|
|
|
func(o options, v string) (options, error) { o.workDir = v; return o, nil }, nil, nil,
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return stringSliceOrNil(o.workDir) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var hostArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Host address to bind HTTP server on.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"host", "h",
|
2021-01-20 17:27:53 +03:00
|
|
|
func(o options, v string) (options, error) { o.bindHost = net.ParseIP(v); return o, nil }, nil, nil,
|
|
|
|
func(o options) []string { return ipSliceOrNil(o.bindHost) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var portArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Port to serve HTTP pages on.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"port", "p",
|
|
|
|
func(o options, v string) (options, error) {
|
|
|
|
var err error
|
|
|
|
var p int
|
|
|
|
minPort, maxPort := 0, 1<<16-1
|
|
|
|
if p, err = strconv.Atoi(v); err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
err = fmt.Errorf("port %q is not a number", v)
|
2020-09-07 11:10:56 +03:00
|
|
|
} else if p < minPort || p > maxPort {
|
|
|
|
err = fmt.Errorf("port %d not in range %d - %d", p, minPort, maxPort)
|
|
|
|
} else {
|
|
|
|
o.bindPort = p
|
|
|
|
}
|
|
|
|
return o, err
|
|
|
|
}, nil, nil,
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return intSliceOrNil(o.bindPort) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var serviceArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Service control action: status, install, uninstall, start, stop, restart, reload (configuration).",
|
2020-09-07 11:10:56 +03:00
|
|
|
"service", "s",
|
|
|
|
func(o options, v string) (options, error) {
|
|
|
|
o.serviceControlAction = v
|
|
|
|
return o, nil
|
|
|
|
}, nil, nil,
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return stringSliceOrNil(o.serviceControlAction) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var logfileArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Path to log file. If empty: write to stdout; if 'syslog': write to system log.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"logfile", "l",
|
|
|
|
func(o options, v string) (options, error) { o.logFile = v; return o, nil }, nil, nil,
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return stringSliceOrNil(o.logFile) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var pidfileArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Path to a file where PID is stored.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"pidfile", "",
|
|
|
|
func(o options, v string) (options, error) { o.pidFile = v; return o, nil }, nil, nil,
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return stringSliceOrNil(o.pidFile) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var checkConfigArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Check configuration and exit.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"check-config", "",
|
|
|
|
nil, func(o options) (options, error) { o.checkConfig = true; return o, nil }, nil,
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return boolSliceOrNil(o.checkConfig) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var noCheckUpdateArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Don't check for updates.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"no-check-update", "",
|
|
|
|
nil, func(o options) (options, error) { o.disableUpdate = true; return o, nil }, nil,
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return boolSliceOrNil(o.disableUpdate) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
2020-09-11 13:19:37 +03:00
|
|
|
var disableMemoryOptimizationArg = arg{
|
2022-04-07 14:07:27 +03:00
|
|
|
"Deprecated. Disable memory optimization.",
|
2020-09-11 13:19:37 +03:00
|
|
|
"no-mem-optimization", "",
|
2022-04-06 19:56:39 +03:00
|
|
|
nil, nil, func(_ options, _ string) (f effect, err error) {
|
|
|
|
log.Info("warning: using --no-mem-optimization flag has no effect and is deprecated")
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
func(o options) []string { return nil },
|
2020-09-11 13:19:37 +03:00
|
|
|
}
|
|
|
|
|
2020-09-07 11:10:56 +03:00
|
|
|
var verboseArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Enable verbose output.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"verbose", "v",
|
|
|
|
nil, func(o options) (options, error) { o.verbose = true; return o, nil }, nil,
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return boolSliceOrNil(o.verbose) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var glinetArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Run in GL-Inet compatibility mode.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"glinet", "",
|
|
|
|
nil, func(o options) (options, error) { o.glinetMode = true; return o, nil }, nil,
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return boolSliceOrNil(o.glinetMode) },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var versionArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
description: "Show the version and exit. Show more detailed version description with -v.",
|
2021-03-31 12:55:21 +03:00
|
|
|
longName: "version",
|
|
|
|
shortName: "",
|
|
|
|
updateWithValue: nil,
|
|
|
|
updateNoValue: nil,
|
|
|
|
effect: func(o options, exec string) (effect, error) {
|
|
|
|
return func() error {
|
|
|
|
if o.verbose {
|
|
|
|
fmt.Println(version.Verbose())
|
|
|
|
} else {
|
|
|
|
fmt.Println(version.Full())
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, nil
|
2020-09-07 11:10:56 +03:00
|
|
|
},
|
2021-03-31 12:55:21 +03:00
|
|
|
serialize: func(o options) []string { return nil },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var helpArg = arg{
|
2021-04-13 15:13:28 +03:00
|
|
|
"Print this help.",
|
2020-09-07 11:10:56 +03:00
|
|
|
"help", "",
|
|
|
|
nil, nil, func(o options, exec string) (effect, error) {
|
|
|
|
return func() error { _ = printHelp(exec); os.Exit(64); return nil }, nil
|
|
|
|
},
|
2020-09-07 11:53:16 +03:00
|
|
|
func(o options) []string { return nil },
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
var noEtcHostsArg = arg{
|
2022-06-02 17:55:48 +03:00
|
|
|
description: "Deprecated. Do not use the OS-provided hosts.",
|
2021-04-12 18:31:45 +03:00
|
|
|
longName: "no-etc-hosts",
|
|
|
|
shortName: "",
|
|
|
|
updateWithValue: nil,
|
|
|
|
updateNoValue: func(o options) (options, error) { o.noEtcHosts = true; return o, nil },
|
2022-06-02 17:55:48 +03:00
|
|
|
effect: func(_ options, _ string) (f effect, err error) {
|
|
|
|
log.Info(
|
|
|
|
"warning: --no-etc-hosts flag is deprecated and will be removed in the future versions",
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
serialize: func(o options) []string { return boolSliceOrNil(o.noEtcHosts) },
|
2021-04-12 18:31:45 +03:00
|
|
|
}
|
|
|
|
|
2021-05-21 14:55:42 +03:00
|
|
|
var localFrontendArg = arg{
|
|
|
|
description: "Use local frontend directories.",
|
|
|
|
longName: "local-frontend",
|
|
|
|
shortName: "",
|
|
|
|
updateWithValue: nil,
|
|
|
|
updateNoValue: func(o options) (options, error) { o.localFrontend = true; return o, nil },
|
|
|
|
effect: nil,
|
|
|
|
serialize: func(o options) []string { return boolSliceOrNil(o.localFrontend) },
|
|
|
|
}
|
|
|
|
|
2020-09-07 11:10:56 +03:00
|
|
|
func init() {
|
|
|
|
args = []arg{
|
|
|
|
configArg,
|
|
|
|
workDirArg,
|
|
|
|
hostArg,
|
|
|
|
portArg,
|
|
|
|
serviceArg,
|
|
|
|
logfileArg,
|
|
|
|
pidfileArg,
|
|
|
|
checkConfigArg,
|
|
|
|
noCheckUpdateArg,
|
2020-09-11 13:19:37 +03:00
|
|
|
disableMemoryOptimizationArg,
|
2021-04-12 18:31:45 +03:00
|
|
|
noEtcHostsArg,
|
2021-05-21 14:55:42 +03:00
|
|
|
localFrontendArg,
|
2020-09-07 11:10:56 +03:00
|
|
|
verboseArg,
|
|
|
|
glinetArg,
|
|
|
|
versionArg,
|
|
|
|
helpArg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUsageLines(exec string, args []arg) []string {
|
|
|
|
usage := []string{
|
|
|
|
"Usage:",
|
|
|
|
"",
|
|
|
|
fmt.Sprintf("%s [options]", exec),
|
|
|
|
"",
|
|
|
|
"Options:",
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
val := ""
|
|
|
|
if arg.updateWithValue != nil {
|
|
|
|
val = " VALUE"
|
|
|
|
}
|
|
|
|
if arg.shortName != "" {
|
|
|
|
usage = append(usage, fmt.Sprintf(" -%s, %-30s %s",
|
|
|
|
arg.shortName,
|
|
|
|
"--"+arg.longName+val,
|
|
|
|
arg.description))
|
|
|
|
} else {
|
|
|
|
usage = append(usage, fmt.Sprintf(" %-34s %s",
|
|
|
|
"--"+arg.longName+val,
|
|
|
|
arg.description))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return usage
|
|
|
|
}
|
|
|
|
|
|
|
|
func printHelp(exec string) error {
|
|
|
|
for _, line := range getUsageLines(exec, args) {
|
|
|
|
_, err := fmt.Println(line)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func argMatches(a arg, v string) bool {
|
|
|
|
return v == "--"+a.longName || (a.shortName != "" && v == "-"+a.shortName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parse(exec string, ss []string) (o options, f effect, err error) {
|
|
|
|
for i := 0; i < len(ss); i++ {
|
|
|
|
v := ss[i]
|
|
|
|
knownParam := false
|
|
|
|
for _, arg := range args {
|
|
|
|
if argMatches(arg, v) {
|
|
|
|
if arg.updateWithValue != nil {
|
|
|
|
if i+1 >= len(ss) {
|
|
|
|
return o, f, fmt.Errorf("got %s without argument", v)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
o, err = arg.updateWithValue(o, ss[i])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if arg.updateNoValue != nil {
|
|
|
|
o, err = arg.updateNoValue(o)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if arg.effect != nil {
|
|
|
|
var eff effect
|
|
|
|
eff, err = arg.effect(o, exec)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if eff != nil {
|
|
|
|
prevf := f
|
2021-03-12 14:32:08 +03:00
|
|
|
f = func() (ferr error) {
|
2020-09-07 11:10:56 +03:00
|
|
|
if prevf != nil {
|
2021-03-12 14:32:08 +03:00
|
|
|
ferr = prevf()
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
2021-03-12 14:32:08 +03:00
|
|
|
if ferr == nil {
|
|
|
|
ferr = eff()
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
2021-03-12 14:32:08 +03:00
|
|
|
return ferr
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
knownParam = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !knownParam {
|
|
|
|
return o, f, fmt.Errorf("unknown option %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2020-09-07 11:53:16 +03:00
|
|
|
|
|
|
|
func shortestFlag(a arg) string {
|
|
|
|
if a.shortName != "" {
|
|
|
|
return "-" + a.shortName
|
|
|
|
}
|
|
|
|
return "--" + a.longName
|
|
|
|
}
|
|
|
|
|
|
|
|
func serialize(o options) []string {
|
|
|
|
ss := []string{}
|
|
|
|
for _, arg := range args {
|
|
|
|
s := arg.serialize(o)
|
|
|
|
if s != nil {
|
|
|
|
ss = append(ss, append([]string{shortestFlag(arg)}, s...)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ss
|
|
|
|
}
|