This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·138 lines (126 loc) · 3.42 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const axios = require('axios');
const ora = require('ora');
const debug = require('debug');
const { io } = require('socket.io-client');
const execaSync = require('execa').commandSync;
const gpg = require('./gpg');
const socket = io('https://chatty-link.herokuapp.com');
socket.on('connect_error', (err) => {
console.error('connection error', err);
});
const spinner = ora('Loading');
const log = debug('user');
// get bash username
const username = execaSync('whoami').stdout;
const args = yargs(hideBin(process.argv))
.usage('Usage: chatty [options]')
.option('with', {
alias: 'w',
type: 'string',
description: 'Initiate a chat session',
})
.option('connect', {
alias: 'c',
type: 'string',
description: 'Connect to an existing session',
})
.option('default-key', {
alias: 'k',
type: 'string',
description: 'default key identifier',
})
.help('h')
.alias('h', 'help')
.epilog('Copyright 2022 Navdeep Saini').argv;
const request = axios.create({
baseURL: 'https://chatty-link.herokuapp.com',
timeout: 10000,
});
/*
* initiates a new session with a gpg key id
*
*/
// flags -h, -c, -i are supposed to be used in seclusion
// using one negates the other
if (args.c) {
// connect to an existing session
spinner.text = `Connecting to an existing session with ${args.c}`;
spinner.start();
let guestKey;
if (args.k) {
guestKey = gpg.getPublicKey(args.k);
} else {
guestKey = gpg.myGpgKey();
}
const hostKey = gpg.getPublicKey(args.c);
log('joining a connection-----------------------');
log('key-> ' + hostKey + ':' + guestKey);
request
.post('/connection', {
guestKey,
hostKey,
})
.then((result) => {
spinner.stop();
socket.emit('verify connection', { roomId: result.data.roomId });
socket.emit('join signal', { roomId: result.data.roomId });
require('./dist/App')({
socket,
roomId: result.data.roomId,
username,
target: args.c,
});
})
.catch((e) => {
if (e?.response?.status === 400) {
console.error("\nBad request: the requested session doesn't exist!");
} else {
console.error('An unknown error occurred!');
}
process.exit(1);
});
} else if (args.w) {
// create a new session
spinner.text = `Creating a new session with ${args.w}`;
spinner.start();
let hostKey;
if (args.k) {
hostKey = gpg.getPublicKey(args.k);
} else {
hostKey = gpg.myGpgKey();
}
const guestKey = gpg.getPublicKey(args.w);
log('creating a connection---------------------------------');
log('key-> ' + hostKey + ':' + guestKey);
request
.post('/session', {
hostKey,
guestKey,
})
.then((result) => {
log('result data:' + JSON.stringify(result.data));
spinner.text = `Waiting for ${args.w} to join`;
socket.emit('verify connection', { roomId: result.data.id });
socket.on('join signal', () => {
spinner.stop();
require('./dist/App')({
socket,
roomId: result.data.id,
username,
target: args.c,
});
});
})
.catch(() => {
console.error('\nAn error occurred, please try again!');
process.exit(1);
});
} else {
console.log(
'no flag passed, please use chatty --help to print the help information'
);
process.exit(1);
}