mirror of
https://github.com/alvierahman90/gohookr.git
synced 2025-10-13 19:04:22 +00:00
Compare commits
2 Commits
c5fef8e42d
...
dev
Author | SHA1 | Date | |
---|---|---|---|
749a3bc9b3
|
|||
8f0cf776b1
|
8
Makefile
8
Makefile
@@ -3,15 +3,17 @@ all: install
|
||||
clean:
|
||||
rm -rf gohookr
|
||||
|
||||
install:
|
||||
go mod tidy
|
||||
go build -o gohookr
|
||||
install: build
|
||||
cp gohookr /usr/local/bin/
|
||||
cp gohookr.service /usr/lib/systemd/system/
|
||||
cp -n config.json /etc/gohookr.json
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now gohookr
|
||||
|
||||
build:
|
||||
go mod tidy
|
||||
go build -o gohookr
|
||||
|
||||
uninstall:
|
||||
systemctl disable --now gohookr
|
||||
rm -rf /usr/local/bin/gohookr /usr/lib/systemd/system/gohookr.service
|
||||
|
13
config.yml
Normal file
13
config.yml
Normal file
@@ -0,0 +1,13 @@
|
||||
listenaddress: 127.0.0.1:8654
|
||||
services:
|
||||
test:
|
||||
script:
|
||||
program: "echo"
|
||||
arguments:
|
||||
- test
|
||||
tests:
|
||||
- program: ./example.sh
|
||||
appendpayload: true
|
||||
disablesignatureverification: false
|
||||
signatureheader: test
|
||||
secret: thisisasecret
|
@@ -1,5 +1,12 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// The struct that represents the config.json file
|
||||
type Config struct {
|
||||
ListenAddress string
|
||||
@@ -33,3 +40,23 @@ func (c Config) Validate() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) Load(config_filename string) error {
|
||||
|
||||
raw_config, err := ioutil.ReadFile(config_filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(raw_config, &c)
|
||||
if err == nil {
|
||||
return c.Validate()
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(raw_config, &c)
|
||||
if err == nil {
|
||||
return c.Validate()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
0
example.sh
Executable file → Normal file
0
example.sh
Executable file → Normal file
5
go.mod
5
go.mod
@@ -2,4 +2,7 @@ module git.alv.cx/alvierahman90/gohookr
|
||||
|
||||
go 1.16
|
||||
|
||||
require github.com/gorilla/mux v1.8.0
|
||||
require (
|
||||
github.com/gorilla/mux v1.8.0
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@@ -1,2 +1,6 @@
|
||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
21
main.go
21
main.go
@@ -4,7 +4,6 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@@ -18,9 +17,10 @@ import (
|
||||
|
||||
var config_filename = "/etc/gohookr.json"
|
||||
var checkSignature = true
|
||||
var c config.Config
|
||||
|
||||
func main() {
|
||||
var c config.Config
|
||||
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/webhooks/{service}", webhookHandler)
|
||||
|
||||
@@ -32,23 +32,28 @@ func main() {
|
||||
checkSignature = p != "true"
|
||||
}
|
||||
|
||||
raw_config, err := ioutil.ReadFile(config_filename)
|
||||
var err = c.Load(config_filename)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
fmt.Printf("CONFIG OK: %s\n", config_filename)
|
||||
fmt.Printf("LISTENING AT: %s\n", c.ListenAddress)
|
||||
|
||||
if err := json.Unmarshal(raw_config, &c); err != nil {
|
||||
panic(err.Error())
|
||||
for _, v := range os.Args {
|
||||
if v == "checkConfig" {
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.Validate(); err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
log.Fatal(http.ListenAndServe(c.ListenAddress, r))
|
||||
}
|
||||
|
||||
func webhookHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var c config.Config
|
||||
var err = c.Load(config_filename)
|
||||
if err != nil {
|
||||
writeResponse(w, 500, "Unable to read config file")
|
||||
}
|
||||
// Check what service is specified in URL (/webhooks/{service}) and if it exists
|
||||
serviceName := string(mux.Vars(r)["service"])
|
||||
service, ok := c.Services[serviceName]
|
||||
|
12
readme.md
12
readme.md
@@ -15,10 +15,17 @@ make
|
||||
Default config path is `/etc/gohookr.json`.
|
||||
It can be overriden by setting environment variable `CONFIG`.
|
||||
|
||||
The config file will be re-read every request so service configs can be changed without restarting
|
||||
the service (unless you want to change the listening port).
|
||||
|
||||
Check below for an example configuration, which should tell you most of the things you need to know
|
||||
to configure gohookr.
|
||||
|
||||
Currently gohookr must be restarted after config changes.
|
||||
You can test your config file by running
|
||||
|
||||
```
|
||||
gohookr checkConfig
|
||||
```
|
||||
|
||||
### Signature Verification
|
||||
|
||||
@@ -32,7 +39,8 @@ For GitHub it would be `sha256=`.
|
||||
|
||||
#### Disable Signature Verification
|
||||
|
||||
You can disable signature verification by setting `DisableSignatureVerification` for a service to `true`.
|
||||
You can disable signature verification by setting `DisableSignatureVerification` for a service to
|
||||
`true`.
|
||||
|
||||
You can disable signature verification for all services by setting environment variable
|
||||
`NO_SIGNATURE_VERIFICATION` to `true`.
|
||||
|
Reference in New Issue
Block a user