summaryrefslogtreecommitdiff
path: root/dialer.go
diff options
context:
space:
mode:
authorSam Whited <sam@samwhited.com>2022-03-16 11:18:11 -0400
committerSam Whited <sam@samwhited.com>2022-03-16 11:18:11 -0400
commit790d6f76ad24ec8153db9eb8dde21c1c6bc44d3f (patch)
tree1a8c28a64155b3210238d72fd4b0f7ac71385400 /dialer.go
parentc51fb38aae17da3de9cbd52cc82822daa4329a8b (diff)
obfsvpn: allow wrapping existing connections
This can be used to add support for additional networks that we don't necessarily want to depend on in the library. Signed-off-by: Sam Whited <sam@samwhited.com>
Diffstat (limited to 'dialer.go')
-rw-r--r--dialer.go18
1 files changed, 15 insertions, 3 deletions
diff --git a/dialer.go b/dialer.go
index 444e0c2..5043aeb 100644
--- a/dialer.go
+++ b/dialer.go
@@ -120,8 +120,22 @@ func NewDialerArgs(args pt.Args) (*Dialer, error) {
}, nil
}
+// Wrap performs the ntor handshake over an existing conection.
+// Wrap ignores the underlying Dialer config.
+func (d *Dialer) Wrap(ctx context.Context, conn net.Conn) (net.Conn, error) {
+ return d.dial(ctx, "", "", func(network, address string) (net.Conn, error) {
+ return conn, nil
+ })
+}
+
// Dial creates an outbound net.Conn and performs the ntor handshake.
func (d *Dialer) Dial(ctx context.Context, network, address string) (net.Conn, error) {
+ return d.dial(ctx, network, address, func(network, address string) (net.Conn, error) {
+ return d.Dialer.DialContext(ctx, network, address)
+ })
+}
+
+func (d *Dialer) dial(ctx context.Context, network, address string, f func(network, address string) (net.Conn, error)) (net.Conn, error) {
if d.cf == nil {
cf, err := (&obfs4.Transport{}).ClientFactory("")
if err != nil {
@@ -134,9 +148,7 @@ func (d *Dialer) Dial(ctx context.Context, network, address string) (net.Conn, e
if err != nil {
return nil, err
}
- return d.cf.Dial(network, address, func(network, address string) (net.Conn, error) {
- return d.Dialer.DialContext(ctx, network, address)
- }, args)
+ return d.cf.Dial(network, address, f, args)
}
// Args returns the dialers options as pluggable transport arguments.