-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
97 lines (86 loc) · 2.62 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// maumirror - A GitHub repo mirroring system using webhooks.
// Copyright (C) 2021 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"errors"
"io"
"net/http"
"github.com/go-playground/webhooks/v6/github"
"github.com/go-playground/webhooks/v6/gitlab"
log "maunium.net/go/maulogger/v2"
)
func checkGLToken(r *http.Request, projectID int64) (repo *CIRepository, err error, code int) {
repo, ok := config.CIRepositories[projectID]
if !ok {
code = http.StatusNotFound
err = errors.New("unknown repository")
return
}
token := r.Header.Get("X-Gitlab-Token")
if token != repo.Secret {
code = http.StatusUnauthorized
err = gitlab.ErrGitLabTokenVerificationFailed
return
}
return repo, nil, http.StatusOK
}
func checkSig(r *http.Request, repoName string) (repo *Repository, err error, code int) {
signature := r.Header.Get("X-Hub-Signature")
if len(signature) == 0 {
code = http.StatusUnauthorized
err = github.ErrMissingHubSignatureHeader
return
}
repo, ok := config.Repositories[repoName]
if !ok {
code = http.StatusNotFound
err = errors.New("unknown repository")
return
}
mac := hmac.New(sha1.New, []byte(repo.Secret))
payload, err := io.ReadAll(r.Body)
if err != nil || len(payload) == 0 {
code = http.StatusBadRequest
err = github.ErrParsingPayload
return
}
_, _ = mac.Write(payload)
expectedMAC := hex.EncodeToString(mac.Sum(nil))
if !hmac.Equal([]byte(signature[5:]), []byte(expectedMAC)) {
code = http.StatusUnauthorized
err = github.ErrHMACVerificationFailed
}
code = http.StatusOK
return
}
func readUserIP(r *http.Request) string {
var ip string
if config.Server.TrustForwardHeaders {
ip = r.Header.Get("X-Forwarded-For")
}
if ip == "" {
ip = r.RemoteAddr
}
return ip
}
func respondErr(w http.ResponseWriter, r *http.Request, err error, status int) {
log.Errorfln("Failed to handle request from %s: %v", readUserIP(r), err.Error())
w.WriteHeader(status)
_, _ = w.Write([]byte(err.Error()))
}