I am trying to make a P2P app over WebRTC with a relay server made in GO. Over different networks I have noticed I am only able to make WebRTC connections for few seconds and receiver automatically kills it . Here is the code for client side:
const defaultConfig = {
addresses: {
listen: ['/webrtc', '/p2p-circuit'],
},
transports: [
circuitRelayTransport({
stopTimeout: 60 * 1000, // Reduced from 120s
reservationTtl: 2 * 60 * 1000, // 2 minutes
}),
webSockets({
filter: filters.all,
}),
webRTC({
rtcConfiguration: {
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' },
{ urls: 'stun:stun1.l.google.com:19302' },
{ urls: 'stun:stun2.l.google.com:19302' },
{ urls: 'stun:stun3.l.google.com:19302' },
{ urls: 'stun:stun4.l.google.com:19302' },
{
url: 'turn:numb.viagenie.ca',
credential: 'muazkh',
username: 'webrtc@live.com',
},
{
url: 'turn:turn.anyfirewall.com:443?transport=tcp',
credential: 'webrtc',
username: 'webrtc',
},
{
url: 'turn:turn.bistri.com:80',
credential: 'homeo',
username: 'homeo',
},
],
iceCandidatePoolSize: 10,
},
dataChannel: {
maxMessageSize: 256 * 1024, // 256KB
maxBufferedAmount: 16 * 1024 * 1024, // 16MB
closeTimeout: 30 * 1000, // 30s
openTimeout: 30 * 1000, // 30s
bufferedAmountLowEventTimeout: 30 * 1000,
},
inboundConnectionTimeout: 60 * 1000, // 60s
}),
],
connectionEncrypters: [noise()],
streamMuxers: [
yamux({
maxInboundStreams: 1000,
maxOutboundStreams: 1000,
maxMessageSize: 256 * 1024 * 1024, // 256MB
enableKeepAlive: true,
keepAliveInterval: 30000, // 30s keepalive
}),
],
services: {
ping: ping({
protocolPrefix: 'ipfs',
maxInboundStreams: 10,
maxOutboundStreams: 10,
timeout: 10000,
}),
identify: identify({
protocolPrefix: 'ipfs',
timeout: 30000,
maxInboundStreams: 10,
maxOutboundStreams: 10,
}),
autoNAT: autoNAT({
protocolPrefix: 'ipfs',
timeout: 30000,
}),
},
peerDiscovery: [
bootstrap({
list: BOOTSTRAP_NODES,
timeout: 30000,
tagName: 'bootstrap',
tagValue: 50,
tagTTL: 120000,
}),
],
connectionGater: {
denyDialMultiaddr: () => false,
denyInboundConnection: () => false,
denyDialPeer: () => false,
denyOutboundConnection: () => false,
denyInboundEncryptedConnection: () => false,
denyOutboundUpgradedConnection: () => false,
denyInboundUpgradedConnection: () => false,
},
connectionManager: {
maxConnections: 100,
minConnections: 5,
dialTimeout: 30 * 1000,
maxPeerAddrsToDial: 25,
maxIncomingPendingConnections: 50,
maxParallelDials: 10,
inboundStreamProtocolNegotiationTimeout: 30 * 1000,
outboundStreamProtocolNegotiationTimeout: 30 * 1000,
inboundConnectionThreshold: 5,
autoDialInterval: 10000,
autoDialMaxQueueLength: 100,
},
};
Relay Server
server, err := libp2p.New(
libp2p.ConnectionManager(cgMgr),
libp2p.ListenAddrStrings(addresses...),
libp2p.Identity(id),
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(websocket.New),
libp2p.Security(noise.ID, noise.New),
libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport),
libp2p.EnableAutoNATv2(),
libp2p.EnableHolePunching(),
libp2p.ForceReachabilityPrivate(),
libp2p.ForceReachabilityPublic(),
)
if err != nil {
log.Fatalf("Failed to create libp2p host: %v", err)
}
// Relay
_, err = relay.New(server, relay.WithACL(&MyACLFilter{}))
if err != nil {
log.Fatalf("Failed to start relay v2 service: %v", err)
}
// Identity
idService, _ := identify.NewIDService(server)
_, err = autonat.New(server)
if err != nil {
log.Fatalf("Failed to start AutoNAT: %v", err)
}
// Holepunch
_, err = holepunch.NewService(server, idService, func() []multiaddr.Multiaddr {
log.Println("Getting listen addresses")
return server.Addrs()
})
if err != nil {
log.Fatalf("Failed to start hole punching service: %v", err)
}