secrez 1.1.6 → 2.0.0

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.
@@ -1,225 +0,0 @@
1
- const { sleep } = require("@secrez/utils");
2
- const { ConfigUtils } = require("@secrez/core");
3
-
4
- class Conf extends require("../Command") {
5
- setHelpAndCompletion() {
6
- this.cliConfig.completion.courier = {
7
- _self: this,
8
- };
9
- this.cliConfig.completion.help.courier = true;
10
- this.optionDefinitions = [
11
- {
12
- name: "help",
13
- alias: "h",
14
- type: Boolean,
15
- },
16
- ];
17
- }
18
-
19
- help() {
20
- return {
21
- description: ["Configure the connection to a local courier"],
22
- examples: ["courier"],
23
- };
24
- }
25
-
26
- async publishToHubIfNotYet(options) {
27
- try {
28
- const { port, caCrt } = options.env.courier;
29
- let id = options.env.courier.tunnel
30
- ? options.env.courier.tunnel.clientId
31
- : 0;
32
- this.prompt.loadingMessage = "Publishing the courier";
33
- this.prompt.loading();
34
- let res = await this.callCourier(
35
- {
36
- action: {
37
- name: "publish",
38
- },
39
- id,
40
- },
41
- port,
42
- caCrt,
43
- "/admin"
44
- );
45
- this.prompt.showLoading = false;
46
- await sleep(100);
47
- try {
48
- process.stdout.clearLine();
49
- } catch (e) {
50
- // most likely we are running workspace testing
51
- }
52
- if (res.info.error) {
53
- throw new Error(
54
- `The connection to the hub ${res.info.hub} is refused. Verify that your Secrez Courier is connecting to an active hub, please, and try again.`
55
- );
56
- } else {
57
- return res.info;
58
- }
59
- } catch (e) {
60
- // console.error(e)
61
- throw new Error(e.message);
62
- }
63
- }
64
-
65
- async isCourierReady(options) {
66
- if (!options.env) {
67
- options.env = await ConfigUtils.getEnv(this.secrez.config);
68
- }
69
- if (options.env.courier) {
70
- const { port, caCrt } = options.env.courier;
71
- return await this.callCourier(
72
- { action: { name: "ready" } },
73
- port,
74
- caCrt,
75
- "/admin"
76
- );
77
- } else {
78
- throw new Error("No courier set up yet.");
79
- }
80
- }
81
-
82
- async getRecentMessages(options = {}) {
83
- const env = await ConfigUtils.getEnv(this.secrez.config);
84
- if (!env.messages) {
85
- env.messages = {};
86
- }
87
- this.lastRead = env.messages.lastRead || 0;
88
- if (env.courier) {
89
- const { port, caCrt } = env.courier;
90
- const payload = Object.assign(
91
- {
92
- minTimestamp: this.lastRead,
93
- },
94
- options
95
- );
96
- let messages = await this.callCourier(payload, port, caCrt, "/messages");
97
- for (let message of messages.result) {
98
- this.lastRead = Math.max(this.lastRead, message.timestamp + 1);
99
- }
100
- env.messages.lastRead = this.lastRead;
101
- await ConfigUtils.putEnv(this.secrez.config, env);
102
- return messages.result.map((e) => this.decryptMessage(e));
103
- } else {
104
- throw new Error("No courier set up yet.");
105
- }
106
- }
107
-
108
- async getSomeMessages(options = {}) {
109
- const env = await ConfigUtils.getEnv(this.secrez.config);
110
- if (env.courier) {
111
- const { port, caCrt } = env.courier;
112
- const payload = options.payload;
113
- let messages = await this.callCourier(payload, port, caCrt, "/messages");
114
- return messages.result.map((e) => this.decryptMessage(e));
115
- } else {
116
- throw new Error("No courier set up yet.");
117
- }
118
- }
119
-
120
- decryptMessage(message) {
121
- message.decrypted = this.secrez.decryptSharedData(
122
- JSON.parse(message.payload).message,
123
- message.publickey
124
- );
125
- return message;
126
- }
127
-
128
- async preInit(options) {
129
- const env = options.env;
130
- let ready;
131
- if (env.courier && env.courier.port) {
132
- let body = await this.isCourierReady(options);
133
- ready = body.success;
134
- if (ready) {
135
- env.courier.caCrt = body.caCrt;
136
- if (!body.tunnel.url) {
137
- body.tunnel = await this.publishToHubIfNotYet(options);
138
- }
139
- env.courier.tunnel = body.tunnel;
140
- await ConfigUtils.putEnv(this.secrez.config, env);
141
- options.ready = true;
142
- }
143
- }
144
- }
145
-
146
- async initCourier(options) {
147
- const env = (options.env = await ConfigUtils.getEnv(this.secrez.config));
148
- await this.preInit(options);
149
- if (process.env.NODE_ENV === "test") {
150
- return await this.setUpCorier(options);
151
- }
152
- if (options.ready) {
153
- this.Logger.reset(
154
- `A courier is already set and is listening on port ${env.courier.port}`
155
- );
156
- } else {
157
- let yes = await this.useConfirm({
158
- message: "No Secrez Courier configured yet.\nDid you launch one?",
159
- default: true,
160
- });
161
- if (yes) {
162
- options.port = await this.useInput({
163
- message: "Which port the courier is listening to?",
164
- });
165
- if (options.port) {
166
- await this.setUpCorier(options);
167
- } else {
168
- this.Logger.grey("Operation canceled");
169
- }
170
- } else {
171
- this.Logger.grey("Operation canceled");
172
- }
173
- }
174
- }
175
-
176
- async setUpCorier(options) {
177
- const port = options.port;
178
- try {
179
- const body = await this.callCourier(
180
- { action: { name: "ready" } },
181
- port,
182
- undefined,
183
- "/admin"
184
- );
185
- if (body.success) {
186
- let previousTunnel;
187
- if (options.env.courier && options.env.courier.port === port) {
188
- previousTunnel = options.env.courier.tunnel;
189
- }
190
- options.env.courier = {
191
- port,
192
- caCrt: body.caCrt,
193
- tunnel: previousTunnel,
194
- };
195
- body.tunnel = await this.publishToHubIfNotYet(options);
196
- options.env.courier.tunnel = body.tunnel;
197
- await ConfigUtils.putEnv(this.secrez.config, options.env);
198
- this.Logger.reset("Connected");
199
- } else {
200
- throw new Error("Courier not found");
201
- }
202
- } catch (e) {
203
- throw new Error("Courier not available");
204
- }
205
- }
206
-
207
- async courier(options) {
208
- return this.initCourier(options);
209
- }
210
-
211
- async exec(options = {}) {
212
- if (options.help) {
213
- return this.showHelp();
214
- }
215
- try {
216
- await this.courier(options);
217
- } catch (e) {
218
- // console.error(e)
219
- this.Logger.red(e.message);
220
- }
221
- await this.prompt.run();
222
- }
223
- }
224
-
225
- module.exports = Conf;
@@ -1,87 +0,0 @@
1
- class Contacts extends require("../../Command") {
2
- setHelpAndCompletion() {
3
- this.cliConfig.chatCompletion.contacts = {
4
- _func: this.selfCompletion(this),
5
- _self: this,
6
- };
7
- this.cliConfig.chatCompletion.help.contacts = true;
8
- this.optionDefinitions = [
9
- {
10
- name: "help",
11
- alias: "h",
12
- type: Boolean,
13
- },
14
- {
15
- name: "list",
16
- alias: "l",
17
- type: Boolean,
18
- default: true,
19
- },
20
- {
21
- name: "add",
22
- alias: "a",
23
- type: String,
24
- },
25
- {
26
- name: "update",
27
- alias: "u",
28
- type: String,
29
- },
30
- {
31
- name: "delete",
32
- alias: "d",
33
- type: String,
34
- },
35
- {
36
- name: "rename",
37
- alias: "r",
38
- type: String,
39
- multiple: true,
40
- },
41
- {
42
- name: "show",
43
- alias: "s",
44
- type: String,
45
- },
46
- ];
47
- }
48
-
49
- help() {
50
- return this.prompt.environment.prompt.commands.contacts.help();
51
- }
52
-
53
- async customCompletion(options, originalLine, defaultOption) {
54
- return [];
55
- }
56
-
57
- async contacts(options) {
58
- return await this.prompt.environment.prompt.commands.contacts.contacts(
59
- options
60
- );
61
- }
62
-
63
- async exec(options = {}) {
64
- if (options.help) {
65
- return this.showHelp();
66
- }
67
- try {
68
- if (!Object.keys(options).length) {
69
- options.list = true;
70
- }
71
- this.validate(options);
72
- let result = await this.contacts(options);
73
- if (!Array.isArray(result)) {
74
- result = [result];
75
- }
76
- for (let r of result) {
77
- this.Logger.reset(r);
78
- }
79
- } catch (e) {
80
- // console.log(e)
81
- this.Logger.red(e.message);
82
- }
83
- await this.prompt.run();
84
- }
85
- }
86
-
87
- module.exports = Contacts;
@@ -1,48 +0,0 @@
1
- const HelpProto = require("../../utils/HelpProto");
2
-
3
- class Help extends require("../../Command") {
4
- constructor(prompt) {
5
- super(prompt);
6
- this.proto = new HelpProto({
7
- prompt: this.prompt,
8
- cliConfig: this.cliConfig,
9
- helpDescription: this.helpDescription,
10
- completions: this.completion,
11
- completionObj: "chatCompletion",
12
- });
13
- }
14
-
15
- setHelpAndCompletion() {
16
- this.optionDefinitions = [
17
- {
18
- name: "command",
19
- alias: "c",
20
- defaultOption: true,
21
- type: String,
22
- },
23
- ];
24
- }
25
-
26
- help() {
27
- return this.proto.help();
28
- }
29
-
30
- async exec(options = {}) {
31
- let help;
32
- let command = options.command;
33
- if (command) {
34
- if (this.prompt.commands[command]) {
35
- help = this.prompt.commands[command].help();
36
- } else {
37
- this.Logger.red("Invalid command.");
38
- return await this.prompt.run();
39
- }
40
- } else {
41
- help = this.help();
42
- }
43
- this.proto.format(help, command);
44
- await this.prompt.run();
45
- }
46
- }
47
-
48
- module.exports = Help;
@@ -1,116 +0,0 @@
1
- const { ConfigUtils } = require("@secrez/core");
2
-
3
- class Join extends require("../../Command") {
4
- setHelpAndCompletion() {
5
- this.cliConfig.chatCompletion.join = {
6
- _func: this.selfCompletion(this),
7
- _self: this,
8
- };
9
- this.cliConfig.chatCompletion.help.join = true;
10
- this.optionDefinitions = [
11
- {
12
- name: "help",
13
- alias: "h",
14
- type: Boolean,
15
- },
16
- {
17
- name: "chat",
18
- alias: "c",
19
- defaultOption: true,
20
- multiple: true,
21
- type: String,
22
- },
23
- ];
24
- }
25
-
26
- help() {
27
- return {
28
- description: ["Joins conversation."],
29
- examples: [
30
- [
31
- "join pan",
32
- 'joins a conversation with the previously-added user "pan"',
33
- ],
34
- ],
35
- };
36
- }
37
-
38
- async getAllUsers() {
39
- let all = await this.prompt.environment.prompt.commands.contacts.list({});
40
- if (all && all.length) {
41
- return all.map((e) => e[0]);
42
- }
43
- return [];
44
- }
45
-
46
- async customCompletion(options, originalLine, currentOption) {
47
- const existingUsers = await this.getAllUsers();
48
- if (options.chat) {
49
- let lastUser = options.chat[options.chat.length - 1];
50
- return existingUsers.filter((e) => {
51
- return RegExp("^" + lastUser).test(e);
52
- });
53
- } else {
54
- return existingUsers;
55
- }
56
- }
57
-
58
- async joinRoom(options) {
59
- const existingUsers = await this.getAllUsers({ asIs: true });
60
- if (typeof options.chat === "string") {
61
- options.chat = [options.chat];
62
- }
63
- if (options.chat.length > 1) {
64
- throw new Error("Multiple chat not supported yet");
65
- }
66
- if (!existingUsers.includes(options.chat[0])) {
67
- throw new Error("Contact not found");
68
- }
69
- let room = [];
70
- for (let contact of options.chat) {
71
- room.push(
72
- await this.prompt.environment.prompt.commands.contacts.show({
73
- show: contact,
74
- asIs: true,
75
- })
76
- );
77
- }
78
- this.prompt.environment.room = room;
79
- this.prompt.start();
80
- }
81
-
82
- async join(options) {
83
- if (!options.chat) {
84
- throw new Error("Missing parameters");
85
- } else {
86
- const env = (options.env = await ConfigUtils.getEnv(this.secrez.config));
87
- if (env.courier) {
88
- await this.prompt.environment.prompt.commands.courier.preInit(options);
89
- if (options.ready) {
90
- await this.joinRoom(options);
91
- if (!this.hint) {
92
- this.Logger.grey(
93
- 'In a room, by default, you send messages, but you can also execute commands. If a message looks like a command, for example "join me tonight", disambiguate it by prefixing it with a slash, like "/join me tonight".'
94
- );
95
- this.hint = true;
96
- }
97
- }
98
- }
99
- }
100
- }
101
-
102
- async exec(options = {}) {
103
- if (options.help) {
104
- return this.showHelp();
105
- }
106
- try {
107
- this.validate(options);
108
- await this.join(options);
109
- } catch (e) {
110
- this.Logger.red(e.message);
111
- }
112
- await this.prompt.run();
113
- }
114
- }
115
-
116
- module.exports = Join;
@@ -1,35 +0,0 @@
1
- class Leave extends require("../../Command") {
2
- setHelpAndCompletion() {
3
- this.cliConfig.chatCompletion.leave = {
4
- _func: this.selfCompletion(this),
5
- _self: this,
6
- };
7
- this.cliConfig.chatCompletion.help.leave = true;
8
- this.optionDefinitions = [
9
- {
10
- name: "help",
11
- alias: "h",
12
- type: Boolean,
13
- },
14
- ];
15
- }
16
-
17
- help() {
18
- return {
19
- description: ["Leaves a room"],
20
- examples: ["leave"],
21
- };
22
- }
23
-
24
- async exec(options = {}) {
25
- if (options.help) {
26
- return this.showHelp();
27
- }
28
- if (this.prompt.environment.room) {
29
- this.prompt.environment.chatPrompt.onBeforeClose();
30
- }
31
- await this.prompt.run();
32
- }
33
- }
34
-
35
- module.exports = Leave;
@@ -1,39 +0,0 @@
1
- class Quit extends require("../../Command") {
2
- setHelpAndCompletion() {
3
- this.cliConfig.chatCompletion.quit = {
4
- _func: this.selfCompletion(this),
5
- _self: this,
6
- };
7
- this.cliConfig.chatCompletion.help.quit = true;
8
- this.optionDefinitions = [
9
- {
10
- name: "help",
11
- alias: "h",
12
- type: Boolean,
13
- },
14
- ];
15
- }
16
-
17
- help() {
18
- return {
19
- description: ["Quits the chat environment"],
20
- examples: ["quit"],
21
- };
22
- }
23
-
24
- async exec(options = {}) {
25
- if (options.help) {
26
- return this.showHelp();
27
- }
28
- /* istanbul ignore if */
29
- if (process.env.NODE_ENV !== "test") {
30
- await this.prompt.saveHistory();
31
- delete this.prompt.environment.chatPrompt;
32
- await this.prompt.environment.prompt.setSigintPosition();
33
- } else {
34
- this.Logger.reset("Chat quit");
35
- }
36
- }
37
- }
38
-
39
- module.exports = Quit;
@@ -1,99 +0,0 @@
1
- const superagent = require("superagent");
2
- const { utils: hubUtils } = require("@secrez/hub");
3
- const { ConfigUtils } = require("@secrez/core");
4
-
5
- class Send extends require("../../Command") {
6
- setHelpAndCompletion() {
7
- this.cliConfig.chatCompletion.send = {
8
- _func: this.selfCompletion(this),
9
- _self: this,
10
- };
11
- this.cliConfig.chatCompletion.help.send = true;
12
- this.optionDefinitions = [
13
- {
14
- name: "help",
15
- alias: "h",
16
- type: Boolean,
17
- },
18
- {
19
- name: "message",
20
- alias: "m",
21
- type: String,
22
- default: true,
23
- },
24
- ];
25
- }
26
-
27
- help() {
28
- return {
29
- description: ["Sends either a room or the chat"],
30
- examples: ["send"],
31
- };
32
- }
33
-
34
- async sendMessage(options) {
35
- const env = (options.env = await ConfigUtils.getEnv(this.secrez.config));
36
- if (!env.courier) {
37
- throw new Error("Courier configuration not found");
38
- }
39
- await this.prompt.environment.prompt.commands.courier.preInit(options);
40
- if (!options.ready) {
41
- throw new Error("Connection with the courier lost");
42
- }
43
- if (!this.prompt.environment.room) {
44
- throw new Error("No room active");
45
- }
46
- let recipient = this.prompt.environment.room[0].publicKey;
47
- let encryptedMessage = this.secrez.encryptSharedData(
48
- options.message,
49
- recipient
50
- );
51
- const { payload: payloadMessage, signature: signatureMessage } =
52
- hubUtils.setPayloadAndSignIt(this.secrez, {
53
- message: encryptedMessage,
54
- });
55
- const { payload: payload2, signature: signature2 } =
56
- hubUtils.setPayloadAndSignIt(this.secrez, {
57
- action: {
58
- name: "send",
59
- recipient,
60
- message: {
61
- payload: payloadMessage,
62
- signature: signatureMessage,
63
- },
64
- },
65
- });
66
- return superagent
67
- .get(`https://localhost:${env.courier.port}/admin`)
68
- .set("Accept", "application/json")
69
- .query({ payload: payload2, signature: signature2 })
70
- .ca(await env.courier.caCrt);
71
- }
72
-
73
- async send(options) {
74
- if (options.message) {
75
- let res = await this.sendMessage(options);
76
- if (!res.body.success) {
77
- throw new Error("Sending failed :o(");
78
- }
79
- }
80
- }
81
-
82
- async exec(options = {}) {
83
- if (options.help) {
84
- return this.showHelp();
85
- }
86
- try {
87
- if (!this.prompt.environment.room) {
88
- throw new Error("You not in a chat room");
89
- }
90
- this.validate(options);
91
- await this.send(options);
92
- } catch (e) {
93
- this.Logger.red(e.message);
94
- }
95
- await this.prompt.run();
96
- }
97
- }
98
-
99
- module.exports = Send;