-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathextension.js
201 lines (174 loc) · 6.36 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
200
201
'use strict';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import St from 'gi://St';
import Clutter from 'gi://Clutter';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
// Start with WAN address as default
var type=4;
function _get_tun0() {
// Run ifconfig and pull the ip address for tun0 or vpn0
var command_output_bytes = GLib.spawn_command_line_sync('/bin/bash -c "ifconfig vpn0 || ifconfig tun0"')[1];
var command_output_string = '';
for (var i = 0; i < command_output_bytes.length; ++i){
var current_character = String.fromCharCode(command_output_bytes[i]);
command_output_string += current_character;
}
var Re = new RegExp(/inet [^ ]+/g);
var matches = command_output_string.match(Re);
var tun0IpAddress;
if (matches) {
tun0IpAddress = matches[0].split(' ')[1];
} else {
tun0IpAddress = '';
}
return tun0IpAddress;
}
function _get_lan_ip4() {
// 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_lan_ip6() {
// Ask the IP stack what route would be used to reach 2001:: (random ipv6 address)
// Specifically, what src would be used for the 1st hop?
var command_output_bytes = GLib.spawn_command_line_sync('ip route get 2001::')[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 2001:xxx:yyy:..."
// 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_wan_ip4() {
// Use the google dns servers to find the publip ip address used for requests
// Force a ipv4 conection, because ipv6 won't be NAT'ed
var command_output_bytes = GLib.spawn_command_line_sync('dig TXT +short o-o.myaddr.l.google.com @ns1.google.com -4')[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;
}
command_output_string=command_output_string.replace('"','').replace('"','').replace('\n','');
// Validate the result looks like an ipv4 address
var Re = new RegExp(/.*\..*\..*\..*/g);
var matches = command_output_string.match(Re);
var wanIpAddress;
if (matches) {
wanIpAddress = command_output_string;
} else {
wanIpAddress = '';
}
return wanIpAddress;
}
class AllIPAddressIndicator extends PanelMenu.Button{
static {
GObject.registerClass(this);
}
_init() {
// Chaining up to the super-class
super._init(0.0, "All IP Addresses Indicator", false);
this.buttonText = new St.Label({
text: 'Loading...',
y_align: Clutter.ActorAlign.CENTER
});
this.add_child(this.buttonText);
this._updateLabel();
}
_toggleView(){
console.log("Updating label for all-ip extension")
if (type===4) {
type=6;
} else if (type===6) {
type=0;
} else if (type===0){
type=1;
} else if (type===1){
type=4
}
this._updateLabel();
}
_updateLabel(){
const refreshTime = 20 // in seconds
if (this._timeout) {
GLib.source_remove(this._timeout);
this._timeout = null;
}
this._timeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, refreshTime, () => {this._updateLabel();});
// Show the right format. 0 = WAN, 4 = IPv4, 6=IPv6
if (type===4) {
this.buttonText.set_text("LAN: "+_get_lan_ip4());
} else if (type===0) {
this.buttonText.set_text("WAN: "+_get_wan_ip4());
} else if (type===6){
this.buttonText.set_text("IP6: "+_get_lan_ip6());
} else {
this.buttonText.set_text("VPN: "+_get_tun0());
}
}
_removeTimeout() {
if (this._timeout) {
this._timeout = null;
}
}
stop() {
if (this._timeout) {
GLib.source_remove(this._timeout);
}
this._timeout = undefined;
this.menu.removeAll();
}
}
export default class AllIPAddressExtension extends Extension {
enable() {
this._indicator = new AllIPAddressIndicator();
Main.panel.addToStatusArea('all-ip-addresses-indicator', this._indicator);
this._indicator.connect('button-press-event', () => this._indicator._toggleView());
}
disable() {
this._indicator.stop();
this._indicator.destroy();
this._indicator = null;
}
}