-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
198 lines (178 loc) · 4.69 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
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
package config
import (
"encoding/json"
"errors"
"fmt"
"net"
"os"
"strings"
"github.com/chenen3/yeager/cert"
)
// tunnel type
const (
TunGRPC = "grpc"
TunQUIC = "quic"
TunHTTP2 = "http2"
)
// Load loads configuration from the given bytes
func Load(bs []byte) (Config, error) {
var c Config
if err := json.Unmarshal(bs, &c); err != nil {
return c, err
}
// backward compatible
for i := range c.TunnelClients {
tc := &c.TunnelClients[i]
if tc.Name == "" && tc.Policy != "" {
tc.Name = tc.Policy
}
}
return c, nil
}
type Config struct {
SOCKSListen string `json:"socksListen,omitempty"`
HTTPListen string `json:"httpListen,omitempty"`
TunnelListens []TunnelListen `json:"tunnelListens,omitempty"`
TunnelClients []TunnelClient `json:"tunnelClients,omitempty"`
Rules []string `json:"rules,omitempty"`
// enable debug logging, start HTTP server for profiling
Debug bool `json:"debug,omitempty"`
}
func mergeLine(s []string) string {
return strings.Join(s, "\n")
}
func splitLine(s string) []string {
return strings.Split(strings.TrimSpace(s), "\n")
}
type TunnelListen struct {
Type string `json:"type"`
Listen string `json:"listen"`
CertFile string `json:"certFile,omitempty"`
CertPEM []string `json:"certPEM,omitempty"`
KeyFile string `json:"keyFile,omitempty"`
KeyPEM []string `json:"keyPEM,omitempty"`
CAFile string `json:"caFile,omitempty"`
CAPEM []string `json:"caPEM,omitempty"`
}
func (tl *TunnelListen) GetCertPEM() ([]byte, error) {
if tl.CertPEM != nil {
return []byte(mergeLine(tl.CertPEM)), nil
}
if tl.CertFile != "" {
return os.ReadFile(tl.CertFile)
}
return nil, errors.New("no PEM data nor file")
}
func (tl *TunnelListen) GetKeyPEM() ([]byte, error) {
if tl.KeyPEM != nil {
return []byte(mergeLine(tl.KeyPEM)), nil
}
if tl.KeyFile != "" {
return os.ReadFile(tl.KeyFile)
}
return nil, errors.New("no PEM data nor file")
}
func (tl *TunnelListen) GetCAPEM() ([]byte, error) {
if tl.CAPEM != nil {
return []byte(mergeLine(tl.CAPEM)), nil
}
if tl.CAFile != "" {
return os.ReadFile(tl.CAFile)
}
return nil, errors.New("no PEM data nor file")
}
type TunnelClient struct {
Name string `json:"name"`
// deprecated, use Name instead
Policy string `json:"policy,omitempty"`
Type string `json:"type"`
Address string `json:"address"` // target server address
CertFile string `json:"certFile,omitempty"`
CertPEM []string `json:"certPEM,omitempty"`
KeyFile string `json:"keyFile,omitempty"`
KeyPEM []string `json:"keyPEM,omitempty"`
CAFile string `json:"caFile,omitempty"`
CAPEM []string `json:"caPEM,omitempty"`
MaxStreamsPerConn int `json:"maxStreamsPerConn,omitempty"`
}
func (tc *TunnelClient) GetCertPEM() ([]byte, error) {
if tc.CertPEM != nil {
return []byte(mergeLine(tc.CertPEM)), nil
}
if tc.CertFile != "" {
return os.ReadFile(tc.CertFile)
}
return nil, errors.New("no PEM data nor file")
}
func (tc *TunnelClient) GetKeyPEM() ([]byte, error) {
if tc.KeyPEM != nil {
return []byte(mergeLine(tc.KeyPEM)), nil
}
if tc.KeyFile != "" {
return os.ReadFile(tc.KeyFile)
}
return nil, errors.New("no PEM data nor file")
}
func (tc *TunnelClient) GetCAPEM() ([]byte, error) {
if tc.CAPEM != nil {
return []byte(mergeLine(tc.CAPEM)), nil
}
if tc.CAFile != "" {
return os.ReadFile(tc.CAFile)
}
return nil, errors.New("no PEM data nor file")
}
func allocPort() (int, error) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return 0, err
}
ln.Close()
return ln.Addr().(*net.TCPAddr).Port, nil
}
// Generate generates a pair of client and server configuration for the given host
func Generate(host string) (cli, srv Config, err error) {
cert, err := cert.Generate(host)
if err != nil {
return
}
tunnelPort, err := allocPort()
if err != nil {
return
}
srv = Config{
TunnelListens: []TunnelListen{
{
Listen: fmt.Sprintf("0.0.0.0:%d", tunnelPort),
Type: TunHTTP2,
CAPEM: splitLine(string(cert.RootCert)),
CertPEM: splitLine(string(cert.ServerCert)),
KeyPEM: splitLine(string(cert.ServerKey)),
},
},
}
socksProxyPort, err := allocPort()
if err != nil {
return
}
httpProxyPort, err := allocPort()
if err != nil {
return
}
cli = Config{
SOCKSListen: fmt.Sprintf("127.0.0.1:%d", socksProxyPort),
HTTPListen: fmt.Sprintf("127.0.0.1:%d", httpProxyPort),
TunnelClients: []TunnelClient{
{
Name: "proxy",
Address: fmt.Sprintf("%s:%d", host, tunnelPort),
Type: TunHTTP2,
CAPEM: splitLine(string(cert.RootCert)),
CertPEM: splitLine(string(cert.ClientCert)),
KeyPEM: splitLine(string(cert.ClientKey)),
},
},
Rules: []string{"final,proxy"},
}
return cli, srv, nil
}