Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feat/merge-stat-pro…
Browse files Browse the repository at this point in the history
…perties
  • Loading branch information
achingbrain committed Jun 28, 2023
2 parents 149c456 + 7fb23cd commit 8242b2d
Show file tree
Hide file tree
Showing 145 changed files with 1,873 additions and 879 deletions.
32 changes: 3 additions & 29 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,42 +167,16 @@ jobs:
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run test:interop -- --bail

test-example:
test-examples:
needs: build
runs-on: ubuntu-latest
strategy:
matrix:
example: [
chat,
connection-encryption,
discovery-mechanisms,
echo,
libp2p-in-the-browser,
peer-and-content-routing,
pnet,
protocol-and-stream-muxing,
pubsub,
transports
]
fail-fast: true
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
with:
directories: |
./examples/node_modules
~/.cache
build: |
cd examples
npm i
npx playwright install
cache_name: cache-examples
- run: |
cd examples
npm run test -- ${{ matrix.example }}
- run: npm run --if-present test:example

multidim-interop:
needs: build
Expand Down Expand Up @@ -238,7 +212,7 @@ jobs:
test-electron-main,
test-electron-renderer,
test-interop,
test-example,
test-examples,
multidim-interop
]
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@

## Structure

- [`/examples/auto-relay`](./examples/auto-relay) Shows how to configure relayed connections
- [`/examples/chat`](./examples/chat) An example chat app using libp2p
- [`/examples/connection-encryption`](./examples/connection-encryption) An example of how to configure connection encrypters
- [`/examples/delegated-routing`](./examples/delegated-routing) How to configure libp2p delegated routers
- [`/examples/discovery-mechanisms`](./examples/discovery-mechanisms) How to configure peer discovery mechanisms
- [`/examples/echo`](./examples/echo) An example echo app
- [`/examples/libp2p-in-the-browser`](./examples/libp2p-in-the-browser) A libp2p node running in the browser
- [`/examples/peer-and-content-routing`](./examples/peer-and-content-routing) How to use peer and content routing
- [`/examples/pnet`](./examples/pnet) How to configure a libp2p private network
- [`/examples/protocol-and-stream-muxing`](./examples/protocol-and-stream-muxing) How to use multiplex protocols streams
- [`/examples/pubsub`](./examples/pubsub) An example using libp2p pubsub
- [`/examples/transports`](./examples/transports) An example using different types of libp2p transport
- [`/interop`](./interop) Multidimension Interop Test
- [`/packages/crypto`](./packages/crypto) Crypto primitives for libp2p
- [`/packages/interface`](./packages/interface) The interface implemented by a libp2p node
- [`/packages/interface-compliance-tests`](./packages/interface-compliance-tests) Compliance tests for JS libp2p interfaces
Expand Down
4 changes: 4 additions & 0 deletions examples/auto-relay/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This project is dual licensed under MIT and Apache-2.0.

MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
5 changes: 5 additions & 0 deletions examples/auto-relay/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
19 changes: 19 additions & 0 deletions examples/auto-relay/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
31 changes: 28 additions & 3 deletions examples/auto-relay/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# Auto relay
# @libp2p/example-auto-relay <!-- omit in toc -->

Auto Relay enables libp2p nodes to dynamically find and bind to relays on the network. Once binding (listening) is done, the node can and should advertise its addresses on the network, allowing any other node to dial it over its bound relay(s).
While direct connections to nodes are preferable, it's not always possible to do so due to NATs or browser limitations.
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster)

> Shows how to configure relayed connections
## Table of contents <!-- omit in toc -->

