forked from MetaCubeX/geo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2ray_site.go
137 lines (130 loc) 路 4.15 KB
/
v2ray_site.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
package convert
import (
"io"
"sort"
"strings"
"github.com/d2184/geo/encoding/clashrule"
"github.com/d2184/geo/encoding/singgeo"
"github.com/d2184/geo/encoding/v2raygeo"
"github.com/sagernet/sing/common"
"gopkg.in/yaml.v3"
)
// V2RaySiteToSing is modified from https://github.com/SagerNet/sing-geosite
func V2RaySiteToSing(geositeList []*v2raygeo.GeoSite, output io.Writer) error {
domainMap := make(map[string][]singgeo.Item, len(geositeList))
for _, vGeositeEntry := range geositeList {
code := strings.ToLower(vGeositeEntry.CountryCode)
domains := make([]singgeo.Item, 0, len(vGeositeEntry.Domain)*2)
attributes := make(map[string][]*v2raygeo.Domain)
for _, domain := range vGeositeEntry.Domain {
if len(domain.Attribute) > 0 {
for _, attribute := range domain.Attribute {
attributes[attribute.Key] = append(attributes[attribute.Key], domain)
}
}
switch domain.Type {
case v2raygeo.Domain_Plain:
domains = append(domains, singgeo.Item{
Type: singgeo.RuleTypeDomainKeyword,
Value: domain.Value,
})
case v2raygeo.Domain_Regex:
domains = append(domains, singgeo.Item{
Type: singgeo.RuleTypeDomainRegex,
Value: domain.Value,
})
case v2raygeo.Domain_Domain:
if strings.Contains(domain.Value, ".") {
domains = append(domains, singgeo.Item{
Type: singgeo.RuleTypeDomain,
Value: domain.Value,
})
}
domains = append(domains, singgeo.Item{
Type: singgeo.RuleTypeDomainSuffix,
Value: "." + domain.Value,
})
case v2raygeo.Domain_Full:
domains = append(domains, singgeo.Item{
Type: singgeo.RuleTypeDomain,
Value: domain.Value,
})
}
}
domainMap[code] = common.Uniq(domains)
for attribute, attributeEntries := range attributes {
attributeDomains := make([]singgeo.Item, 0, len(attributeEntries)*2)
for _, domain := range attributeEntries {
switch domain.Type {
case v2raygeo.Domain_Plain:
attributeDomains = append(attributeDomains, singgeo.Item{
Type: singgeo.RuleTypeDomainKeyword,
Value: domain.Value,
})
case v2raygeo.Domain_Regex:
attributeDomains = append(attributeDomains, singgeo.Item{
Type: singgeo.RuleTypeDomainRegex,
Value: domain.Value,
})
case v2raygeo.Domain_Domain:
if strings.Contains(domain.Value, ".") {
attributeDomains = append(attributeDomains, singgeo.Item{
Type: singgeo.RuleTypeDomain,
Value: domain.Value,
})
}
attributeDomains = append(attributeDomains, singgeo.Item{
Type: singgeo.RuleTypeDomainSuffix,
Value: "." + domain.Value,
})
case v2raygeo.Domain_Full:
attributeDomains = append(attributeDomains, singgeo.Item{
Type: singgeo.RuleTypeDomain,
Value: domain.Value,
})
}
}
domainMap[code+"@"+attribute] = common.Uniq(attributeDomains)
}
}
return singgeo.Write(output, domainMap)
}
func V2RayToYamlByCode(geositeList []*v2raygeo.GeoSite, output io.Writer, targetCode string) error {
domainMap := make(map[string][]string, len(geositeList))
for _, vGeositeEntry := range geositeList {
code := strings.ToLower(vGeositeEntry.CountryCode)
if strings.EqualFold(code, targetCode) {
domains := make([]string, 0, len(vGeositeEntry.Domain)*2)
attributes := make(map[string][]*v2raygeo.Domain)
for _, domain := range vGeositeEntry.Domain {
if len(domain.Attribute) > 0 {
for _, attribute := range domain.Attribute {
attributes[attribute.Key] = append(attributes[attribute.Key], domain)
}
}
ruleType := clashrule.GetClashRule(domain.Type)
switch domain.Type {
case v2raygeo.Domain_Plain:
domains = append(domains, ruleType+domain.Value)
case v2raygeo.Domain_Domain:
domains = append(domains, ruleType+domain.Value)
case v2raygeo.Domain_Full:
domains = append(domains, ruleType+domain.Value)
case v2raygeo.Domain_Regex:
continue
}
}
sort.Strings(domains)
domainMap[targetCode] = common.Uniq(domains)
}
}
yamlOutput := map[string]any{
"payload": domainMap[targetCode],
}
yamlBytes, err := yaml.Marshal(yamlOutput)
if err != nil {
return err
}
_, err = output.Write(yamlBytes)
return err
}