mirror of
https://github.com/whyour/qinglong.git
synced 2026-02-12 14:05:38 +08:00
Address code review feedback
- Add error handling for file read operations (getOSReleaseInfo, getCurrentMirrorDomain) - Fix trailing slash bug: add slash before replacement, not after - Escape special regex characters in domain names to prevent incorrect replacements - Fix OS detection order: check Ubuntu before Debian to avoid misidentification - Add escapeRegExp helper function for regex escaping Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
parent
d43d563622
commit
c329c8acd4
|
|
@ -596,8 +596,13 @@ export function isDemoEnv() {
|
||||||
let osType: 'Debian' | 'Ubuntu' | 'Alpine' | undefined;
|
let osType: 'Debian' | 'Ubuntu' | 'Alpine' | undefined;
|
||||||
|
|
||||||
async function getOSReleaseInfo(): Promise<string> {
|
async function getOSReleaseInfo(): Promise<string> {
|
||||||
const osRelease = await fs.readFile('/etc/os-release', 'utf8');
|
try {
|
||||||
return osRelease;
|
const osRelease = await fs.readFile('/etc/os-release', 'utf8');
|
||||||
|
return osRelease;
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(`Failed to read /etc/os-release: ${error}`);
|
||||||
|
return '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDebian(osReleaseInfo: string): boolean {
|
function isDebian(osReleaseInfo: string): boolean {
|
||||||
|
|
@ -620,10 +625,11 @@ export async function detectOS(): Promise<
|
||||||
|
|
||||||
if (platform === 'linux') {
|
if (platform === 'linux') {
|
||||||
const osReleaseInfo = await getOSReleaseInfo();
|
const osReleaseInfo = await getOSReleaseInfo();
|
||||||
if (isDebian(osReleaseInfo)) {
|
// Check Ubuntu before Debian since Ubuntu is based on Debian
|
||||||
osType = 'Debian';
|
if (isUbuntu(osReleaseInfo)) {
|
||||||
} else if (isUbuntu(osReleaseInfo)) {
|
|
||||||
osType = 'Ubuntu';
|
osType = 'Ubuntu';
|
||||||
|
} else if (isDebian(osReleaseInfo)) {
|
||||||
|
osType = 'Debian';
|
||||||
} else if (isAlpine(osReleaseInfo)) {
|
} else if (isAlpine(osReleaseInfo)) {
|
||||||
osType = 'Alpine';
|
osType = 'Alpine';
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -643,18 +649,27 @@ export async function detectOS(): Promise<
|
||||||
async function getCurrentMirrorDomain(
|
async function getCurrentMirrorDomain(
|
||||||
filePath: string,
|
filePath: string,
|
||||||
): Promise<string | null> {
|
): Promise<string | null> {
|
||||||
const fileContent = await fs.readFile(filePath, 'utf8');
|
try {
|
||||||
const lines = fileContent.split('\n');
|
const fileContent = await fs.readFile(filePath, 'utf8');
|
||||||
for (const line of lines) {
|
const lines = fileContent.split('\n');
|
||||||
if (line.trim().startsWith('#')) {
|
for (const line of lines) {
|
||||||
continue;
|
if (line.trim().startsWith('#')) {
|
||||||
}
|
continue;
|
||||||
const match = line.match(/https?:\/\/[^\/]+/);
|
}
|
||||||
if (match) {
|
const match = line.match(/https?:\/\/[^\/]+/);
|
||||||
return match[0];
|
if (match) {
|
||||||
|
return match[0];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(`Failed to read mirror configuration file ${filePath}: ${error}`);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return null;
|
}
|
||||||
|
|
||||||
|
function escapeRegExp(string: string): string {
|
||||||
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function replaceDomainInFile(
|
async function replaceDomainInFile(
|
||||||
|
|
@ -662,16 +677,19 @@ async function replaceDomainInFile(
|
||||||
oldDomainWithScheme: string,
|
oldDomainWithScheme: string,
|
||||||
newDomainWithScheme: string,
|
newDomainWithScheme: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
let fileContent = await fs.readFile(filePath, 'utf8');
|
// Ensure the new domain has a trailing slash before replacement
|
||||||
let updatedContent = fileContent.replace(
|
|
||||||
new RegExp(oldDomainWithScheme, 'g'),
|
|
||||||
newDomainWithScheme,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!newDomainWithScheme.endsWith('/')) {
|
if (!newDomainWithScheme.endsWith('/')) {
|
||||||
newDomainWithScheme += '/';
|
newDomainWithScheme += '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let fileContent = await fs.readFile(filePath, 'utf8');
|
||||||
|
// Escape special regex characters in the old domain
|
||||||
|
const escapedOldDomain = escapeRegExp(oldDomainWithScheme);
|
||||||
|
let updatedContent = fileContent.replace(
|
||||||
|
new RegExp(escapedOldDomain, 'g'),
|
||||||
|
newDomainWithScheme,
|
||||||
|
);
|
||||||
|
|
||||||
await writeFileWithLock(filePath, updatedContent);
|
await writeFileWithLock(filePath, updatedContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user