- [0. Setup the example](#0-setup-the-example)
- [1. Set up a relay node](#1-set-up-a-relay-node)
- [2. Set up a listener node with Auto Relay Enabled](#2-set-up-a-listener-node-with-auto-relay-enabled)
- [3. Set up a dialer node for testing connectivity](#3-set-up-a-dialer-node-for-testing-connectivity)
- [4. What is next?](#4-what-is-next)
- [License](#license)
- [Contribution](#contribution)

## 0. Setup the example

Expand Down Expand Up @@ -173,3 +187,14 @@ As you can see from the output, the remote address of the established connection
Before moving into production, there are a few things that you should take into account.

A relay node should not advertise its private address in a real world scenario, as the node would not be reachable by others. You should provide an array of public addresses in the libp2p `addresses.announce` option. If you are using websockets, bear in mind that due to browser’s security policies you cannot establish unencrypted connection from secure context. The simplest solution is to setup SSL with nginx and proxy to the node and setup a domain name for the certificate.

## License

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

## Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
8 changes: 5 additions & 3 deletions examples/auto-relay/dialer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createLibp2p } from 'libp2p'
import { webSockets } from '@libp2p/websockets'
/* eslint-disable no-console */

import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { mplex } from '@libp2p/mplex'
import { webSockets } from '@libp2p/websockets'
import { multiaddr } from '@multiformats/multiaddr'
import { yamux } from '@chainsafe/libp2p-yamux'
import { createLibp2p } from 'libp2p'
import { circuitRelayTransport } from 'libp2p/circuit-relay'
import { identifyService } from 'libp2p/identify'

Expand Down
8 changes: 5 additions & 3 deletions examples/auto-relay/listener.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createLibp2p } from 'libp2p'
import { webSockets } from '@libp2p/websockets'
/* eslint-disable no-console */

import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { mplex } from '@libp2p/mplex'
import { webSockets } from '@libp2p/websockets'
import { multiaddr } from '@multiformats/multiaddr'
import { yamux } from '@chainsafe/libp2p-yamux'
import { createLibp2p } from 'libp2p'
import { circuitRelayTransport } from 'libp2p/circuit-relay'
import { identifyService } from 'libp2p/identify'

Expand Down
46 changes: 46 additions & 0 deletions examples/auto-relay/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@libp2p/example-auto-relay",
"version": "0.0.0",
"description": "Shows how to configure relayed connections",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/examples/auto-relay#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/libp2p/js-libp2p.git"
},
"bugs": {
"url": "https://github.com/libp2p/js-libp2p/issues"
},
"type": "module",
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
},
"scripts": {
"lint": "aegir lint",
"test:example": "node test.js"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^12.0.1",
"@chainsafe/libp2p-yamux": "^4.0.2",
"@libp2p/mplex": "^8.0.0",
"@libp2p/websockets": "^6.0.0",
"@multiformats/multiaddr": "^12.1.3",
"libp2p": "^0.45.0"
},
"devDependencies": {
"aegir": "^39.0.10",
"execa": "^7.1.1",
"p-defer": "^4.0.0",
"uint8arrays": "^4.0.4"
},
"private": true
}
10 changes: 6 additions & 4 deletions examples/auto-relay/relay.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createLibp2p } from 'libp2p'
import { webSockets } from '@libp2p/websockets'
/* eslint-disable no-console */

import { noise } from '@chainsafe/libp2p-noise'
import { mplex } from '@libp2p/mplex'
import { yamux } from '@chainsafe/libp2p-yamux'
import { mplex } from '@libp2p/mplex'
import { webSockets } from '@libp2p/websockets'
import { createLibp2p } from 'libp2p'
import { circuitRelayServer } from 'libp2p/circuit-relay'
import { identifyService } from 'libp2p/identify'

Expand All @@ -20,7 +22,7 @@ async function main () {
noise()
],
streamMuxers: [
yamux(),mplex()
yamux(), mplex()
],
services: {
identify: identifyService(),
Expand Down
122 changes: 60 additions & 62 deletions examples/auto-relay/test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import { fileURLToPath } from 'url'
import { execa } from 'execa'
import pDefer from 'p-defer'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { fileURLToPath } from 'url'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

Expand All @@ -13,81 +13,79 @@ function startProcess (name, args = []) {
})
}

