2019-07-01 01:07:58 +03: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.
|
|
|
|
|
2019-06-29 00:39:41 +03:00
|
|
|
package caddycmd
|
|
|
|
|
|
|
|
import (
|
2019-07-05 18:59:13 +03:00
|
|
|
"bytes"
|
2019-06-29 00:39:41 +03:00
|
|
|
"crypto/rand"
|
2019-07-05 18:59:13 +03:00
|
|
|
"encoding/json"
|
2019-06-29 00:39:41 +03:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2019-07-05 18:59:13 +03:00
|
|
|
"io"
|
2019-06-29 00:39:41 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
2019-07-05 18:59:13 +03:00
|
|
|
"net/http"
|
2019-06-29 00:39:41 +03:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2019-07-20 19:44:54 +03:00
|
|
|
|
2019-06-29 04:28:28 +03:00
|
|
|
"strings"
|
2019-06-29 00:39:41 +03:00
|
|
|
|
2019-07-02 21:37:06 +03:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2019-08-09 21:05:47 +03:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
2019-09-14 08:40:29 +03:00
|
|
|
"github.com/keybase/go-ps"
|
2019-06-29 04:28:28 +03:00
|
|
|
"github.com/mholt/certmagic"
|
2019-06-29 00:39:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func cmdStart() (int, error) {
|
|
|
|
startCmd := flag.NewFlagSet("start", flag.ExitOnError)
|
|
|
|
startCmdConfigFlag := startCmd.String("config", "", "Configuration file")
|
2019-08-09 21:05:47 +03:00
|
|
|
startCmdConfigAdapterFlag := startCmd.String("config-adapter", "", "Name of config adapter to apply")
|
2019-06-29 00:39:41 +03:00
|
|
|
startCmd.Parse(os.Args[2:])
|
|
|
|
|
|
|
|
// 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 {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("opening listener for success confirmation: %v", err)
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2019-08-09 21:05:47 +03:00
|
|
|
if *startCmdConfigAdapterFlag != "" {
|
|
|
|
cmd.Args = append(cmd.Args, "--config-adapter", *startCmdConfigAdapterFlag)
|
|
|
|
}
|
2019-06-29 00:39:41 +03:00
|
|
|
stdinpipe, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("creating stdin pipe: %v", err)
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
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 {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("generating random confirmation bytes: %v", err)
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// begin writing the confirmation bytes to the child's
|
|
|
|
// stdin; use a goroutine since the child hasn't been
|
|
|
|
// started yet, and writing sychronously would result
|
|
|
|
// in a deadlock
|
|
|
|
go func() {
|
|
|
|
stdinpipe.Write(expect)
|
|
|
|
stdinpipe.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// start the process
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("starting caddy process: %v", err)
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-07-08 01:39:21 +03:00
|
|
|
if !strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2019-06-29 00:39:41 +03:00
|
|
|
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() {
|
2019-07-08 01:39:21 +03:00
|
|
|
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
|
2019-06-29 00:39:41 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
// when one of the goroutines unblocks, we're done and can exit
|
|
|
|
select {
|
|
|
|
case <-success:
|
2019-09-19 03:01:32 +03:00
|
|
|
fmt.Printf("Successfully started Caddy (pid=%d)\n", cmd.Process.Pid)
|
2019-06-29 00:39:41 +03:00
|
|
|
case err := <-exit:
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("caddy process exited with error: %v", err)
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeSuccess, nil
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRun() (int, error) {
|
|
|
|
runCmd := flag.NewFlagSet("run", flag.ExitOnError)
|
|
|
|
runCmdConfigFlag := runCmd.String("config", "", "Configuration file")
|
2019-08-09 21:05:47 +03:00
|
|
|
runCmdConfigAdapterFlag := runCmd.String("config-adapter", "", "Name of config adapter to apply")
|
|
|
|
runCmdPrintEnvFlag := runCmd.Bool("print-env", false, "Print environment")
|
2019-06-29 00:39:41 +03:00
|
|
|
runCmdPingbackFlag := runCmd.String("pingback", "", "Echo confirmation bytes to this address on success")
|
|
|
|
runCmd.Parse(os.Args[2:])
|
|
|
|
|
2019-07-18 19:58:31 +03:00
|
|
|
// if we are supposed to print the environment, do that first
|
|
|
|
if *runCmdPrintEnvFlag {
|
|
|
|
exitCode, err := cmdEnviron()
|
|
|
|
if err != nil {
|
|
|
|
return exitCode, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:05:47 +03:00
|
|
|
// get the config in caddy's native format
|
|
|
|
config, err := loadConfig(*runCmdConfigFlag, *runCmdConfigAdapterFlag)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
|
2019-06-29 04:28:28 +03:00
|
|
|
// set a fitting User-Agent for ACME requests
|
|
|
|
goModule := caddy.GoModule()
|
|
|
|
cleanModVersion := strings.TrimPrefix(goModule.Version, "v")
|
|
|
|
certmagic.UserAgent = "Caddy/" + cleanModVersion
|
|
|
|
|
2019-06-29 00:39:41 +03:00
|
|
|
// start the admin endpoint along with any initial config
|
2019-08-09 21:05:47 +03:00
|
|
|
err = caddy.StartAdmin(config)
|
2019-06-29 00:39:41 +03:00
|
|
|
if err != nil {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("starting caddy administration endpoint: %v", err)
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
defer caddy.StopAdmin()
|
|
|
|
|
|
|
|
// 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 {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("reading confirmation bytes from stdin: %v", err)
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
conn, err := net.Dial("tcp", *runCmdPingbackFlag)
|
|
|
|
if err != nil {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("dialing confirmation address: %v", err)
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
_, err = conn.Write(confirmationBytes)
|
|
|
|
if err != nil {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("writing confirmation bytes to %s: %v", *runCmdPingbackFlag, err)
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdStop() (int, error) {
|
|
|
|
processList, err := ps.Processes()
|
|
|
|
if err != nil {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("listing processes: %v", err)
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
2019-07-20 19:44:54 +03:00
|
|
|
thisProcName := getProcessName()
|
2019-06-29 00:39:41 +03:00
|
|
|
var found bool
|
|
|
|
for _, p := range processList {
|
|
|
|
// the process we're looking for should have the same name but different PID
|
|
|
|
if p.Executable() == thisProcName && p.Pid() != os.Getpid() {
|
|
|
|
found = true
|
|
|
|
fmt.Printf("pid=%d\n", p.Pid())
|
2019-07-20 19:44:54 +03:00
|
|
|
|
2019-06-29 00:39:41 +03:00
|
|
|
if err := gracefullyStopProcess(p.Pid()); err != nil {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup, err
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("Caddy is not running")
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
fmt.Println(" success")
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeSuccess, nil
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
|
2019-07-05 18:59:13 +03:00
|
|
|
func cmdReload() (int, error) {
|
|
|
|
reloadCmd := flag.NewFlagSet("load", flag.ExitOnError)
|
|
|
|
reloadCmdConfigFlag := reloadCmd.String("config", "", "Configuration file")
|
2019-08-09 21:05:47 +03:00
|
|
|
reloadCmdConfigAdapterFlag := reloadCmd.String("config-adapter", "", "Name of config adapter to apply")
|
2019-07-05 18:59:13 +03:00
|
|
|
reloadCmdAddrFlag := reloadCmd.String("address", "", "Address of the administration listener, if different from config")
|
|
|
|
reloadCmd.Parse(os.Args[2:])
|
|
|
|
|
|
|
|
// a configuration is required
|
|
|
|
if *reloadCmdConfigFlag == "" {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("no configuration to load (use --config)")
|
2019-07-05 18:59:13 +03:00
|
|
|
}
|
|
|
|
|
2019-08-09 21:05:47 +03:00
|
|
|
// get the config in caddy's native format
|
|
|
|
config, err := loadConfig(*reloadCmdConfigFlag, *reloadCmdConfigAdapterFlag)
|
2019-07-05 18:59:13 +03:00
|
|
|
if err != nil {
|
2019-08-09 21:05:47 +03:00
|
|
|
return caddy.ExitCodeFailedStartup, err
|
2019-07-05 18:59:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the address of the admin listener and craft endpoint URL
|
|
|
|
adminAddr := *reloadCmdAddrFlag
|
|
|
|
if adminAddr == "" {
|
|
|
|
var tmpStruct struct {
|
|
|
|
Admin caddy.AdminConfig `json:"admin"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(config, &tmpStruct)
|
|
|
|
if err != nil {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("unmarshaling admin listener address from config: %v", err)
|
2019-07-05 18:59:13 +03:00
|
|
|
}
|
|
|
|
adminAddr = tmpStruct.Admin.Listen
|
|
|
|
}
|
|
|
|
if adminAddr == "" {
|
|
|
|
adminAddr = caddy.DefaultAdminListen
|
|
|
|
}
|
|
|
|
adminEndpoint := fmt.Sprintf("http://%s/load", adminAddr)
|
|
|
|
|
|
|
|
// send the configuration to the instance
|
|
|
|
resp, err := http.Post(adminEndpoint, "application/json", bytes.NewReader(config))
|
|
|
|
if err != nil {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("sending configuration to instance: %v", err)
|
2019-07-05 18:59:13 +03:00
|
|
|
}
|
|
|
|
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 {
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("HTTP %d: reading error message: %v", resp.StatusCode, err)
|
2019-07-05 18:59:13 +03:00
|
|
|
}
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("caddy responded with error: HTTP %d: %s", resp.StatusCode, respBody)
|
2019-07-05 18:59:13 +03:00
|
|
|
}
|
|
|
|
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeSuccess, nil
|
2019-07-05 18:59:13 +03:00
|
|
|
}
|
|
|
|
|
2019-06-29 00:39:41 +03:00
|
|
|
func cmdVersion() (int, error) {
|
2019-06-29 04:28:28 +03:00
|
|
|
goModule := caddy.GoModule()
|
2019-06-29 00:39:41 +03:00
|
|
|
if goModule.Sum != "" {
|
|
|
|
// a build with a known version will also have a checksum
|
2019-06-29 04:28:28 +03:00
|
|
|
fmt.Printf("%s %s\n", goModule.Version, goModule.Sum)
|
2019-06-29 00:39:41 +03:00
|
|
|
} else {
|
|
|
|
fmt.Println(goModule.Version)
|
|
|
|
}
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeSuccess, nil
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdListModules() (int, error) {
|
|
|
|
for _, m := range caddy.Modules() {
|
|
|
|
fmt.Println(m)
|
|
|
|
}
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeSuccess, nil
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdEnviron() (int, error) {
|
|
|
|
for _, v := range os.Environ() {
|
|
|
|
fmt.Println(v)
|
|
|
|
}
|
2019-07-12 19:07:11 +03:00
|
|
|
return caddy.ExitCodeSuccess, nil
|
2019-06-29 00:39:41 +03:00
|
|
|
}
|
2019-08-09 21:05:47 +03:00
|
|
|
|
|
|
|
func cmdAdaptConfig() (int, error) {
|
|
|
|
adaptCmd := flag.NewFlagSet("adapt", flag.ExitOnError)
|
|
|
|
adaptCmdAdapterFlag := adaptCmd.String("adapter", "", "Name of config adapter")
|
|
|
|
adaptCmdInputFlag := adaptCmd.String("input", "", "Configuration file to adapt")
|
|
|
|
adaptCmdPrettyFlag := adaptCmd.Bool("pretty", false, "Format the output for human readability")
|
|
|
|
adaptCmd.Parse(os.Args[2:])
|
|
|
|
|
|
|
|
if *adaptCmdAdapterFlag == "" || *adaptCmdInputFlag == "" {
|
|
|
|
return caddy.ExitCodeFailedStartup,
|
|
|
|
fmt.Errorf("usage: caddy adapt-config --adapter <name> --input <file>")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-22 22:38:37 +03:00
|
|
|
opts := make(map[string]interface{})
|
2019-08-09 21:05:47 +03:00
|
|
|
if *adaptCmdPrettyFlag {
|
|
|
|
opts["pretty"] = "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
log.Printf("[WARNING][%s] %s:%d: %s", *adaptCmdAdapterFlag, warn.File, warn.Line, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// print result to stdout
|
|
|
|
fmt.Println(string(adaptedConfig))
|
|
|
|
|
|
|
|
return caddy.ExitCodeSuccess, nil
|
|
|
|
}
|