Giter VIP home page Giter VIP logo

subins2000 / p2pt Goto Github PK

View Code? Open in Web Editor NEW
518.0 13.0 55.0 266 KB

Simple WebRTC Peer 2 Peer connections using WebTorrent trackers as the signalling server. Use WebTorrent trackers for any kind of WebRTC app ! 🔥 Make WebRTC apps fast & easy ! 🚀⭐

Home Page: https://WebDrop.Space

License: MIT License

JavaScript 100.00%
webtorrent p2p webrtc webtorrent-trackers signalling-server websocket-trackers

p2pt's Introduction

P2PT

Simple library to establish P2P connections, communicate messages using WebTorrent Trackers (WebSocket) as the signalling server. Make any kind of WebRTC app using WebTorrent trackers as general-purpose WebRTC signalling servers.

Works in both browser & node environment.

Features

  • Easy to use API
  • Send long messages: Data splitted into chunks, sent, received and reassembled all by the library !
  • Use WebSocket Trackers as signalling servers
  • JSON messaging system
  • Send & Respond to messages in a chain using Promise

How Does It Work ?

The amazing WebTorrent library created a new kind of Torrent Trackers called "WebSocket Trackers" also known as "WebTorrent Trackers". Some torrent clients can use these new trackers to share files.

Browser torrent clients (example: BTorrent) only have the capability to communicate to these WebTorrent trackers and other browser peers (known as web peers). Because, JavaScript in browser can't directly make TCP/IP connections and communicate. Read more about how WebTorrent works here.

WebRTC is the method by which browsers can communicate to other browsers peer to peer (P2P). WebTorrent makes use of WebRTC for sharing Torrents on web.

But, to establish P2P connections, a signalling server is needed. Signalling servers can be made in any way. But, you'll have to host it yourself. In WebTorrent, it's the WebSocket trackers that are the signalling servers. What if we use those trackers to establish P2P connections for our apps ?! That is what P2PT does ! :)

How do we find peers for torrent to download ? We use a magnet link. That magnet link has a unique identifier for our torrent called the Info Hash. This ID will be unique for all torrents.

Similarly, to build our apps, we use a identifier. This identifier is converted to a valid Info Hash and sent to our WebTorrent trackers who will give us a list of web peers. These web peers would be the other users also using our app :

var p2pt = new P2PT(trackersAnnounceURLs, 'myApp')

And that is how P2PT works.

Examples

Apps Built With P2PT

Add yours here ! Send a PR ! 🚀

Simple Example

Open this webpage in two separate browser windows. You'll see the messages. It's a codepen, you can fiddle with the code there.

p2pt's People

Contributors

anupam-git avatar arilotter avatar elvistony avatar prinzpiuz avatar pubkey avatar subins2000 avatar thaunknown avatar therealadityashankar avatar

Stargazers

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

Watchers

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

p2pt's Issues

Consider switching from randombytes to some other dependency that does the same thing

[email protected] is causing problems with [email protected] and [email protected] because of its use of "global.crypto" as seen on the trace below with the relevant code snippet from randombytes

browser.js:16 Uncaught ReferenceError: global is not defined
    at node_modules/.pnpm/[email protected]/node_modules/randombytes/browser.js (browser.js:16:14)
    at __require (chunk-RSJERJUL.js?v=ff5c038d:3:50)
    at node_modules/.pnpm/[email protected]/node_modules/simple-peer/index.js (index.js:4:21)
    at __require (chunk-RSJERJUL.js?v=ff5c038d:3:50)
    at node_modules/.pnpm/[email protected]/node_modules/bittorrent-tracker/lib/client/websocket-tracker.js (websocket-tracker.js:3:14)
    at __require (chunk-RSJERJUL.js?v=ff5c038d:3:50)
    at node_modules/.pnpm/[email protected]/node_modules/p2pt/p2pt.js (p2pt.js:7:26)
    at __require (chunk-RSJERJUL.js?v=ff5c038d:3:50)
    at dep:p2pt:1:16
node_modules/.pnpm/[email protected]/node_modules/randombytes/browser.js @ browser.js:16
__require @ chunk-RSJERJUL.js?v=ff5c038d:3
node_modules/.pnpm/[email protected]/node_modules/simple-peer/index.js @ index.js:4
__require @ chunk-RSJERJUL.js?v=ff5c038d:3
node_modules/.pnpm/[email protected]/node_modules/bittorrent-tracker/lib/client/websocket-tracker.js @ websocket-tracker.js:3
__require @ chunk-RSJERJUL.js?v=ff5c038d:3
node_modules/.pnpm/[email protected]/node_modules/p2pt/p2pt.js @ p2pt.js:7
__require @ chunk-RSJERJUL.js?v=ff5c038d:3
(anonymous) @ dep:p2pt:1

browser.js line 16
the code on the randombytes repo

var crypto = global.crypto || global.msCrypto

