-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathUrl.js
144 lines (112 loc) · 3.5 KB
/
Url.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
/*
* Copyright (c) 2018-present, Evgeny Nadymov
*
* This source code is licensed under the GPL v.3.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import punycode from './Punycode';
import OptionStore from '../Stores/OptionStore';
let tMeUrl = null;
const telegramUrls = [
't.me/',
'telegram.org/',
'telegram.dog/',
tMeUrl
];
function getTelegramUrls() {
if (!tMeUrl) {
tMeUrl = OptionStore.get('t_me_url');
if (tMeUrl) {
const url = tMeUrl.value
.toLowerCase()
.replace('https://', '')
.replace('http://', '')
telegramUrls.push(url);
}
}
return telegramUrls;
}
export function isTelegramLink(href) {
if (!href) return false;
const lowerCaseUrl = href
.toLowerCase()
.replace('https://', '')
.replace('http://', '');
if (lowerCaseUrl.startsWith('tg://')) {
return true;
}
return getTelegramUrls().some(x => lowerCaseUrl.startsWith(x));
}
export function getHref(url, mail) {
if (!url) return null;
if (mail) return url.startsWith('mailto:') ? url : 'mailto:' + url;
if (url.startsWith('tg:')) return url;
return url.startsWith('http') ? url : 'http://' + url;
}
export function isUrlSafe(displayText, url) {
if (displayText && displayText !== url) {
return false;
}
if (hasRTLOSymbol(url)) {
return false;
}
return true;
}
export function getDecodedUrl(url, mail) {
if (!url) return null;
const href = getHref(url, mail);
try {
let decodedHref = decodeURI(href);
const domain = decodedHref.match(/^(https?|tg):\/\/([^\/:?#]+)(?:[\/:?#]|$)/i)[1];
decodedHref = decodedHref.replace(domain, punycode.ToUnicode(domain));
return decodedHref;
} catch (error) {
console.log('SafeLink.getDecodedUrl error ', url, error);
}
return null;
}
const regExpRTLO = /\u202e/;
export function hasRTLOSymbol(url) {
if (!url) return false;
return regExpRTLO.test(url);
}
const regExpDomainExplicit = new RegExp(
'(?:([a-zA-Z]+):\\/\\/)((?:[A-Za-z' +
'\xD0\x90-\xD0\xAF\xD0\x81' +
'\xD0\xB0-\xD1\x8F\xD1\x91' +
'0-9\\-\\_]+\\.){0,10}([A-Za-z' +
'\xD1\x80\xD1\x84' +
'\\-\\d]{2,22})(\\:\\d+)?)'
);
const regExpDomain = new RegExp(
'(?:([a-zA-Z]+):\\/\\/)?((?:[A-Za-z' +
'\xD0\x90-\xD0\xAF\xD0\x81' +
'\xD0\xB0-\xD1\x8F\xD1\x91' +
'0-9\\-\\_]+\\.){1,10}([A-Za-z' +
'\xD1\x80\xD1\x84' +
'\\-\\d]{2,22})(\\:\\d+)?)'
);
const regExpProtocol = new RegExp('^([a-zA-Z]+):\\/\\/');
// https://github.com/telegramdesktop/tdesktop/blob/4e80d54be130eca76129f2c4995fe685d1014442/Telegram/SourceFiles/base/qthelp_url.cpp#L105
export function validateUrl(value) {
// value = punycode.ToASCII(value);
const trimmed = value.trim();
if (!trimmed) {
return null;
}
const match = trimmed.match(regExpDomainExplicit);
if (!match) {
const domainMatch = trimmed.match(regExpDomain);
if (!domainMatch || domainMatch.index !== 0) {
return null;
}
return 'http://' + trimmed;
} else if (match.index !== 0) {
return null;
}
const protocolMatch = trimmed.match(regExpProtocol);
return protocolMatch && isGoodProtocol(protocolMatch[0]) ? trimmed : null;
}
function isGoodProtocol(value) {
return ['http', 'https', 'tg'].some(x => value.toLowerCase().indexOf(x) === 0);
}