summaryrefslogtreecommitdiffstats
path: root/src/scripts/utils.js
blob: e456d2358584bcde5f01095029a6338ae1e7f2b3 (plain)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
'use strict';

// ----------------- Constants -----------------------------
const FOXYPROXY_BASIC = false;

// Bit-wise flags so we can add/remove these independently. We may add more later so PROTOCOL_ALL is future-proof.
const PROTOCOL_ALL = 1; // in case other protocols besides http and https are supported later
const PROTOCOL_HTTP = 2;
const PROTOCOL_HTTPS = 4;


// import | pac
const PROXY_TYPE_HTTP = 1;
const PROXY_TYPE_HTTPS = 2;
const PROXY_TYPE_SOCKS5 = 3;
const PROXY_TYPE_SOCKS4 = 4;
const PROXY_TYPE_NONE = 5; // DIRECT
const PROXY_TYPE_PAC = 6;
const PROXY_TYPE_WPAD = 7;
const PROXY_TYPE_SYSTEM = 8;
const PROXY_TYPE_PASS = 9;


const PATTERN_TYPE_WILDCARD = 1;
const PATTERN_TYPE_REGEXP = 2;

// Storage keys that are not proxy settings
const NON_PROXY_KEYS = ['mode', 'logging', 'sync', 'browserVersion', 'foxyProxyVersion', 'foxyProxyEdition', 'nextIndex'];

// bg | import | proxy | utils
const PATTERN_ALL_WHITE = {
  title: 'all URLs',
  active: true,
  pattern: '*',
  type: 1,                    // PATTERN_TYPE_WILDCARD,
  protocols: 1                // PROTOCOL_ALL
};

const DEFAULT_COLOR = '#66cc66'; // default proxy color

// patterns | proxy
// the local-internal blacklist, always used as a set
const blacklistSet = [
  {
    title: "local hostnames (usually no dots in the name). Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
    pattern: "^(?:[^:@/]+(?::[^@/]+)?@)?(?:localhost|127\\.\\d+\\.\\d+\\.\\d+)(?::\\d+)?(?:/.*)?$",
  },
  {
    title: "local subnets (IANA reserved address space). Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
    pattern: "^(?:[^:@/]+(?::[^@/]+)?@)?(?:192\\.168\\.\\d+\\.\\d+|10\\.\\d+\\.\\d+\\.\\d+|172\\.(?:1[6789]|2[0-9]|3[01])\\.\\d+\\.\\d+)(?::\\d+)?(?:/.*)?$",
  },
  {
    title: "localhost - matches the local host optionally prefixed by a user:password authentication string and optionally suffixed by a port number. The entire local subnet (127.0.0.0/8) matches. Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
    pattern: "^(?:[^:@/]+(?::[^@/]+)?@)?[\\w-]+(?::\\d+)?(?:/.*)?$"
  }
].map (item => {
  item.active = true;
  item.type = 2;              // PATTERN_TYPE_REGEXP,
  item.protocols = 1;         // PROTOCOL_ALL
  return item;
});

// ----------------- Utils ---------------------------------
class Utils {

  static notify(message, title = 'FoxyProxy') {
    // the id is not used anywhere and can be omitted, it is only useful if you want to manually close the notification early
    chrome.notifications.create('foxyproxy', {
      type: 'basic',
      iconUrl: '/images/icon.svg',
      title,
      message
    });
  }

  // options | popup
  static isUnsupportedType(type) {
    //return type === PROXY_TYPE_PAC || type === PROXY_TYPE_WPAD || type === PROXY_TYPE_SYSTEM || type === PROXY_TYPE_PASS;
    return [PROXY_TYPE_PAC, PROXY_TYPE_WPAD, PROXY_TYPE_SYSTEM, PROXY_TYPE_PASS].includes(type);
  }