since this library only uses randombytes to generate peer ids, i think it doesn't really need a library that uses the browser's crypto thing
but you guys did it that way for a reason, so if i'm wrong please correct me and explain why it's not the case :)

addTrack to peer connection

Hello,

I read the comment on Issue: #29 and then tried to add a video track to the connection analog the description from here: https://webrtc.org/getting-started/remote-streams . The first issue was to get the peerConnection, which I managed to do with a hook at:

this._pc = new (this._wrtc.RTCPeerConnection)(this.config)

else I could only grab the peerConnection at:

p2pt.on('peerconnect', peer => peer._pc)

which according to the https://webrtc.org/getting-started/remote-streams here would be too late but I am not certain. Anyhow, in a further step I add the video and audio tracks:

p2pt.on('track', event => console.log('p2ptTrack', event))
pc.addEventListener('track', event => console.log('pcTrack', event))
// https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
navigator.mediaDevices.getUserMedia({
audio: true, // We want an audio track
video: true // And we want a video track
}) .then(localStream => {
// https://webrtc.org/getting-started/remote-streams
localStream.getTracks().forEach(track => pc.addTrack(track, localStream))
})

I was not able to get any 'track' event. Even when I did overwrite or hook within:

this._pc.ontrack = event => { this._onTrack(event) }

Does anyone have a solution for this problem?

Usage limits

I know this can be silly but I am curious about can we connect same chat room with 15.000 users?

Modernify

Will be done in branch modernify

  • Use vite
  • Move to typescript

Add a discord chat

there are so-so many things possible from this,

  • one can build a truly decentralized social network !, that's actually peer to peer hosted
  • If one build a wasm/javascript blockchain, one could probably build entire e-commerce/video-sharing/practically possibly anything with this

I didn't know this was possible, I was initially quite frustrated by the nature of webtorrent to only provide static sites, but this is truly amazing !

I'd love to talk more about this, tbh this is like really freaking awesome

Questions about scalability

From what I've been reading in your documentation and some of the issues here, P2PT is a fully meshed browser to browser P2P solution. Is that correct to say it is "fully meshed?"

If so, what would occur if 50,000 people suddenly ran your codepen examples? Would everyone's browsers crash? I believe this issue may address that question, but I want to be certain. Apologies if it is a duplicate.

If so, couldn't P2PT be updated to provide a wide "gossip" protocol similar to the way bugout is doing? If possible, it could take this library to the next level and address issues of scalability at the library level and make it more popular and useful.

can you do video streaming?

I want to create an app where I can transmit videos with more than 5 people using this library, is it possible to do it? if so, how?

Possibility to destroy only tracker connection and not peer connections

Lets say i'm building an app that only wants to connect to peers initially, but later can disconnect to the tracker because we have established a connection. This does not work currently because the destroy function disconnects everything.

proposal:

Implement additional method for disconnecting trackers:

p2p.destroyTrackers()

Does not connect to peer when using multiple trackers

Test

Connect two peers together (same code used on both peers). In my first example, the peers find each other in about 5seconds (right after tracker is connected, which takes roughly 5sec). In my second setup, both peers connect to both trackers, but neither of them receive a "peerconnect" event.

Setup that works:

Host a ws tracker on port 8000, localhost

var trackersAnnounceURLs = [
  "ws://localhost:8000"
]

const client = new P2PT(trackersAnnounceURLs, 'MyTrackerIDHere')
client.on('peerconnect', peer => console.log("Connected to", peer.id))
client.start()

Setup that DOESN'T work:

Host two ws trackers on port 8000, and 8001, localhost

var trackersAnnounceURLs = [
  "ws://localhost:8000",
  "ws://localhost:8001",
]

const client = new P2PT(trackersAnnounceURLs, 'MyTrackerIDHere')
client.on('peerconnect', peer => console.log("Connected to", peer.id))
client.start()

It does seem like something is fishy here, because if I add a third tracker, I get segmentation fault...

Documentation

Hey.
This library seems cool and could have millions of usages.

It needs a good documentation even if it is self explanatory. Let me know if you are interested in a documentation. I can send a PR :)

Hello Mr Decentralized 🙏

I have a question to you, because from your projects one can see, you are THE guy who surely understands this! 🙏
And i was inspired by you!

No1 (question in the picture)
image

No2 (basically)
image

Would you aggree this works?

I call it "Webtorrent Beacon in Space" btw.

add peer

can I add peer knowing only its id?

Rewrite with Typescript

The current version is written with javascript and is missing type definitions.

This is a proposal to rewrite the library with Typescript.

同学,您这个项目引入了674个开源组件,存在7个漏洞,辛苦升级一下

检测到 subins2000/p2pt 一共引入了674个开源组件,存在7个漏洞

