@peerbit/server 3.0.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/aws.browser.d.ts +0 -0
- package/lib/esm/aws.browser.js +3 -0
- package/lib/esm/aws.browser.js.map +1 -0
- package/lib/esm/aws.d.ts +19 -0
- package/lib/esm/aws.js +185 -1
- package/lib/esm/aws.js.map +1 -1
- package/lib/esm/cli.js +590 -324
- package/lib/esm/cli.js.map +1 -1
- package/lib/esm/client.d.ts +11 -5
- package/lib/esm/client.js +64 -11
- package/lib/esm/client.js.map +1 -1
- package/lib/esm/docker.browser.d.ts +0 -0
- package/lib/esm/docker.browser.js +3 -0
- package/lib/esm/docker.browser.js.map +1 -0
- package/lib/esm/domain.js +1 -1
- package/lib/esm/domain.js.map +1 -1
- package/lib/esm/peerbit.d.ts +1 -1
- package/lib/esm/remotes.d.ts +15 -2
- package/lib/esm/remotes.js +8 -8
- package/lib/esm/remotes.js.map +1 -1
- package/lib/esm/routes.d.ts +3 -2
- package/lib/esm/routes.js +5 -4
- package/lib/esm/routes.js.map +1 -1
- package/lib/esm/server.d.ts +3 -1
- package/lib/esm/server.js +32 -19
- package/lib/esm/server.js.map +1 -1
- package/lib/esm/trust.d.ts +1 -1
- package/lib/esm/trust.js.map +1 -1
- package/lib/ui/assets/aws.browser-4ed993c7.js +1 -0
- package/lib/ui/assets/index-5ed0229d.js +77 -0
- package/lib/ui/index.html +1 -1
- package/package.json +13 -7
- package/src/aws.browser.ts +1 -0
- package/src/aws.ts +250 -1
- package/src/cli.ts +677 -354
- package/src/client.ts +76 -11
- package/src/docker.browser.ts +1 -0
- package/src/domain.ts +1 -1
- package/src/peerbit.ts +1 -1
- package/src/remotes.ts +24 -10
- package/src/routes.ts +5 -4
- package/src/server.ts +43 -23
- package/src/trust.ts +1 -1
- package/lib/ui/assets/index-cac7195d.js +0 -77
package/lib/ui/index.html
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
Learn how to configure a non-root public URL by running `npm run build`.
|
|
24
24
|
-->
|
|
25
25
|
<title>Peerbit</title>
|
|
26
|
-
<script type="module" crossorigin src="/assets/index-
|
|
26
|
+
<script type="module" crossorigin src="/assets/index-5ed0229d.js"></script>
|
|
27
27
|
<link rel="stylesheet" href="/assets/index-5265c558.css">
|
|
28
28
|
</head>
|
|
29
29
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/server",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"author": "dao.xyz",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,7 +23,10 @@
|
|
|
23
23
|
"./lib/esm/config.js": "./lib/esm/config.browser.js",
|
|
24
24
|
"./config.js": "./lib/esm/config.browser.js",
|
|
25
25
|
"./lib/esm/remotes.js": "./lib/esm/remotes.browser.js",
|
|
26
|
-
"./remotes.js": "./lib/esm/remotes.browser.js"
|
|
26
|
+
"./remotes.js": "./lib/esm/remotes.browser.js",
|
|
27
|
+
"./lib/esm/docker.js": "./lib/esm/docker.browser.js",
|
|
28
|
+
"./docker.js": "./lib/esm/docker.browser.js",
|
|
29
|
+
"./lib/esm/aws.js": "./lib/esm/aws.browser.js"
|
|
27
30
|
},
|
|
28
31
|
"files": [
|
|
29
32
|
"lib",
|
|
@@ -52,22 +55,25 @@
|
|
|
52
55
|
},
|
|
53
56
|
"devDependencies": {
|
|
54
57
|
"@peerbit/test-lib": "^0.0.1",
|
|
55
|
-
"@peerbit/test-utils": "1.0.
|
|
58
|
+
"@peerbit/test-utils": "1.0.18",
|
|
56
59
|
"@types/tmp": "^0.2.3",
|
|
57
60
|
"@types/yargs": "^17.0.24",
|
|
58
61
|
"aws-sdk": "^2.1259.0",
|
|
59
62
|
"dotenv": "^16.1.4"
|
|
60
63
|
},
|
|
61
64
|
"dependencies": {
|
|
62
|
-
"@
|
|
63
|
-
"@dao-xyz/libp2p-noise": "^12.0.1",
|
|
65
|
+
"@dao-xyz/libp2p-noise": "^13.0.1",
|
|
64
66
|
"axios": "^1.4.0",
|
|
65
67
|
"chalk": "^5.3.0",
|
|
66
|
-
"peerbit": "
|
|
68
|
+
"peerbit": "2.0.0",
|
|
67
69
|
"tar-stream": "^3.1.6",
|
|
68
70
|
"tmp": "^0.2.1",
|
|
69
71
|
"yargs": "^17.7.2",
|
|
70
72
|
"zlib": "^1.0.5"
|
|
71
73
|
},
|
|
72
|
-
"
|
|
74
|
+
"optionalDependencies": {
|
|
75
|
+
"@aws-sdk/client-ec2": "^3.390.0",
|
|
76
|
+
"@aws-sdk/client-route-53": "^3.391.0"
|
|
77
|
+
},
|
|
78
|
+
"gitHead": "bc0b13f17bf92ec4284d95153f047faf2a336b11"
|
|
73
79
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// Unsupported
|
package/src/aws.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { delay } from "@peerbit/time";
|
|
2
|
+
import { type PeerId } from "@libp2p/interface/peer-id";
|
|
2
3
|
|
|
3
4
|
export const createRecord = async (options: {
|
|
4
5
|
domain: string;
|
|
@@ -10,6 +11,7 @@ export const createRecord = async (options: {
|
|
|
10
11
|
"@aws-sdk/client-route-53"
|
|
11
12
|
);
|
|
12
13
|
const { isIPv4, isIPv6 } = await import("net");
|
|
14
|
+
const { getMyIp } = await import("./domain.js");
|
|
13
15
|
|
|
14
16
|
const myIp = await getMyIp();
|
|
15
17
|
const v4 = isIPv4(myIp);
|
|
@@ -46,3 +48,250 @@ export const createRecord = async (options: {
|
|
|
46
48
|
});
|
|
47
49
|
await client.send(cmd);
|
|
48
50
|
};
|
|
51
|
+
|
|
52
|
+
const setupUserData = (email: string, grantAccess: PeerId[] = []) => {
|
|
53
|
+
const peerIdStrings = grantAccess.map((x) => x.toString());
|
|
54
|
+
return `#!/bin/bash
|
|
55
|
+
cd /home/ubuntu
|
|
56
|
+
curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\
|
|
57
|
+
sudo apt-get install -y nodejs
|
|
58
|
+
npm install -g @peerbit/server
|
|
59
|
+
sudo peerbit domain test --email ${email}
|
|
60
|
+
peerbit start ${peerIdStrings.map((key) => `--ga ${key}`)} > log.txt 2>&1 &
|
|
61
|
+
`;
|
|
62
|
+
};
|
|
63
|
+
const PURPOSE_TAG_NAME = "Purpose";
|
|
64
|
+
const PURPOSE_TAG_VALUE = "Peerbit";
|
|
65
|
+
|
|
66
|
+
// Ubuntu Server 20.04 LTS (HVM), SSD Volume Type (64-bit (Arm))
|
|
67
|
+
export const AWS_LINUX_ARM_AMIs: Record<string, string> = {
|
|
68
|
+
/* "af-south-1" */
|
|
69
|
+
"ap-northeast-1": "ami-01444f83954203c6f",
|
|
70
|
+
"ap-northeast-2": "ami-0ac62099928d25fec",
|
|
71
|
+
"ap-northeast-3": "ami-0efdceaebc778c5f3",
|
|
72
|
+
"ap-south-1": "ami-0df6182e39efe7c4d",
|
|
73
|
+
"ap-southeast-1": "ami-01d87e25d3c65ec37",
|
|
74
|
+
"ap-southeast-2": "ami-0641fc20c25fdd380",
|
|
75
|
+
/* "ap-south-2", */
|
|
76
|
+
/* "ap-southeast-3",
|
|
77
|
+
"ap-southeast-4", */
|
|
78
|
+
"ca-central-1": "ami-0a3e942fe4813672b",
|
|
79
|
+
/* "cn-north-1",
|
|
80
|
+
"cn-northwest-1", */
|
|
81
|
+
"eu-central-1": "ami-0d85ad3aa712d96af",
|
|
82
|
+
/* "eu-central-2", */
|
|
83
|
+
"eu-north-1": "ami-0ff124a3d7381bfec",
|
|
84
|
+
/* "eu-south-1",
|
|
85
|
+
"eu-south-2", */
|
|
86
|
+
"eu-west-1": "ami-09c59b011574e4c96",
|
|
87
|
+
"eu-west-2": "ami-03e26d11b665ac7be",
|
|
88
|
+
"eu-west-3": "ami-00771c0ac817397bd",
|
|
89
|
+
/* "il-central-1",
|
|
90
|
+
"me-central-1",
|
|
91
|
+
"me-south-1", */
|
|
92
|
+
"sa-east-1": "ami-08dc4b989f93eafb9",
|
|
93
|
+
"us-east-1": "ami-097d5b19d4f1a7d1b",
|
|
94
|
+
"us-east-2": "ami-0071e4b30f26879e2",
|
|
95
|
+
/* "us-gov-east-1",
|
|
96
|
+
"us-gov-west-1", */
|
|
97
|
+
"us-west-1": "ami-0dca369228f3b2ce7",
|
|
98
|
+
"us-west-2": "ami-0c79a55dda52434da",
|
|
99
|
+
};
|
|
100
|
+
export const launchNodes = async (properties: {
|
|
101
|
+
region?: string;
|
|
102
|
+
email: string;
|
|
103
|
+
count?: number;
|
|
104
|
+
size?: "micro" | "small" | "medium" | "large" | "xlarge" | "2xlarge";
|
|
105
|
+
namePrefix?: string;
|
|
106
|
+
grantAccess?: PeerId[];
|
|
107
|
+
}): Promise<
|
|
108
|
+
{ instanceId: string; publicIp: string; name: string; region: string }[]
|
|
109
|
+
> => {
|
|
110
|
+
if (properties.count && properties.count > 10) {
|
|
111
|
+
throw new Error(
|
|
112
|
+
"Unexpected node launch count: " +
|
|
113
|
+
properties.count +
|
|
114
|
+
". To prevent unwanted behaviour you can also launch 10 nodes at once"
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
const count = properties.count || 1;
|
|
118
|
+
|
|
119
|
+
const {
|
|
120
|
+
EC2Client,
|
|
121
|
+
CreateTagsCommand,
|
|
122
|
+
RunInstancesCommand,
|
|
123
|
+
DescribeSecurityGroupsCommand,
|
|
124
|
+
CreateSecurityGroupCommand,
|
|
125
|
+
AuthorizeSecurityGroupIngressCommand,
|
|
126
|
+
DescribeInstancesCommand,
|
|
127
|
+
} = await import("@aws-sdk/client-ec2");
|
|
128
|
+
const client = new EC2Client({ region: properties.region });
|
|
129
|
+
const regionString = await client.config.region();
|
|
130
|
+
|
|
131
|
+
let securityGroupOut = (
|
|
132
|
+
await client.send(
|
|
133
|
+
new DescribeSecurityGroupsCommand({
|
|
134
|
+
Filters: [
|
|
135
|
+
{ Name: "tag:" + PURPOSE_TAG_NAME, Values: [PURPOSE_TAG_VALUE] },
|
|
136
|
+
],
|
|
137
|
+
})
|
|
138
|
+
)
|
|
139
|
+
)?.SecurityGroups?.[0];
|
|
140
|
+
if (!securityGroupOut) {
|
|
141
|
+
securityGroupOut = await client.send(
|
|
142
|
+
new CreateSecurityGroupCommand({
|
|
143
|
+
GroupName: "peerbit-node",
|
|
144
|
+
Description: "Security group for running Peerbit nodes",
|
|
145
|
+
})
|
|
146
|
+
);
|
|
147
|
+
await client.send(
|
|
148
|
+
new CreateTagsCommand({
|
|
149
|
+
Resources: [securityGroupOut.GroupId!],
|
|
150
|
+
Tags: [{ Key: PURPOSE_TAG_NAME, Value: PURPOSE_TAG_VALUE }],
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
await client.send(
|
|
154
|
+
new AuthorizeSecurityGroupIngressCommand({
|
|
155
|
+
GroupId: securityGroupOut.GroupId,
|
|
156
|
+
IpPermissions: [
|
|
157
|
+
{
|
|
158
|
+
IpRanges: [{ CidrIp: "0.0.0.0/0" }],
|
|
159
|
+
IpProtocol: "tcp",
|
|
160
|
+
FromPort: 80,
|
|
161
|
+
ToPort: 80,
|
|
162
|
+
}, // Frontend
|
|
163
|
+
{
|
|
164
|
+
IpRanges: [{ CidrIp: "0.0.0.0/0" }],
|
|
165
|
+
IpProtocol: "tcp",
|
|
166
|
+
FromPort: 443,
|
|
167
|
+
ToPort: 443,
|
|
168
|
+
}, // Frontend SSL
|
|
169
|
+
{
|
|
170
|
+
IpRanges: [{ CidrIp: "0.0.0.0/0" }],
|
|
171
|
+
IpProtocol: "tcp",
|
|
172
|
+
FromPort: 9002,
|
|
173
|
+
ToPort: 9002,
|
|
174
|
+
}, // HTTPS api
|
|
175
|
+
{
|
|
176
|
+
IpRanges: [{ CidrIp: "0.0.0.0/0" }],
|
|
177
|
+
IpProtocol: "tcp",
|
|
178
|
+
FromPort: 8082,
|
|
179
|
+
ToPort: 8082,
|
|
180
|
+
}, // HTTP api
|
|
181
|
+
{
|
|
182
|
+
IpRanges: [{ CidrIp: "0.0.0.0/0" }],
|
|
183
|
+
IpProtocol: "tcp",
|
|
184
|
+
FromPort: 4002,
|
|
185
|
+
ToPort: 4005,
|
|
186
|
+
}, // libp2p
|
|
187
|
+
{
|
|
188
|
+
IpRanges: [{ CidrIp: "0.0.0.0/0" }],
|
|
189
|
+
IpProtocol: "tcp",
|
|
190
|
+
FromPort: 22,
|
|
191
|
+
ToPort: 22,
|
|
192
|
+
}, // SSH
|
|
193
|
+
],
|
|
194
|
+
})
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
const instanceTag =
|
|
198
|
+
"Peerbit" + (properties.namePrefix ? "-" + properties.namePrefix : "");
|
|
199
|
+
let existingCounter =
|
|
200
|
+
(
|
|
201
|
+
await client.send(
|
|
202
|
+
new DescribeInstancesCommand({
|
|
203
|
+
Filters: [{ Name: "tag:Purpose", Values: [instanceTag] }],
|
|
204
|
+
})
|
|
205
|
+
)
|
|
206
|
+
).Reservations?.length || 0;
|
|
207
|
+
|
|
208
|
+
console.log(regionString, AWS_LINUX_ARM_AMIs[regionString]);
|
|
209
|
+
const instanceOut = await client.send(
|
|
210
|
+
new RunInstancesCommand({
|
|
211
|
+
ImageId: AWS_LINUX_ARM_AMIs[regionString],
|
|
212
|
+
SecurityGroupIds: [securityGroupOut.GroupId!],
|
|
213
|
+
InstanceType: "t4g." + (properties.size || "micro"),
|
|
214
|
+
UserData: Buffer.from(
|
|
215
|
+
setupUserData(properties.email, properties.grantAccess)
|
|
216
|
+
).toString("base64"),
|
|
217
|
+
MinCount: count,
|
|
218
|
+
MaxCount: count,
|
|
219
|
+
// InstanceInitiatedShutdownBehavior: 'terminate' // to enable termination when node shutting itself down
|
|
220
|
+
})
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
if (!instanceOut.Instances || instanceOut.Instances.length === 0) {
|
|
224
|
+
throw new Error("Failed to create instance");
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const names: string[] = [];
|
|
228
|
+
for (const instance of instanceOut.Instances) {
|
|
229
|
+
existingCounter++;
|
|
230
|
+
const name =
|
|
231
|
+
(properties.namePrefix ? properties.namePrefix : "peerbit-node") +
|
|
232
|
+
"-" +
|
|
233
|
+
existingCounter;
|
|
234
|
+
names.push(name);
|
|
235
|
+
await client.send(
|
|
236
|
+
new CreateTagsCommand({
|
|
237
|
+
Resources: [instance.InstanceId!],
|
|
238
|
+
Tags: [
|
|
239
|
+
{ Key: "Name", Value: name },
|
|
240
|
+
{ Key: "Purpose", Value: instanceTag },
|
|
241
|
+
],
|
|
242
|
+
})
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// wait for instance ips to become available
|
|
247
|
+
let publicIps: string[] = [];
|
|
248
|
+
for (let i = 0; i < 10; i++) {
|
|
249
|
+
const info = await client.send(
|
|
250
|
+
new DescribeInstancesCommand({
|
|
251
|
+
InstanceIds: instanceOut.Instances.map((x) => x.InstanceId!),
|
|
252
|
+
})
|
|
253
|
+
);
|
|
254
|
+
const foundInstances = info
|
|
255
|
+
.Reservations!.map((x) => x.Instances!.map((y) => y))
|
|
256
|
+
.flat()!;
|
|
257
|
+
const foundIps: string[] = [];
|
|
258
|
+
for (const out of instanceOut.Instances) {
|
|
259
|
+
const foundInstance = foundInstances.find(
|
|
260
|
+
(x) => x!.InstanceId === out.InstanceId!
|
|
261
|
+
);
|
|
262
|
+
if (!foundInstance!.PublicIpAddress) {
|
|
263
|
+
await delay(3000);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
foundIps.push(foundInstance!.PublicIpAddress!);
|
|
267
|
+
}
|
|
268
|
+
publicIps = foundIps;
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (publicIps.length === 0) {
|
|
273
|
+
throw new Error("Failed to resolve IPs for created instances");
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return publicIps.map((v, ix) => {
|
|
277
|
+
return {
|
|
278
|
+
instanceId: instanceOut.Instances![ix].InstanceId!,
|
|
279
|
+
publicIp: v,
|
|
280
|
+
name: names[ix],
|
|
281
|
+
region: regionString,
|
|
282
|
+
};
|
|
283
|
+
}); // TODO types
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
export const terminateNode = async (properties: {
|
|
287
|
+
instanceId: string;
|
|
288
|
+
region?: string;
|
|
289
|
+
}) => {
|
|
290
|
+
const { EC2Client, TerminateInstancesCommand } = await import(
|
|
291
|
+
"@aws-sdk/client-ec2"
|
|
292
|
+
);
|
|
293
|
+
const client = new EC2Client({ region: properties.region });
|
|
294
|
+
await client.send(
|
|
295
|
+
new TerminateInstancesCommand({ InstanceIds: [properties.instanceId] })
|
|
296
|
+
);
|
|
297
|
+
};
|