|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "webhook-go/utils" |
| 10 | +) |
| 11 | + |
| 12 | +// 启动服务 |
| 13 | +func StartService(address string, port string) error { |
| 14 | + http.HandleFunc("/", index) |
| 15 | + http.HandleFunc("/webHooks", webHooks) |
| 16 | + |
| 17 | + utils.Log2file(fmt.Sprintf("service starting... %s:%s", address, port), "") |
| 18 | + return http.ListenAndServe(fmt.Sprintf("%s:%s", address, port), nil) |
| 19 | +} |
| 20 | + |
| 21 | +// 首页 |
| 22 | +func index(w http.ResponseWriter, r *http.Request) { |
| 23 | + utils.Log2file(string(r.URL.Host), "") |
| 24 | + fmt.Fprintln(w, "{\"code\":200, \"description\":\"service running...\"}") |
| 25 | +} |
| 26 | + |
| 27 | +// 自动编译 |
| 28 | +func webHooks(w http.ResponseWriter, r *http.Request) { |
| 29 | + if err := LoadConfig(); err != nil { |
| 30 | + utils.Log2file(err.Error(), "") |
| 31 | + os.Exit(1) |
| 32 | + } |
| 33 | + if strings.ToUpper(r.Method) != "POST" { |
| 34 | + fmt.Fprintln(w, "{\"code\":200, \"error\":\"Error Method or unknow request url\"}") |
| 35 | + return |
| 36 | + } |
| 37 | + if !VerifyEvent(r.Header, "push") { |
| 38 | + fmt.Fprintln(w, "{\"code\":200, \"error\":\"Unmatch x-github-event\"}") |
| 39 | + return |
| 40 | + } |
| 41 | + bodyContent, err := ioutil.ReadAll(r.Body) |
| 42 | + if err != nil { |
| 43 | + fmt.Fprintln(w, "{\"code\":200, \"error\":\"Error Response Body\"}") |
| 44 | + return |
| 45 | + } |
| 46 | + defer r.Body.Close() |
| 47 | + |
| 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\"}") |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + if !VerifySignature(r.Header, string(bodyContent), config[id].Secret) { |
| 57 | + utils.Log2file("验证失败", config[id].Logfile) |
| 58 | + fmt.Fprintln(w, "{\"code\":200, \"error\":\"Signature error\"}") |
| 59 | + } |
| 60 | + fmt.Fprintln(w, "{\"code\":200, \"description\":\"OK\"}") |
| 61 | + utils.Log2file("验证通过,启动部署任务", config[id].Logfile) |
| 62 | + AddNewTask(id) |
| 63 | +} |
| 64 | + |
| 65 | +// 验证Signature |
| 66 | +func VerifySignature(header http.Header, data string, secret string) bool { |
| 67 | + signature := header.Get("X-Hub-Signature") |
| 68 | + if signature != "" && len(signature) > 0 { |
| 69 | + signature = strings.Split(signature, "=")[0] |
| 70 | + return signature == utils.ComputeHash1(data, secret) |
| 71 | + } |
| 72 | + signature = header.Get("X-Gitea-Signature") |
| 73 | + |
| 74 | + if signature == "" || len(signature) <= 0 { |
| 75 | + signature = header.Get("X-Gogs-Signature") |
| 76 | + } |
| 77 | + |
| 78 | + return signature == utils.ComputeHmacSha256(data, secret) |
| 79 | +} |
| 80 | + |
| 81 | +// 验证Event |
| 82 | +func VerifyEvent(header http.Header, event string) bool { |
| 83 | + e := header.Get("X-Gitea-Event") |
| 84 | + |
| 85 | + if e == "" || len(e) <= 0 { |
| 86 | + e = header.Get("X-Gogs-Event") |
| 87 | + } |
| 88 | + |
| 89 | + if e == "" || len(e) <= 0 { |
| 90 | + e = header.Get("X-GitHub-Event") |
| 91 | + } |
| 92 | + |
| 93 | + return event == strings.Trim(e, "UTF-8") |
| 94 | +} |
0 commit comments