-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
199 lines (178 loc) · 5.57 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* WARP Toggle by Vlad Krupinskii 2025 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
import GObject from "gi://GObject";
import Gio from "gi://Gio";
import GLib from "gi://GLib";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import {
Extension,
gettext as _,
} from "resource:///org/gnome/shell/extensions/extension.js";
import {
QuickToggle,
SystemIndicator,
} from "resource:///org/gnome/shell/ui/quickSettings.js";
// Executes a shell command and returns its stdout output.
async function runCommand(cmd) {
return new Promise((resolve, reject) => {
try {
console.log(`Executing command: warp-cli ${cmd}`);
const proc = new Gio.Subprocess({
argv: ["warp-cli", cmd],
flags:
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE,
});
proc.init(null);
proc.communicate_utf8_async(null, null, (proc, res) => {
try {
let [ok, stdout, stderr] = proc.communicate_utf8_finish(res);
if (!ok || proc.get_exit_status() !== 0) {
console.error("Failed to execute warp-cli:", cmd, stderr);
reject(stderr);
return;
}
resolve(stdout ? stdout.trim() : "");
} catch (e) {
reject(e);
}
});
} catch (e) {
reject(e);
}
});
}
const WarpToggle = GObject.registerClass(
class WarpToggle extends QuickToggle {
constructor() {
super({
title: _("WARP"),
iconName: "network-vpn-symbolic",
toggleMode: true,
checked: false,
});
this._attempts = 0;
this._maxAttempts = 5;
this._statusCheckId = null; // Store timeout ID
this._updateStatus(); // Initial check
this._startCheckingStatus(); // Start periodic updates
// Monitor state changes
this.connect("clicked", () => {
this._attempts = 0;
runCommand(this.checked ? "connect" : "disconnect");
this._startCheckingStatus(); // Restart checking when user toggles
});
}
async _updateStatus() {
try {
const connected = await this._isWarpConnected();
if (connected) {
this._attempts = 0;
this.checked = true;
this._stopCheckingStatus(); // Stop updates when connected
} else {
this._attempts++;
this.checked = false;
if (this._attempts >= this._maxAttempts) {
Main.notifyError("WARP Toggle", "Can't connect to WARP");
this._attempts = 0;
}
}
} catch (err) {
console.error("Error checking Warp status:", err);
}
}
_startCheckingStatus() {
if (this._statusCheckId) return; // Prevent duplicate timers
this._statusCheckId = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
10,
() => {
this._updateStatus();
return GLib.SOURCE_CONTINUE;
},
);
}
_stopCheckingStatus() {
if (this._statusCheckId) {
GLib.source_remove(this._statusCheckId);
this._statusCheckId = null;
}
}
async _isWarpConnected() {
try {
const status = await runCommand("status");
if (!status) {
console.error("Warp status command returned empty response.");
return false;
}
console.log("Warp status:", status);
const res = status.toLowerCase();
return res.includes("connected") || res.includes("connecting");
} catch (err) {
console.error("Error checking Warp status:", err);
return false;
}
}
destroy() {
this._stopCheckingStatus();
super.destroy();
}
},
);
const WarpIndicator = GObject.registerClass(
class WarpIndicator extends SystemIndicator {
constructor() {
super();
// Create the indicator icon
this._indicator = this._addIndicator();
this._indicator.iconName = "network-vpn-symbolic";
// Create the toggle button and bind its visibility to connection state
const toggle = new WarpToggle();
toggle.bind_property(
"checked",
this._indicator,
"visible",
GObject.BindingFlags.SYNC_CREATE,
);
this.quickSettingsItems.push(toggle);
}
destroy() {
this.quickSettingsItems.forEach((item) => item.destroy());
super.destroy();
}
},
);
export default class WARPExtension extends Extension {
enable() {
// Check if warp-cli exists before executing.
if (!(GLib.find_program_in_path("warp-cli") ? true : false)) {
console.error("warp-cli command not found");
Main.notifyError("WARP Toggle", "warp-cli command not found");
return;
}
// Add the indicator to the system panel
this._indicator = new WarpIndicator();
Main.panel.statusArea.quickSettings.addExternalIndicator(this._indicator);
}
disable() {
if (this._indicator) {
this._indicator.destroy();
this._indicator = null;
}
}
}