Skip to content

Commit d695151

Browse files
author
bajins
committed
修改验证仓库方式
1 parent d400f04 commit d695151

File tree

4 files changed

+77
-20
lines changed

4 files changed

+77
-20
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,25 @@
33
支持GitHub、Gitea、Gogs
44

55

6-
> http://127.0.0.1:8000/webHooks?id=test
6+
## Configuration
7+
8+
> URL:http://127.0.0.1:8000/webHooks
9+
10+
```json
11+
{
12+
"这里是仓库full_name的值": {
13+
"logfile": "test-gitea-webhook.log",
14+
"secret": "在Webhooks中设定的secret",
15+
"commands": [
16+
"data/update_repo.sh"
17+
]
18+
}
19+
}
20+
```
21+
22+
## 运行
23+
24+
```bash
25+
# -h为地址(默认0.0.0.0),-p为端口(默认8000)
26+
./webhook-go -h 127.0.0.1 -p 8000
27+
```

data/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"这里是id的值": {
2+
"这里是仓库full_name的值": {
33
"logfile": "test-gitea-webhook.log",
44
"secret": "在Webhooks中设定的secret",
55
"commands": [

server.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package main
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
57
"io/ioutil"
68
"net/http"
79
"os"
10+
"reflect"
811
"strings"
912
"webhook-go/utils"
1013
)
@@ -44,21 +47,58 @@ func webHooks(w http.ResponseWriter, r *http.Request) {
4447
return
4548
}
4649
defer r.Body.Close()
50+
if len(bodyContent) <= 0 {
51+
fmt.Fprintln(w, "{\"code\":200, \"error\":\"Response Body is empty\"}")
52+
return
53+
}
54+
55+
var contentMap map[string]interface{}
4756

48-
// 解析参数,填充到Form、PostForm
49-
r.ParseForm()
50-
id := r.Form["id"][0]
51-
if id == "" || len(id) <= 0 {
52-
fmt.Fprintln(w, "{\"code\":200, \"error\":\"id is empty\"}")
57+
ct := r.Header.Get("Content-Type")
58+
// 针对`Gitea`请使用`v1.10.0-rc2`以下版本issue
59+
// https://github.com/go-gitea/gitea/issues/7700
60+
if ct == "" || len(ct) <= 0 {
61+
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
62+
ct = r.Header.Get("Content-Type")
63+
}
64+
ct = strings.ToLower(ct)
65+
if ct == "application/x-www-form-urlencoded" {
66+
// 恢复Body内容
67+
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyContent))
68+
// 解析参数,填充到Form、PostForm
69+
err = r.ParseForm()
70+
if r.Form == nil || len(r.Form) <= 0 {
71+
fmt.Fprintln(w, "{\"code\":200, \"error\":\"Response Body is empty\"}")
72+
return
73+
}
74+
payload := r.Form["payload"][0]
75+
json.Unmarshal([]byte(payload), &contentMap)
76+
} else if ct == "application/json" {
77+
err = json.Unmarshal(bodyContent, &contentMap)
78+
if err != nil {
79+
fmt.Fprintln(w, "{\"code\":200, \"error\":\"Unmatch Response Body\"}")
80+
return
81+
}
82+
}
83+
if contentMap == nil || contentMap["repository"] == nil {
84+
fmt.Fprintln(w, "{\"code\":200, \"error\":\"Unmatch Response Body\"}")
85+
return
86+
}
87+
id := contentMap["repository"].(map[string]interface{})["full_name"].(string)
88+
//id = strings.ToLower(id)
89+
config := config[id]
90+
if reflect.DeepEqual(config, Config{}) {
91+
fmt.Fprintln(w, "{\"code\":200, \"error\":\"Config is not found\"}")
5392
return
5493
}
5594

56-
if !VerifySignature(r.Header, string(bodyContent), config[id].Secret) {
57-
utils.Log2file("验证失败", config[id].Logfile)
95+
if !VerifySignature(r.Header, string(bodyContent), config.Secret) {
96+
utils.Log2file("验证失败", config.Logfile)
5897
fmt.Fprintln(w, "{\"code\":200, \"error\":\"Signature error\"}")
98+
return
5999
}
60100
fmt.Fprintln(w, "{\"code\":200, \"description\":\"OK\"}")
61-
utils.Log2file("验证通过,启动部署任务", config[id].Logfile)
101+
utils.Log2file("验证通过,启动部署任务", config.Logfile)
62102
AddNewTask(id)
63103
}
64104

@@ -74,7 +114,6 @@ func VerifySignature(header http.Header, data string, secret string) bool {
74114
if signature == "" || len(signature) <= 0 {
75115
signature = header.Get("X-Gogs-Signature")
76116
}
77-
78117
return signature == utils.ComputeHmacSha256(data, secret)
79118
}
80119

@@ -85,10 +124,8 @@ func VerifyEvent(header http.Header, event string) bool {
85124
if e == "" || len(e) <= 0 {
86125
e = header.Get("X-Gogs-Event")
87126
}
88-
89127
if e == "" || len(e) <= 0 {
90128
e = header.Get("X-GitHub-Event")
91129
}
92-
93130
return event == strings.Trim(e, "UTF-8")
94131
}

utils/log2file.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@ var (
1414
func Log2file(content, logName string) {
1515
var err error
1616

17+
if logName == "" || len(logName) <= 0 {
18+
logName = "webhook-go.log"
19+
}
20+
dir, file := path.Split(logName)
21+
logsDir = path.Join(logsDir, dir)
1722
if _, err := os.Stat(logsDir); err != nil {
1823
err = os.MkdirAll(logsDir, 0711)
1924
if err != nil {
2025
return
2126
}
2227
}
23-
24-
if logName == "" || len(logName) <= 0 {
25-
logName = "webhook-go.log"
26-
}
27-
28-
logPath := path.Join(logsDir, logName)
29-
28+
logPath := path.Join(logsDir, file)
3029
if _, err := os.Stat(logPath); err != nil {
3130
_, err = os.Create(logPath)
3231
if err != nil {

0 commit comments

Comments
 (0)