-
Notifications
You must be signed in to change notification settings - Fork 117
/
main.go
186 lines (158 loc) · 4.27 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package gitserver
import (
"context"
"crypto/tls"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
handlers "agola.io/agola/internal/git-handler"
slog "agola.io/agola/internal/log"
"agola.io/agola/internal/services/config"
"agola.io/agola/internal/util"
"github.com/gorilla/mux"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
errors "golang.org/x/xerrors"
)
var level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
var logger = slog.New(level)
var log = logger.Sugar()
const (
gitSuffix = ".git"
)
func repoPathIsValid(reposDir, repoPath string) (bool, error) {
// a parent cannot end with .git
parts := strings.Split(repoPath, "/")
for _, part := range parts[:len(parts)-1] {
if strings.HasSuffix(part, gitSuffix) {
return false, errors.Errorf("path %q contains a parent directory with .git suffix", repoPath)
}
}
// check that a subdirectory doesn't exists
reposDir, err := filepath.Abs(reposDir)
if err != nil {
return false, err
}
path := repoPath
_, err = os.Stat(path)
if err != nil && !os.IsNotExist(err) {
return false, err
}
if !os.IsNotExist(err) {
// if it exists assume it's valid
return true, nil
}
for {
path = filepath.Dir(path)
if len(path) <= len(reposDir) {
break
}
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
return false, err
}
// a parent path cannot end with .git
if strings.HasSuffix(path, gitSuffix) {
return false, nil
}
if !os.IsNotExist(err) {
// if a parent exists return not valid
return false, nil
}
}
return true, nil
}
func repoExists(repoAbsPath string) (bool, error) {
_, err := os.Stat(repoAbsPath)
if err != nil && !os.IsNotExist(err) {
return false, err
}
return !os.IsNotExist(err), nil
}
func repoAbsPath(reposDir, repoPath string) (string, bool, error) {
valid, err := repoPathIsValid(reposDir, repoPath)
if err != nil {
return "", false, err
}
if !valid {
return "", false, handlers.ErrWrongRepoPath
}
repoFSPath, err := filepath.Abs(filepath.Join(reposDir, repoPath))
if err != nil {
return "", false, err
}
exists, err := repoExists(repoFSPath)
if err != nil {
return "", false, err
}
return repoFSPath, exists, nil
}
func Matcher(matchRegexp *regexp.Regexp) mux.MatcherFunc {
return func(r *http.Request, rm *mux.RouteMatch) bool {
return matchRegexp.MatchString(r.URL.Path)
}
}
type Gitserver struct {
c *config.Gitserver
}
func NewGitserver(c *config.Gitserver) (*Gitserver, error) {
if c.Debug {
level.SetLevel(zapcore.DebugLevel)
}
return &Gitserver{
c: c,
}, nil
}
func (s *Gitserver) Run(ctx context.Context) error {
gitSmartHandler := handlers.NewGitSmartHandler(logger, s.c.DataDir, true, repoAbsPath, nil)
fetchFileHandler := handlers.NewFetchFileHandler(logger, s.c.DataDir, repoAbsPath)
router := mux.NewRouter()
router.MatcherFunc(Matcher(handlers.InfoRefsRegExp)).Handler(gitSmartHandler)
router.MatcherFunc(Matcher(handlers.UploadPackRegExp)).Handler(gitSmartHandler)
router.MatcherFunc(Matcher(handlers.ReceivePackRegExp)).Handler(gitSmartHandler)
router.MatcherFunc(Matcher(handlers.FetchFileRegExp)).Handler(fetchFileHandler)
var tlsConfig *tls.Config
if s.c.Web.TLS {
var err error
tlsConfig, err = util.NewTLSConfig(s.c.Web.TLSCertFile, s.c.Web.TLSKeyFile, "", false)
if err != nil {
log.Errorf("err: %+v")
return err
}
}
httpServer := http.Server{
Addr: s.c.Web.ListenAddress,
Handler: router,
TLSConfig: tlsConfig,
}
lerrCh := make(chan error)
go func() {
lerrCh <- httpServer.ListenAndServe()
}()
select {
case <-ctx.Done():
log.Infof("gitserver exiting")
httpServer.Close()
case err := <-lerrCh:
if err != nil {
log.Errorf("http server listen error: %v", err)
return err
}
}
return nil
}