1- Activate the VPN by clicking on the panel icon.
2- The panel icon changes when the VPN is activated, and changes again when it is deactivated.
However, I noticed a significant reduction in battery time. I'm not sure if it's due to the applet, because I made some other changes in LM. So the question came to me: can applets consume a lot of battery power?
This is the code:
Code: Select all
const Applet = imports.ui.applet;
const Main = imports.ui.main;
const GLib = imports.gi.GLib;
const Util = imports.misc.util;
function MyVPNApplet(orientation, panelHeight, instanceId) {
this._init(orientation, panelHeight, instanceId);
}
MyVPNApplet.prototype = {
__proto__: Applet.TextIconApplet.prototype,
_init: function(orientation, panelHeight, instanceId) {
Applet.TextIconApplet.prototype._init.call(this, orientation, panelHeight, instanceId);
this.set_applet_icon_name("network-vpn-no-route-symbolic");
// Conectar la señal de clic del applet a la función on_applet_clicked
this.connect('button-press-event', this.on_applet_clicked.bind(this));
},
_updateStatus: function() {
let [res, out] = GLib.spawn_command_line_sync("protonvpn-cli s");
let status = new TextDecoder().decode(out).trim();
if (status === "No active Proton VPN connection.") {
this.set_applet_icon_name("network-vpn-no-route-symbolic");
} else {
this.set_applet_icon_name("network-vpn-symbolic");
}
},
on_applet_clicked: function() {
// Ejecutar el script al hacer clic en el applet
Util.spawnCommandLine("VPN_Script.sh");
// Actualizar el estado después de ejecutar el script
GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 6, this._updateStatus.bind(this));
}
};
function main(metadata, orientation, panelHeight, instanceId) {
return new MyVPNApplet(orientation, panelHeight, instanceId);
}
Code: Select all
# Check VPN status
vpn_status=$(protonvpn-cli s)
if [[ $vpn_status == *"No active Proton VPN connection."* ]]; then
# You are not connected, connect the VPN
echo "Connecting..."
protonvpn-cli c -f
else
# You are connected, disconnect the VPN.
echo "Disconnecting..."
protonvpn-cli d
fi