2019-10-01 11:23:58 +08:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package caddycmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-30 04:21:55 +08:00
|
|
|
"context"
|
2019-10-01 11:23:58 +08:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2019-10-01 13:43:39 +08:00
|
|
|
"reflect"
|
2020-01-01 07:56:19 +08:00
|
|
|
"runtime"
|
2019-10-01 13:43:39 +08:00
|
|
|
"runtime/debug"
|
2019-10-09 10:12:15 +08:00
|
|
|
"sort"
|
2019-10-01 11:23:58 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
2020-03-01 01:12:16 +08:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
2019-11-16 06:45:18 +08:00
|
|
|
"go.uber.org/zap"
|
2019-10-01 11:23:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func cmdStart(fl Flags) (int, error) {
|
|
|
|
startCmdConfigFlag := fl.String("config")
|
2019-10-01 13:43:39 +08:00
|
|
|
startCmdConfigAdapterFlag := fl.String("adapter")
|
2020-05-14 01:28:15 +08:00
|
|
|
startCmdPidfileFlag := fl.String("pidfile")
|
2020-03-23 12:58:24 +08:00
|
|
|
startCmdWatchFlag := fl.Bool("watch")
|
2019-10-01 11:23:58 +08:00
|
|
|
|
|
|
|
// open a listener to which the child process will connect when
|
|
|
|
// it is ready to confirm that it has successfully started
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("opening listener for success confirmation: %v", err)
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
// craft the command with a pingback address and with a
|
|
|
|
// pipe for its stdin, so we can tell it our confirmation
|
|
|
|
// code that we expect so that some random port scan at
|
|
|
|
// the most unfortunate time won't fool us into thinking
|
|
|
|
// the child succeeded (i.e. the alternative is to just
|
|
|
|
// wait for any connection on our listener, but better to
|
|
|
|
// ensure it's the process we're expecting - we can be
|
|
|
|
// sure by giving it some random bytes and having it echo
|
|
|
|
// them back to us)
|
|
|
|
cmd := exec.Command(os.Args[0], "run", "--pingback", ln.Addr().String())
|
|
|
|
if startCmdConfigFlag != "" {
|
|
|
|
cmd.Args = append(cmd.Args, "--config", startCmdConfigFlag)
|
|
|
|
}
|
|
|
|
if startCmdConfigAdapterFlag != "" {
|
2019-10-01 13:43:39 +08:00
|
|
|
cmd.Args = append(cmd.Args, "--adapter", startCmdConfigAdapterFlag)
|
2019-10-01 11:23:58 +08:00
|
|
|
}
|
2020-03-23 12:58:24 +08:00
|
|
|
if startCmdWatchFlag {
|
|
|
|
cmd.Args = append(cmd.Args, "--watch")
|
|
|
|
}
|
2020-05-14 01:28:15 +08:00
|
|
|
if startCmdPidfileFlag != "" {
|
|
|
|
cmd.Args = append(cmd.Args, "--pidfile", startCmdPidfileFlag)
|
|
|
|
}
|
2019-10-01 11:23:58 +08:00
|
|
|
stdinpipe, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("creating stdin pipe: %v", err)
|
|
|
|
}
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
// generate the random bytes we'll send to the child process
|
|
|
|
expect := make([]byte, 32)
|
|
|
|
_, err = rand.Read(expect)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("generating random confirmation bytes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// begin writing the confirmation bytes to the child's
|
|
|
|
// stdin; use a goroutine since the child hasn't been
|
2020-02-25 15:16:47 +08:00
|
|
|
// started yet, and writing synchronously would result
|
2019-10-01 11:23:58 +08:00
|
|
|
// in a deadlock
|
|
|
|
go func() {
|
|
|
|
stdinpipe.Write(expect)
|
|
|
|
stdinpipe.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// start the process
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("starting caddy process: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// there are two ways we know we're done: either
|
|
|
|
// the process will connect to our listener, or
|
|
|
|
// it will exit with an error
|
|
|
|
success, exit := make(chan struct{}), make(chan error)
|
|
|
|
|
|
|
|
// in one goroutine, we await the success of the child process
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = handlePingbackConn(conn, expect)
|
|
|
|
if err == nil {
|
|
|
|
close(success)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// in another goroutine, we await the failure of the child process
|
|
|
|
go func() {
|
|
|
|
err := cmd.Wait() // don't send on this line! Wait blocks, but send starts before it unblocks
|
|
|
|
exit <- err // sending on separate line ensures select won't trigger until after Wait unblocks
|
|
|
|
}()
|
|
|
|
|
|
|
|
// when one of the goroutines unblocks, we're done and can exit
|
|
|
|
select {
|
|
|
|
case <-success:
|
2020-03-14 03:02:47 +08:00
|
|
|
fmt.Printf("Successfully started Caddy (pid=%d) - Caddy is running in the background\n", cmd.Process.Pid)
|
2019-10-01 11:23:58 +08:00
|
|
|
case err := <-exit:
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("caddy process exited with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRun(fl Flags) (int, error) {
|
2020-05-22 03:09:49 +08:00
|
|
|
caddy.TrapSignals()
|
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
runCmdConfigFlag := fl.String("config")
|
2019-10-01 13:43:39 +08:00
|
|
|
runCmdConfigAdapterFlag := fl.String("adapter")
|
2020-01-01 07:56:19 +08:00
|
|
|
runCmdResumeFlag := fl.Bool("resume")
|
2020-05-16 05:49:51 +08:00
|
|
|
runCmdLoadEnvfileFlag := fl.String("envfile")
|
2019-10-01 13:43:39 +08:00
|
|
|
runCmdPrintEnvFlag := fl.Bool("environ")
|
2020-03-23 12:58:24 +08:00
|
|
|
runCmdWatchFlag := fl.Bool("watch")
|
2020-05-14 01:28:15 +08:00
|
|
|
runCmdPidfileFlag := fl.String("pidfile")
|
2019-10-01 11:23:58 +08:00
|
|
|
runCmdPingbackFlag := fl.String("pingback")
|
|
|
|
|
2020-05-16 05:49:51 +08:00
|
|
|
// load all additional envs as soon as possible
|
|
|
|
if runCmdLoadEnvfileFlag != "" {
|
|
|
|
if err := loadEnvFromFile(runCmdLoadEnvfileFlag); err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("loading additional environment variables: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
// if we are supposed to print the environment, do that first
|
|
|
|
if runCmdPrintEnvFlag {
|
|
|
|
printEnvironment()
|
|
|
|
}
|
|
|
|
|
2020-01-01 07:47:35 +08:00
|
|
|
// TODO: This is TEMPORARY, until the RCs
|
|
|
|
moveStorage()
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2020-01-01 07:56:19 +08:00
|
|
|
// load the config, depending on flags
|
|
|
|
var config []byte
|
|
|
|
var err error
|
|
|
|
if runCmdResumeFlag {
|
|
|
|
config, err = ioutil.ReadFile(caddy.ConfigAutosavePath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// not a bad error; just can't resume if autosave file doesn't exist
|
|
|
|
caddy.Log().Info("no autosave file exists", zap.String("autosave_file", caddy.ConfigAutosavePath))
|
|
|
|
runCmdResumeFlag = false
|
|
|
|
} else if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
} else {
|
2020-04-05 03:29:25 +08:00
|
|
|
if runCmdConfigFlag == "" {
|
|
|
|
caddy.Log().Info("resuming from last configuration",
|
|
|
|
zap.String("autosave_file", caddy.ConfigAutosavePath))
|
|
|
|
} else {
|
|
|
|
// if they also specified a config file, user should be aware that we're not
|
|
|
|
// using it (doing so could lead to data/config loss by overwriting!)
|
|
|
|
caddy.Log().Warn("--config and --resume flags were used together; ignoring --config and resuming from last configuration",
|
|
|
|
zap.String("autosave_file", caddy.ConfigAutosavePath))
|
|
|
|
}
|
2020-01-01 07:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// we don't use 'else' here since this value might have been changed in 'if' block; i.e. not mutually exclusive
|
2020-03-23 12:58:24 +08:00
|
|
|
var configFile string
|
2020-01-01 07:56:19 +08:00
|
|
|
if !runCmdResumeFlag {
|
2020-03-23 12:58:24 +08:00
|
|
|
config, configFile, err = loadConfig(runCmdConfigFlag, runCmdConfigAdapterFlag)
|
2020-01-01 07:56:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 03:05:20 +08:00
|
|
|
// run the initial config
|
|
|
|
err = caddy.Load(config, true)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("loading initial config: %v", err)
|
2019-10-01 11:23:58 +08:00
|
|
|
}
|
2020-01-01 07:56:19 +08:00
|
|
|
caddy.Log().Info("serving initial configuration")
|
2019-10-01 11:23:58 +08:00
|
|
|
|
|
|
|
// if we are to report to another process the successful start
|
|
|
|
// of the server, do so now by echoing back contents of stdin
|
|
|
|
if runCmdPingbackFlag != "" {
|
|
|
|
confirmationBytes, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("reading confirmation bytes from stdin: %v", err)
|
|
|
|
}
|
|
|
|
conn, err := net.Dial("tcp", runCmdPingbackFlag)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("dialing confirmation address: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
_, err = conn.Write(confirmationBytes)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("writing confirmation bytes to %s: %v", runCmdPingbackFlag, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 12:58:24 +08:00
|
|
|
// if enabled, reload config file automatically on changes
|
|
|
|
// (this better only be used in dev!)
|
|
|
|
if runCmdWatchFlag {
|
|
|
|
go watchConfigFile(configFile, runCmdConfigAdapterFlag)
|
|
|
|
}
|
|
|
|
|
2020-05-14 01:28:15 +08:00
|
|
|
// create pidfile
|
|
|
|
if runCmdPidfileFlag != "" {
|
|
|
|
err := caddy.PIDFile(runCmdPidfileFlag)
|
|
|
|
if err != nil {
|
|
|
|
caddy.Log().Error("unable to write PID file",
|
|
|
|
zap.String("pidfile", runCmdPidfileFlag),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 07:47:35 +08:00
|
|
|
// warn if the environment does not provide enough information about the disk
|
|
|
|
hasXDG := os.Getenv("XDG_DATA_HOME") != "" &&
|
|
|
|
os.Getenv("XDG_CONFIG_HOME") != "" &&
|
|
|
|
os.Getenv("XDG_CACHE_HOME") != ""
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
if os.Getenv("HOME") == "" && os.Getenv("USERPROFILE") == "" && !hasXDG {
|
|
|
|
caddy.Log().Warn("neither HOME nor USERPROFILE environment variables are set - please fix; some assets might be stored in ./caddy")
|
|
|
|
}
|
|
|
|
case "plan9":
|
|
|
|
if os.Getenv("home") == "" && !hasXDG {
|
|
|
|
caddy.Log().Warn("$home environment variable is empty - please fix; some assets might be stored in ./caddy")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if os.Getenv("HOME") == "" && !hasXDG {
|
|
|
|
caddy.Log().Warn("$HOME environment variable is empty - please fix; some assets might be stored in ./caddy")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
2019-11-16 06:45:18 +08:00
|
|
|
func cmdStop(fl Flags) (int, error) {
|
|
|
|
stopCmdAddrFlag := fl.String("address")
|
|
|
|
|
2020-05-30 04:21:55 +08:00
|
|
|
err := apiRequest(stopCmdAddrFlag, http.MethodPost, "/stop", nil)
|
2019-10-01 11:23:58 +08:00
|
|
|
if err != nil {
|
2020-05-30 04:21:55 +08:00
|
|
|
caddy.Log().Warn("failed using API to stop instance", zap.Error(err))
|
2019-12-24 03:41:05 +08:00
|
|
|
return caddy.ExitCodeFailedStartup, err
|
2019-10-01 11:23:58 +08:00
|
|
|
}
|
2019-11-16 06:45:18 +08:00
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdReload(fl Flags) (int, error) {
|
|
|
|
reloadCmdConfigFlag := fl.String("config")
|
2019-10-01 13:43:39 +08:00
|
|
|
reloadCmdConfigAdapterFlag := fl.String("adapter")
|
2019-10-01 11:23:58 +08:00
|
|
|
reloadCmdAddrFlag := fl.String("address")
|
|
|
|
|
|
|
|
// get the config in caddy's native format
|
2020-03-23 12:58:24 +08:00
|
|
|
config, configFile, err := loadConfig(reloadCmdConfigFlag, reloadCmdConfigAdapterFlag)
|
2019-10-01 11:23:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
2020-03-23 12:58:24 +08:00
|
|
|
if configFile == "" {
|
2020-01-23 01:04:58 +08:00
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("no config file to load")
|
|
|
|
}
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2020-05-30 04:21:55 +08:00
|
|
|
// get the address of the admin listener; use flag if specified
|
2019-10-01 11:23:58 +08:00
|
|
|
adminAddr := reloadCmdAddrFlag
|
2020-01-23 00:33:22 +08:00
|
|
|
if adminAddr == "" && len(config) > 0 {
|
2019-10-01 11:23:58 +08:00
|
|
|
var tmpStruct struct {
|
|
|
|
Admin caddy.AdminConfig `json:"admin"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(config, &tmpStruct)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("unmarshaling admin listener address from config: %v", err)
|
|
|
|
}
|
|
|
|
adminAddr = tmpStruct.Admin.Listen
|
|
|
|
}
|
|
|
|
|
2020-05-30 04:21:55 +08:00
|
|
|
err = apiRequest(adminAddr, http.MethodPost, "/load", bytes.NewReader(config))
|
2019-11-16 06:45:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("sending configuration to instance: %v", err)
|
2019-10-01 11:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdVersion(_ Flags) (int, error) {
|
|
|
|
goModule := caddy.GoModule()
|
2020-01-18 00:50:23 +08:00
|
|
|
fmt.Print(goModule.Version)
|
2019-10-01 11:23:58 +08:00
|
|
|
if goModule.Sum != "" {
|
|
|
|
// a build with a known version will also have a checksum
|
2020-01-18 00:50:23 +08:00
|
|
|
fmt.Printf(" %s", goModule.Sum)
|
2019-10-01 11:23:58 +08:00
|
|
|
}
|
2020-01-18 00:50:23 +08:00
|
|
|
if goModule.Replace != nil {
|
|
|
|
fmt.Printf(" => %s", goModule.Replace.Path)
|
|
|
|
if goModule.Replace.Version != "" {
|
|
|
|
fmt.Printf(" %s", goModule.Replace.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println()
|
2019-10-01 11:23:58 +08:00
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
2020-01-11 02:53:07 +08:00
|
|
|
func cmdBuildInfo(fl Flags) (int, error) {
|
|
|
|
bi, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("no build information")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("path: %s\n", bi.Path)
|
|
|
|
fmt.Printf("main: %s %s %s\n", bi.Main.Path, bi.Main.Version, bi.Main.Sum)
|
|
|
|
fmt.Println("dependencies:")
|
|
|
|
|
|
|
|
for _, goMod := range bi.Deps {
|
|
|
|
fmt.Printf("%s %s %s", goMod.Path, goMod.Version, goMod.Sum)
|
|
|
|
if goMod.Replace != nil {
|
|
|
|
fmt.Printf(" => %s %s %s", goMod.Replace.Path, goMod.Replace.Version, goMod.Replace.Sum)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
2019-10-01 13:43:39 +08:00
|
|
|
func cmdListModules(fl Flags) (int, error) {
|
|
|
|
versions := fl.Bool("versions")
|
|
|
|
|
|
|
|
bi, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok || !versions {
|
|
|
|
// if there's no build information,
|
|
|
|
// just print out the modules
|
|
|
|
for _, m := range caddy.Modules() {
|
|
|
|
fmt.Println(m)
|
|
|
|
}
|
|
|
|
return caddy.ExitCodeSuccess, nil
|
2019-10-01 11:23:58 +08:00
|
|
|
}
|
2019-10-01 13:43:39 +08:00
|
|
|
|
2019-12-11 04:36:46 +08:00
|
|
|
for _, modID := range caddy.Modules() {
|
|
|
|
modInfo, err := caddy.GetModule(modID)
|
2019-10-01 13:43:39 +08:00
|
|
|
if err != nil {
|
|
|
|
// that's weird
|
2019-12-11 04:36:46 +08:00
|
|
|
fmt.Println(modID)
|
2019-10-01 13:43:39 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// to get the Caddy plugin's version info, we need to know
|
|
|
|
// the package that the Caddy module's value comes from; we
|
|
|
|
// can use reflection but we need a non-pointer value (I'm
|
|
|
|
// not sure why), and since New() should return a pointer
|
|
|
|
// value, we need to dereference it first
|
|
|
|
iface := interface{}(modInfo.New())
|
|
|
|
if rv := reflect.ValueOf(iface); rv.Kind() == reflect.Ptr {
|
|
|
|
iface = reflect.New(reflect.TypeOf(iface).Elem()).Elem().Interface()
|
|
|
|
}
|
|
|
|
modPkgPath := reflect.TypeOf(iface).PkgPath()
|
|
|
|
|
|
|
|
// now we find the Go module that the Caddy module's package
|
|
|
|
// belongs to; we assume the Caddy module package path will
|
|
|
|
// be prefixed by its Go module path, and we will choose the
|
|
|
|
// longest matching prefix in case there are nested modules
|
|
|
|
var matched *debug.Module
|
|
|
|
for _, dep := range bi.Deps {
|
|
|
|
if strings.HasPrefix(modPkgPath, dep.Path) {
|
|
|
|
if matched == nil || len(dep.Path) > len(matched.Path) {
|
|
|
|
matched = dep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we could find no matching module, just print out
|
2019-12-11 04:36:46 +08:00
|
|
|
// the module ID instead
|
2019-10-01 13:43:39 +08:00
|
|
|
if matched == nil {
|
2019-12-11 04:36:46 +08:00
|
|
|
fmt.Println(modID)
|
2019-10-01 13:43:39 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-11 04:36:46 +08:00
|
|
|
fmt.Printf("%s %s\n", modID, matched.Version)
|
2019-10-01 13:43:39 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdEnviron(_ Flags) (int, error) {
|
|
|
|
printEnvironment()
|
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdAdaptConfig(fl Flags) (int, error) {
|
2019-10-01 13:43:39 +08:00
|
|
|
adaptCmdInputFlag := fl.String("config")
|
2019-10-01 11:23:58 +08:00
|
|
|
adaptCmdAdapterFlag := fl.String("adapter")
|
|
|
|
adaptCmdPrettyFlag := fl.Bool("pretty")
|
2019-10-02 01:02:13 +08:00
|
|
|
adaptCmdValidateFlag := fl.Bool("validate")
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2020-02-05 01:48:02 +08:00
|
|
|
// if no input file was specified, try a default
|
|
|
|
// Caddyfile if the Caddyfile adapter is plugged in
|
|
|
|
if adaptCmdInputFlag == "" && caddyconfig.GetAdapter("caddyfile") != nil {
|
|
|
|
_, err := os.Stat("Caddyfile")
|
|
|
|
if err == nil {
|
|
|
|
// default Caddyfile exists
|
|
|
|
adaptCmdInputFlag = "Caddyfile"
|
|
|
|
caddy.Log().Info("using adjacent Caddyfile")
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
// default Caddyfile exists, but error accessing it
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("accessing default Caddyfile: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if adaptCmdInputFlag == "" {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("input file required when there is no Caddyfile in current directory (use --config flag)")
|
|
|
|
}
|
|
|
|
if adaptCmdAdapterFlag == "" {
|
2019-10-01 11:23:58 +08:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
2020-02-05 01:48:02 +08:00
|
|
|
fmt.Errorf("adapter name is required (use --adapt flag or leave unspecified for default)")
|
2019-10-01 11:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cfgAdapter := caddyconfig.GetAdapter(adaptCmdAdapterFlag)
|
|
|
|
if cfgAdapter == nil {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("unrecognized config adapter: %s", adaptCmdAdapterFlag)
|
|
|
|
}
|
|
|
|
|
|
|
|
input, err := ioutil.ReadFile(adaptCmdInputFlag)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("reading input file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := make(map[string]interface{})
|
|
|
|
if adaptCmdPrettyFlag {
|
|
|
|
opts["pretty"] = "true"
|
|
|
|
}
|
2020-01-13 09:20:19 +08:00
|
|
|
opts["filename"] = adaptCmdInputFlag
|
2019-10-01 11:23:58 +08:00
|
|
|
|
|
|
|
adaptedConfig, warnings, err := cfgAdapter.Adapt(input, opts)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// print warnings to stderr
|
|
|
|
for _, warn := range warnings {
|
|
|
|
msg := warn.Message
|
|
|
|
if warn.Directive != "" {
|
|
|
|
msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message)
|
|
|
|
}
|
2019-10-29 04:39:37 +08:00
|
|
|
fmt.Fprintf(os.Stderr, "[WARNING][%s] %s:%d: %s\n", adaptCmdAdapterFlag, warn.File, warn.Line, msg)
|
2019-10-01 11:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// print result to stdout
|
|
|
|
fmt.Println(string(adaptedConfig))
|
|
|
|
|
2019-10-02 01:02:13 +08:00
|
|
|
// validate output if requested
|
|
|
|
if adaptCmdValidateFlag {
|
|
|
|
var cfg *caddy.Config
|
|
|
|
err = json.Unmarshal(adaptedConfig, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("decoding config: %v", err)
|
|
|
|
}
|
|
|
|
err = caddy.Validate(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("validation: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
2019-10-01 13:43:39 +08:00
|
|
|
func cmdValidateConfig(fl Flags) (int, error) {
|
|
|
|
validateCmdConfigFlag := fl.String("config")
|
|
|
|
validateCmdAdapterFlag := fl.String("adapter")
|
|
|
|
|
2020-03-09 14:09:15 +08:00
|
|
|
input, _, err := loadConfig(validateCmdConfigFlag, validateCmdAdapterFlag)
|
2019-10-01 13:43:39 +08:00
|
|
|
if err != nil {
|
2020-03-09 14:09:15 +08:00
|
|
|
return caddy.ExitCodeFailedStartup, err
|
2019-10-01 13:43:39 +08:00
|
|
|
}
|
2019-12-13 05:30:22 +08:00
|
|
|
input = caddy.RemoveMetaFields(input)
|
|
|
|
|
2019-10-01 13:43:39 +08:00
|
|
|
var cfg *caddy.Config
|
|
|
|
err = json.Unmarshal(input, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("decoding config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = caddy.Validate(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Valid configuration")
|
|
|
|
|
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
2020-03-16 11:18:31 +08:00
|
|
|
func cmdFmt(fl Flags) (int, error) {
|
2020-03-01 01:12:16 +08:00
|
|
|
formatCmdConfigFile := fl.Arg(0)
|
|
|
|
if formatCmdConfigFile == "" {
|
|
|
|
formatCmdConfigFile = "Caddyfile"
|
|
|
|
}
|
2020-03-16 11:18:31 +08:00
|
|
|
overwrite := fl.Bool("overwrite")
|
2020-03-01 01:12:16 +08:00
|
|
|
|
|
|
|
input, err := ioutil.ReadFile(formatCmdConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("reading input file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := caddyfile.Format(input)
|
|
|
|
|
2020-03-16 11:18:31 +08:00
|
|
|
if overwrite {
|
2020-03-01 01:12:16 +08:00
|
|
|
err = ioutil.WriteFile(formatCmdConfigFile, output, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Print(string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
func cmdHelp(fl Flags) (int, error) {
|
|
|
|
const fullDocs = `Full documentation is available at:
|
2020-01-01 13:51:55 +08:00
|
|
|
https://caddyserver.com/docs/command-line`
|
2019-10-01 11:23:58 +08:00
|
|
|
|
|
|
|
args := fl.Args()
|
|
|
|
if len(args) == 0 {
|
|
|
|
s := `Caddy is an extensible server platform.
|
|
|
|
|
|
|
|
usage:
|
|
|
|
caddy <command> [<args...>]
|
|
|
|
|
|
|
|
commands:
|
|
|
|
`
|
2019-10-09 10:12:15 +08:00
|
|
|
keys := make([]string, 0, len(commands))
|
|
|
|
for k := range commands {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
|
|
cmd := commands[k]
|
2019-10-02 01:02:13 +08:00
|
|
|
short := strings.TrimSuffix(cmd.Short, ".")
|
|
|
|
s += fmt.Sprintf(" %-15s %s\n", cmd.Name, short)
|
2019-10-01 11:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
s += "\nUse 'caddy help <command>' for more information about a command.\n"
|
|
|
|
s += "\n" + fullDocs + "\n"
|
|
|
|
|
|
|
|
fmt.Print(s)
|
|
|
|
|
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
} else if len(args) > 1 {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("can only give help with one command")
|
|
|
|
}
|
|
|
|
|
|
|
|
subcommand, ok := commands[args[0]]
|
|
|
|
if !ok {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("unknown command: %s", args[0])
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:02:13 +08:00
|
|
|
helpText := strings.TrimSpace(subcommand.Long)
|
|
|
|
if helpText == "" {
|
|
|
|
helpText = subcommand.Short
|
|
|
|
if !strings.HasSuffix(helpText, ".") {
|
|
|
|
helpText += "."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
result := fmt.Sprintf("%s\n\nusage:\n caddy %s %s\n",
|
2019-10-02 01:02:13 +08:00
|
|
|
helpText,
|
2019-10-01 11:23:58 +08:00
|
|
|
subcommand.Name,
|
|
|
|
strings.TrimSpace(subcommand.Usage),
|
|
|
|
)
|
|
|
|
|
|
|
|
if help := flagHelp(subcommand.Flags); help != "" {
|
|
|
|
result += fmt.Sprintf("\nflags:\n%s", help)
|
|
|
|
}
|
|
|
|
|
|
|
|
result += "\n" + fullDocs + "\n"
|
|
|
|
|
|
|
|
fmt.Print(result)
|
|
|
|
|
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|
2019-11-16 06:45:18 +08:00
|
|
|
|
2020-05-30 04:21:55 +08:00
|
|
|
// apiRequest makes an API request to the endpoint adminAddr with the
|
|
|
|
// given HTTP method and request URI. If body is non-nil, it will be
|
|
|
|
// assumed to be Content-Type application/json.
|
|
|
|
func apiRequest(adminAddr, method, uri string, body io.Reader) error {
|
|
|
|
// parse the admin address
|
|
|
|
if adminAddr == "" {
|
|
|
|
adminAddr = caddy.DefaultAdminListen
|
|
|
|
}
|
|
|
|
parsedAddr, err := caddy.ParseNetworkAddress(adminAddr)
|
|
|
|
if err != nil || parsedAddr.PortRangeSize() > 1 {
|
|
|
|
return fmt.Errorf("invalid admin address %s: %v", adminAddr, err)
|
|
|
|
}
|
|
|
|
origin := parsedAddr.JoinHostPort(0)
|
|
|
|
if parsedAddr.IsUnixNetwork() {
|
|
|
|
origin = "unixsocket" // hack so that http.NewRequest() is happy
|
|
|
|
}
|
|
|
|
|
|
|
|
// form the request
|
|
|
|
req, err := http.NewRequest(method, "http://"+origin+uri, body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("making request: %v", err)
|
|
|
|
}
|
|
|
|
if parsedAddr.IsUnixNetwork() {
|
|
|
|
// When listening on a unix socket, the admin endpoint doesn't
|
|
|
|
// accept any Host header because there is no host:port for
|
|
|
|
// a unix socket's address. The server's host check is fairly
|
|
|
|
// strict for security reasons, so we don't allow just any
|
|
|
|
// Host header. For unix sockets, the Host header must be
|
|
|
|
// empty. Unfortunately, Go makes it impossible to make HTTP
|
|
|
|
// requests with an empty Host header... except with this one
|
|
|
|
// weird trick. (Hopefully they don't fix it. It's already
|
|
|
|
// hard enough to use HTTP over unix sockets.)
|
|
|
|
//
|
|
|
|
// An equivalent curl command would be something like:
|
|
|
|
// $ curl --unix-socket caddy.sock http:/:$REQUEST_URI
|
|
|
|
req.URL.Host = " "
|
|
|
|
req.Host = ""
|
|
|
|
} else {
|
|
|
|
req.Header.Set("Origin", origin)
|
|
|
|
}
|
|
|
|
if body != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
|
|
|
// make an HTTP client that dials our network type, since admin
|
|
|
|
// endpoints aren't always TCP, which is what the default transport
|
|
|
|
// expects; reuse is not of particular concern here
|
|
|
|
client := http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
|
|
|
|
return net.Dial(parsedAddr.Network, parsedAddr.JoinHostPort(0))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
2019-11-16 06:45:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("performing request: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// if it didn't work, let the user know
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1024*10))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("HTTP %d: reading error message: %v", resp.StatusCode, err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("caddy responded with error: HTTP %d: %s", resp.StatusCode, respBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|