-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
181 lines (153 loc) · 6.16 KB
/
extension.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const vscode = require('vscode');
const DHT = require('holesail-server');
const holesailClient = require('holesail-client');
const ncp = require("copy-paste");
let server;
let client;
function activate(context) {
let share = vscode.commands.registerCommand('holesail-liveports.liveports-share', function () {
function validateInput(value) {
if (isNaN(value)) {
return "Port must be a valid number";
}
return null;
}
const addressRegex = /^(localhost|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$/;
vscode.window.showInputBox({
prompt: "Enter port number",
placeHolder: "Port",
validateInput
}).then(port => {
if (!port) {
return;
}
vscode.window.showInputBox({
prompt: "Enter address",
placeHolder: "127.0.0.1",
validateInput: (value) => {
if (!value.match(addressRegex)) {
return "Invalid address format";
}
return null;
}
}).then(address => {
if (!address) {
address = '127.0.0.1';
}
server = new DHT();
server.serve({port: port, address: address}, () => {
console.log(address + ":" + port, "is now live on the following key =>");
console.log('Server public key:', server.getPublicKey());
vscode.window.showInformationMessage(address + ":" + port + ' Server public key:', server.getPublicKey()).then(selected => {
if (selected === server.getPublicKey()) {
ncp.copy(selected, function () {
vscode.window.showInformationMessage('Key copied to clipboard.');
})
}
});
});
});
});
});
let connect = vscode.commands.registerCommand('holesail-liveports.liveports-connect', function () {
function validateInput(value) {
if (isNaN(value)) {
return "Port must be a valid number";
}
return null;
}
vscode.window.showInputBox({
prompt: "Enter connection key",
placeHolder: "Connection Key (Required)",
}).then(key => {
if (!key) {
return;
}
vscode.window.showInputBox({
prompt: "Enter Port (Optional)",
placeHolder: "8000",
validateInput
}).then(port => {
if (!port) {
port = '8000';
}
client = new holesailClient(key)
client.connect({port: port, address:"localhost"}, () => {
const url = "http://localhost:" + port + "/";
vscode.window.showInformationMessage("Connected to the client", url).then((selection) => {
if (selection === url) {
vscode.env.openExternal(vscode.Uri.parse(url));
}
});
});
});
});
});
let showOptions1 = vscode.commands.registerCommand('holesail-liveports.showOptions1', function () {
const options = [
{ label: 'Share', command: 'holesail-liveports.liveports-share' },
{ label: 'Destroy Server', command: 'holesail-liveports.destroyServer' },
];
vscode.window.showQuickPick(options, {
placeHolder: 'Select an option',
matchOnDetail: true
}).then(option => {
if (option) {
vscode.commands.executeCommand(option.command);
}
});
});
let showOptions2 = vscode.commands.registerCommand('holesail-liveports.showOptions2', function () {
const options = [
{ label: 'Connect', command: 'holesail-liveports.liveports-connect' },
{ label: 'Disconnect client', command: 'holesail-liveports.disconnectClient' },
];
vscode.window.showQuickPick(options, {
placeHolder: 'Select an option',
matchOnDetail: true
}).then(option => {
if (option) {
vscode.commands.executeCommand(option.command);
}
});
});
let destroyServer = vscode.commands.registerCommand('holesail-liveports.destroyServer', function () {
if (server) {
server.destroy();
vscode.window.showInformationMessage('Server destroyed.');
} else {
vscode.window.showInformationMessage('No server to destroy.');
}
});
let disconnectClient = vscode.commands.registerCommand('holesail-liveports.disconnectClient', function () {
if (client) {
client.destroy();
vscode.window.showInformationMessage('Client Disconnected.');
} else {
vscode.window.showInformationMessage('Client not connected.');
}
});
let statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusBarItem.text = "⛵ Share Ports";
statusBarItem.tooltip = "Share Ports";
statusBarItem.command = "holesail-liveports.showOptions1";
statusBarItem.show();
context.subscriptions.push(statusBarItem);
let statusBarItem2 = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusBarItem2.text = "⛵ connect Ports";
statusBarItem2.tooltip = "connect Ports";
statusBarItem2.command = "holesail-liveports.showOptions2";
statusBarItem2.show();
context.subscriptions.push(statusBarItem2);
context.subscriptions.push(showOptions1);
context.subscriptions.push(showOptions2);
context.subscriptions.push(destroyServer);
context.subscriptions.push(disconnectClient);
context.subscriptions.push(connect);
context.subscriptions.push(share);
}
function deactivate() {}
module.exports = {
activate,
deactivate
}