-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
examples/js-libp2p-example-pnet/.github/pull_request_template.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# ⚠️ IMPORTANT ⚠️ | ||
|
||
# Please do not create a Pull Request for this repository | ||
|
||
The contents of this repository are automatically synced from the parent [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples) so any changes made to the standalone repository will be lost after the next sync. | ||
|
||
Please open a PR against [js-libp2p Examples](https://github.com/libp2p/js-libp2p-examples) instead. | ||
|
||
## Contributing | ||
|
||
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. | ||
|
||
1. Fork the [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples) | ||
2. Create your Feature Branch (`git checkout -b feature/amazing-example`) | ||
3. Commit your Changes (`git commit -a -m 'feat: add some amazing example'`) | ||
4. Push to the Branch (`git push origin feature/amazing-example`) | ||
5. Open a Pull Request |
19 changes: 19 additions & 0 deletions
19
examples/js-libp2p-example-pnet/.github/workflows/sync.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: pull | ||
|
||
on: | ||
workflow_dispatch | ||
|
||
jobs: | ||
sync: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Pull from another repository | ||
uses: ipfs-examples/actions-pull-directory-from-repo@main | ||
with: | ||
source-repo: libp2p/js-libp2p-examples | ||
source-folder-path: examples/${{ github.event.repository.name }} | ||
source-branch: main | ||
target-branch: main | ||
git-username: github-actions | ||
git-email: github-actions@github.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# @libp2p/example-pnet | ||
|
||
[![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-examples.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-examples) | ||
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p-examples/ci.yml?branch=main\&style=flat-square)](https://github.com/libp2p/js-libp2p-examples/actions/workflows/ci.yml?query=branch%3Amain) | ||
|
||
> How to configure a libp2p private network | ||
libp2p networks allow any peer to connect to any other peer and to communicate | ||
with them via protocol streams. | ||
|
||
What if you only want to allow a certain subset of peers to connect to you? It's | ||
possible to use a pre-shared key to create a private network on top of the | ||
public libp2p network using the `@libp2p/pnet` module. | ||
|
||
## Setup | ||
|
||
1. Install the modules in the libp2p root directory, `npm install` and`npm run build`. | ||
|
||
## Run | ||
|
||
Running the example will cause two nodes with the same swarm key to be started | ||
and exchange basic information. | ||
|
||
``` | ||
node index.js | ||
``` | ||
|
||
### Using different keys | ||
|
||
This example includes `TASK` comments that can be used to try the example with | ||
different swarm keys. This will allow you to see how nodes will fail to connect | ||
if they are on different private networks and try to connect to one another. | ||
|
||
To change the swarm key of one of the nodes, look through `index.js` for | ||
comments starting with `TASK` to indicate where lines are that pertain to | ||
changing the swarm key of node 2. | ||
|
||
### Exploring the repos | ||
|
||
Once you've run the example you can take a look at the repos in the `./tmp` | ||
directory to see how they differ, including the swarm keys. You should see a | ||
`swarm.key` file in each of the repos and when the nodes are on the same private | ||
network this contents of the `swarm.key` files should be the same. | ||
|
||
## 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint no-console: ["off"] */ | ||
|
||
import { generateKey } from '@libp2p/pnet' | ||
import { pipe } from 'it-pipe' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
import { privateLibp2pNode } from './libp2p-node.js' | ||
|
||
// Create a Uint8Array and write the swarm key to it | ||
const swarmKey = new Uint8Array(95) | ||
generateKey(swarmKey) | ||
|
||
// This key is for testing a different key not working | ||
const otherSwarmKey = new Uint8Array(95) | ||
generateKey(otherSwarmKey) | ||
|
||
const node1 = await privateLibp2pNode(swarmKey) | ||
|
||
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect | ||
const node2 = await privateLibp2pNode(swarmKey) | ||
// const node2 = await privateLibp2pNode(otherSwarmKey) | ||
|
||
console.log('nodes started...') | ||
|
||
// connect node1 to node2 | ||
await node1.dial(node2.getMultiaddrs()) | ||
|
||
node2.handle('/private', ({ stream }) => { | ||
pipe( | ||
stream, | ||
async function (source) { | ||
for await (const msg of source) { | ||
console.log(uint8ArrayToString(msg.subarray())) | ||
} | ||
} | ||
) | ||
}) | ||
|
||
const stream = await node1.dialProtocol(node2.peerId, '/private') | ||
|
||
await pipe( | ||
[uint8ArrayFromString('This message is sent on a private network')], | ||
stream | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { preSharedKey } from '@libp2p/pnet' | ||
import { tcp } from '@libp2p/tcp' | ||
import { createLibp2p } from 'libp2p' | ||
|
||
/** | ||
* privateLibp2pNode returns a libp2p node function that will use the swarm | ||
* key with the given `swarmKey` to create the Protector | ||
* | ||
* @param {any} swarmKey | ||
*/ | ||
export async function privateLibp2pNode (swarmKey) { | ||
const node = await createLibp2p({ | ||
addresses: { | ||
listen: ['/ip4/0.0.0.0/tcp/0'] | ||
}, | ||
transports: [tcp()], // We're only using the TCP transport for this example | ||
streamMuxers: [yamux()], // We're only using yamux muxing | ||
// Let's make sure to use identifying crypto in our pnet since the protector | ||
// doesn't care about node identity, and only the presence of private keys | ||
connectionEncrypters: [noise()], | ||
// Leave peer discovery empty, we don't want to find peers. We could omit | ||
// the property, but it's being left in for explicit readability. | ||
// We should explicitly dial pnet peers, or use a custom discovery service | ||
// for finding nodes in our pnet | ||
peerDiscovery: [], | ||
connectionProtector: preSharedKey({ | ||
psk: swarmKey | ||
}) | ||
}) | ||
|
||
return node | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "@libp2p/example-pnet", | ||
"version": "0.0.0", | ||
"description": "How to configure a libp2p private network", | ||
"license": "Apache-2.0 OR MIT", | ||
"homepage": "https://github.com/libp2p/js-libp2p-examples/tree/master/examples/js-libp2p-example-pnet#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/libp2p/js-libp2p-examples.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/libp2p/js-libp2p-examples/issues" | ||
}, | ||
"type": "module", | ||
"scripts": { | ||
"test": "test-node-example test/*" | ||
}, | ||
"dependencies": { | ||
"@chainsafe/libp2p-noise": "^16.0.0", | ||
"@chainsafe/libp2p-yamux": "^7.0.0", | ||
"@libp2p/pnet": "^2.0.1", | ||
"@libp2p/tcp": "^10.0.0", | ||
"it-pipe": "^3.0.1", | ||
"libp2p": "^2.0.0", | ||
"uint8arrays": "^5.1.0" | ||
}, | ||
"devDependencies": { | ||
"test-ipfs-example": "^1.1.0" | ||
}, | ||
"private": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import path from 'path' | ||
import { fileURLToPath } from 'url' | ||
import { waitForOutput } from 'test-ipfs-example/node' | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
|
||
await waitForOutput('This message is sent on a private network', 'node', [path.join(__dirname, '../index.js')], { | ||
cwd: __dirname | ||
}) |