Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5bddfb0

Browse files
committedAug 21, 2023
rename proto package to pb
1 parent 6ec7194 commit 5bddfb0

File tree

7 files changed

+245
-245
lines changed

7 files changed

+245
-245
lines changed
 

‎p2p/protocol/autonatv2/autonat.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/libp2p/go-libp2p/core/host"
1313
"github.com/libp2p/go-libp2p/core/network"
1414
"github.com/libp2p/go-libp2p/core/peer"
15-
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pbv2"
15+
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pb"
1616
ma "github.com/multiformats/go-multiaddr"
1717
manet "github.com/multiformats/go-multiaddr/net"
1818
"golang.org/x/exp/rand"
@@ -28,6 +28,9 @@ const (
2828
streamTimeout = time.Minute
2929
dialBackStreamTimeout = 5 * time.Second
3030
dialBackDialTimeout = 30 * time.Second
31+
maxHandshakeSizeBytes = 100_000
32+
minHandshakeSizeBytes = 30_000
33+
maxPeerAddresses = 50
3134
)
3235

3336
var (
@@ -142,7 +145,7 @@ type Result struct {
142145
// Reachability of the dialed address
143146
Reachability network.Reachability
144147
// Status is the outcome of the dialback
145-
Status pbv2.DialStatus
148+
Status pb.DialStatus
146149
}
147150

148151
// CheckReachability makes a single dial request for checking reachability. For highPriorityAddrs dial charge is paid

‎p2p/protocol/autonatv2/autonat_test.go

+55-55
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
bhost "github.com/libp2p/go-libp2p/p2p/host/blank"
1616
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
1717
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
18-
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pbv2"
18+
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pb"
1919

2020
"github.com/libp2p/go-msgio/pbio"
2121
ma "github.com/multiformats/go-multiaddr"
@@ -38,7 +38,7 @@ func newAutoNAT(t *testing.T, dialer host.Host, opts ...AutoNATOption) *AutoNAT
3838
return an
3939
}
4040

