-
Notifications
You must be signed in to change notification settings - Fork 5
/
allowed.go
58 lines (49 loc) · 1.1 KB
/
allowed.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
// Copyright 2021 Changkun Ou. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
import (
"log"
"os"
"strings"
"gopkg.in/yaml.v3"
)
type allowed struct {
Production bool `yaml:"production"`
Domain []string `yaml:"domain"`
GitHub []string `yaml:"github"`
}
func (a *allowed) isAllowed(source string, isDomain bool) bool {
allow := false
if isDomain {
for idx := range a.Domain {
if strings.Contains(source, a.Domain[idx]) {
allow = true
break
}
}
} else {
for idx := range a.GitHub {
if strings.Contains(source, a.GitHub[idx]) {
allow = true
break
}
}
}
return allow
}
var source = &allowed{}
func init() {
d, err := os.ReadFile("./allowed.yml")
if err != nil {
log.Fatalf("failed to load trusted sources: %v", err)
}
err = yaml.Unmarshal(d, source)
if err != nil {
log.Fatalf("failed to parse trusted sources: %v", err)
}
if !source.Production {
source.Domain = append(source.Domain, "http://localhost")
source.Domain = append(source.Domain, "http://0.0.0.0")
}
}