2019-03-27 05:45:51 +08:00
|
|
|
package caddy2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-03-27 09:42:52 +08:00
|
|
|
"log"
|
2019-03-27 05:45:51 +08:00
|
|
|
"strings"
|
2019-03-27 09:42:52 +08:00
|
|
|
"sync"
|
2019-04-03 05:31:02 +08:00
|
|
|
"sync/atomic"
|
2019-03-27 05:45:51 +08:00
|
|
|
"time"
|
2019-04-26 03:54:48 +08:00
|
|
|
|
|
|
|
"github.com/mholt/certmagic"
|
2019-03-27 05:45:51 +08:00
|
|
|
)
|
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
// Run runs Caddy with the given config.
|
|
|
|
func Run(cfg *Config) error {
|
2019-04-08 14:00:14 +08:00
|
|
|
// allow only one call to Start at a time,
|
|
|
|
// since various calls to LoadModule()
|
|
|
|
// access shared map moduleInstances
|
|
|
|
startMu.Lock()
|
|
|
|
defer startMu.Unlock()
|
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
// because we will need to roll back any state
|
|
|
|
// modifications if this function errors, we
|
|
|
|
// keep a single error value and scope all
|
|
|
|
// sub-operations to their own functions to
|
|
|
|
// ensure this error value does not get
|
|
|
|
// overridden or missed when it should have
|
|
|
|
// been set by a short assignment
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// prepare the new config for use
|
|
|
|
cfg.apps = make(map[string]App)
|
2019-04-08 14:00:14 +08:00
|
|
|
cfg.moduleStates = make(map[string]interface{})
|
|
|
|
|
|
|
|
// reset the shared moduleInstances map; but
|
|
|
|
// keep a temporary reference to the current
|
|
|
|
// one so we can transfer over any necessary
|
2019-04-26 03:54:48 +08:00
|
|
|
// state to the new modules or to roll back
|
|
|
|
// if necessary
|
2019-04-08 14:00:14 +08:00
|
|
|
oldModuleInstances := moduleInstances
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
moduleInstances = oldModuleInstances
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
moduleInstances = make(map[string][]interface{})
|
2019-03-27 05:45:51 +08:00
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
// set up storage and make it CertMagic's default storage, too
|
|
|
|
err = func() error {
|
|
|
|
if cfg.StorageRaw != nil {
|
|
|
|
val, err := LoadModuleInline("system", "caddy.storage", cfg.StorageRaw)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading storage module: %v", err)
|
|
|
|
}
|
|
|
|
stor, err := val.(StorageConverter).CertMagicStorage()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating storage value: %v", err)
|
|
|
|
}
|
|
|
|
cfg.storage = stor
|
|
|
|
cfg.StorageRaw = nil // allow GC to deallocate - TODO: Does this help?
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
if cfg.storage == nil {
|
|
|
|
cfg.storage = &certmagic.FileStorage{Path: dataDir()}
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
certmagic.Default.Storage = cfg.storage
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
// Load, Provision, Validate
|
|
|
|
err = func() error {
|
|
|
|
for modName, rawMsg := range cfg.AppsRaw {
|
|
|
|
val, err := LoadModule(modName, rawMsg)
|
2019-03-27 09:42:52 +08:00
|
|
|
if err != nil {
|
2019-04-26 03:54:48 +08:00
|
|
|
return fmt.Errorf("loading app module '%s': %v", modName, err)
|
2019-03-27 09:42:52 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
cfg.apps[modName] = val.(App)
|
2019-03-27 09:42:52 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-27 09:42:52 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
|
|
|
|
// swap old config with the new one, and
|
|
|
|
// roll back this change if anything fails
|
|
|
|
currentCfgMu.Lock()
|
2019-04-08 14:00:14 +08:00
|
|
|
oldCfg := currentCfg
|
2019-04-26 03:54:48 +08:00
|
|
|
currentCfg = cfg
|
2019-03-27 09:42:52 +08:00
|
|
|
currentCfgMu.Unlock()
|
2019-04-26 03:54:48 +08:00
|
|
|
defer func() {
|
2019-04-08 14:00:14 +08:00
|
|
|
if err != nil {
|
2019-04-26 03:54:48 +08:00
|
|
|
currentCfgMu.Lock()
|
|
|
|
currentCfg = oldCfg
|
|
|
|
currentCfgMu.Unlock()
|
2019-04-08 14:00:14 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Start
|
|
|
|
err = func() error {
|
|
|
|
h := Handle{cfg}
|
|
|
|
for name, a := range cfg.apps {
|
|
|
|
err := a.Start(h)
|
|
|
|
if err != nil {
|
|
|
|
for otherAppName, otherApp := range cfg.apps {
|
|
|
|
err := otherApp.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("aborting app %s: %v", otherAppName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s app module: start: %v", name, err)
|
|
|
|
}
|
2019-04-08 14:00:14 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop
|
|
|
|
if oldCfg != nil {
|
|
|
|
for name, a := range oldCfg.apps {
|
|
|
|
err := a.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] stop %s: %v", name, err)
|
2019-04-08 14:00:14 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 05:31:02 +08:00
|
|
|
// shut down listeners that are no longer being used
|
2019-04-26 03:54:48 +08:00
|
|
|
err = func() error {
|
|
|
|
listenersMu.Lock()
|
|
|
|
for key, info := range listeners {
|
|
|
|
if atomic.LoadInt32(&info.usage) == 0 {
|
|
|
|
err := info.ln.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] closing listener %s: %v", info.ln.Addr(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(listeners, key)
|
2019-04-03 05:31:02 +08:00
|
|
|
}
|
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
listenersMu.Unlock()
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-03 05:31:02 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
// App is a thing that Caddy runs.
|
|
|
|
type App interface {
|
|
|
|
Start(Handle) error
|
|
|
|
Stop() error
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config represents a Caddy configuration.
|
|
|
|
type Config struct {
|
2019-04-26 03:54:48 +08:00
|
|
|
StorageRaw json.RawMessage `json:"storage"`
|
|
|
|
storage certmagic.Storage
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
TestVal string `json:"testval"`
|
2019-04-26 03:54:48 +08:00
|
|
|
AppsRaw map[string]json.RawMessage `json:"apps"`
|
2019-04-08 14:00:14 +08:00
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
// apps stores the decoded Apps values,
|
2019-04-08 14:00:14 +08:00
|
|
|
// keyed by module name.
|
2019-04-26 03:54:48 +08:00
|
|
|
apps map[string]App
|
2019-04-08 14:00:14 +08:00
|
|
|
|
|
|
|
// moduleStates stores the optional "global" state
|
|
|
|
// values of every module used by this configuration,
|
|
|
|
// keyed by module name.
|
|
|
|
moduleStates map[string]interface{}
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
// Handle allows app modules to access
|
|
|
|
// the top-level Config in a controlled
|
|
|
|
// manner without needing to rely on
|
|
|
|
// global state.
|
|
|
|
type Handle struct {
|
|
|
|
current *Config
|
|
|
|
}
|
|
|
|
|
2019-04-29 23:22:00 +08:00
|
|
|
// App returns the configured app named name. If no app with
|
|
|
|
// that name is currently configured, a new empty one will be
|
2019-05-17 01:46:17 +08:00
|
|
|
// instantiated. (The app module must still be registered.)
|
2019-04-29 23:22:00 +08:00
|
|
|
func (h Handle) App(name string) (interface{}, error) {
|
|
|
|
if app, ok := h.current.apps[name]; ok {
|
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
modVal, err := LoadModule(name, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("instantiating new module %s: %v", name, err)
|
|
|
|
}
|
|
|
|
h.current.apps[name] = modVal.(App)
|
|
|
|
return modVal, nil
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStorage returns the configured Caddy storage implementation.
|
|
|
|
// If no storage implementation is explicitly configured, the
|
|
|
|
// default one is returned instead, as long as there is a current
|
|
|
|
// configuration loaded.
|
|
|
|
func GetStorage() certmagic.Storage {
|
|
|
|
currentCfgMu.RLock()
|
|
|
|
defer currentCfgMu.RUnlock()
|
|
|
|
if currentCfg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return currentCfg.storage
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
// Duration is a JSON-string-unmarshable duration type.
|
|
|
|
type Duration time.Duration
|
|
|
|
|
|
|
|
// UnmarshalJSON satisfies json.Unmarshaler.
|
2019-04-01 10:41:29 +08:00
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
2019-03-27 05:45:51 +08:00
|
|
|
dd, err := time.ParseDuration(strings.Trim(string(b), `"`))
|
2019-04-01 10:41:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-27 05:45:51 +08:00
|
|
|
cd := Duration(dd)
|
|
|
|
d = &cd
|
2019-04-01 10:41:29 +08:00
|
|
|
return nil
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 10:42:55 +08:00
|
|
|
// CtxKey is a value type for use with context.WithValue.
|
|
|
|
type CtxKey string
|
2019-04-08 14:00:14 +08:00
|
|
|
|
|
|
|
// currentCfg is the currently-loaded configuration.
|
|
|
|
var (
|
|
|
|
currentCfg *Config
|
2019-04-26 03:54:48 +08:00
|
|
|
currentCfgMu sync.RWMutex
|
2019-04-08 14:00:14 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// moduleInstances stores the individual instantiated
|
|
|
|
// values of modules, keyed by module name. The list
|
|
|
|
// of instances of each module get passed into the
|
|
|
|
// respective module's OnLoad callback, so they can
|
|
|
|
// set up any global state and/or make sure their
|
|
|
|
// configuration, when viewed as a whole, is valid.
|
|
|
|
// Since this list is shared, only one Start() routine
|
|
|
|
// must be allowed to happen at any given time.
|
|
|
|
var moduleInstances = make(map[string][]interface{})
|
|
|
|
|
|
|
|
// startMu ensures that only one Start() happens at a time.
|
2019-04-26 03:54:48 +08:00
|
|
|
// This is important since moduleInstances is shared state.
|
2019-04-08 14:00:14 +08:00
|
|
|
var startMu sync.Mutex
|