Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-tls
Commits
c005fc5a
Unverified
Commit
c005fc5a
authored
Nov 30, 2018
by
Marten Seemann
Committed by
GitHub
Nov 30, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5 from libp2p/use-context
close the underlying connection when the context is canceled
parents
29d15e5b
f692110d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
10 deletions
+76
-10
transport.go
transport.go
+38
-10
transport_test.go
transport_test.go
+38
-0
No files found.
transport.go
View file @
c005fc5a
...
...
@@ -43,23 +43,51 @@ var _ cs.Transport = &Transport{}
// SecureInbound runs the TLS handshake as a server.
func
(
t
*
Transport
)
SecureInbound
(
ctx
context
.
Context
,
insecure
net
.
Conn
)
(
cs
.
Conn
,
error
)
{
serv
:=
tls
.
Server
(
insecure
,
t
.
identity
.
Config
)
// TODO: use the ctx
// see https://github.com/golang/go/issues/18482
if
err
:=
serv
.
Handshake
();
err
!=
nil
{
return
nil
,
err
}
return
t
.
setupConn
(
serv
)
return
t
.
handshake
(
ctx
,
insecure
,
serv
)
}
// SecureOutbound runs the TLS handshake as a client.
func
(
t
*
Transport
)
SecureOutbound
(
ctx
context
.
Context
,
insecure
net
.
Conn
,
p
peer
.
ID
)
(
cs
.
Conn
,
error
)
{
cl
:=
tls
.
Client
(
insecure
,
t
.
identity
.
ConfigForPeer
(
p
))
// TODO: use the ctx
// see https://github.com/golang/go/issues/18482
if
err
:=
cl
.
Handshake
();
err
!=
nil
{
return
t
.
handshake
(
ctx
,
insecure
,
cl
)
}
func
(
t
*
Transport
)
handshake
(
ctx
context
.
Context
,
// in Go 1.10, we need to close the underlying net.Conn
// in Go 1.11 this was fixed, and tls.Conn.Close() works as well
insecure
net
.
Conn
,
tlsConn
*
tls
.
Conn
,
)
(
cs
.
Conn
,
error
)
{
// There's no way to pass a context to tls.Conn.Handshake().
// See https://github.com/golang/go/issues/18482.
// Close the connection instead.
done
:=
make
(
chan
struct
{})
defer
close
(
done
)
go
func
()
{
select
{
case
<-
done
:
case
<-
ctx
.
Done
()
:
insecure
.
Close
()
}
}()
if
err
:=
tlsConn
.
Handshake
();
err
!=
nil
{
// if the context was canceled, return the context error
if
ctxErr
:=
ctx
.
Err
();
ctxErr
!=
nil
{
return
nil
,
ctxErr
}
return
nil
,
err
}
conn
,
err
:=
t
.
setupConn
(
tlsConn
)
if
err
!=
nil
{
// if the context was canceled, return the context error
if
ctxErr
:=
ctx
.
Err
();
ctxErr
!=
nil
{
return
nil
,
ctxErr
}
return
nil
,
err
}
return
t
.
setupConn
(
cl
)
return
conn
,
nil
}
func
(
t
*
Transport
)
setupConn
(
tlsConn
*
tls
.
Conn
)
(
cs
.
Conn
,
error
)
{
...
...
transport_test.go
View file @
c005fc5a
...
...
@@ -96,6 +96,44 @@ var _ = Describe("Transport", func() {
Expect
(
string
(
b
))
.
To
(
Equal
(
"foobar"
))
})
It
(
"fails when the context of the outgoing connection is canceled"
,
func
()
{
clientTransport
,
err
:=
New
(
clientKey
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
serverTransport
,
err
:=
New
(
serverKey
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
clientInsecureConn
,
serverInsecureConn
:=
connect
()
go
func
()
{
defer
GinkgoRecover
()
_
,
err
:=
serverTransport
.
SecureInbound
(
context
.
Background
(),
serverInsecureConn
)
Expect
(
err
)
.
To
(
HaveOccurred
())
}()
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
cancel
()
_
,
err
=
clientTransport
.
SecureOutbound
(
ctx
,
clientInsecureConn
,
serverID
)
Expect
(
err
)
.
To
(
MatchError
(
context
.
Canceled
))
})
It
(
"fails when the context of the incoming connection is canceled"
,
func
()
{
clientTransport
,
err
:=
New
(
clientKey
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
serverTransport
,
err
:=
New
(
serverKey
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
clientInsecureConn
,
serverInsecureConn
:=
connect
()
go
func
()
{
defer
GinkgoRecover
()
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
cancel
()
_
,
err
:=
serverTransport
.
SecureInbound
(
ctx
,
serverInsecureConn
)
Expect
(
err
)
.
To
(
MatchError
(
context
.
Canceled
))
}()
_
,
err
=
clientTransport
.
SecureOutbound
(
context
.
Background
(),
clientInsecureConn
,
serverID
)
Expect
(
err
)
.
To
(
HaveOccurred
())
})
It
(
"fails if the peer ID doesn't match"
,
func
()
{
thirdPartyID
,
_
:=
createPeer
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment