2019-03-27 02:00:54 +08:00
|
|
|
package caddy2
|
|
|
|
|
|
|
|
import (
|
2019-03-27 09:42:52 +08:00
|
|
|
"bytes"
|
2019-03-27 02:00:54 +08:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-03-27 09:42:52 +08:00
|
|
|
"net/http/pprof"
|
2019-03-27 02:00:54 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-03-27 09:42:52 +08:00
|
|
|
"time"
|
2019-06-06 01:10:19 +08:00
|
|
|
|
|
|
|
"github.com/rs/cors"
|
2019-03-27 02:00:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfgEndptSrv *http.Server
|
|
|
|
cfgEndptSrvMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
// StartAdmin starts Caddy's administration endpoint.
|
|
|
|
func StartAdmin(addr string) error {
|
2019-03-27 02:00:54 +08: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-28 02:36:30 +08:00
|
|
|
///// BEGIN PPROF STUFF (TODO: Temporary) /////
|
2019-03-27 09:42:52 +08: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-27 02:00:54 +08:00
|
|
|
for _, m := range GetModules("admin") {
|
2019-05-22 04:22:21 +08:00
|
|
|
route := m.New().(AdminRoute)
|
2019-03-27 02:00:54 +08:00
|
|
|
mux.Handle(route.Pattern, route)
|
|
|
|
}
|
|
|
|
|
2019-06-06 01:10:19 +08:00
|
|
|
handler := cors.Default().Handler(mux)
|
|
|
|
|
2019-03-27 02:00:54 +08:00
|
|
|
cfgEndptSrv = &http.Server{
|
2019-06-06 01:10:19 +08:00
|
|
|
Handler: handler,
|
2019-03-27 09:42:52 +08:00
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
|
|
IdleTimeout: 5 * time.Second,
|
|
|
|
MaxHeaderBytes: 1024 * 256,
|
2019-03-27 02:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
go cfgEndptSrv.Serve(ln)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
// StopAdmin stops the API endpoint.
|
|
|
|
func StopAdmin() error {
|
2019-03-27 02:00:54 +08: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-27 05:45:51 +08:00
|
|
|
// AdminRoute represents a route for the admin endpoint.
|
|
|
|
type AdminRoute struct {
|
|
|
|
http.Handler
|
|
|
|
Pattern string
|
|
|
|
}
|
|
|
|
|
2019-03-27 02:00:54 +08:00
|
|
|
func handleLoadConfig(w http.ResponseWriter, r *http.Request) {
|
2019-03-27 09:42:52 +08:00
|
|
|
r.Close = true
|
2019-03-27 02:00:54 +08: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-27 05:45:51 +08:00
|
|
|
// Load loads and starts a configuration.
|
2019-03-27 02:00:54 +08:00
|
|
|
func Load(r io.Reader) error {
|
2019-03-27 09:42:52 +08: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-26 03:54:48 +08:00
|
|
|
var cfg *Config
|
2019-03-27 09:42:52 +08:00
|
|
|
err = json.Unmarshal(buf.Bytes(), &cfg)
|
2019-03-27 02:00:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decoding config: %v", err)
|
|
|
|
}
|
2019-03-27 09:42:52 +08:00
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
err = Run(cfg)
|
2019-03-27 05:45:51 +08:00
|
|
|
if err != nil {
|
2019-04-26 03:54:48 +08:00
|
|
|
return fmt.Errorf("running: %v", err)
|
2019-03-27 02:00:54 +08:00
|
|
|
}
|
2019-03-27 09:42:52 +08:00
|
|
|
|
2019-03-27 02:00:54 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-27 09:42:52 +08:00
|
|
|
|
|
|
|
var bufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
|
|
|
}
|