将url中localhost端口改为当前host端口
This commit is contained in:
@ -1195,6 +1195,55 @@ class Tools {
|
||||
return `${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`;
|
||||
}
|
||||
|
||||
replaceHost(url, host) {
|
||||
let newUrl = url
|
||||
if(!url || !host) {
|
||||
return url
|
||||
}
|
||||
try {
|
||||
if (!url.startsWith("http")) {
|
||||
//说明是本地的json,在磁盘中存在的
|
||||
if (!url.includes(":")) {
|
||||
if (this.options.host) {
|
||||
let o = new URL(url, this.options.host)
|
||||
newUrl = o.href
|
||||
}
|
||||
}
|
||||
return newUrl
|
||||
}
|
||||
else {
|
||||
// 移除可能的用户名:密码前缀
|
||||
const authRegex = /^[^@]+@/;
|
||||
if (authRegex.test(url)) {
|
||||
url = url.replace(authRegex, '');
|
||||
}
|
||||
|
||||
// 添加协议前缀(如果没有)
|
||||
if (!/^[a-z]+:\/\//i.test(url)) {
|
||||
url = 'http://' + url;
|
||||
}
|
||||
|
||||
const parsedUrl = new URL(url);
|
||||
const parsedUrl2 = new URL(host);
|
||||
let hostname = parsedUrl.hostname;
|
||||
let port = parsedUrl.port;
|
||||
|
||||
// 处理IPv6地址(如果有括号)
|
||||
if (hostname.startsWith('[') && hostname.endsWith(']')) {
|
||||
hostname = hostname.slice(1, -1);
|
||||
}
|
||||
if ((hostname === 'localhost' || hostname === '127.0.0.1') && parseInt(port, 10) !== 55110) {
|
||||
parsedUrl.port = parsedUrl2.port
|
||||
parsedUrl.protocol = parsedUrl2.protocol
|
||||
newUrl = parsedUrl.toString()
|
||||
}
|
||||
return newUrl
|
||||
}
|
||||
} catch (error) {
|
||||
return newUrl
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user