Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohaodu committed Jun 10, 2024
1 parent 738c64b commit 9fd0efd
Show file tree
Hide file tree
Showing 6 changed files with 7,238 additions and 8,767 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@libp2p/prometheus-metrics": "^3.0.24",
"@libp2p/pubsub-peer-discovery": "^10.0.2",
"@libp2p/simple-metrics": "^1.0.2",
"@libp2p/tcp": "^9.0.26",
"@libp2p/tls": "^1.0.12",
"@libp2p/webrtc": "^4.0.33",
"@libp2p/websockets": "^8.0.24",
Expand Down
15 changes: 13 additions & 2 deletions packages/local-chat-x-core/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import { Libp2p, createLibp2p } from "libp2p";
import { noise } from "@chainsafe/libp2p-noise";
import { yamux } from "@chainsafe/libp2p-yamux";
import { circuitRelayServer } from "@libp2p/circuit-relay-v2";
import {
circuitRelayServer,
circuitRelayTransport,
} from "@libp2p/circuit-relay-v2";
import { webSockets } from "@libp2p/websockets";
import * as filters from "@libp2p/websockets/filters";
import { identify } from "@libp2p/identify";
Expand All @@ -19,6 +22,7 @@ import { pubsubPeerDiscovery } from "@libp2p/pubsub-peer-discovery";
import { tls } from "@libp2p/tls";
import { prometheusMetrics } from "@libp2p/prometheus-metrics";
import { bootstrap } from "@libp2p/bootstrap";
import { tcp } from "@libp2p/tcp";
const topics = [
`webChatX._peer-discovery._p2p._pubsub`, // It's recommended but not required to extend the global space
];
Expand All @@ -40,12 +44,18 @@ export default class Libp2pManager {
const peerId = await createEd25519PeerId();
return await createLibp2p({
addresses: {
listen: ["/ip4/0.0.0.0/tcp/9000/ws", "/webrtc"], // 替换为实际希望监听的 IP 和端口
listen: ["/ip4/0.0.0.0/tcp/9000/ws", "/ip4/0.0.0.0/tcp/9500"], // 替换为实际希望监听的 IP 和端口
},
transports: [
webSockets({
filter: filters.all,
}),
circuitRelayTransport({
//允许当前节点建立和接受中继连接
discoverRelays: 0, // 要找到多少个网络中继
reservationConcurrency: 1, // 一次要尝试保留多少个继电器的插槽
}),
tcp(),
],
connectionEncryption: [noise(), tls()],
streamMuxers: [yamux()],
Expand Down Expand Up @@ -168,6 +178,7 @@ export default class Libp2pManager {
// 创建并初始化 Relay 节点
this.libp2p = await this.createRelayNode();
(this.libp2p.services.dht as KadDHT).setMode("server");
(this.libp2p.services.lanDHT as KadDHT).setMode("server");
// 获取 Relay 节点的监听地址,并确保找到一个 IPv4 地址
const listenMultiaddr = this.libp2p.getMultiaddrs();
// 如果提供了 onStarted 回调,则在服务启动后调用它,传入监听地址
Expand Down
15 changes: 13 additions & 2 deletions packages/test-cloud-chat-x-core-full/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import { Libp2p, createLibp2p } from "libp2p";
import { noise } from "@chainsafe/libp2p-noise";
import { yamux } from "@chainsafe/libp2p-yamux";
import { circuitRelayServer } from "@libp2p/circuit-relay-v2";
import {
circuitRelayServer,
circuitRelayTransport,
} from "@libp2p/circuit-relay-v2";
import { webSockets } from "@libp2p/websockets";
import * as filters from "@libp2p/websockets/filters";
import { identify } from "@libp2p/identify";
Expand All @@ -19,6 +22,7 @@ import { tls } from "@libp2p/tls";
import { prometheusMetrics } from "@libp2p/prometheus-metrics";
import { bootstrap } from "@libp2p/bootstrap";
import { parsePrivateKeySecretToPeerId } from "./util.js";
import { tcp } from "@libp2p/tcp";
const topics = [
`webChatX._peer-discovery._p2p._pubsub`, // It's recommended but not required to extend the global space
];
Expand All @@ -40,12 +44,18 @@ export default class Libp2pManager {
const peerId = await parsePrivateKeySecretToPeerId();
return await createLibp2p({
addresses: {
listen: ["/ip4/0.0.0.0/tcp/9090/ws"], // 替换为实际希望监听的 IP 和端口
listen: ["/ip4/0.0.0.0/tcp/9090/ws", "/ip4/0.0.0.0/tcp/9500"], // 替换为实际希望监听的 IP 和端口
},
transports: [
webSockets({
filter: filters.all,
}),
circuitRelayTransport({
//允许当前节点建立和接受中继连接
discoverRelays: 0, // 要找到多少个网络中继
reservationConcurrency: 1, // 一次要尝试保留多少个继电器的插槽
}),
tcp(),
],
connectionEncryption: [noise(), tls()],
streamMuxers: [yamux()],
Expand Down Expand Up @@ -174,6 +184,7 @@ export default class Libp2pManager {
// 创建并初始化 Relay 节点
this.libp2p = await this.createRelayNode();
(this.libp2p.services.dht as KadDHT).setMode("server");
(this.libp2p.services.lanDHT as KadDHT).setMode("server");
// 获取 Relay 节点的监听地址,并确保找到一个 IPv4 地址
const listenMultiaddr = this.libp2p.getMultiaddrs();
// 如果提供了 onStarted 回调,则在服务启动后调用它,传入监听地址
Expand Down
15 changes: 13 additions & 2 deletions packages/test-local-chat-x-core-full/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import { Libp2p, createLibp2p } from "libp2p";
import { noise } from "@chainsafe/libp2p-noise";
import { yamux } from "@chainsafe/libp2p-yamux";
import { circuitRelayServer } from "@libp2p/circuit-relay-v2";
import {
circuitRelayServer,
circuitRelayTransport,
} from "@libp2p/circuit-relay-v2";
import { webSockets } from "@libp2p/websockets";
import * as filters from "@libp2p/websockets/filters";
import { identify } from "@libp2p/identify";
Expand All @@ -19,6 +22,7 @@ import { tls } from "@libp2p/tls";
import { prometheusMetrics } from "@libp2p/prometheus-metrics";
import { bootstrap } from "@libp2p/bootstrap";
import { parsePrivateKeySecretToPeerId } from "./util.js";
import { tcp } from "@libp2p/tcp";
const topics = [
`webChatX._peer-discovery._p2p._pubsub`, // It's recommended but not required to extend the global space
];
Expand All @@ -40,12 +44,18 @@ export default class Libp2pManager {
const peerId = await parsePrivateKeySecretToPeerId();
return await createLibp2p({
addresses: {
listen: ["/ip4/0.0.0.0/tcp/9080/ws"], // 替换为实际希望监听的 IP 和端口
listen: ["/ip4/0.0.0.0/tcp/9080/ws", "/ip4/0.0.0.0/tcp/9400"], // 替换为实际希望监听的 IP 和端口
},
transports: [
webSockets({
filter: filters.all,
}),
circuitRelayTransport({
//允许当前节点建立和接受中继连接
discoverRelays: 0, // 要找到多少个网络中继
reservationConcurrency: 1, // 一次要尝试保留多少个继电器的插槽
}),
tcp(),
],
connectionEncryption: [noise(), tls()],
streamMuxers: [yamux()],
Expand Down Expand Up @@ -173,6 +183,7 @@ export default class Libp2pManager {
// 创建并初始化 Relay 节点
this.libp2p = await this.createRelayNode();
(this.libp2p.services.dht as KadDHT).setMode("server");
(this.libp2p.services.lanDHT as KadDHT).setMode("server");
// 获取 Relay 节点的监听地址,并确保找到一个 IPv4 地址
const listenMultiaddr = this.libp2p.getMultiaddrs();
// 如果提供了 onStarted 回调,则在服务启动后调用它,传入监听地址
Expand Down
11 changes: 9 additions & 2 deletions packages/web-chat-x-express/src/controllers/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { pubsubPeerDiscovery } from "@libp2p/pubsub-peer-discovery";
import { tls } from "@libp2p/tls";
import { prometheusMetrics } from "@libp2p/prometheus-metrics";
import { bootstrap } from "@libp2p/bootstrap";
import { tcp } from "@libp2p/tcp";
const topics = [
`webChatX._peer-discovery._p2p._pubsub`, // It's recommended but not required to extend the global space
];
Expand All @@ -45,7 +46,11 @@ export default class Libp2pManager {
const peerId = await peerIdFromKeys(keyPair.public.bytes, keyPair.bytes);
return await createLibp2p({
addresses: {
listen: ["/ip4/0.0.0.0/tcp/9000/wss", "/ip4/0.0.0.0/tcp/10000/ws"], // 替换为实际希望监听的 IP 和端口
listen: [
"/ip4/0.0.0.0/tcp/9000/wss",
"/ip4/0.0.0.0/tcp/10000/ws",
"/ip4/0.0.0.0/tcp/9500",
], // 替换为实际希望监听的 IP 和端口
},
transports: [
webSockets({
Expand All @@ -56,6 +61,7 @@ export default class Libp2pManager {
discoverRelays: 0, // 要找到多少个网络中继
reservationConcurrency: 1, // 一次要尝试保留多少个继电器的插槽
}),
tcp(),
],
connectionEncryption: [noise(), tls()],
streamMuxers: [yamux()],
Expand All @@ -65,6 +71,7 @@ export default class Libp2pManager {
relay: circuitRelayServer({
advertise: true,
}),
pubsub: gossipsub(),
dht: kadDHT({
kBucketSize: 20,
clientMode: false,
Expand All @@ -78,7 +85,6 @@ export default class Libp2pManager {
peerInfoMapper: removePublicAddressesMapper,
clientMode: false,
}),
pubsub: gossipsub(),
},
peerDiscovery: [
mdns(),
Expand Down Expand Up @@ -176,6 +182,7 @@ export default class Libp2pManager {

this.libp2p = await this.createRelayNode();
(this.libp2p.services.dht as KadDHT).setMode("server");
(this.libp2p.services.lanDHT as KadDHT).setMode("server");
// 获取 Relay 节点的监听地址,并确保找到一个 IPv4 地址
const listenMultiaddr = this.libp2p.getMultiaddrs();
this.handleListenEvent();
Expand Down
Loading

0 comments on commit 9fd0efd

Please sign in to comment.