export async function test () {
let output1 = ''
let output2 = ''
let output3 = ''
let relayAddr
let autoRelayAddr
let output1 = ''
let output2 = ''
let output3 = ''
let relayAddr
let autoRelayAddr

const proc1Ready = pDefer()
const proc2Ready = pDefer()
const proc1Ready = pDefer()
const proc2Ready = pDefer()

// Step 1 process
process.stdout.write('relay.js\n')
// Step 1 process
process.stdout.write('relay.js\n')

const proc1 = startProcess('relay.js')
proc1.all.on('data', async (data) => {
process.stdout.write(data)
const proc1 = startProcess('relay.js')
proc1.all.on('data', async (data) => {
process.stdout.write(data)

output1 += uint8ArrayToString(data)
output1 += uint8ArrayToString(data)

if (output1.includes('Listening on:') && output1.includes('/p2p/')) {
relayAddr = output1.trim().split('Listening on:\n')[1].split('\n')[0]
proc1Ready.resolve()
}
})
if (output1.includes('Listening on:') && output1.includes('/p2p/')) {
relayAddr = output1.trim().split('Listening on:\n')[1].split('\n')[0]
proc1Ready.resolve()
}
})

await proc1Ready.promise
process.stdout.write('==================================================================\n')
await proc1Ready.promise
process.stdout.write('==================================================================\n')

// Step 2 process
process.stdout.write('listener.js\n')
// Step 2 process
process.stdout.write('listener.js\n')

const proc2 = startProcess('listener.js', [relayAddr])
proc2.all.on('data', async (data) => {
process.stdout.write(data)
const proc2 = startProcess('listener.js', [relayAddr])
proc2.all.on('data', async (data) => {
process.stdout.write(data)

output2 += uint8ArrayToString(data)
output2 += uint8ArrayToString(data)

if (output2.includes('Advertising with a relay address of') && output2.includes('/p2p/')) {
autoRelayAddr = output2.trim().split('Advertising with a relay address of ')[1].trim()
proc2Ready.resolve()
}
})
if (output2.includes('Advertising with a relay address of') && output2.includes('/p2p/')) {
autoRelayAddr = output2.trim().split('Advertising with a relay address of ')[1].trim()
proc2Ready.resolve()
}
})

await proc2Ready.promise
process.stdout.write('==================================================================\n')
await proc2Ready.promise
process.stdout.write('==================================================================\n')

// Step 3 process
process.stdout.write('dialer.js\n')
// Step 3 process
process.stdout.write('dialer.js\n')

const proc3 = startProcess('dialer.js', [autoRelayAddr])
proc3.all.on('data', async (data) => {
process.stdout.write(data)
const proc3 = startProcess('dialer.js', [autoRelayAddr])
proc3.all.on('data', async (data) => {
process.stdout.write(data)

output3 += uint8ArrayToString(data)
output3 += uint8ArrayToString(data)

if (output3.includes('Connected to the auto relay node via')) {
const remoteAddr = output3.trim().split('Connected to the auto relay node via ')[1]
if (output3.includes('Connected to the auto relay node via')) {
const remoteAddr = output3.trim().split('Connected to the auto relay node via ')[1]

if (remoteAddr === autoRelayAddr) {
proc3.kill()
proc2.kill()
proc1.kill()
} else {
throw new Error('dialer did not dial through the relay')
}
if (remoteAddr === autoRelayAddr) {
proc3.kill()
proc2.kill()
proc1.kill()
} else {
throw new Error('dialer did not dial through the relay')
}
})

await Promise.all([
proc1,
proc2,
proc3
]).catch((err) => {
if (err.signal !== 'SIGTERM') {
throw err
}
})
}
}
})

await Promise.all([
proc1,
proc2,
proc3
]).catch((err) => {
if (err.signal !== 'SIGTERM') {
throw err
}
})
Loading

0 comments on commit 8242b2d

Please sign in to comment.