mirror of
https://github.com/adnanh/webhook.git
synced 2025-05-22 21:32:32 +00:00
separated github from post handler
This commit is contained in:
parent
478d189de8
commit
a5d79fddd9
2 changed files with 78 additions and 29 deletions
94
webhook.go
94
webhook.go
|
@ -7,6 +7,7 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -70,49 +71,84 @@ func rootHandler() string {
|
|||
return fmt.Sprintf("webhook %s running for %s serving %d hook(s)\n", version, time.Since(appStart).String(), webhooks.Count())
|
||||
}
|
||||
|
||||
func hookHandler(req *http.Request, params martini.Params) string {
|
||||
defer req.Body.Close()
|
||||
body, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
l4g.Warn("Error occurred while trying to read the request body: %s", err)
|
||||
func githubHandler(id string, body []byte, signature string, params interface{}) {
|
||||
if hook := webhooks.Match(id, params); hook != nil {
|
||||
if hook.Secret != "" {
|
||||
if signature == "" {
|
||||
l4g.Error("Hook %s got matched and contains the secret, but the request didn't contain any signature.", hook.ID)
|
||||
return
|
||||
}
|
||||
|
||||
mac := hmac.New(sha1.New, []byte(hook.Secret))
|
||||
mac.Write(body)
|
||||
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
||||
|
||||
if !hmac.Equal([]byte(signature), []byte(expectedMAC)) {
|
||||
l4g.Error("Hook %s got matched and contains the secret, but the request contained invalid signature. Expected %s, got %s.", hook.ID, expectedMAC, signature)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command(hook.Command)
|
||||
cmd.Dir = hook.Cwd
|
||||
out, err := cmd.Output()
|
||||
l4g.Info("Hook %s triggered successfully! Command output:\n%s\nError: %+v", hook.ID, out, err)
|
||||
}
|
||||
}
|
||||
|
||||
payloadJSON := make(map[string]interface{})
|
||||
func defaultPostHookHandler(id string, formValues url.Values) {
|
||||
if hook := webhooks.Match(id, make(map[string]interface{})); hook != nil {
|
||||
args := make([]string, 0)
|
||||
args = append(args, hook.Command)
|
||||
for i := range hook.Args {
|
||||
if arg := formValues[hook.Args[i]]; len(arg) > 0 {
|
||||
args = append(args, arg[0])
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command(hook.Command)
|
||||
cmd.Args = args
|
||||
cmd.Dir = hook.Cwd
|
||||
|
||||
out, err := cmd.Output()
|
||||
|
||||
l4g.Info("Hook %s triggered successfully! Command output:\n%s\nError: %+v", hook.ID, out, err)
|
||||
}
|
||||
}
|
||||
|
||||
func hookHandler(req *http.Request, params martini.Params) string {
|
||||
if req.Header.Get("Content-Type") == "application/json" {
|
||||
defer req.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
l4g.Warn("Error occurred while trying to read the request body: %s", err)
|
||||
}
|
||||
|
||||
payloadJSON := make(map[string]interface{})
|
||||
|
||||
decoder := json.NewDecoder(strings.NewReader(string(body)))
|
||||
decoder.UseNumber()
|
||||
|
||||
err := decoder.Decode(&payloadJSON)
|
||||
err = decoder.Decode(&payloadJSON)
|
||||
|
||||
if err != nil {
|
||||
l4g.Warn("Error occurred while trying to parse the payload as JSON: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
go func(id string, body []byte, signature string, params interface{}) {
|
||||
if hook := webhooks.Match(id, params); hook != nil {
|
||||
if hook.Secret != "" {
|
||||
if signature == "" {
|
||||
l4g.Error("Hook %s got matched and contains the secret, but the request didn't contain any signature.", hook.ID)
|
||||
return
|
||||
}
|
||||
githubSignature := ""
|
||||
|
||||
mac := hmac.New(sha1.New, []byte(hook.Secret))
|
||||
mac.Write(body)
|
||||
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
||||
|
||||
if !hmac.Equal([]byte(signature), []byte(expectedMAC)) {
|
||||
l4g.Error("Hook %s got matched and contains the secret, but the request contained invalid signature. Expected %s, got %s.", hook.ID, expectedMAC, signature)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command(hook.Command, "", "", hook.Cwd)
|
||||
out, _ := cmd.Output()
|
||||
l4g.Info("Hook %s triggered successfully! Command output:\n%s", hook.ID, out)
|
||||
if len(req.Header.Get("X-Hub-Signature")) > 5 {
|
||||
githubSignature = req.Header.Get("X-Hub-Signature")[5:]
|
||||
}
|
||||
}(params["id"], body, req.Header.Get("X-Hub-Signature")[5:], payloadJSON)
|
||||
|
||||
if strings.Contains(req.Header.Get("User-Agent"), "Github") {
|
||||
go githubHandler(params["id"], body, githubSignature, payloadJSON)
|
||||
}
|
||||
} else {
|
||||
req.ParseForm()
|
||||
go defaultPostHookHandler(params["id"], req.Form)
|
||||
}
|
||||
|
||||
return "Got it, thanks. :-)"
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue