2019-03-26 13:00:54 -05:00
|
|
|
package caddy2
|
|
|
|
|
|
|
|
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-03-26 13:00:54 -05:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-03-26 20:42:52 -05:00
|
|
|
"time"
|
2019-03-26 13:00:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfgEndptSrv *http.Server
|
|
|
|
cfgEndptSrvMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
2019-03-26 16:45:51 -05:00
|
|
|
// StartAdmin starts Caddy's administration endpoint.
|
|
|
|
func StartAdmin(addr string) error {
|
2019-03-26 13:00:54 -05:00
|
|
|
cfgEndptSrvMu.Lock()
|
|
|
|
defer cfgEndptSrvMu.Unlock()
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/load", handleLoadConfig)
|
|
|
|
|
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-03-26 13:00:54 -05:00
|
|
|
for _, m := range GetModules("admin") {
|
2019-05-21 15:22:21 -05:00
|
|
|
route := m.New().(AdminRoute)
|
2019-03-26 13:00:54 -05:00
|
|
|
mux.Handle(route.Pattern, route)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgEndptSrv = &http.Server{
|
2019-03-26 20:42:52 -05:00
|
|
|
Handler: mux,
|
|
|
|
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)
|
|
|
|
|
|
|
|
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-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-03-26 13:00:54 -05:00
|
|
|
if r.Method != "POST" {
|
|
|
|
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-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
|
|
|
|
|
|
|
var bufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
|
|
|
}
|