  // bg | pattern-tester | validate-pattern
  static wildcardToRegExp(pat) {

    let start = 0, end = pat.length, matchOptionalSubdomains = false;

    if (pat[0] === '.') { pat = '*' + pat; }

    if (pat.startsWith('**')) {
      // Strip asterisks from front and back
      while (pat[start] === '*' && start < end) start++;
      while (pat[end - 1] === '*' && start < end) end--;
      // If there's only an asterisk left, match everything
      if (end - start == 1 && pat[start] == '*') return '';
    }
    else if (pat.startsWith('*.')) { matchOptionalSubdomains = true; }

    let regExpStr = pat.substring(start, end+1)
      // $& replaces with the string found, but with that string escaped
      .replace(/[$.+()^{}\]\[|]/g, '\\$&')
      .replace(/\*/g, '.*')
      .replace(/\?/g, '.');

    if (matchOptionalSubdomains) {
        // Non-capturing group that matches:
        // any group of non-whitespace characters following by an optional . repeated zero or more times
        regExpStr = '(?:\\S+\\.)*' + regExpStr.substring(4);
    }

    // Leading or ending double-asterisks mean exact starting and ending positions
    if (start === 0) { regExpStr = '^' + regExpStr; }
    if (end === pat.length) { regExpStr += '$'; }
    return regExpStr;
  }

  // Prep the patternObject for matching: convert wildcards to regexp,
  // store the originalPattern which the user entered so we can display if needed, etc.
  // Return null if patternObject is inactive or there is an error.
	static processPatternObject(patternObject) {
    if (patternObject.active) {
      // Store the original pattern so if this pattern matches something,
      // we can display whatever the user entered ("original") in the log.
      patternObject.originalPattern = patternObject.pattern;
      if (patternObject.type === PATTERN_TYPE_WILDCARD) {
        patternObject.pattern = Utils.wildcardToRegExp(patternObject.pattern);
      }
      try {
        // Convert to real RegExp, not just a string. Validate. If invalid, notify user.
        patternObject.pattern = new RegExp(patternObject.pattern, 'i');
        return patternObject;
      }
      catch(e) {
  			console.error(`Error creating regexp for pattern: ${patternObject.pattern}`, e);
  			Utils.notify(`Error creating regular expression for pattern ${regExpStr}`);
  		}
    }
    return null;
	}

  // import | pattern
  static importFile(file, mimeTypeArr, maxSizeBytes, jsonOrXml, callback) {

    if (!file) {
      alert('There was an error');
      return;
    }

    // Check MIME type // Ch65 no filetype for JSON
    if (!mimeTypeArr.includes(file.type)) {
      alert('Unsupported file format');
      return;
    }

    if (file.size > maxSizeBytes) {
      alert('Filesize is too large');
      return;
    }

    const reader  = new FileReader();
    reader.onloadend = () => {
      if (reader.error) {
        alert('Error reading file.');
        return;
      }

      let settings;
      try {
        if (jsonOrXml === 'json') { settings = JSON.parse(reader.result); }
        else if (jsonOrXml === 'xml') {
          settings = new DOMParser().parseFromString(reader.result, 'text/xml');
          if (settings.documentElement.nodeName === 'parsererror') { throw new Error(); }
        }
      }
      catch(e) {
        console.log(e);
        alert("Error parsing file. Please remove sensitive data from the file, and then email it to support@getfoxyproxy.org so we can fix bugs in our parser.");
        return;
      }
      if (settings && confirm('This will overwite existing proxy settings. Are you sure?')) { callback(settings); }
      else { callback(); }

    };
    reader.onerror = () => { alert('Error reading file'); };
    reader.readAsText(file);
  }

