Skip to content

Commit d927246

Browse files
committed
split responsibility in maybeWrapDialerForObfuscation
1 parent 5a9a546 commit d927246

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

vpn/client.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ func (c *Client) start(ctx context.Context) error {
148148

149149
mux.SetEventListener(c.EventListener)
150150

151+
c.emit(EventHandshake)
152+
151153
err = mux.Handshake(ctx)
152154
if err != nil {
153155
conn.Close()
@@ -194,12 +196,16 @@ func (c *Client) dial(ctx context.Context) (net.Conn, error) {
194196
case <-ctx.Done():
195197
return nil, ctx.Err()
196198
default:
197-
// TODO this message is not informative if obfuscation is set.
199+
// TODO(ainghazal): this message is not informative if obfuscation is set.
198200
msg := fmt.Sprintf("Connecting to %s:%s with proto %s",
199201
c.Opts.Remote, c.Opts.Port, strings.ToUpper(proto))
200202
logger.Info(msg)
201203

202-
dialer, err := c.maybeWrapDialerForObfuscation()
204+
d := c.getDialer()
205+
dialer, err := c.maybeWrapDialerForObfuscation(d)
206+
if err != nil {
207+
return nil, fmt.Errorf("%w: %s", ErrDialError, err)
208+
}
203209

204210
conn, err := dialer.DialContext(ctx, proto, net.JoinHostPort(c.Opts.Remote, c.Opts.Port))
205211
if err != nil {
@@ -209,16 +215,20 @@ func (c *Client) dial(ctx context.Context) (net.Conn, error) {
209215
}
210216
}
211217

218+
// getDialer returns any DialerContext that has been set up in the Dialer
219+
// field, or alternatively a net.Dialer instance as a fallback.
212220
func (c *Client) getDialer() DialerContext {
213221
if c.Dialer == nil {
214222
return &net.Dialer{}
215223
}
216224
return c.Dialer
217225
}
218226

219-
func (c *Client) maybeWrapDialerForObfuscation() (DialerContext, error) {
220-
dialer := c.getDialer()
221-
227+
// maybeWrapDialerForObfuscation checks the result of the ProxyType() method
228+
// call in client.Opts and, if needed, will wrap the passed dialer to use any
229+
// needed obfuscation proxy. It returns the possibly wrapped DialerContext and
230+
// any error raised during the wrapping operation.
231+
func (c *Client) maybeWrapDialerForObfuscation(d DialerContext) (DialerContext, error) {
222232
switch c.Opts.ProxyType() {
223233
case proxyOBFS4:
224234
obfsNode, err := obfs4.NewProxyNodeFromURI(c.Opts.ProxyOBFS4)
@@ -233,11 +243,13 @@ func (c *Client) maybeWrapDialerForObfuscation() (DialerContext, error) {
233243
obfsDialer := obfs4.NewDialer(obfsNode)
234244
// TODO modify obfs4 to accept a context, right now it expects a DialFunc..
235245
obfsDialer.Dialer = func(net, addr string) (net.Conn, error) {
236-
return dialer.DialContext(context.Background(), net, addr)
246+
return d.DialContext(context.Background(), net, addr)
237247
}
238248
return obfsDialer, nil
249+
case nullProxy:
250+
return d, nil
239251
default:
240-
return dialer, nil
252+
return nil, ErrBadProxy
241253
}
242254
}
243255

vpn/events.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package vpn
66
//
77

88
const (
9+
// Be very careful when altering the order of these event; other libraries
10+
// might be trusting their values, so please release a new version and
11+
// document the changes in that case.
912
EventReady = iota
1013
EventDialDone
1114
EventHandshake

0 commit comments

Comments
 (0)