2019-06-30 17:07:58 -05: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-14 12:58:28 -05:00
|
|
|
package caddy
|
2019-03-26 13:00:54 -05:00
|
|
|
|
|
|
|
import (
|
2019-03-26 20:42:52 -05:00
|
|
|
"bytes"
|
2019-03-26 13:00:54 -05:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-03-26 20:42:52 -05:00
|
|
|
"net/http/pprof"
|
2019-07-20 11:48:46 -05:00
|
|
|
"os"
|
2019-03-26 13:00:54 -05:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-03-26 20:42:52 -05:00
|
|
|
"time"
|
2019-06-05 12:10:19 -05:00
|
|
|
|
2019-07-20 11:48:46 -05:00
|
|
|
"github.com/mholt/certmagic"
|
2019-06-05 12:10:19 -05:00
|
|
|
"github.com/rs/cors"
|
2019-03-26 13:00:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfgEndptSrv *http.Server
|
|
|
|
cfgEndptSrvMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
2019-06-28 16:39:41 -05:00
|
|
|
// AdminConfig configures the admin endpoint.
|
|
|
|
type AdminConfig struct {
|
|
|
|
Listen string `json:"listen,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultAdminConfig is the default configuration
|
|
|
|
// for the administration endpoint.
|
|
|
|
var DefaultAdminConfig = &AdminConfig{
|
2019-07-05 10:59:13 -05:00
|
|
|
Listen: DefaultAdminListen,
|
2019-06-28 16:39:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartAdmin starts Caddy's administration endpoint,
|
|
|
|
// bootstrapping it with an optional configuration
|
|
|
|
// in the format of JSON bytes. It opens a listener
|
|
|
|
// resource. When no longer needed, StopAdmin should
|
|
|
|
// be called.
|
|
|
|
func StartAdmin(initialConfigJSON []byte) error {
|
2019-03-26 13:00:54 -05:00
|
|
|
cfgEndptSrvMu.Lock()
|
|
|
|
defer cfgEndptSrvMu.Unlock()
|
|
|
|
|
2019-06-28 16:39:41 -05:00
|
|
|
adminConfig := DefaultAdminConfig
|
|
|
|
if len(initialConfigJSON) > 0 {
|
|
|
|
var config *Config
|
|
|
|
err := json.Unmarshal(initialConfigJSON, &config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unmarshaling bootstrap config: %v", err)
|
|
|
|
}
|
|
|
|
if config != nil && config.Admin != nil {
|
|
|
|
adminConfig = config.Admin
|
|
|
|
}
|
|
|
|
if cfgEndptSrv != nil {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err := cfgEndptSrv.Shutdown(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("shutting down old admin endpoint: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", adminConfig.Listen)
|
2019-03-26 13:00:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/load", handleLoadConfig)
|
2019-07-20 11:48:46 -05:00
|
|
|
mux.HandleFunc("/stop", handleStop)
|
2019-03-26 13:00:54 -05:00
|
|
|
|
2019-03-27 13:36:30 -05:00
|
|
|
///// BEGIN PPROF STUFF (TODO: Temporary) /////
|
2019-03-26 20:42:52 -05:00
|
|
|
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
|
|
|
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
|
|
|
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
|
|
|
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
|
|
|
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
|
|
|
///// END PPROF STUFF //////
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
for _, m := range GetModules("admin.routers") {
|
|
|
|
adminrtr := m.New().(AdminRouter)
|
|
|
|
for _, route := range adminrtr.Routes() {
|
2019-08-09 13:05:47 -05:00
|
|
|
mux.Handle(route.Pattern, route)
|
|
|
|
}
|
2019-03-26 13:00:54 -05:00
|
|
|
}
|
|
|
|
|
2019-06-05 12:10:19 -05:00
|
|
|
handler := cors.Default().Handler(mux)
|
|
|
|
|
2019-03-26 13:00:54 -05:00
|
|
|
cfgEndptSrv = &http.Server{
|
2019-06-05 12:10:19 -05:00
|
|
|
Handler: handler,
|
2019-03-26 20:42:52 -05:00
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
|
|
IdleTimeout: 5 * time.Second,
|
|
|
|
MaxHeaderBytes: 1024 * 256,
|
2019-03-26 13:00:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
go cfgEndptSrv.Serve(ln)
|
|
|
|
|
2019-06-28 16:39:41 -05:00
|
|
|
log.Println("Caddy 2 admin endpoint listening on", adminConfig.Listen)
|
|
|
|
|
|
|
|
if len(initialConfigJSON) > 0 {
|
|
|
|
err := Load(bytes.NewReader(initialConfigJSON))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading initial config: %v", err)
|
|
|
|
}
|
|
|
|
log.Println("Caddy 2 serving initial configuration")
|
|
|
|
}
|
|
|
|
|
2019-03-26 13:00:54 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-26 16:45:51 -05:00
|
|
|
// StopAdmin stops the API endpoint.
|
|
|
|
func StopAdmin() error {
|
2019-03-26 13:00:54 -05:00
|
|
|
cfgEndptSrvMu.Lock()
|
|
|
|
defer cfgEndptSrvMu.Unlock()
|
|
|
|
|
|
|
|
if cfgEndptSrv == nil {
|
|
|
|
return fmt.Errorf("no server")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cfgEndptSrv.Shutdown(context.Background()) // TODO
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("shutting down server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgEndptSrv = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
// AdminRouter is a type which can return routes for the admin API.
|
|
|
|
type AdminRouter interface {
|
|
|
|
Routes() []AdminRoute
|
|
|
|
}
|
|
|
|
|
2019-03-26 16:45:51 -05:00
|
|
|
// AdminRoute represents a route for the admin endpoint.
|
|
|
|
type AdminRoute struct {
|
|
|
|
http.Handler
|
|
|
|
Pattern string
|
|
|
|
}
|
|
|
|
|
2019-03-26 13:00:54 -05:00
|
|
|
func handleLoadConfig(w http.ResponseWriter, r *http.Request) {
|
2019-03-26 20:42:52 -05:00
|
|
|
r.Close = true
|
2019-07-20 11:48:46 -05:00
|
|
|
if r.Method != http.MethodPost {
|
2019-03-26 13:00:54 -05:00
|
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(r.Header.Get("Content-Type"), "/json") {
|
|
|
|
http.Error(w, "unacceptable Content-Type", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := Load(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ADMIN][ERROR] loading config: %v", err)
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 11:48:46 -05:00
|
|
|
func handleStop(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("[ADMIN] Initiating shutdown")
|
|
|
|
if err := stopAndCleanup(); err != nil {
|
|
|
|
log.Printf("[ADMIN][ERROR] stopping: %v \n", err)
|
|
|
|
}
|
|
|
|
log.Println("[ADMIN] Exiting")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopAndCleanup() error {
|
|
|
|
if err := Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
certmagic.CleanUpOwnLocks()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-26 16:45:51 -05:00
|
|
|
// Load loads and starts a configuration.
|
2019-03-26 13:00:54 -05:00
|
|
|
func Load(r io.Reader) error {
|
2019-03-26 20:42:52 -05:00
|
|
|
buf := bufPool.Get().(*bytes.Buffer)
|
|
|
|
buf.Reset()
|
|
|
|
defer bufPool.Put(buf)
|
|
|
|
|
|
|
|
_, err := io.Copy(buf, io.LimitReader(r, 1024*1024))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:54:48 -05:00
|
|
|
var cfg *Config
|
2019-03-26 20:42:52 -05:00
|
|
|
err = json.Unmarshal(buf.Bytes(), &cfg)
|
2019-03-26 13:00:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decoding config: %v", err)
|
|
|
|
}
|
2019-03-26 20:42:52 -05:00
|
|
|
|
2019-04-25 14:54:48 -05:00
|
|
|
err = Run(cfg)
|
2019-03-26 16:45:51 -05:00
|
|
|
if err != nil {
|
2019-04-25 14:54:48 -05:00
|
|
|
return fmt.Errorf("running: %v", err)
|
2019-03-26 13:00:54 -05:00
|
|
|
}
|
2019-03-26 20:42:52 -05:00
|
|
|
|
2019-03-26 13:00:54 -05:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-26 20:42:52 -05:00
|
|
|
|
2019-07-05 10:59:13 -05:00
|
|
|
// DefaultAdminListen is the address for the admin
|
|
|
|
// listener, if none is specified at startup.
|
|
|
|
var DefaultAdminListen = "localhost:2019"
|
|
|
|
|
2019-03-26 20:42:52 -05:00
|
|
|
var bufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
|
|
|
}
|