2015-10-27 04:55:03 +08:00
|
|
|
// Package caddy implements the Caddy web server as a service.
|
|
|
|
//
|
|
|
|
// To use this package, follow a few simple steps:
|
|
|
|
//
|
|
|
|
// 1. Set the AppName and AppVersion variables.
|
|
|
|
// 2. Call LoadCaddyfile() to get the Caddyfile (it
|
|
|
|
// might have been piped in as part of a restart).
|
|
|
|
// You should pass in your own Caddyfile loader.
|
|
|
|
// 3. Call caddy.Start() to start Caddy, caddy.Stop()
|
|
|
|
// to stop it, or caddy.Restart() to restart it.
|
|
|
|
//
|
|
|
|
// You should use caddy.Wait() to wait for all Caddy servers
|
|
|
|
// to quit before your process exits.
|
2015-10-27 03:34:31 +08:00
|
|
|
package caddy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-11-13 03:58:01 +08:00
|
|
|
"log"
|
2015-10-27 03:34:31 +08:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-11-15 09:00:18 +08:00
|
|
|
"time"
|
2015-10-27 03:34:31 +08:00
|
|
|
|
2015-10-29 12:54:27 +08:00
|
|
|
"github.com/mholt/caddy/caddy/letsencrypt"
|
2015-10-27 03:34:31 +08:00
|
|
|
"github.com/mholt/caddy/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Configurable application parameters
|
|
|
|
var (
|
2015-11-19 22:56:28 +08:00
|
|
|
// AppName is the name of the application.
|
|
|
|
AppName string
|
|
|
|
|
|
|
|
// AppVersion is the version of the application.
|
2015-11-03 03:28:50 +08:00
|
|
|
AppVersion string
|
2015-10-27 03:34:31 +08:00
|
|
|
|
2015-11-19 22:56:28 +08:00
|
|
|
// Quiet when set to true, will not show any informative output on initialization.
|
2015-10-27 03:34:31 +08:00
|
|
|
Quiet bool
|
|
|
|
|
2015-11-15 09:00:18 +08:00
|
|
|
// HTTP2 indicates whether HTTP2 is enabled or not.
|
2015-10-27 03:34:31 +08:00
|
|
|
HTTP2 bool // TODO: temporary flag until http2 is standard
|
2015-11-13 03:58:01 +08:00
|
|
|
|
2015-11-15 09:00:18 +08:00
|
|
|
// PidFile is the path to the pidfile to create.
|
2015-11-13 03:58:01 +08:00
|
|
|
PidFile string
|
2015-11-15 09:00:18 +08:00
|
|
|
|
|
|
|
// GracefulTimeout is the maximum duration of a graceful shutdown.
|
|
|
|
GracefulTimeout time.Duration
|
2015-10-27 03:34:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// caddyfile is the input configuration text used for this process
|
|
|
|
caddyfile Input
|
|
|
|
|
|
|
|
// caddyfileMu protects caddyfile during changes
|
|
|
|
caddyfileMu sync.Mutex
|
|
|
|
|
2015-11-03 03:28:50 +08:00
|
|
|
// errIncompleteRestart occurs if this process is a fork
|
2015-10-27 03:34:31 +08:00
|
|
|
// of the parent but no Caddyfile was piped in
|
2015-11-13 14:54:42 +08:00
|
|
|
errIncompleteRestart = errors.New("incomplete restart")
|
2015-10-27 03:34:31 +08:00
|
|
|
|
|
|
|
// servers is a list of all the currently-listening servers
|
|
|
|
servers []*server.Server
|
|
|
|
|
|
|
|
// serversMu protects the servers slice during changes
|
|
|
|
serversMu sync.Mutex
|
|
|
|
|
|
|
|
// wg is used to wait for all servers to shut down
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
// loadedGob is used if this is a child process as part of
|
|
|
|
// a graceful restart; it is used to map listeners to their
|
|
|
|
// index in the list of inherited file descriptors. This
|
|
|
|
// variable is not safe for concurrent access.
|
|
|
|
loadedGob caddyfileGob
|
2015-11-11 10:46:18 +08:00
|
|
|
|
2015-11-13 03:58:01 +08:00
|
|
|
// startedBefore should be set to true if caddy has been started
|
|
|
|
// at least once (does not indicate whether currently running).
|
2015-11-11 10:46:18 +08:00
|
|
|
startedBefore bool
|
2015-10-27 03:34:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-11-03 03:28:50 +08:00
|
|
|
// DefaultHost is the default host.
|
2015-11-19 01:05:13 +08:00
|
|
|
DefaultHost = ""
|
2015-11-03 03:28:50 +08:00
|
|
|
// DefaultPort is the default port.
|
2015-10-27 03:34:31 +08:00
|
|
|
DefaultPort = "2015"
|
2015-11-03 03:28:50 +08:00
|
|
|
// DefaultRoot is the default root folder.
|
2015-10-27 03:34:31 +08:00
|
|
|
DefaultRoot = "."
|
|
|
|
)
|
|
|
|
|
|
|
|
// Start starts Caddy with the given Caddyfile. If cdyfile
|
2015-11-15 09:00:18 +08:00
|
|
|
// is nil, the LoadCaddyfile function will be called to get
|
|
|
|
// one.
|
|
|
|
//
|
|
|
|
// This function blocks until all the servers are listening.
|
2015-10-27 03:34:31 +08:00
|
|
|
//
|
2015-11-15 09:00:18 +08:00
|
|
|
// Note (POSIX): If Start is called in the child process of a
|
|
|
|
// restart more than once within the duration of the graceful
|
|
|
|
// cutoff (i.e. the child process called Start a first time,
|
|
|
|
// then called Stop, then Start again within the first 5 seconds
|
|
|
|
// or however long GracefulTimeout is) and the Caddyfiles have
|
|
|
|
// at least one listener address in common, the second Start
|
|
|
|
// may fail with "address already in use" as there's no
|
|
|
|
// guarantee that the parent process has relinquished the
|
|
|
|
// address before the grace period ends.
|
2015-11-10 02:52:43 +08:00
|
|
|
func Start(cdyfile Input) (err error) {
|
2015-11-13 14:54:42 +08:00
|
|
|
// If we return with no errors, we must do two things: tell the
|
|
|
|
// parent that we succeeded and write to the pidfile.
|
2015-11-13 03:58:01 +08:00
|
|
|
defer func() {
|
2015-11-13 14:54:42 +08:00
|
|
|
if err == nil {
|
|
|
|
signalSuccessToParent() // TODO: Is doing this more than once per process a bad idea? Start could get called more than once in other apps.
|
|
|
|
if PidFile != "" {
|
|
|
|
err := writePidFile()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] Could not write pidfile: %v", err)
|
|
|
|
}
|
2015-11-13 03:58:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-10-27 03:34:31 +08:00
|
|
|
|
|
|
|
// Input must never be nil; try to load something
|
|
|
|
if cdyfile == nil {
|
|
|
|
cdyfile, err = LoadCaddyfile(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
caddyfileMu.Lock()
|
|
|
|
caddyfile = cdyfile
|
|
|
|
caddyfileMu.Unlock()
|
|
|
|
|
2015-11-02 00:46:23 +08:00
|
|
|
// load the server configs (activates Let's Encrypt)
|
|
|
|
configs, err := loadConfigs(path.Base(cdyfile.Path()), bytes.NewReader(cdyfile.Body()))
|
2015-10-29 12:54:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// group virtualhosts by address
|
|
|
|
groupings, err := arrangeBindings(configs)
|
2015-10-27 04:28:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-27 03:34:31 +08:00
|
|
|
|
|
|
|
// Start each server with its one or more configurations
|
2015-10-27 04:28:29 +08:00
|
|
|
err = startServers(groupings)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-27 03:34:31 +08:00
|
|
|
}
|
2015-11-11 10:46:18 +08:00
|
|
|
startedBefore = true
|
2015-10-27 03:34:31 +08:00
|
|
|
|
|
|
|
// Show initialization output
|
2015-11-04 03:01:54 +08:00
|
|
|
if !Quiet && !IsRestart() {
|
2015-10-27 03:34:31 +08:00
|
|
|
var checkedFdLimit bool
|
|
|
|
for _, group := range groupings {
|
|
|
|
for _, conf := range group.Configs {
|
|
|
|
// Print address of site
|
|
|
|
fmt.Println(conf.Address())
|
|
|
|
|
|
|
|
// Note if non-localhost site resolves to loopback interface
|
|
|
|
if group.BindAddr.IP.IsLoopback() && !isLocalhost(conf.Host) {
|
|
|
|
fmt.Printf("Notice: %s is only accessible on this machine (%s)\n",
|
|
|
|
conf.Host, group.BindAddr.IP.String())
|
|
|
|
}
|
|
|
|
if !checkedFdLimit && !group.BindAddr.IP.IsLoopback() && !isLocalhost(conf.Host) {
|
|
|
|
checkFdlimit()
|
|
|
|
checkedFdLimit = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-27 04:28:29 +08:00
|
|
|
// startServers starts all the servers in groupings,
|
|
|
|
// taking into account whether or not this process is
|
2015-10-30 14:19:43 +08:00
|
|
|
// a child from a graceful restart or not. It blocks
|
|
|
|
// until the servers are listening.
|
2015-11-09 22:45:37 +08:00
|
|
|
func startServers(groupings bindingGroup) error {
|
2015-10-30 14:19:43 +08:00
|
|
|
var startupWg sync.WaitGroup
|
2015-11-10 02:52:43 +08:00
|
|
|
errChan := make(chan error, len(groupings)) // must be buffered to allow Serve functions below to return if stopped later
|
2015-10-30 14:19:43 +08:00
|
|
|
|
|
|
|
for _, group := range groupings {
|
2015-11-15 09:00:18 +08:00
|
|
|
s, err := server.New(group.BindAddr.String(), group.Configs, GracefulTimeout)
|
2015-10-27 04:28:29 +08:00
|
|
|
if err != nil {
|
2015-11-06 05:07:34 +08:00
|
|
|
return err
|
2015-10-27 04:28:29 +08:00
|
|
|
}
|
2016-01-04 08:05:10 +08:00
|
|
|
s.HTTP2 = HTTP2 // TODO: This setting is temporary
|
|
|
|
s.ReqCallback = letsencrypt.RequestCallback // ensures we can solve ACME challenges while running
|
2015-10-27 04:28:29 +08:00
|
|
|
|
|
|
|
var ln server.ListenerFile
|
2015-11-04 03:01:54 +08:00
|
|
|
if IsRestart() {
|
2015-10-27 04:28:29 +08:00
|
|
|
// Look up this server's listener in the map of inherited file descriptors;
|
2015-11-06 05:07:34 +08:00
|
|
|
// if we don't have one, we must make a new one (later).
|
2015-10-27 04:28:29 +08:00
|
|
|
if fdIndex, ok := loadedGob.ListenerFds[s.Addr]; ok {
|
|
|
|
file := os.NewFile(fdIndex, "")
|
|
|
|
|
|
|
|
fln, err := net.FileListener(file)
|
|
|
|
if err != nil {
|
2015-11-06 05:07:34 +08:00
|
|
|
return err
|
2015-10-27 04:28:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ln, ok = fln.(server.ListenerFile)
|
|
|
|
if !ok {
|
2015-11-06 05:07:34 +08:00
|
|
|
return errors.New("listener for " + s.Addr + " was not a ListenerFile")
|
2015-10-27 04:28:29 +08:00
|
|
|
}
|
|
|
|
|
2015-11-15 09:00:18 +08:00
|
|
|
file.Close()
|
|
|
|
delete(loadedGob.ListenerFds, s.Addr)
|
2015-10-27 04:28:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
2015-10-30 14:19:43 +08:00
|
|
|
go func(s *server.Server, ln server.ListenerFile) {
|
2015-10-27 04:28:29 +08:00
|
|
|
defer wg.Done()
|
2015-11-11 10:46:18 +08:00
|
|
|
|
|
|
|
// run startup functions that should only execute when
|
|
|
|
// the original parent process is starting.
|
|
|
|
if !IsRestart() && !startedBefore {
|
|
|
|
err := s.RunFirstStartupFuncs()
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the server
|
2015-10-27 04:28:29 +08:00
|
|
|
if ln != nil {
|
2015-11-06 06:21:13 +08:00
|
|
|
errChan <- s.Serve(ln)
|
2015-10-27 04:28:29 +08:00
|
|
|
} else {
|
2015-11-06 06:21:13 +08:00
|
|
|
errChan <- s.ListenAndServe()
|
2015-10-27 04:28:29 +08:00
|
|
|
}
|
2015-10-30 14:19:43 +08:00
|
|
|
}(s, ln)
|
|
|
|
|
|
|
|
startupWg.Add(1)
|
|
|
|
go func(s *server.Server) {
|
|
|
|
defer startupWg.Done()
|
|
|
|
s.WaitUntilStarted()
|
|
|
|
}(s)
|
2015-10-27 04:28:29 +08:00
|
|
|
|
|
|
|
serversMu.Lock()
|
|
|
|
servers = append(servers, s)
|
|
|
|
serversMu.Unlock()
|
|
|
|
}
|
2015-10-30 14:19:43 +08:00
|
|
|
|
2015-11-15 09:00:18 +08:00
|
|
|
// Close the remaining (unused) file descriptors to free up resources
|
|
|
|
if IsRestart() {
|
|
|
|
for key, fdIndex := range loadedGob.ListenerFds {
|
|
|
|
os.NewFile(fdIndex, "").Close()
|
|
|
|
delete(loadedGob.ListenerFds, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 03:01:54 +08:00
|
|
|
// Wait for all servers to finish starting
|
2015-10-30 14:19:43 +08:00
|
|
|
startupWg.Wait()
|
|
|
|
|
2015-11-04 03:01:54 +08:00
|
|
|
// Return the first error, if any
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
2015-11-06 06:21:13 +08:00
|
|
|
// "use of closed network connection" is normal if it was a graceful shutdown
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-04 03:01:54 +08:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2015-10-27 04:28:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-27 04:55:03 +08:00
|
|
|
// Stop stops all servers. It blocks until they are all stopped.
|
2015-10-30 14:19:43 +08:00
|
|
|
// It does NOT execute shutdown callbacks that may have been
|
2015-11-15 11:56:34 +08:00
|
|
|
// configured by middleware (they must be executed separately).
|
2015-10-27 03:34:31 +08:00
|
|
|
func Stop() error {
|
2015-10-29 12:54:27 +08:00
|
|
|
letsencrypt.Deactivate()
|
|
|
|
|
2015-10-27 03:34:31 +08:00
|
|
|
serversMu.Lock()
|
|
|
|
for _, s := range servers {
|
2015-11-15 09:00:18 +08:00
|
|
|
if err := s.Stop(); err != nil {
|
|
|
|
log.Printf("[ERROR] Stopping %s: %v", s.Addr, err)
|
|
|
|
}
|
2015-10-27 03:34:31 +08:00
|
|
|
}
|
2015-10-29 12:54:27 +08:00
|
|
|
servers = []*server.Server{} // don't reuse servers
|
2015-10-27 03:34:31 +08:00
|
|
|
serversMu.Unlock()
|
2015-10-29 12:54:27 +08:00
|
|
|
|
2015-10-27 03:34:31 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait blocks until all servers are stopped.
|
|
|
|
func Wait() {
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2015-11-15 09:00:18 +08:00
|
|
|
// LoadCaddyfile loads a Caddyfile, prioritizing a Caddyfile
|
|
|
|
// piped from stdin as part of a restart (only happens on first call
|
|
|
|
// to LoadCaddyfile). If it is not a restart, this function tries
|
|
|
|
// calling the user's loader function, and if that returns nil, then
|
|
|
|
// this function resorts to the default configuration. Thus, if there
|
|
|
|
// are no other errors, this function always returns at least the
|
|
|
|
// default Caddyfile.
|
2015-10-27 03:34:31 +08:00
|
|
|
func LoadCaddyfile(loader func() (Input, error)) (cdyfile Input, err error) {
|
|
|
|
// If we are a fork, finishing the restart is highest priority;
|
|
|
|
// piped input is required in this case.
|
2015-11-04 03:01:54 +08:00
|
|
|
if IsRestart() {
|
2015-10-27 03:34:31 +08:00
|
|
|
err := gob.NewDecoder(os.Stdin).Decode(&loadedGob)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-30 07:23:20 +08:00
|
|
|
cdyfile = loadedGob.Caddyfile
|
2015-10-27 03:34:31 +08:00
|
|
|
}
|
|
|
|
|
2015-11-07 04:22:22 +08:00
|
|
|
// Try user's loader
|
2015-10-27 03:34:31 +08:00
|
|
|
if cdyfile == nil && loader != nil {
|
|
|
|
cdyfile, err = loader()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise revert to default
|
|
|
|
if cdyfile == nil {
|
2015-11-03 23:10:16 +08:00
|
|
|
cdyfile = DefaultInput()
|
2015-10-27 03:34:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyfileFromPipe loads the Caddyfile input from f if f is
|
|
|
|
// not interactive input. f is assumed to be a pipe or stream,
|
|
|
|
// such as os.Stdin. If f is not a pipe, no error is returned
|
|
|
|
// but the Input value will be nil. An error is only returned
|
|
|
|
// if there was an error reading the pipe, even if the length
|
|
|
|
// of what was read is 0.
|
|
|
|
func CaddyfileFromPipe(f *os.File) (Input, error) {
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err == nil && fi.Mode()&os.ModeCharDevice == 0 {
|
|
|
|
// Note that a non-nil error is not a problem. Windows
|
|
|
|
// will not create a stdin if there is no pipe, which
|
|
|
|
// produces an error when calling Stat(). But Unix will
|
|
|
|
// make one either way, which is why we also check that
|
|
|
|
// bitmask.
|
|
|
|
// BUG: Reading from stdin after this fails (e.g. for the let's encrypt email address) (OS X)
|
|
|
|
confBody, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return CaddyfileInput{
|
|
|
|
Contents: confBody,
|
|
|
|
Filepath: f.Name(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// not having input from the pipe is not itself an error,
|
|
|
|
// just means no input to return.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-10-27 04:55:03 +08:00
|
|
|
// Caddyfile returns the current Caddyfile
|
|
|
|
func Caddyfile() Input {
|
|
|
|
caddyfileMu.Lock()
|
|
|
|
defer caddyfileMu.Unlock()
|
|
|
|
return caddyfile
|
|
|
|
}
|
|
|
|
|
2015-10-27 03:34:31 +08:00
|
|
|
// Input represents a Caddyfile; its contents and file path
|
|
|
|
// (which should include the file name at the end of the path).
|
|
|
|
// If path does not apply (e.g. piped input) you may use
|
|
|
|
// any understandable value. The path is mainly used for logging,
|
|
|
|
// error messages, and debugging.
|
|
|
|
type Input interface {
|
|
|
|
// Gets the Caddyfile contents
|
|
|
|
Body() []byte
|
|
|
|
|
|
|
|
// Gets the path to the origin file
|
|
|
|
Path() string
|
2015-10-27 07:57:32 +08:00
|
|
|
|
|
|
|
// IsFile returns true if the original input was a file on the file system
|
|
|
|
// that could be loaded again later if requested.
|
|
|
|
IsFile() bool
|
2015-10-27 03:34:31 +08:00
|
|
|
}
|