-
Notifications
You must be signed in to change notification settings - Fork 10
/
pconn.go
182 lines (163 loc) · 4.46 KB
/
pconn.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
package pconn
import (
"context"
"net"
"github.com/aperturerobotics/bifrost/peer"
"github.com/aperturerobotics/bifrost/transport"
"github.com/aperturerobotics/bifrost/transport/common/dialer"
transport_quic "github.com/aperturerobotics/bifrost/transport/common/quic"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/quic-go/quic-go"
"github.com/sirupsen/logrus"
)
// Transport implements a bifrost transport with a Quic-based packet conn.
// Transport UUIDs are deterministic and based on the LocalAddr() of the pconn.
type Transport struct {
// Transport is the underlying quic transport
*transport_quic.Transport
// ctx is the root context
ctx context.Context
// le is the logger
le *logrus.Entry
// peerID is the local peer id
peerID peer.ID
// privKey is the local private key
privKey crypto.PrivKey
// pc is the underlying packet conn.
pc net.PacketConn
// handler is the transport handler
handler transport.TransportHandler
// opts are extra options
opts *Opts
// addrParser parses an address from a string
// if nil, the dialer will not function
addrParser func(addr string) (net.Addr, error)
// staticPeerMap is a map of peer ids to dialing addresses
// may be nil
staticPeerMap map[string]*dialer.DialerOpts
// quicConfig is the quic configuration
quicConfig *quic.Config
// quicTpt is the quic transport
quicTpt *quic.Transport
}
// NewTransport constructs a new packet-conn based transport.
func NewTransport(
ctx context.Context,
le *logrus.Entry,
privKey crypto.PrivKey,
tc transport.TransportHandler,
opts *Opts,
// if uuid is 0, generates a uuid based on the local address
uuid uint64,
// pc is the packet conn
pc net.PacketConn,
// addrParser parses addresses to net.Addr for dialing
// can be nil
addrParser func(addr string) (net.Addr, error),
// staticPeerMap is a map of peer ids to dialing addresses
// may be nil
staticPeerMap map[string]*dialer.DialerOpts,
) (*Transport, error) {
if opts == nil {
opts = &Opts{}
}
peerID, err := peer.IDFromPrivateKey(privKey)
if err != nil {
return nil, err
}
tpt := &Transport{
ctx: ctx,
le: le,
pc: pc,
handler: tc,
opts: opts,
peerID: peerID,
privKey: privKey,
addrParser: addrParser,
staticPeerMap: staticPeerMap,
}
var dialFn transport_quic.DialFunc
if addrParser != nil {
dialFn = func(ctx context.Context, addr string) (quic.Connection, net.Addr, error) {
// parse the addr to a net.Addr
na, err := addrParser(addr)
if err != nil {
return nil, na, err
}
// dial via quic
conn, _, err := transport_quic.DialSessionViaTransport(
ctx,
le,
opts.GetQuic(),
tpt.quicTpt,
tpt.Transport.GetIdentity(),
na,
"",
)
if err != nil {
return nil, na, err
}
return conn, na, nil
}
}
// Build quic transport
tpt.Transport, err = transport_quic.NewTransport(
ctx,
le,
uuid,
pc.LocalAddr(),
privKey,
tc,
opts.GetQuic(),
dialFn,
)
if err != nil {
return nil, err
}
tpt.quicConfig = transport_quic.BuildQuicConfig(le, opts.GetQuic())
tpt.quicTpt = &quic.Transport{Conn: pc}
return tpt, nil
}
// GetPeerID returns the peer ID.
func (t *Transport) GetPeerID() peer.ID {
return t.peerID
}
// Execute executes the transport as configured, returning any fatal error.
func (t *Transport) Execute(ctx context.Context) error {
t.le.
WithField("local-addr", t.LocalAddr().String()).
Info("starting to listen with quic + tls")
// Configure TLS to allow any incoming remote peer.
tlsConf := transport_quic.BuildIncomingTlsConf(t.Transport.GetIdentity(), "")
ln, err := t.quicTpt.Listen(tlsConf, t.quicConfig)
if err != nil {
return err
}
defer t.pc.Close()
defer ln.Close()
// accept new connections
for {
sess, err := ln.Accept(ctx)
if err != nil {
return err
}
_, err = t.HandleSession(ctx, sess)
if err != nil {
t.le.WithError(err).Warn("cannot build link for session")
_ = sess.CloseWithError(500, "cannot build link")
continue
}
}
}
// GetPeerDialer returns the dialing information for a peer.
// Called when resolving EstablishLink.
// Return nil, nil to indicate not found or unavailable.
func (t *Transport) GetPeerDialer(ctx context.Context, peerID peer.ID) (*dialer.DialerOpts, error) {
return t.staticPeerMap[peerID.String()], nil
}
// Close closes the transport, returning any errors closing.
func (t *Transport) Close() error {
return nil
}
// _ is a type assertion
var _ transport.Transport = ((*Transport)(nil))