-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcreate.ts
More file actions
260 lines (232 loc) · 7.27 KB
/
Copy pathcreate.ts
File metadata and controls
260 lines (232 loc) · 7.27 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import * as vscode from "vscode";
import { CloudflareTunnel, CloudflareTunnelStatus } from "../tunnel";
import { cloudflared } from "../cmd/cloudflared";
import { cloudflareTunnelProvider } from "../providers/tunnels";
import { cloudflareTunnelStatusBar } from "../statusbar/statusbar";
import { showErrorMessage, showInformationMessage } from "../utils";
import { globalState } from "../state/global";
import { config } from "../state/config";
import { TunnelPreset } from "../types";
import * as constants from "../constants";
const TRYCLOUDFLARE_LABEL = "trycloudflare.com (random subdomain)";
function portValidateInput(value: string): string | undefined {
if (!value) {
return undefined;
}
const port = parseInt(value, 10);
if (!port) {
return "Please enter a valid port number.";
}
if (port < 1 || port > 65535) {
return "Port number must be between 1 and 65535.";
}
if (cloudflareTunnelProvider.hasPort(port)) {
return "Port number is already in use.";
}
return undefined;
}
async function getPortInput(): Promise<number | null> {
const response = await vscode.window.showInputBox({
title: "Port number",
value: config.defaultPort.toString(),
prompt: "Select your local port number.",
ignoreFocusOut: true,
validateInput: portValidateInput,
});
return response ? parseInt(response, 10) : null;
}
function hostnameValidateInput(value: string): string | undefined {
if (!value) {
return undefined;
}
if (!/^[a-zA-Z0-9.-]+$/.test(value)) {
return "Invalid hostname. Only alphanumeric characters, dots, and dashes are allowed.";
}
if (cloudflareTunnelProvider.hasHostname(value)) {
return "Hostname is already in use.";
}
return undefined;
}
async function getHostname(): Promise<string | null> {
if (globalState.isLoggedIn) {
return (
(await vscode.window.showInputBox({
title: "Hostname",
value: config.defaultHostname,
placeHolder: "Enter a hostname",
ignoreFocusOut: true,
prompt:
"Your domain hostname. If not specified anything, it will generate a `.trycloudflare.com` subdomain. Make sure to login and give proper permissions before changing this setting. Example: `mytunnel.mydomain.com`",
validateInput: hostnameValidateInput,
})) || null
);
}
return null;
}
interface PresetQuickPickItem extends vscode.QuickPickItem {
preset?: TunnelPreset;
isNew?: boolean;
}
function presetLabel(preset: TunnelPreset): string {
const target = preset.hostname ?? TRYCLOUDFLARE_LABEL;
return `${preset.port} → ${target}`;
}
async function pickPreset(): Promise<TunnelPreset | "new" | null> {
const presets = globalState.tunnelPresets;
if (presets.length === 0) {
return "new";
}
const deleteButton: vscode.QuickInputButton = {
iconPath: new vscode.ThemeIcon("trash"),
tooltip: "Delete preset",
};
const buildItems = (current: TunnelPreset[]): PresetQuickPickItem[] => [
{
label: "$(add) New tunnel...",
description: "Configure a new port and hostname",
isNew: true,
},
...current.map<PresetQuickPickItem>((preset) => ({
label: `$(star) ${presetLabel(preset)}`,
description: cloudflareTunnelProvider.hasPort(preset.port)
? "Port already in use"
: undefined,
preset,
buttons: [deleteButton],
})),
];
return new Promise((resolve) => {
let settled = false;
const settle = (value: TunnelPreset | "new" | null): void => {
if (settled) {
return;
}
settled = true;
resolve(value);
};
const quickPick = vscode.window.createQuickPick<PresetQuickPickItem>();
quickPick.title = "Create Cloudflare Tunnel";
quickPick.placeholder = "Select a preset or create a new tunnel";
quickPick.ignoreFocusOut = true;
quickPick.items = buildItems(presets);
quickPick.onDidTriggerItemButton((event) => {
if (!event.item.preset) {
return;
}
globalState
.removeTunnelPreset(event.item.preset)
.then(() => {
const remaining = globalState.tunnelPresets;
if (remaining.length === 0) {
quickPick.hide();
settle("new");
return;
}
quickPick.items = buildItems(remaining);
})
.catch((err) => {
showErrorMessage(err);
quickPick.hide();
settle(null);
});
});
quickPick.onDidAccept(() => {
const [selected] = quickPick.selectedItems;
quickPick.hide();
if (!selected) {
settle(null);
return;
}
if (selected.isNew) {
settle("new");
return;
}
settle(selected.preset ?? null);
});
quickPick.onDidHide(() => {
quickPick.dispose();
settle(null);
});
quickPick.show();
});
}
async function createTunnel(): Promise<void> {
const selection = await pickPreset();
if (selection === null) {
return;
}
let port: number;
let hostname: string | null;
if (selection === "new") {
const portInput = await getPortInput();
if (!portInput) {
return;
}
port = portInput;
hostname = await getHostname();
} else {
if (cloudflareTunnelProvider.hasPort(selection.port)) {
showErrorMessage(
Error(`Port ${selection.port} is already in use by a running tunnel.`)
);
return;
}
if (
selection.hostname &&
cloudflareTunnelProvider.hasHostname(selection.hostname)
) {
showErrorMessage(
Error(
`Hostname ${selection.hostname} is already in use by a running tunnel.`
)
);
return;
}
port = selection.port;
hostname = selection.hostname;
}
try {
const tunnel = new CloudflareTunnel(config.localHostname, port, hostname);
cloudflareTunnelProvider.addTunnel(tunnel);
tunnel.subscribe(cloudflareTunnelProvider);
tunnel.subscribe(cloudflareTunnelStatusBar);
try {
await vscode.window.withProgress<void>(
{
location: vscode.ProgressLocation.Notification,
title: `Starting cloudflare tunnel for ${tunnel.url}. [(Show logs)](command:${constants.Commands.openOutputChannel})\n`,
cancellable: true,
},
async (progress, token) => {
token.onCancellationRequested(() => {
cloudflared.stop(tunnel);
cloudflareTunnelProvider.removeTunnel(tunnel);
});
if (hostname) {
progress.report({ message: "Creating tunnel..." });
await cloudflared.createTunnel(tunnel);
progress.report({ message: "Creating route dns..." });
await cloudflared.routeDns(tunnel);
}
progress.report({ message: "Starting tunnel..." });
await cloudflared.startTunnel(tunnel);
tunnel.process?.on("exit", () => {
cloudflareTunnelProvider.removeTunnel(tunnel);
});
}
);
tunnel.status = CloudflareTunnelStatus.running;
await globalState.upsertTunnelPreset({ port, hostname });
await showInformationMessage(
"Your Cloudflare Tunnel has been created!",
tunnel.tunnelUri
);
} catch (ex) {
cloudflareTunnelProvider.removeTunnel(tunnel);
showErrorMessage(ex);
}
} catch (ex) {
showErrorMessage(ex);
}
}
export default createTunnel;