forked from Josholith/gnome-extension-lan-ip-address
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
147 lines (120 loc) · 4.34 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
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const Gio = imports.gi.Gio;
const St = imports.gi.St;
const Lang = imports.lang;
const PanelMenu = imports.ui.panelMenu;
const Clutter = imports.gi.Clutter;
const GLib = imports.gi.GLib;
const ShellToolkit = imports.gi.St;
const GObject = imports.gi.GObject;
const ExtensionUtils = imports.misc.extensionUtils;
const Extension = ExtensionUtils.getCurrentExtension();
// function _get_lan_ip() {
// // Ask the IP stack what route would be used to reach 1.1.1.1 (Cloudflare DNS)
// // Specifically, what src would be used for the 1st hop?
// var command_output_bytes = GLib.spawn_command_line_sync('ip route get 1.1.1.1')[1];
// var command_output_string = '';
// for (var current_character_index = 0;
// current_character_index < command_output_bytes.length;
// ++current_character_index)
// {
// var current_character = String.fromCharCode(command_output_bytes[current_character_index]);
// command_output_string += current_character;
// }
// // Output of the "ip route" command will be a string
// // " ... src 1.2.3.4 ..."
// // So basically we want the next token (word) immediately after the "src"
// // word, and nothing else. This is considerd our LAN IP address.
// var Re = new RegExp(/src [^ ]+/g);
// var matches = command_output_string.match(Re);
// var lanIpAddress;
// if (matches) {
// lanIpAddress = matches[0].split(' ')[1];
// } else {
// lanIpAddress = '';
// }
// return lanIpAddress;
// }
function _get_dev_ip(device) {
// Ask the IP stack what the current IP address of the device is
var cmd = 'ip a s ' + device;
var command_output_bytes = GLib.spawn_command_line_sync(cmd)[1];
var command_output_string = '';
for (var current_character_index = 0;
current_character_index < command_output_bytes.length;
++current_character_index)
{
var current_character = String.fromCharCode(command_output_bytes[current_character_index]);
command_output_string += current_character;
}
var Re = new RegExp(/([0-9]{1,3}[.]){3}[0-9]{1,3}/g);
var matches = command_output_string.match(Re);
var ipAddress = '';
if (matches) {
ipAddress = matches[0];
}
return ipAddress;
}
const LanIpAddressIndicator = GObject.registerClass(class LanIpAddressIndicator extends PanelMenu.Button {
_init() {
super._init(0.0, "LAN IP Address Indicator", false);
this.boxLayout = new St.BoxLayout();
this.iconVpn = new St.Icon({
style_class: 'system-status-icon',
});
this.iconVpn.set_gicon(Gio.icon_new_for_string(Extension.path + '/network-vpn-symbolic.svg'));
this.buttonText = new St.Label({
text: 'Loading...',
y_align: Clutter.ActorAlign.CENTER
});
this.boxLayout.add_child(this.iconVpn);
this.boxLayout.add_child(this.buttonText);
this.add_child(this.boxLayout)
this._updateLabel();
}
_updateLabel() {
const refreshTime = 5 // in seconds
if (this._timeout) {
Mainloop.source_remove(this._timeout);
this._timeout = null;
}
this._timeout = Mainloop.timeout_add_seconds(refreshTime, Lang.bind(this, this._updateLabel));
// this.buttonText.set_text(_get_lan_ip());
var ipAddress = _get_dev_ip('tun0');
if (ipAddress == '') {
this.boxLayout.visible = true;
this.buttonText.set_text('----');
}
else {
this.boxLayout.visible = true;
this.buttonText.set_text(ipAddress);
}
}
_removeTimeout() {
if (this._timeout) {
this._timeout = null;
}
}
stop() {
if (this._timeout) {
Mainloop.source_remove(this._timeout);
}
this._timeout = undefined;
this.menu.removeAll();
}
});
let _indicator;
function init() {
log('LAN IP Address extension initialized');
}
function enable() {
log('LAN IP Address extension enabled');
_indicator = new LanIpAddressIndicator();
Main.panel.addToStatusArea('lan-ip-address-indicator', _indicator);
}
function disable() {
log('LAN IP Address extension disabled');
_indicator.stop();
_indicator.destroy();
}