  // import | options
  static exportFile() {

    chrome.storage.local.get(null, result => {
      browser.runtime.getBrowserInfo().then((bi) => {
        !result.sync ? Utils.saveAs(result, bi.version) : chrome.storage.sync.get(null, result => {
          Utils.saveAs(result, bi.version, true);
        });
      });
    });
  }
  // exportFile helper
  static saveAs(data, browserVersion, sync) {

    const settings = data; //Utils.prepareForSettings(data);
    // Browser version and extension version. These are used for debugging.
    settings.browserVersion = browserVersion;
    settings.foxyProxyVersion = chrome.runtime.getManifest().version;
    settings.foxyProxyEdition = FOXYPROXY_BASIC ? 'basic' : 'standard';
    settings.sync = sync;
    const blob = new Blob([JSON.stringify(settings, null, 2)], {type : 'text/plain;charset=utf-8'});
    const filename = chrome.i18n.getMessage('extensionName') + '_' + new Date().toISOString().substring(0, 10) + '.json';
    chrome.downloads.download({
      url: URL.createObjectURL(blob),
      filename,
      saveAs: true,
      conflictAction: 'uniquify'
    });
  }

  static updateIcon(iconPath, color, title, titleIsKey, badgeText, badgeTextIsKey) {
    chrome.browserAction.setIcon({path: iconPath});
    if (color) {
      chrome.browserAction.setBadgeBackgroundColor({color: color});
    }
    else {
      // TODO: confirm this is OK to do
      chrome.browserAction.setBadgeBackgroundColor({color: null});
    }
    if (title) {
      chrome.browserAction.setTitle({title: 'FoxyProxy: ' + (titleIsKey ? chrome.i18n.getMessage(title) : title)});
    }
    else {
      chrome.browserAction.setTitle({title: ''});
    }
    if (badgeText) {
      chrome.browserAction.setBadgeText({text: badgeTextIsKey ? chrome.i18n.getMessage(badgeText) : badgeText});
    }
    else {
      chrome.browserAction.setBadgeText({text: ''});
    }
  }

  static getProxyTitle(proxySetting) {
    if (proxySetting.title) {
      return proxySetting.title;
    }
    else if (proxySetting.type === PROXY_TYPE_NONE) {
      return 'Direct (no proxy)';
    }
    else {
      return `${proxySetting.address}:${proxySetting.port}`;
    }
  }

/*
  // utils only used for export, will be removed as DB format export is adapted
  static prepareForSettings(settings = {}) {

    //if (settings && !settings.mode) { }// 5.0 settings

    let lastResortFound = false;
    const prefKeys = Object.keys(settings);

    const def = {
      id: LASTRESORT,
      active: true,
      title: 'Default',
      notes: 'These are the settings that are used when no patterns match a URL.',
      color: '#0055E5',
      type: PROXY_TYPE_NONE,
      whitePatterns: [PATTERN_ALL_WHITE],
      blackPatterns: []
    };

    // base format
    const ret = {
      mode: 'disabled',
      proxySettings: [],
      logging: {
        size: 500,
        active: true
      }
    };

    if (!prefKeys.length) {                                     // settings is {}
      ret.proxySettings = [def];
      return ret;
    }

    prefKeys.forEach(key => {

      switch (key) {

        case 'mode':
        case 'logging':
          ret[key] = settings[key];
          break;

        case 'sync': break;                                 // do nothing

        default:
          const temp = settings[key];
          temp.id = key;                                    // Copy the id into the object
          temp.id === LASTRESORT && (lastResortFound = true);
          ret.proxySettings.push(temp);
      }
    });

    ret.proxySettings.sort((a, b) => a.index - b.index);
    ret.proxySettings.forEach(item => delete item.index);   // Re-calculated when/if this object is written to disk again (user may move proxySetting up/down)

    !lastResortFound && ret.proxySettings.push(def);        // add default lastresort

    return ret;
  }
*/

  static getUniqueId() {
    // We don't need cryptographically secure UUIDs, just something unique
    return Math.random().toString(36).substring(7) + new Date().getTime();
  }

  static stripBadChars(str) {
    return str ? str.replace(/[&<>"']+/g, '') : null;
  }

}