41-
func parseAddrs(t *testing.T, msg *pbv2.Message) []ma.Multiaddr {
41+
func parseAddrs(t *testing.T, msg *pb.Message) []ma.Multiaddr {
4242
t.Helper()
4343
req := msg.GetDialRequest()
4444
addrs := make([]ma.Multiaddr, 0)
@@ -93,7 +93,7 @@ func TestClientRequest(t *testing.T) {
9393
p.SetStreamHandler(DialProtocol, func(s network.Stream) {
9494
gotReq.Store(true)
9595
r := pbio.NewDelimitedReader(s, maxMsgSize)
96-
var msg pbv2.Message
96+
var msg pb.Message
9797
if err := r.ReadMsg(&msg); err != nil {
9898
t.Error(err)
9999
return
@@ -139,12 +139,12 @@ func TestClientServerError(t *testing.T) {
139139
}},
140140
{handler: func(s network.Stream) {
141141
r := pbio.NewDelimitedReader(s, maxMsgSize)
142-
var msg pbv2.Message
142+
var msg pb.Message
143143
r.ReadMsg(&msg)
144144
w := pbio.NewDelimitedWriter(s)
145-
w.WriteMsg(&pbv2.Message{
146-
Msg: &pbv2.Message_DialRequest{
147-
DialRequest: &pbv2.DialRequest{
145+
w.WriteMsg(&pb.Message{
146+
Msg: &pb.Message_DialRequest{
147+
DialRequest: &pb.DialRequest{
148148
Addrs: [][]byte{},
149149
Nonce: 0,
150150
},
@@ -182,12 +182,12 @@ func TestClientDataRequest(t *testing.T) {
182182
}{
183183
{handler: func(s network.Stream) {
184184
r := pbio.NewDelimitedReader(s, maxMsgSize)
185-
var msg pbv2.Message
185+
var msg pb.Message
186186
r.ReadMsg(&msg)
187187
w := pbio.NewDelimitedWriter(s)
188-
w.WriteMsg(&pbv2.Message{
189-
Msg: &pbv2.Message_DialDataRequest{
190-
DialDataRequest: &pbv2.DialDataRequest{
188+
w.WriteMsg(&pb.Message{
189+
Msg: &pb.Message_DialDataRequest{
190+
DialDataRequest: &pb.DialDataRequest{
191191
AddrIdx: 0,
192192
NumBytes: 10000,
193193
},
@@ -210,12 +210,12 @@ func TestClientDataRequest(t *testing.T) {
210210
}},
211211
{handler: func(s network.Stream) {
212212
r := pbio.NewDelimitedReader(s, maxMsgSize)
213-
var msg pbv2.Message
213+
var msg pb.Message
214214
r.ReadMsg(&msg)
215215
w := pbio.NewDelimitedWriter(s)
216-
w.WriteMsg(&pbv2.Message{
217-
Msg: &pbv2.Message_DialDataRequest{
218-
DialDataRequest: &pbv2.DialDataRequest{
216+
w.WriteMsg(&pb.Message{
217+
Msg: &pb.Message_DialDataRequest{
218+
DialDataRequest: &pb.DialDataRequest{
219219
AddrIdx: 1,
220220
NumBytes: 10000,
221221
},
@@ -229,12 +229,12 @@ func TestClientDataRequest(t *testing.T) {
229229
}},
230230
{handler: func(s network.Stream) {
231231
r := pbio.NewDelimitedReader(s, maxMsgSize)
232-
var msg pbv2.Message
232+
var msg pb.Message
233233
r.ReadMsg(&msg)
234234
w := pbio.NewDelimitedWriter(s)
235-
w.WriteMsg(&pbv2.Message{
236-
Msg: &pbv2.Message_DialDataRequest{
237-
DialDataRequest: &pbv2.DialDataRequest{
235+
w.WriteMsg(&pb.Message{
236+
Msg: &pb.Message_DialDataRequest{
237+
DialDataRequest: &pb.DialDataRequest{
238238
AddrIdx: 0,
239239
NumBytes: 1000_000,
240240
},
@@ -275,18 +275,18 @@ func TestClientDialBacks(t *testing.T) {
275275
{
276276
handler: func(s network.Stream) {
277277
r := pbio.NewDelimitedReader(s, maxMsgSize)
278-
var msg pbv2.Message
278+
var msg pb.Message
279279
if err := r.ReadMsg(&msg); err != nil {
280280
t.Error(err)
281281
}
282-
resp := &pbv2.DialResponse{
283-
Status: pbv2.DialResponse_ResponseStatus_OK,
284-
DialStatus: pbv2.DialStatus_OK,
282+
resp := &pb.DialResponse{
283+
Status: pb.DialResponse_ResponseStatus_OK,
284+
DialStatus: pb.DialStatus_OK,
285285
AddrIdx: 0,
286286
}
287287
w := pbio.NewDelimitedWriter(s)
288-
w.WriteMsg(&pbv2.Message{
289-
Msg: &pbv2.Message_DialResponse{
288+
w.WriteMsg(&pb.Message{
289+
Msg: &pb.Message_DialResponse{
290290
DialResponse: resp,
291291
},
292292
})
@@ -297,7 +297,7 @@ func TestClientDialBacks(t *testing.T) {
297297
{
298298
handler: func(s network.Stream) {
299299
r := pbio.NewDelimitedReader(s, maxMsgSize)
300-
var msg pbv2.Message
300+
var msg pb.Message
301301
r.ReadMsg(&msg)
302302
req := msg.GetDialRequest()
303303
addrs := parseAddrs(t, &msg)
@@ -311,15 +311,15 @@ func TestClientDialBacks(t *testing.T) {
311311
return
312312
}
313313
w := pbio.NewDelimitedWriter(as)
314-
w.WriteMsg(&pbv2.DialBack{Nonce: req.Nonce})
314+
w.WriteMsg(&pb.DialBack{Nonce: req.Nonce})
315315
as.CloseWrite()
316316

317317
w = pbio.NewDelimitedWriter(s)
318-
w.WriteMsg(&pbv2.Message{
319-
Msg: &pbv2.Message_DialResponse{
320-
DialResponse: &pbv2.DialResponse{
321-
Status: pbv2.DialResponse_ResponseStatus_OK,
322-
DialStatus: pbv2.DialStatus_OK,
318+
w.WriteMsg(&pb.Message{
319+
Msg: &pb.Message_DialResponse{
320+
DialResponse: &pb.DialResponse{
321+
Status: pb.DialResponse_ResponseStatus_OK,
322+
DialStatus: pb.DialStatus_OK,
323323
AddrIdx: 0,
324324
},
325325
},
@@ -331,7 +331,7 @@ func TestClientDialBacks(t *testing.T) {
331331
{
332332
handler: func(s network.Stream) {
333333
r := pbio.NewDelimitedReader(s, maxMsgSize)
334-
var msg pbv2.Message
334+
var msg pb.Message
335335
r.ReadMsg(&msg)
336336
req := msg.GetDialRequest()
337337
addrs := parseAddrs(t, &msg)
@@ -346,7 +346,7 @@ func TestClientDialBacks(t *testing.T) {
346346
return
347347
}
348348
ww := pbio.NewDelimitedWriter(as)
349-
if err := ww.WriteMsg(&pbv2.DialBack{Nonce: req.Nonce - 1}); err != nil {
349+
if err := ww.WriteMsg(&pb.DialBack{Nonce: req.Nonce - 1}); err != nil {
350350
s.Reset()
351351
as.Reset()
352352
return
@@ -359,11 +359,11 @@ func TestClientDialBacks(t *testing.T) {
359359
}()
360360

361361
w := pbio.NewDelimitedWriter(s)
362-
w.WriteMsg(&pbv2.Message{
363-
Msg: &pbv2.Message_DialResponse{
364-
DialResponse: &pbv2.DialResponse{
365-
Status: pbv2.DialResponse_ResponseStatus_OK,
366-
DialStatus: pbv2.DialStatus_OK,
362+
w.WriteMsg(&pb.Message{
363+
Msg: &pb.Message_DialResponse{
364+
DialResponse: &pb.DialResponse{
365+
Status: pb.DialResponse_ResponseStatus_OK,
366+
DialStatus: pb.DialStatus_OK,
367367
AddrIdx: 0,
368368
},
369369
},
@@ -375,7 +375,7 @@ func TestClientDialBacks(t *testing.T) {
375375
{
376376
handler: func(s network.Stream) {
377377
r := pbio.NewDelimitedReader(s, maxMsgSize)
378-
var msg pbv2.Message
378+
var msg pb.Message
379379
r.ReadMsg(&msg)
380380
req := msg.GetDialRequest()
381381
addrs := parseAddrs(t, &msg)
@@ -391,7 +391,7 @@ func TestClientDialBacks(t *testing.T) {
391391
}
392392

393393
w := pbio.NewDelimitedWriter(as)
394-
if err := w.WriteMsg(&pbv2.DialBack{Nonce: req.Nonce}); err != nil {
394+
if err := w.WriteMsg(&pb.DialBack{Nonce: req.Nonce}); err != nil {
395395
t.Error("failed to write nonce", err)
396396
s.Reset()
397397
as.Reset()
@@ -406,11 +406,11 @@ func TestClientDialBacks(t *testing.T) {
406406

407407
w = pbio.NewDelimitedWriter(s)
408408

409-
w.WriteMsg(&pbv2.Message{
410-
Msg: &pbv2.Message_DialResponse{
411-
DialResponse: &pbv2.DialResponse{
412-
Status: pbv2.DialResponse_ResponseStatus_OK,
413-
DialStatus: pbv2.DialStatus_OK,
409+
w.WriteMsg(&pb.Message{
410+
Msg: &pb.Message_DialResponse{
411+
DialResponse: &pb.DialResponse{
412+
Status: pb.DialResponse_ResponseStatus_OK,
413+
DialStatus: pb.DialStatus_OK,
414414
AddrIdx: 1,
415415
},
416416
},
@@ -422,7 +422,7 @@ func TestClientDialBacks(t *testing.T) {
422422
{
423423
handler: func(s network.Stream) {
424424
r := pbio.NewDelimitedReader(s, maxMsgSize)
425-
var msg pbv2.Message
425+
var msg pb.Message
426426
r.ReadMsg(&msg)
427427
req := msg.GetDialRequest()
428428
addrs := parseAddrs(t, &msg)
@@ -438,7 +438,7 @@ func TestClientDialBacks(t *testing.T) {
438438
}
439439

440440
w := pbio.NewDelimitedWriter(as)
441-
if err := w.WriteMsg(&pbv2.DialBack{Nonce: req.Nonce}); err != nil {
441+
if err := w.WriteMsg(&pb.DialBack{Nonce: req.Nonce}); err != nil {
442442
t.Error("failed to write nonce", err)
443443
s.Reset()
444444
as.Reset()
@@ -453,11 +453,11 @@ func TestClientDialBacks(t *testing.T) {
453453

454454
w = pbio.NewDelimitedWriter(s)
455455

456-
w.WriteMsg(&pbv2.Message{
457-
Msg: &pbv2.Message_DialResponse{
458-
DialResponse: &pbv2.DialResponse{
459-
Status: pbv2.DialResponse_ResponseStatus_OK,
460-
DialStatus: pbv2.DialStatus_OK,
456+
w.WriteMsg(&pb.Message{
457+
Msg: &pb.Message_DialResponse{
458+
DialResponse: &pb.DialResponse{
459+
Status: pb.DialResponse_ResponseStatus_OK,
460+
DialStatus: pb.DialStatus_OK,
461461
AddrIdx: 10,
462462
},
463463
},
@@ -480,12 +480,12 @@ func TestClientDialBacks(t *testing.T) {
480480
} else {
481481
require.NoError(t, err)
482482
require.Equal(t, res.Reachability, network.ReachabilityUnknown)
483-
require.NotEqual(t, res.Status, pbv2.DialStatus_OK, "got: %d", res.Status)
483+
require.NotEqual(t, res.Status, pb.DialStatus_OK, "got: %d", res.Status)
484484
}
485485
} else {
486486
require.NoError(t, err)
487487
require.Equal(t, res.Reachability, network.ReachabilityPublic)
488-
require.Equal(t, res.Status, pbv2.DialStatus_OK)
488+
require.Equal(t, res.Status, pb.DialStatus_OK)
489489
}
490490
})
491491
}

‎p2p/protocol/autonatv2/client.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
"github.com/libp2p/go-libp2p/core/host"
1010
"github.com/libp2p/go-libp2p/core/network"
1111
"github.com/libp2p/go-libp2p/core/peer"
12-
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pbv2"
12+
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pb"
1313
"github.com/libp2p/go-msgio/pbio"
1414
ma "github.com/multiformats/go-multiaddr"
1515
"golang.org/x/exp/rand"
1616
"golang.org/x/exp/slices"
1717
)
1818

19-
//go:generate protoc --go_out=. --go_opt=Mpbv2/autonat.proto=./pbv2 pbv2/autonat.proto
19+
//go:generate protoc --go_out=. --go_opt=Mpb/autonatv2.proto=./pb pb/autonatv2.proto
2020

2121
// Client implements the client for making dial requests for AutoNAT v2. It verifies successful
2222
// dials and provides an option to send data for dial requests.
@@ -113,20 +113,20 @@ func (ac *Client) CheckReachability(ctx context.Context, p peer.ID, highPriority
113113
}
114114

115115
resp := msg.GetDialResponse()
116-
if resp.GetStatus() != pbv2.DialResponse_ResponseStatus_OK {
116+
if resp.GetStatus() != pb.DialResponse_ResponseStatus_OK {
117117
// server couldn't dial any requested address
118-
if resp.GetStatus() == pbv2.DialResponse_E_DIAL_REFUSED {
118+
if resp.GetStatus() == pb.DialResponse_E_DIAL_REFUSED {
119119
return Result{}, fmt.Errorf("dial request: %w", ErrDialRefused)
120120
}
121121
return Result{}, fmt.Errorf("dial request: status %d %s", resp.GetStatus(),
122-
pbv2.DialStatus_name[int32(resp.GetStatus())])
122+
pb.DialStatus_name[int32(resp.GetStatus())])
123123
}
124-
if resp.GetDialStatus() == pbv2.DialStatus_UNUSED {
124+
if resp.GetDialStatus() == pb.DialStatus_UNUSED {
125125
return Result{}, fmt.Errorf("dial request failed: received invalid dial status 0")
126126
}
127127

128128
var dialBackAddr ma.Multiaddr
129-
if resp.GetDialStatus() == pbv2.DialStatus_OK && int(resp.AddrIdx) < len(highPriorityAddrs)+len(lowPriorityAddrs) {
129+
if resp.GetDialStatus() == pb.DialStatus_OK && int(resp.AddrIdx) < len(highPriorityAddrs)+len(lowPriorityAddrs) {
130130
timer := time.NewTimer(dialBackStreamTimeout)
131131
select {
132132
case at := <-ch:
@@ -139,7 +139,7 @@ func (ac *Client) CheckReachability(ctx context.Context, p peer.ID, highPriority
139139
return ac.newResults(resp, highPriorityAddrs, lowPriorityAddrs, dialBackAddr)
140140
}
141141

142-
func (ac *Client) newResults(resp *pbv2.DialResponse, highPriorityAddrs []ma.Multiaddr, lowPriorityAddrs []ma.Multiaddr, dialBackAddr ma.Multiaddr) (Result, error) {
142+
func (ac *Client) newResults(resp *pb.DialResponse, highPriorityAddrs []ma.Multiaddr, lowPriorityAddrs []ma.Multiaddr, dialBackAddr ma.Multiaddr) (Result, error) {
143143
if int(resp.AddrIdx) >= len(highPriorityAddrs)+len(lowPriorityAddrs) {
144144
return Result{}, fmt.Errorf("addrIdx out of range: %d 0-%d", resp.AddrIdx, len(highPriorityAddrs)+len(lowPriorityAddrs)-1)
145145
}
@@ -155,13 +155,13 @@ func (ac *Client) newResults(resp *pbv2.DialResponse, highPriorityAddrs []ma.Mul
155155
rch := network.ReachabilityUnknown
156156
status := resp.DialStatus
157157
switch status {
158-
case pbv2.DialStatus_OK:
158+
case pb.DialStatus_OK:
159159
if areAddrsConsistent(dialBackAddr, addr) {
160160
rch = network.ReachabilityPublic
161161
} else {
162-
status = pbv2.DialStatus_E_DIAL_BACK_ERROR
162+
status = pb.DialStatus_E_DIAL_BACK_ERROR
163163
}
164-
case pbv2.DialStatus_E_DIAL_ERROR:
164+
case pb.DialStatus_E_DIAL_ERROR:
165165
rch = network.ReachabilityPrivate
166166
}
167167
return Result{
@@ -172,11 +172,11 @@ func (ac *Client) newResults(resp *pbv2.DialResponse, highPriorityAddrs []ma.Mul
172172
}, nil
173173
}
174174

175-
func (ac *Client) sendDialData(req *pbv2.DialDataRequest, w pbio.Writer, msg *pbv2.Message) error {
175+
func (ac *Client) sendDialData(req *pb.DialDataRequest, w pbio.Writer, msg *pb.Message) error {
176176
nb := req.GetNumBytes()
177-
ddResp := &pbv2.DialDataResponse{Data: ac.dialData}
178-
*msg = pbv2.Message{
179-
Msg: &pbv2.Message_DialDataResponse{
177+
ddResp := &pb.DialDataResponse{Data: ac.dialData}
178+
*msg = pb.Message{
179+
Msg: &pb.Message_DialDataResponse{
180180
DialDataResponse: ddResp,
181181
},
182182
}
@@ -194,17 +194,17 @@ func (ac *Client) sendDialData(req *pbv2.DialDataRequest, w pbio.Writer, msg *pb
194194
return nil
195195
}
196196

197-
func newDialRequest(highPriorityAddrs, lowPriorityAddrs []ma.Multiaddr, nonce uint64) pbv2.Message {
197+
func newDialRequest(highPriorityAddrs, lowPriorityAddrs []ma.Multiaddr, nonce uint64) pb.Message {
198198
addrbs := make([][]byte, len(highPriorityAddrs)+len(lowPriorityAddrs))
199199
for i, a := range highPriorityAddrs {
200200
addrbs[i] = a.Bytes()
201201
}
202202
for i, a := range lowPriorityAddrs {
203203
addrbs[len(highPriorityAddrs)+i] = a.Bytes()
204204
}
205-
return pbv2.Message{
206-
Msg: &pbv2.Message_DialRequest{
207-
DialRequest: &pbv2.DialRequest{
205+
return pb.Message{
206+
Msg: &pb.Message_DialRequest{
207+
DialRequest: &pb.DialRequest{
208208
Addrs: addrbs,
209209
Nonce: nonce,
210210
},
@@ -234,7 +234,7 @@ func (ac *Client) handleDialBack(s network.Stream) {
234234
defer s.Close()
235235

236236
r := pbio.NewDelimitedReader(s, maxMsgSize)
237-
var msg pbv2.DialBack
237+
var msg pb.DialBack
238238
if err := r.ReadMsg(&msg); err != nil {
239239
log.Debugf("failed to read dialback msg from %s: %s", s.Conn().RemotePeer(), err)
240240
s.Reset()

‎p2p/protocol/autonatv2/pbv2/autonat.pb.go ‎p2p/protocol/autonatv2/pb/autonatv2.pb.go

+134-135
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎p2p/protocol/autonatv2/pbv2/autonat.proto ‎p2p/protocol/autonatv2/pb/autonatv2.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
syntax = "proto3";
22

3-
package autonatv2.pbv2;
3+
package autonatv2.pb;
44

55
message Message {
66
oneof msg {

‎p2p/protocol/autonatv2/server.go

+26-28
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/libp2p/go-libp2p/core/network"
1111
"github.com/libp2p/go-libp2p/core/peer"
1212
"github.com/libp2p/go-libp2p/core/peerstore"
13-
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pbv2"
13+
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pb"
1414
"github.com/libp2p/go-msgio/pbio"
1515

1616
ma "github.com/multiformats/go-multiaddr"
@@ -20,11 +20,6 @@ import (
2020

2121
type dataRequestPolicyFunc = func(s network.Stream, dialAddr ma.Multiaddr) bool
2222

23-
const (
24-
maxHandshakeSizeBytes = 100_000
25-
minHandshakeSizeBytes = 30_000
26-
)
27-
2823
type Server struct {
2924
dialer host.Host
3025
host host.Host
@@ -77,7 +72,7 @@ func (as *Server) handleDialRequest(s network.Stream) {
7772

7873
p := s.Conn().RemotePeer()
7974
r := pbio.NewDelimitedReader(s, maxMsgSize)
80-
var msg pbv2.Message
75+
var msg pb.Message
8176
if err := r.ReadMsg(&msg); err != nil {
8277
s.Reset()
8378
log.Debugf("failed to read request from %s: %s", p, err)
@@ -93,6 +88,9 @@ func (as *Server) handleDialRequest(s network.Stream) {
9388
var dialAddr ma.Multiaddr
9489
var addrIdx int
9590
for i, ab := range msg.GetDialRequest().GetAddrs() {
91+
if i >= maxPeerAddresses {
92+
break
93+
}
9694
a, err := ma.NewMultiaddrBytes(ab)
9795
if err != nil {
9896
continue
@@ -108,10 +106,10 @@ func (as *Server) handleDialRequest(s network.Stream) {
108106

109107
w := pbio.NewDelimitedWriter(s)
110108
if dialAddr == nil {
111-
msg = pbv2.Message{
112-
Msg: &pbv2.Message_DialResponse{
113-
DialResponse: &pbv2.DialResponse{
114-
Status: pbv2.DialResponse_E_DIAL_REFUSED,
109+
msg = pb.Message{
110+
Msg: &pb.Message_DialResponse{
111+
DialResponse: &pb.DialResponse{
112+
Status: pb.DialResponse_E_DIAL_REFUSED,
115113
},
116114
},
117115
}
@@ -125,10 +123,10 @@ func (as *Server) handleDialRequest(s network.Stream) {
125123

126124
isDialDataRequired := as.dialDataRequestPolicy(s, dialAddr)
127125
if !as.limiter.Accept(p, isDialDataRequired) {
128-
msg = pbv2.Message{
129-
Msg: &pbv2.Message_DialResponse{
130-
DialResponse: &pbv2.DialResponse{
131-
Status: pbv2.DialResponse_E_REQUEST_REJECTED,
126+
msg = pb.Message{
127+
Msg: &pb.Message_DialResponse{
128+
DialResponse: &pb.DialResponse{
129+
Status: pb.DialResponse_E_REQUEST_REJECTED,
132130
},
133131
},
134132
}
@@ -150,10 +148,10 @@ func (as *Server) handleDialRequest(s network.Stream) {
150148
}
151149
}
152150
status := as.dialBack(s.Conn().RemotePeer(), dialAddr, nonce)
153-
msg = pbv2.Message{
154-
Msg: &pbv2.Message_DialResponse{
155-
DialResponse: &pbv2.DialResponse{
156-
Status: pbv2.DialResponse_ResponseStatus_OK,
151+
msg = pb.Message{
152+
Msg: &pb.Message_DialResponse{
153+
DialResponse: &pb.DialResponse{
154+
Status: pb.DialResponse_ResponseStatus_OK,
157155
DialStatus: status,
158156
AddrIdx: uint32(addrIdx),
159157
},
@@ -177,11 +175,11 @@ func amplificationAttackPrevention(s network.Stream, dialAddr ma.Multiaddr) bool
177175
return !connIP.Equal(dialIP)
178176
}
179177

180-
func getDialData(w pbio.Writer, r pbio.Reader, msg *pbv2.Message, addrIdx int) error {
178+
func getDialData(w pbio.Writer, r pbio.Reader, msg *pb.Message, addrIdx int) error {
181179
numBytes := minHandshakeSizeBytes + rand.Intn(maxHandshakeSizeBytes-minHandshakeSizeBytes)
182-
*msg = pbv2.Message{
183-
Msg: &pbv2.Message_DialDataRequest{
184-
DialDataRequest: &pbv2.DialDataRequest{
180+
*msg = pb.Message{
181+
Msg: &pb.Message_DialDataRequest{
182+
DialDataRequest: &pb.DialDataRequest{
185183
AddrIdx: uint32(addrIdx),
186184
NumBytes: uint64(numBytes),
187185
},
@@ -203,7 +201,7 @@ func getDialData(w pbio.Writer, r pbio.Reader, msg *pbv2.Message, addrIdx int) e
203201
return nil
204202
}
205203

206-
func (as *Server) dialBack(p peer.ID, addr ma.Multiaddr, nonce uint64) pbv2.DialStatus {
204+
func (as *Server) dialBack(p peer.ID, addr ma.Multiaddr, nonce uint64) pb.DialStatus {
207205
ctx, cancel := context.WithTimeout(context.Background(), dialBackDialTimeout)
208206
as.dialer.Peerstore().AddAddr(p, addr, peerstore.TempAddrTTL)
209207
defer func() {
@@ -214,15 +212,15 @@ func (as *Server) dialBack(p peer.ID, addr ma.Multiaddr, nonce uint64) pbv2.Dial
214212
}()
215213
s, err := as.dialer.NewStream(ctx, p, DialBackProtocol)
216214
if err != nil {
217-
return pbv2.DialStatus_E_DIAL_ERROR
215+
return pb.DialStatus_E_DIAL_ERROR
218216
}
219217
defer s.Close()
220218
s.SetDeadline(as.now().Add(dialBackStreamTimeout))
221219

222220
w := pbio.NewDelimitedWriter(s)
223-
if err := w.WriteMsg(&pbv2.DialBack{Nonce: nonce}); err != nil {
221+
if err := w.WriteMsg(&pb.DialBack{Nonce: nonce}); err != nil {
224222
s.Reset()
225-
return pbv2.DialStatus_E_DIAL_BACK_ERROR
223+
return pb.DialStatus_E_DIAL_BACK_ERROR
226224
}
227225

228226
// Since the underlying connection is on a separate dialer, it'll be closed after this function returns.
@@ -233,7 +231,7 @@ func (as *Server) dialBack(p peer.ID, addr ma.Multiaddr, nonce uint64) pbv2.Dial
233231
b := make([]byte, 1) // Read 1 byte here because 0 len reads are free to return (0, nil) immediately
234232
s.Read(b)
235233

236-
return pbv2.DialStatus_OK
234+
return pb.DialStatus_OK
237235
}
238236

239237
// rateLimiter implements a sliding window rate limit of requests per minute.

‎p2p/protocol/autonatv2/server_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/libp2p/go-libp2p/core/test"
1010
bhost "github.com/libp2p/go-libp2p/p2p/host/blank"
1111
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
12-
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pbv2"
12+
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pb"
1313
ma "github.com/multiformats/go-multiaddr"
1414
"github.com/stretchr/testify/require"
1515
)
@@ -89,7 +89,7 @@ func TestServerDataRequest(t *testing.T) {
8989
Idx: 0,
9090
Addr: quicAddr,
9191
Reachability: network.ReachabilityPublic,
92-
Status: pbv2.DialStatus_OK,
92+
Status: pb.DialStatus_OK,
9393
}, res)
9494
}
9595

@@ -112,7 +112,7 @@ func TestServerDial(t *testing.T) {
112112
Idx: 0,
113113
Addr: randAddr,
114114
Reachability: network.ReachabilityPrivate,
115-
Status: pbv2.DialStatus_E_DIAL_ERROR,
115+
Status: pb.DialStatus_E_DIAL_ERROR,
116116
}, res)
117117

118118
res, err = c.CheckReachability(context.Background(), nil, c.host.Addrs())
@@ -121,7 +121,7 @@ func TestServerDial(t *testing.T) {
121121
Idx: 0,
122122
Addr: hostAddrs[0],
123123
Reachability: network.ReachabilityPublic,
124-
Status: pbv2.DialStatus_OK,
124+
Status: pb.DialStatus_OK,
125125
}, res)
126126
}
127127

0 commit comments

Comments
 (0)
Please sign in to comment.