-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
114 lines (94 loc) · 2.65 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
package config
import (
"encoding/json"
"errors"
"os"
)
var c Config
func C() Config {
return c
}
// LoadJSON load config from bytes in JSON format
func LoadJSON(bs []byte) (*Config, error) {
var conf Config
err := json.Unmarshal(bs, &conf)
if err != nil {
return nil, err
}
c = conf
return &conf, nil
}
// LoadFile load config from JSON file
func LoadFile(filename string) (*Config, error) {
bs, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return LoadJSON(bs)
}
type Config struct {
Inbounds Inbounds `json:"inbounds,omitempty"` // 入站代理
Outbounds []*YeagerClient `json:"outbounds,omitempty"` // 出站代理
Rules []string `json:"rules,omitempty"` // 路由规则
// developer only
Debug bool `json:"debug,omitempty"`
// 参考 transport/grpc/pool.go 如何预估连接池大小
GrpcChannelPoolSize int `json:"grpcChannelPoolSize,omitempty"`
}
type Inbounds struct {
SOCKS *struct {
Listen string `json:"listen"`
} `json:"socks,omitempty"`
HTTP *struct {
Listen string `json:"listen"`
} `json:"http,omitempty"`
Yeager *YeagerServer `json:"yeager,omitempty"`
}
type Transport string
const (
TransTCP Transport = "tcp" // plain text, be caution
TransTLS Transport = "tls"
TransGRPC Transport = "grpc"
TransQUIC Transport = "quic"
)
type MutualTLS struct {
CertFile string `json:"certFile"`
CertPEM []byte `json:"-"`
KeyFile string `json:"keyFile"`
KeyPEM []byte `json:"-"`
CAFile string `json:"caFile"`
CAPEM []byte `json:"-"`
}
type YeagerServer struct {
Listen string `json:"listen"`
Transport Transport `json:"transport"`
MutualTLS MutualTLS `json:"mutualTLS,omitempty"` // available when transport is tls, grpc or quic
}
type YeagerClient struct {
Tag string `json:"tag"` // 出站标记,用于路由规则指定出站代理
Address string `json:"address"` // server address to be connected
Transport Transport `json:"transport"`
MutualTLS MutualTLS `json:"mutualTLS,omitempty"` // available when transport is tls, grpc or quic
}
const (
EnvYeagerAddress = "YEAGER_ADDRESS"
EnvYeagerTransport = "YEAGER_TRANSPORT"
)
// Deprecated
// LoadEnv generate configuration from environment variables,
// suitable for server side plaintext
func LoadEnv() (conf *Config, err error) {
address, ok := os.LookupEnv(EnvYeagerAddress)
if !ok {
return nil, errors.New("missing " + EnvYeagerAddress)
}
transport, ok := os.LookupEnv(EnvYeagerTransport)
if !ok {
return nil, errors.New("missing " + EnvYeagerTransport)
}
sc := &YeagerServer{
Listen: address,
Transport: Transport(transport),
}
return &Config{Inbounds: Inbounds{Yeager: sc}}, nil
}