11package main
22
33import (
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}
0 commit comments