added client calls for running servers

This commit is contained in:
Christopher Ramey 2021-03-07 18:40:45 -09:00
parent 86f304e1b8
commit aa65b64867
4 changed files with 90 additions and 4 deletions

View File

@ -15,6 +15,14 @@ type Command struct {
Signature []byte `json:"sig,omitempty"` Signature []byte `json:"sig,omitempty"`
} }
func NewCommand(cm string) *Command {
return &Command{
Expires: time.Now().Add(time.Second * 5),
Command: cm,
Scheme: "hmac-sha256",
}
}
func ParseCommand(jsn []byte) (*Command, error) { func ParseCommand(jsn []byte) (*Command, error) {
cmd := &Command{} cmd := &Command{}
err := json.Unmarshal(jsn, cmd) err := json.Unmarshal(jsn, cmd)

44
client/client.go Normal file
View File

@ -0,0 +1,44 @@
package client
import (
"git.binarythought.com/cdramey/alrm/api"
"git.binarythought.com/cdramey/alrm/config"
"net/http"
"net/url"
)
func Shutdown(cfg *config.Config) error {
return doCommand(cfg, "shutdown")
}
func Restart(cfg *config.Config) error {
return doCommand(cfg, "restart")
}
func doCommand(cfg *config.Config, cm string) error {
aurl, err := url.Parse("http://" + cfg.Listen + "/api")
if err != nil {
return err
}
cmd := api.NewCommand(cm)
err = cmd.Sign(cfg.APIKey)
if err != nil {
return err
}
cjson, err := cmd.JSON()
if err != nil {
return err
}
params := url.Values{}
params.Add("cmd", string(cjson))
resp, err := http.PostForm(aurl.String(), params)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}

29
main.go
View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"git.binarythought.com/cdramey/alrm/config" "git.binarythought.com/cdramey/alrm/config"
"git.binarythought.com/cdramey/alrm/server" "git.binarythought.com/cdramey/alrm/server"
"git.binarythought.com/cdramey/alrm/client"
"os" "os"
"strings" "strings"
) )
@ -130,6 +131,32 @@ func main() {
} }
} }
case "shutdown":
cfg, err := config.ReadConfig(*cfgpath, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
err = client.Shutdown(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
case "restart":
cfg, err := config.ReadConfig(*cfgpath, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
err = client.Restart(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
case "help", "": case "help", "":
printUsage() printUsage()
@ -149,4 +176,6 @@ func printUsage() {
fmt.Printf(" run a check manually: %s [args] check <host/group>\n", os.Args[0]) fmt.Printf(" run a check manually: %s [args] check <host/group>\n", os.Args[0])
fmt.Printf(" test an alarm: %s [args] alarm <name>\n", os.Args[0]) fmt.Printf(" test an alarm: %s [args] alarm <name>\n", os.Args[0])
fmt.Printf(" start server (forground): %s [args] server\n", os.Args[0]) fmt.Printf(" start server (forground): %s [args] server\n", os.Args[0])
fmt.Printf(" shutdown server: %s [args] shutdown\n", os.Args[0])
fmt.Printf(" restart server: %s [args] restart\n", os.Args[0])
} }

View File

@ -2,22 +2,27 @@ package server
import ( import (
"fmt" "fmt"
"net/http"
"git.binarythought.com/cdramey/alrm/api" "git.binarythought.com/cdramey/alrm/api"
"net/http"
) )
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path { switch r.URL.Path {
case "/api": case "/api":
g := r.URL.Query() if err := r.ParseForm(); err != nil {
c := g.Get("cmd") http.Error(w, fmt.Sprintf("form parse error: %s", err.Error()),
http.StatusBadRequest)
return
}
c := r.FormValue("cmd")
if c == "" { if c == "" {
http.Error(w, "no command given", http.StatusBadRequest) http.Error(w, "no command given", http.StatusBadRequest)
return return
} }
cmd, err := api.ParseCommand([]byte(c)) cmd, err := api.ParseCommand([]byte(c))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("error parsing command: %s", err.Error()), http.Error(w, fmt.Sprintf("command parse error: %s", err.Error()),
http.StatusBadRequest) http.StatusBadRequest)
return return
} }