Giter Club home page Giter Club logo

hole-punching-go-libp2p's Introduction

说明

该仓库没有参考价值,本人不是从事该方面工作,研究纯属兴趣。如有需要请直接看go-libp2p的example和对应的源码。

go-libp2p p2p传输

解决在NAT后面的两个节点之间进行通信

下载

$ git clone https://github.com/zfunnily/hole-punching-go-libp2p.git hole-punching

使用方法

编译 & 启动

$ cd hole-punching && go mod tidy
$ ./build.sh

启动中继服务

./relay -sp 3001
2021/11/08 15:10:01 Run './chat -relay /ip4/172.16.3.205/tcp/3001/p2p/QmVJoYJC447ZVqQxWfWUecFAXPFd6QbT9mQvHuew3jaCBd' on another console.
2021/11/08 15:10:01 You can replace 192.168.0.100 with public IP as well.
2021/11/08 15:10:01 Waiting for incoming connection

在窗口A启动chat 连上中继

./chat -relay /ip4/172.16.3.205/tcp/3001/p2p/QmVJoYJC447ZVqQxWfWUecFAXPFd6QbT9mQvHuew3jaCBd
2021/11/08 15:10:09 start ConnectNodeByRelay.... QmWtaMSNakQ8x5LUf4TpMb9JQy3PPSTPS4yNjRFyxAmaom /ip4/172.16.3.205/tcp/56309
2021/11/08 15:10:09 receive onFirstP2PMsg:  Hello Im QmT2qBsSWkTHPHkgfd6aUZQiF7j2ArmWF9NwQ8R62Wixrw

在窗口B启动chat 连上中继

./chat -relay /ip4/172.16.3.205/tcp/3001/p2p/QmVJoYJC447ZVqQxWfWUecFAXPFd6QbT9mQvHuew3jaCBd
2021/11/08 15:10:09 start ConnectNodeByRelay.... QmT2qBsSWkTHPHkgfd6aUZQiF7j2ArmWF9NwQ8R62Wixrw /ip4/172.16.3.205/tcp/56306
2021/11/08 15:10:09 receive onFirstP2PMsg:  Hello Im QmWtaMSNakQ8x5LUf4TpMb9JQy3PPSTPS4yNjRFyxAmaom
2021/11/08 15:10:14 Cmdp2p start ....
2021/11/08 15:10:14 opening p2p chat stream
2021/11/08 15:10:14 [INFO] p2p chat connected!
  • A和B连上了中继后,中继服务会返回对方的地址 s.Conn.RemoteMultiaddr()
  • A和B接收到了地址后,发送了第一条消息FirstP2PMsg后,则过了5秒后直连对方节点n.HoleService.DirectConnect(peerInfo.ID),代码可见,(自动p2p打洞).
  • A和B可以进行p2p沟通。可以试着把relay-server关闭,然后继续发送消息。可见消息能发送成功。

这里p2p的打洞流程参考 coordination.go:

func (hs *Service) handleNewStream(s network.Stream) {
// Check directionality of the underlying connection.
// Peer A receives an inbound connection from peer B.
// Peer A opens a new hole punch stream to peer B.
// Peer B receives this stream, calling this function.
// Peer B sees the underlying connection as an outbound connection.
}

更改日志

  • 22/04/29 增加一个ubuntu下可执行的relay-server
  • 22/04/29 chat.go注释直连代码. 只能通过中继模式通信。
  • go-libp2p的代码打洞的代码都已经写好了。看测试用例,理解了之后就能写出来了。
  • 后续有时间再研究。

博客地址:https://zfunnily.github.io/2021/11/gop2pfour/

hole-punching-go-libp2p's People

Contributors

zfunnily avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hole-punching-go-libp2p's Issues

关于直连代码拦截

直连拦截后只能通过中继进行p2p通信,请问有无解决方法可以避免拦截重新实现打洞直连?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.