diff options
author | Sam Whited <sam@samwhited.com> | 2022-03-16 11:18:11 -0400 |
---|---|---|
committer | Sam Whited <sam@samwhited.com> | 2022-03-16 11:18:11 -0400 |
commit | 790d6f76ad24ec8153db9eb8dde21c1c6bc44d3f (patch) | |
tree | 1a8c28a64155b3210238d72fd4b0f7ac71385400 /listener.go | |
parent | c51fb38aae17da3de9cbd52cc82822daa4329a8b (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 'listener.go')
-rw-r--r-- | listener.go | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/listener.go b/listener.go index 4163cb9..593032f 100644 --- a/listener.go +++ b/listener.go @@ -38,20 +38,16 @@ func NewListenConfigCert(cert string) (*ListenConfig, error) { }, nil } -// Listen announces on the local network address. -// -// See func net.Dial for a description of the network and address parameters. -func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (*Listener, error) { - ln, err := lc.ListenConfig.Listen(ctx, network, address) - if err != nil { - return nil, err - } +// Wrap takes an existing net.Listener and wraps it in a listener that is +// configured to perform the ntor handshake. +// Values from the inner net.ListenConfig are ignored. +func (lc *ListenConfig) Wrap(ctx context.Context, ln net.Listener) (*Listener, error) { args := make(pt.Args) args.Add("node-id", lc.NodeID.Hex()) args.Add("private-key", lc.PrivateKey.Hex()) seed := ntor.KeySeed{} if bytes.Equal(lc.Seed[:], seed[:]) { - _, err = rand.Read(seed[:]) + _, err := rand.Read(seed[:]) if err != nil { return nil, err } @@ -66,6 +62,17 @@ func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (*L return &Listener{sf: sf, ln: ln}, nil } +// Listen announces on the local network address. +// +// See func net.Dial for a description of the network and address parameters. +func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (*Listener, error) { + ln, err := lc.ListenConfig.Listen(ctx, network, address) + if err != nil { + return nil, err + } + return lc.Wrap(ctx, ln) +} + // Listener is a network listener that accepts obfuscated connections and // performs the ntor handshake on them. type Listener struct { |