diff --git a/config/cfg.go b/config/cfg.go index daf79a514..260bfc56d 100644 --- a/config/cfg.go +++ b/config/cfg.go @@ -26,7 +26,7 @@ var ( ChainNodeType = "test" //P2P P2P_hostIp = "127.0.0.1" - P2P_sourcePort = 4001 + P2P_sourcePort = 60801 BootstrapPeers addrList ) @@ -56,8 +56,8 @@ func Init() { return } ServerPort = section.Key("port").MustInt(60020) - ReadTimeout = time.Duration(section.Key("readTimeout").MustInt(5)) * time.Second - WriteTimeout = time.Duration(section.Key("writeTimeout").MustInt(5)) * time.Second + ReadTimeout = time.Duration(section.Key("readTimeout").MustInt(60)) * time.Second + WriteTimeout = time.Duration(section.Key("writeTimeout").MustInt(60)) * time.Second htlcNode, err := Cfg.GetSection("htlc") if err != nil { diff --git a/config/conf.ini b/config/conf.ini index ddc43e8ee..3f295465a 100644 --- a/config/conf.ini +++ b/config/conf.ini @@ -12,7 +12,7 @@ pass = omnipass123 [p2p] hostIp = 127.0.0.1 -sourcePort = 60801 +sourcePort = 60081 [tracker] ;Trackers offer such anomymous full node services:monitor node service quality, channel balances if the channel is not private, update routing table for connected nodes, broadcaste transactions, etc. @@ -21,6 +21,6 @@ sourcePort = 60801 ;We suggest you to connect the trackers we deployed for the public: ;https://omnilaboratory.github.io/obd/#/nodes-in-testnet ; -;host = 62.234.216.108:60060 -host = 127.0.0.1:60060 +host = 62.234.216.108:60060 +;host = 127.0.0.1:60060 diff --git a/go.mod b/go.mod index e9abb602d..d60d2a619 100644 --- a/go.mod +++ b/go.mod @@ -21,9 +21,11 @@ require ( github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible github.com/lestrrat-go/strftime v1.0.1 // indirect github.com/libp2p/go-libp2p v0.12.0 + github.com/libp2p/go-libp2p-circuit v0.4.0 github.com/libp2p/go-libp2p-core v0.7.0 github.com/libp2p/go-libp2p-discovery v0.5.0 github.com/libp2p/go-libp2p-kad-dht v0.11.1 + github.com/libp2p/go-libp2p-swarm v0.3.1 github.com/multiformats/go-multiaddr v0.3.1 github.com/satori/go.uuid v1.2.0 github.com/shopspring/decimal v1.2.0 diff --git a/lightclient/p2p_util.go b/lightclient/p2p_util.go index 0da35a708..3dadd54bb 100644 --- a/lightclient/p2p_util.go +++ b/lightclient/p2p_util.go @@ -15,6 +15,7 @@ import ( "github.com/libp2p/go-libp2p-core/peerstore" discovery "github.com/libp2p/go-libp2p-discovery" dht "github.com/libp2p/go-libp2p-kad-dht" + swarm "github.com/libp2p/go-libp2p-swarm" "github.com/multiformats/go-multiaddr" "github.com/omnilaboratory/obd/bean" "github.com/omnilaboratory/obd/config" @@ -38,6 +39,7 @@ const protocolIdForBetweenObd = "obd/betweenObd/1.0.1" const protocolIdForScanObd = "obd/forScanObd/1.0.1" var hostNode host.Host +var relayNode string var localServerDest string var p2PLocalPeerId string @@ -75,6 +77,7 @@ func StartP2PNode() (err error) { hostNode, err = libp2p.New( ctx, libp2p.ListenAddrs(sourceMultiAddr), + libp2p.EnableRelay(), libp2p.Identity(prvKey), ) if err != nil { @@ -97,7 +100,8 @@ func StartP2PNode() (err error) { hostNode.SetStreamHandler(protocolIdForScanObd, handleScanStream) hostNode.SetStreamHandler(protocolIdForBetweenObd, handleStream) - kademliaDHT, _ := dht.New(ctx, hostNode, dht.Mode(dht.ModeAuto)) + //kademliaDHT, _ := dht.New(ctx, hostNode, dht.Mode(dht.ModeAutoServer)) + kademliaDHT, _ := dht.New(ctx, hostNode) if err != nil { log.Println(err) return err @@ -112,13 +116,18 @@ func StartP2PNode() (err error) { for _, peerAddr := range config.BootstrapPeers { peerInfo, _ := peer.AddrInfoFromP2pAddr(peerAddr) wg.Add(1) + go func() { defer wg.Done() err = hostNode.Connect(ctx, *peerInfo) + if err != nil { log.Println(err, peerInfo) } else { log.Println("connected to bootstrap node ", *peerInfo) + if len(relayNode) == 0 { + relayNode = peerInfo.ID.Pretty() + } } }() } @@ -135,6 +144,7 @@ func connP2PNode(dest string) (string, error) { log.Println("wrong dest address") return "", errors.New("wrong dest address") } + ctx := context.Background() destMaddr, err := multiaddr.NewMultiaddr(dest) if err != nil { @@ -157,6 +167,24 @@ func connP2PNode(dest string) (string, error) { return " Remote peer has been connected", nil } + relayAddr, err := multiaddr.NewMultiaddr("/p2p/" + relayNode + "/p2p-circuit/p2p/" + destHostPeerInfo.ID.Pretty()) + if err != nil { + log.Println(err) + return "", err + } + hostNode.Network().(*swarm.Swarm).Backoff().Clear(destHostPeerInfo.ID) + peerRelayInfo := peer.AddrInfo{ + ID: destHostPeerInfo.ID, + Addrs: []multiaddr.Multiaddr{relayAddr}, + } + + if err := hostNode.Connect(ctx, peerRelayInfo); err != nil { + log.Println(err) + return "", err + } else { + log.Println("Connection established with RELAY node:", relayAddr) + } + hostNode.Peerstore().AddAddrs(destHostPeerInfo.ID, destHostPeerInfo.Addrs, peerstore.PermanentAddrTTL) stream, err := hostNode.NewStream(context.Background(), destHostPeerInfo.ID, protocolIdForBetweenObd) diff --git a/tracker/config/conf.ini b/tracker/config/conf.ini index 31a5c5898..cd60f56ec 100644 --- a/tracker/config/conf.ini +++ b/tracker/config/conf.ini @@ -12,5 +12,5 @@ pass = omnipass123 [p2p] localHostIp = 127.0.0.1 -sourcePort = 60800 -bootstrapPeers = /ip4/127.0.0.1/tcp/60800/p2p/QmaBNPR88FMbdjm6UScNRMLUiiu7i6sdbz48jRhC6UmRzR \ No newline at end of file +sourcePort = 60080 +;bootstrapPeers = /ip4/127.0.0.1/tcp/60080/p2p/QmaBNPR88FMbdjm6UScNRMLUiiu7i6sdbz48jRhC6UmRzR \ No newline at end of file diff --git a/tracker/service/p2p_service.go b/tracker/service/p2p_service.go index f96239b88..0775a300f 100644 --- a/tracker/service/p2p_service.go +++ b/tracker/service/p2p_service.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "fmt" "github.com/libp2p/go-libp2p" + circuit "github.com/libp2p/go-libp2p-circuit" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/network" @@ -42,7 +43,7 @@ func StartP2PNode() { ctx, libp2p.ListenAddrs(sourceMultiAddr), libp2p.Identity(prvKey), - libp2p.EnableRelay(), + libp2p.EnableRelay(circuit.OptHop), ) if err != nil { panic(err) @@ -50,7 +51,7 @@ func StartP2PNode() { cfg.P2pLocalAddress = fmt.Sprintf("/ip4/%s/tcp/%v/p2p/%s", cfg.P2P_hostIp, cfg.P2P_sourcePort, hostNode.ID().Pretty()) log.Println("local p2p node address: ", cfg.P2pLocalAddress) - kademliaDHT, _ := dht.New(ctx, hostNode, dht.Mode(dht.ModeAutoServer)) + kademliaDHT, _ := dht.New(ctx, hostNode, dht.Mode(dht.ModeServer)) if err != nil { panic(err)