漏洞标题:serialize-javascript 代码问题漏洞
缺陷组件:[email protected]
漏洞编号:CVE-2020-7660
漏洞描述:Verizon serialize-javascript是美国威瑞森电信(Verizon)公司的一款支持将JavaScript序列化为 JSON超集的软件包。
serialize-javascript 3.1.0之前版本中存在代码问题漏洞。远程攻击者可借助index.js文件中的‘deleteFunctions’函数利用该漏洞注入任意代码。
国家漏洞库信息:https://www.cnvd.org.cn/flaw/show/CNVD-2020-53801
影响范围:(∞, 3.1.0)
最小修复版本:3.1.0
缺陷组件引入路径:[email protected]>[email protected]>[email protected]>[email protected]

另外还有7个漏洞,详细报告:https://mofeisec.com/jr?p=i9e74a

How to send files?

How do you send files with this? do you just wait in the "msg" event for file transfer complete, or do you have to assemble the chunks?
I also tried using it with simple-peer-files, but no luck.
Sorry I am still new with WebRTC and simple peer, I would really appreciate if someone can explain the process a bit better

Chrome and Spinoffs eg. Edge not working

Hi and first off, great Repo!

I have tested your example: https://weedshaker.github.io/event-driven-web-components-p2pt/indexExample.html
and also wrote my event driven example with basically the exact same usage of the api: https://weedshaker.github.io/event-driven-web-components-p2pt/

And I just can't make Chrome, Brave or Edge connect to each other. Although, my Chrome Version 108.0.5359.124 on Linux works with localhost. Could it be, that those Chromium-Browser have an issue with the github pages enforcing HTTPS?

Tracker-swarm to help with low quality public tracker servers

I created the following package that lets anyone create a tracker server and subsequent tracker server swarm around a shared application name: tracker-swarm. I built this to solve problems with unresponsive public tracker servers. I've tested deployments in Heroku and remixes on Glitch, and it seems to be working pretty well. It should work with P2PT. I've only tested with Bugout so far.

Issues connecting with Peers on Different Networks

I recently noticed I faced issues in Connecting to 2 nodes in different networks. Board-IO
The Devices were on 2 Networks,

  • One on Jio
  • One on Keralavision

Both Didn't connect but on the Keralavision Network i noticed some ........ disconnected console log messages.

I'm assuming this is caused coz of the IP Pool issue of Jio Network? Coz I've heard some of the IP Addresses are IPv6.

I Tested Jio to Jio and it worked fine (for some time). I'm thinking its happening when both devices are from IPv4 and IPv6 separately.

Please look into it.... Thanks in advance!

Unexpected behaviour when bundled by Vite (and likely WebPack and other bundlers)

I'm trying to use this library inside of a Vue project with Vite, however, peer connections get instantly closed in that setup. I also had to add @esbuild-plugins/node-globals-polyfill and events packages in order to get P2PT to work in Vite at all, so perhaps it could be related to that. If I include P2PT using a <script> tag in the same setup it works without a problem.

I have created the following reproduction projects on Stackblitz:

If you open the import project in 2 separate tabs, you will see something along these lines:

_peerId e3560c413ad577eac4067144ac915ad9ac84fc82
trackerconnect wss://tracker.btorrent.xyz
peerclose a7e07222c5299d33686b64a70e95494a49f181a8

As you can see, the peerclose event is fired right after the tracker connection is established, where peerconnect is expected instead.

If you open the script project, where the only difference is the way P2PT is imported, you will see something along these lines:

_peerId 0e35820543c1491086ff837c80fc7f34aa9dec86
trackerconnect wss://tracker.btorrent.xyz
peerconnect 4bf610b001ff297abe9180a59379e30d462155da

I've also tried something similar on a WebPack project and had the same issue as with Vite, although I did not try to isolate it in a reproducible project. I suspect this issue might occur with other bundlers as well.

Man in the middle attack warnings - update connection to be more secure

trackers are vulnerable to mitm attackes,, if I set-up a "free" tracker, then then, when the connection is supposed to happen I connect the client to my "evil webrtc device" instead of the actual device he wants to, and connect the "evil webrtc device" to the intended recipient, boom, mitm attack

How does one prevent an MITM attack/ensure the data is unread ?

  • send spoof data (which could be large occasionally)
  • encrypt data FROM THE START, symmetric encryption should be fine if both the target and the owner know the secret key

TURN Server and ICE Candidates

Hi, if the communication cannot be established for some reasons, is there a way to add a iceServers configuration to WebRTC?

Great Lib, thanks ... I use it to establish a classroom in LiaScript :-)

Slow tracker connection

I've been starting to experiment with a p2p protocol using webRTC and initially wrote my own signaling server. Then I realized that I might be able to use webtorrent signaling server. So I found this library and after 30 mins of coding, I had replaced my tracker logic with this library. By then a public webtorrent tracker could be used to connect clients together. Great!

However, it is much slower to connect to the tracker than my old logic. Even if I run a local tracker on my machine, it takes a few seconds for it to connect (like 5sec). I'm just curious why it's so slow. My own implementation is nearly instant. I guess it might be because if magnet link generation and stuff like that, but it still beats me. I'm thinking if this might have been a downgrade because of that.

Anyway, thanks for the library.

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.