forked from PuerNya/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
571 lines (532 loc) · 14.6 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package main
import (
"bufio"
"context"
"io"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
R "github.com/juju/ratelimit"
"github.com/sagernet/fswatch"
L "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
"github.com/spf13/cobra"
)
var log = L.NewDefaultFactory(
context.Background(),
L.Formatter{
BaseTime: time.Now(),
FullTimestamp: true,
TimestampFormat: "-0700 2006-01-02 15:04:05",
},
os.Stdout,
"",
nil,
false,
).Logger()
var client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
var (
disableColor bool
runningPort int
domainListPath string
blacklistPath string
bandwidthLimit int
denyWebPage bool
denyWebPageList []string
)
var BandwidthLimiter *R.Bucket
var Blacklist []RepoInfo
var AcceptDomain = []string{
"github.com",
"raw.github.com",
"raw.githubusercontent.com",
"gist.github.com",
"objects.githubusercontent.com",
"gist.githubusercontent.com",
"codeload.github.com",
"api.github.com",
}
var command = &cobra.Command{
Use: "git-proxy",
Short: "A HTTP service to proxy git requests",
Run: run,
}
func init() {
command.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output")
command.PersistentFlags().IntVarP(&runningPort, "running-port", "p", 30000, "disable color output")
command.PersistentFlags().StringVarP(&domainListPath, "domain-list-path", "d", "domainlist.txt", "set accept domain")
command.PersistentFlags().StringVarP(&blacklistPath, "blacklist-path", "b", "blacklist.txt", "set repository blacklist")
command.PersistentFlags().IntVarP(&bandwidthLimit, "bandwidth-limit", "l", 0, "set total bandwidth limit (MB/s), 0 as no limit")
command.PersistentFlags().BoolVarP(&denyWebPage, "deny-web-page", "", false, "deny web page requests")
command.PersistentFlags().StringSliceVarP(&denyWebPageList, "deny-web-page-list", "", []string{"github.com", "gist.github.com"}, "deny web page requests list")
}
func main() {
if err := command.Execute(); err != nil {
log.Fatal(err)
}
}
type HTTPError struct {
Message string `json:"message"`
Example string `json:"example"`
}
func (e *HTTPError) Error() string {
return e.Message
}
func newError(msg string) *HTTPError {
return &HTTPError{
Message: msg,
Example: "https://abc.com/https://github.com/github/docs.git",
}
}
func run(*cobra.Command, []string) {
if bandwidthLimit > 0 {
BandwidthLimiter = R.NewBucketWithRate(float64(bandwidthLimit*1024*1024), int64(bandwidthLimit*1024*1024))
log.Info("Bandwidth limit is set as ", bandwidthLimit, "MB/s")
}
if denyWebPage && len(denyWebPageList) > 0 {
log.Info("Denying web page requests, domain list: [", strings.Join(denyWebPageList, ", "), "]")
}
if watcher, err := loadDomainList(); err == nil {
err = watcher.Start()
if err == nil {
log.Info("Watching accept domain list")
defer watcher.Close()
} else {
log.Error(E.Cause(err, "Start watch accept domain list"))
watcher.Close()
}
}
if watcher, err := loadBlackList(); err == nil {
err = watcher.Start()
if err == nil {
log.Info("Watching repository blacklist")
defer watcher.Close()
} else {
log.Error(E.Cause(err, "Start watch repository blacklist"))
watcher.Close()
}
}
listen := M.ParseSocksaddr(":" + strconv.Itoa(runningPort))
listener := listenTCP(listen)
chiRouter := chi.NewRouter()
chiRouter.Group(func(r chi.Router) {
r.Use(middleware.RealIP)
r.Use(setContext)
r.Use(commonLog)
r.Get("/", hello)
r.Mount("/", finalHandle())
})
server := &http.Server{
Addr: listener.Addr().String(),
Handler: chiRouter,
}
go func() {
err := server.Serve(listener)
if err != nil {
log.Fatal(err)
}
}()
log.Info("Start http serve success")
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
defer signal.Stop(osSignals)
<-osSignals
}
type FileReader struct {
LineChan chan string
CloseSignal chan struct{}
}
func NewFileReader(path string) (*FileReader, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
reader := FileReader{
LineChan: make(chan string),
CloseSignal: make(chan struct{}),
}
go func() {
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
lr := []rune(line)
if lr[0] == '#' || (len(lr) > 1 && lr[0] == '/' && lr[1] == '/') {
continue
}
for i, r := range lr {
if r == '#' || (r == '/' && i < len(lr)-1 && lr[i+1] == '/') {
line = strings.TrimSpace(string(lr[:i]))
break
}
}
reader.LineChan <- line
}
reader.CloseSignal <- struct{}{}
}()
return &reader, nil
}
func (r *FileReader) Close() {
close(r.LineChan)
close(r.CloseSignal)
}
func loadDomainList() (*fswatch.Watcher, error) {
err := loadDomainListData()
if err != nil {
return nil, err
}
watcher, err := fswatch.NewWatcher(fswatch.Options{
Path: []string{domainListPath},
Callback: func(path string) {
log.Info("Accept domain list changed, reloading")
loadDomainListData()
},
})
if err != nil {
log.Error(E.Cause(err, "Create accept domain list watcher"))
return nil, err
}
return watcher, nil
}
func loadDomainListData() error {
reader, err := NewFileReader(domainListPath)
if err != nil {
return err
}
var domainList []string
var needBreak bool
for {
if needBreak {
break
}
select {
case <-reader.CloseSignal:
needBreak = true
continue
case line := <-reader.LineChan:
if net.ParseIP(line) != nil {
continue
}
domainList = append(domainList, line)
}
}
if len(domainList) > 0 {
AcceptDomain = domainList
log.Info("Custom accept domain list loaded")
} else {
log.Warn("Custom accept domain list is empty")
}
return nil
}
func loadBlackList() (*fswatch.Watcher, error) {
err := loadBlackListData()
if err != nil {
return nil, err
}
path, _ := filepath.Abs(blacklistPath)
watcher, err := fswatch.NewWatcher(fswatch.Options{
Path: []string{path},
Callback: func(path string) {
log.Info("Repository blacklist changed, reloading")
loadBlackListData()
},
})
if err != nil {
log.Error(E.Cause(err, "Create repository blacklist watcher"))
return nil, err
}
return watcher, nil
}
func loadBlackListData() error {
reader, err := NewFileReader(blacklistPath)
if err != nil {
return err
}
var blacklist []RepoInfo
for {
var needBreak bool
select {
case line := <-reader.LineChan:
if !common.Any([]rune(line), func(it rune) bool {
return it == '/'
}) {
continue
}
splited := strings.Split(line, "/")
user := splited[0]
repo := splited[1]
if user == "" {
user = "*"
}
if repo == "" {
repo = "*"
} else if strings.HasSuffix(repo, ".git") {
repo = repo[:len(repo)-4]
}
blacklist = append(blacklist, RepoInfo{user, repo})
case <-reader.CloseSignal:
needBreak = true
}
if needBreak {
break
}
}
if len(blacklist) > 0 {
Blacklist = blacklist
log.Info("Custom repository blacklist loaded")
} else {
log.Warn("Custom repository blacklist is empty")
}
return nil
}
type RepoInfo struct {
User string
Repo string
}
func (r *RepoInfo) Match(user string, repo string) bool {
return EasyWildcardMatch(strings.ToLower(user), strings.ToLower(r.User)) && EasyWildcardMatch(strings.ToLower(repo), strings.ToLower(r.Repo))
}
func EasyWildcardMatch(s string, p string) bool {
if p == "*" || (s == "" && p == "") {
return true
}
if s == "" || p == "" {
return false
}
pr := []rune(p)
sr := []rune(s)
var nextS, nextP string
if len(pr) > 1 {
nextP = string(pr[1:])
}
if len(sr) > 1 {
nextS = string(sr[1:])
}
if pr[0] == '*' {
return EasyWildcardMatch(s, nextP) || EasyWildcardMatch(nextS, nextP) || EasyWildcardMatch(nextS, p)
} else if pr[0] == '?' {
return EasyWildcardMatch(nextS, nextP)
} else {
return sr[0] == pr[0] && EasyWildcardMatch(nextS, nextP)
}
}
func listenTCP(address M.Socksaddr) net.Listener {
var listener net.Listener
for {
var err error
listener, err = net.Listen("tcp", address.String())
if err == nil {
break
}
address.Port = address.Port + 1
}
log.Info("Listening tcp port ", address.Port)
return listener
}
func hello(w http.ResponseWriter, r *http.Request) {
render.Status(r, http.StatusOK)
render.PlainText(w, r, "Hello to visit git-proxy")
}
func setContext(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(L.ContextWithNewID(r.Context())))
})
}
func commonLog(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.InfoContext(r.Context(), "New ", r.Method, " request from ", r.RemoteAddr, " to ", r.URL.RequestURI())
next.ServeHTTP(w, r)
})
}
func finalHandle() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
finalHandler(r).ServeHTTP(w, r)
})
}
func finalHandler(r *http.Request) http.Handler {
requestURIURL, err := url.Parse(r.URL.RequestURI()[1:])
if err != nil {
return responseWithError(E.Cause(err, "Parse request uri as url"))
}
if common.Any(AcceptDomain, func(it string) bool {
return it == requestURIURL.Host
}) {
if len(requestURIURL.Path) < 2 {
return sendRequestWithURL(requestURIURL)
}
splited := strings.Split(requestURIURL.Path[1:], "/")
var user, repo string
if len(splited) == 0 || (len(splited) == 1 && len(splited[0]) == 0) {
return sendRequestWithURL(requestURIURL)
}
user = splited[0]
if len(splited) > 1 {
repo = splited[1]
}
if repo == "" {
log.InfoContext(r.Context(), "Found user: ", user)
} else {
log.InfoContext(r.Context(), "Found user: ", user, " repository: ", repo)
}
if common.Any(Blacklist, func(it RepoInfo) bool {
result := it.Match(strings.ToLower(user), strings.ToLower(repo))
if result {
log.InfoContext(r.Context(), "Match blocked repository: ", it.User, "/", it.Repo)
}
return result
}) {
return responseWithWarn("Blocked repository")
} else {
return sendRequestWithURL(requestURIURL)
}
}
if r.Referer() != "" {
rawRefererURL, err := url.Parse(r.Referer())
if err != nil {
return responseWithError(E.Cause(err, "Parse referer url"))
}
refererURL, err := url.Parse(rawRefererURL.RequestURI()[1:])
if err != nil {
return responseWithError(E.Cause(err, "Parse referer url request uri as url"))
}
if common.Any(AcceptDomain, func(it string) bool {
return it == refererURL.Host
}) {
finalURL, err := refererURL.Parse(r.URL.RequestURI())
if err != nil {
return responseWithError(E.Cause(err, "Parse request uri as path with referer url"))
}
return responseWithRedirect(finalURL)
}
}
if requestURIURL.Scheme == "" {
return responseWithError(E.New("URL scheme request"))
}
if requestURIURL.Host == "" {
return responseWithError(E.New("URL host request"))
}
return responseWithError(E.New("Unsupported url host"))
}
func responseWithWarn(msg string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.WarnContext(r.Context(), msg)
render.Status(r, http.StatusInternalServerError)
render.PlainText(w, r, msg)
})
}
func responseWithError(err error) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.ErrorContext(r.Context(), err)
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError(err.Error()))
})
}
func responseWithRedirect(URL *url.URL) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.InfoContext(r.Context(), "Success redirect request: ", r.URL.RequestURI(), " to: /", URL.String())
w.Header().Set("Location", "/"+URL.String())
w.WriteHeader(http.StatusTemporaryRedirect)
})
}
var _ io.Reader = (*LimitReader)(nil)
type LimitReader struct {
reader io.Reader
bucket *R.Bucket
}
func NewLimitReader(reader io.Reader, bucket *R.Bucket) *LimitReader {
return &LimitReader{
reader: reader,
bucket: bucket,
}
}
func (lr *LimitReader) Read(p []byte) (int, error) {
sliceLen := int64(len(p))
available := lr.bucket.TakeAvailable(sliceLen)
if available == 0 {
return 0, nil
}
if available == sliceLen {
return lr.reader.Read(p)
}
temp := make([]byte, available)
defer copy(p, temp)
return lr.reader.Read(temp)
}
func sendRequestWithURL(URL *url.URL) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
request, err := http.NewRequest(r.Method, URL.String(), r.Body)
if err != nil {
responseWithError(E.Cause(err, "Build request")).ServeHTTP(w, r)
return
}
for key, values := range r.Header {
if key == "Host" {
continue
}
delete(request.Header, key)
for _, value := range values {
request.Header.Add(key, value)
}
}
request.URL.User = r.URL.User
request.URL.RawQuery = r.URL.RawQuery
request.URL.Fragment = r.URL.Fragment
request.URL.RawFragment = r.URL.RawFragment
request.Header = r.Header
response, err := client.Do(request)
if err != nil {
responseWithError(E.Cause(err, "Send request")).ServeHTTP(w, r)
return
}
defer response.Body.Close()
if denyWebPage && len(denyWebPageList) > 0 && response.StatusCode == http.StatusOK && strings.Contains(strings.ToLower(response.Header.Get("Content-Type")), "text/html") && common.Any(denyWebPageList, func(it string) bool {
return strings.ToLower(it) == strings.ToLower(URL.Host)
}) {
responseWithError(E.New("Refuse to serve web page")).ServeHTTP(w, r)
return
}
isRedirectResponse := common.Any([]int{http.StatusMovedPermanently, http.StatusFound, http.StatusTemporaryRedirect, http.StatusPermanentRedirect}, func(it int) bool {
return it == response.StatusCode
})
for key, values := range response.Header {
delete(w.Header(), key)
for _, value := range values {
if isRedirectResponse && key == "Location" && len(value) > 0 && []rune(value)[0] != '/' {
if locationURL, err := url.Parse(value); err == nil && common.Any(AcceptDomain, func(it string) bool {
return it == locationURL.Host
}) {
value = "/" + value
}
}
w.Header().Add(key, value)
}
}
w.WriteHeader(response.StatusCode)
if BandwidthLimiter != nil {
io.Copy(w, NewLimitReader(response.Body, BandwidthLimiter))
} else {
io.Copy(w, response.Body)
}
log.InfoContext(ctx, "Success proxy request: ", URL, " , method: ", request.Method, " , status: ", response.StatusCode)
})
}