2019-06-09 07:24:20 +00:00
|
|
|
package autonat
|
|
|
|
|
|
|
|
import (
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-02-22 21:58:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/autonat/pb"
|
2019-06-09 07:24:20 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2019-06-09 07:24:20 +00:00
|
|
|
)
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
//go:generate protoc --proto_path=$PWD:$PWD/../../.. --go_out=. --go_opt=Mpb/autonat.proto=./pb pb/autonat.proto
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// AutoNATProto identifies the autonat service protocol
|
2019-06-09 07:24:20 +00:00
|
|
|
const AutoNATProto = "/libp2p/autonat/1.0.0"
|
|
|
|
|
|
|
|
func newDialMessage(pi peer.AddrInfo) *pb.Message {
|
|
|
|
msg := new(pb.Message)
|
|
|
|
msg.Type = pb.Message_DIAL.Enum()
|
|
|
|
msg.Dial = new(pb.Message_Dial)
|
|
|
|
msg.Dial.Peer = new(pb.Message_PeerInfo)
|
|
|
|
msg.Dial.Peer.Id = []byte(pi.ID)
|
|
|
|
msg.Dial.Peer.Addrs = make([][]byte, len(pi.Addrs))
|
|
|
|
for i, addr := range pi.Addrs {
|
|
|
|
msg.Dial.Peer.Addrs[i] = addr.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
func newDialResponseOK(addr ma.Multiaddr) *pb.Message_DialResponse {
|
|
|
|
dr := new(pb.Message_DialResponse)
|
|
|
|
dr.Status = pb.Message_OK.Enum()
|
|
|
|
dr.Addr = addr.Bytes()
|
|
|
|
return dr
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDialResponseError(status pb.Message_ResponseStatus, text string) *pb.Message_DialResponse {
|
|
|
|
dr := new(pb.Message_DialResponse)
|
|
|
|
dr.Status = status.Enum()
|
|
|
|
dr.StatusText = &text
|
|
|
|
return dr
|
|
|
|
}
|