-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkindling.go
204 lines (178 loc) · 6.1 KB
/
kindling.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
199
200
201
202
203
204
package kindling
import (
"context"
"embed"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"net/url"
"os"
"time"
"github.com/Jigsaw-Code/outline-sdk/transport"
"github.com/Jigsaw-Code/outline-sdk/x/smart"
"github.com/getlantern/fronted"
)
var log *slog.Logger
// Kindling is the interface that wraps the basic Dial and DialContext methods for control
// plane traffic.
type Kindling interface {
// NewHTTPClient returns a new HTTP client that is configured to use kindling.
NewHTTPClient() *http.Client
}
type httpDialer func(ctx context.Context, addr string) (http.RoundTripper, error)
type kindling struct {
httpDialers []httpDialer
logWriter io.Writer
}
// Make sure that kindling implements the Kindling interface.
var _ Kindling = &kindling{}
// Option is a functional option type that allows us to configure the Client.
type Option func(*kindling)
// NewKindling returns a new Kindling.
func NewKindling(options ...Option) Kindling {
k := &kindling{}
k.logWriter = os.Stdout
// Apply all the functional options to configure the client.
for _, opt := range options {
opt(k)
}
log = slog.New(slog.NewTextHandler(k.logWriter, &slog.HandlerOptions{}))
return k
}
// NewHTTPClient implements the Kindling interface.
func (k *kindling) NewHTTPClient() *http.Client {
// Create a specialized HTTP transport that concurrently races between fronted and smart dialer.
// All options are tried in parallel and the first one to succeed is used.
// If all options fail, the last error is returned.
return &http.Client{
Transport: k.newRaceTransport(),
}
}
// WithDomainFronting is a functional option that enables domain fronting for the Kindling.
func WithDomainFronting(configURL, countryCode string) Option {
return func(k *kindling) {
frontedDialer, err := k.newFrontedDialer(configURL, countryCode)
if err != nil {
log.Error("Failed to create fronted dialer", "error", err)
return
}
k.httpDialers = append(k.httpDialers, frontedDialer)
}
}
// WithDoHTunnel is a functional option that enables DNS over HTTPS (DoH) tunneling for the Kindling.
func WithDoHTunnel() Option {
return func(k *kindling) {
}
}
// WithLogWriter is a functional option that sets the log writer for the Kindling.
// By default, the log writer is set to os.Stdout.
// This should be the first option to be applied to the Kindling to ensure that all logs are captured.
func WithLogWriter(w io.Writer) Option {
return func(k *kindling) {
k.logWriter = w
}
}
// WithProxyless is a functional option that enables proxyless mode for the Kindling such that
// it accesses the control plane directly using a variety of proxyless techniques.
func WithProxyless(domains ...string) Option {
return func(k *kindling) {
smartDialer, err := k.newSmartHTTPDialer(domains...)
if err != nil {
log.Error("Failed to create smart dialer", "error", err)
return
}
k.httpDialers = append(k.httpDialers, smartDialer)
}
}
func (k *kindling) newRaceTransport() http.RoundTripper {
// Now create a RoundTripper that races between the available options.
return newRaceTransport(k.httpDialers...)
}
func (k *kindling) newFrontedDialer(configURL, countryCode string) (httpDialer, error) {
// Parse the domain from the URL.
u, err := url.Parse(configURL)
if err != nil {
log.Error("Failed to parse URL", "error", err)
return nil, fmt.Errorf("failed to parse URL: %v", err)
}
// Extract the domain from the URL.
domain := u.Host
// First, download the file from the specified URL using the smart dialer.
// Then, create a new fronted instance with the downloaded file.
trans, err := k.newSmartHTTPTransport(domain)
if err != nil {
log.Error("Failed to create smart HTTP transport", "error", err)
return nil, fmt.Errorf("failed to create smart HTTP transport: %v", err)
}
httpClient := &http.Client{
Transport: trans,
}
fr := fronted.NewFronted(
fronted.WithHTTPClient(httpClient),
fronted.WithConfigURL(configURL),
fronted.WithCountryCode(countryCode),
)
return fr.NewConnectedRoundTripper, nil
}
func (k *kindling) newSmartHTTPDialer(domains ...string) (httpDialer, error) {
d, err := k.newSmartDialer(domains...)
if err != nil {
return nil, fmt.Errorf("failed to create smart dialer: %v", err)
}
return func(ctx context.Context, addr string) (http.RoundTripper, error) {
streamConn, err := d.DialStream(ctx, addr)
if err != nil {
return nil, fmt.Errorf("failed to dial stream: %v", err)
}
return k.newTransportWithDialContext(func(ctx context.Context, network, addr string) (net.Conn, error) {
return streamConn, nil
}), nil
}, nil
}
func (k *kindling) newSmartHTTPTransport(domains ...string) (*http.Transport, error) {
d, err := k.newSmartDialer(domains...)
if err != nil {
log.Error("Failed to create smart dialer", "error", err)
return nil, fmt.Errorf("failed to create smart dialer: %v", err)
}
return k.newTransportWithDialContext(func(ctx context.Context, network, addr string) (net.Conn, error) {
streamConn, err := d.DialStream(ctx, addr)
if err != nil {
return nil, fmt.Errorf("failed to dial stream: %v", err)
}
return streamConn, nil
}), nil
}
func (k *kindling) newTransportWithDialContext(dialContext func(ctx context.Context, network, addr string) (net.Conn, error)) *http.Transport {
return &http.Transport{
DialContext: dialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 20 * time.Second,
ExpectContinueTimeout: 4 * time.Second,
}
}
//go:embed smart_dialer_config.yml
var embedFS embed.FS
func (k *kindling) newSmartDialer(domains ...string) (transport.StreamDialer, error) {
finder := &smart.StrategyFinder{
TestTimeout: 5 * time.Second,
LogWriter: k.logWriter,
StreamDialer: &transport.TCPDialer{},
PacketDialer: &transport.UDPDialer{},
}
configBytes, err := embedFS.ReadFile("smart_dialer_config.yml")
if err != nil {
log.Error("Failed to read smart dialer config", "error", err)
return nil, err
}
dialer, err := finder.NewDialer(context.Background(), domains, configBytes)
if err != nil {
log.Error("Failed to create smart dialer", "error", err)
return nil, err
}
return dialer, nil
}