This repository has been archived by the owner on Jun 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
122 lines (94 loc) · 2.27 KB
/
config.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
package config
import (
"encoding/json"
"os"
"github.com/AkinoKaede/naruse/dispatcher"
"github.com/AkinoKaede/naruse/vmess"
"github.com/v2fly/v2ray-core/v4/common/protocol"
"github.com/v2fly/v2ray-core/v4/common/uuid"
)
type Config struct {
Groups []Group `json:"groups"`
}
type Group struct {
Listen string `json:"listen"`
Port int `json:"port"`
TCPFastOpen bool `json:"tcpFastOpen"`
AntiReplay bool `json:"antiReplay"`
Servers []Server `json:"servers"`
}
type Server struct {
Target string `json:"target"`
ID []string `json:"id"`
TCPFastOpen bool `json:"tcpFastOpen"`
}
func BuildConfig(path string) (*Config, error) {
config := new(Config)
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if err = json.Unmarshal(b, config); err != nil {
return nil, err
}
return config, nil
}
func (c *Config) Build() ([]*dispatcher.Dispatcher, error) {
dispatchers := make([]*dispatcher.Dispatcher, len(c.Groups))
for i, g := range c.Groups {
dispatcher, err := g.Build()
if err != nil {
return nil, err
}
dispatchers[i] = dispatcher
}
return dispatchers, nil
}
func (g *Group) Build() (*dispatcher.Dispatcher, error) {
accounts, err := g.AsAccounts()
if err != nil {
return nil, err
}
validator := &vmess.Validator{
AuthIDMatcher: vmess.NewAuthIDMatchers[g.AntiReplay](),
}
for _, account := range accounts {
validator.Add(account)
}
return &dispatcher.Dispatcher{
ListenAddr: g.Listen,
Port: g.Port,
TCPFastOpen: g.TCPFastOpen,
Validator: validator,
}, nil
}
func (g *Group) AsAccounts() ([]*vmess.Account, error) {
accounts := make([]*vmess.Account, 0)
for _, s := range g.Servers {
serverAccounts, err := s.AsAccounts()
if err != nil {
return nil, err
}
accounts = append(accounts, serverAccounts...)
}
return accounts, nil
}
func (s *Server) AsAccounts() ([]*vmess.Account, error) {
accounts := make([]*vmess.Account, len(s.ID))
for i, id := range s.ID {
uuid, err := uuid.ParseString(id)
if err != nil {
return nil, err
}
vID := protocol.NewID(uuid)
account := &vmess.Account{
ID: vID,
Server: &vmess.Server{
Target: s.Target,
TCPFastOpen: s.TCPFastOpen,
},
}
accounts[i] = account
}
return accounts, nil
}