-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.js
More file actions
194 lines (171 loc) · 10.3 KB
/
Copy pathconvert.js
File metadata and controls
194 lines (171 loc) · 10.3 KB
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
async function convertScripts() {
const fs = require('fs-extra');
const path = require('path');
const inputDir = 'QuantumultX';
const surgeDir = 'Surge';
const loonDir = 'Loon';
try {
// Ensure output directories exist
console.log(`Ensuring Surge directory exists: ${surgeDir}`);
await fs.ensureDir(surgeDir);
console.log(`Surge directory created/exists: ${await fs.pathExists(surgeDir)}`);
console.log(`Ensuring Loon directory exists: ${loonDir}`);
await fs.ensureDir(loonDir);
console.log(`Loon directory created/exists: ${await fs.pathExists(loonDir)}`);
// Read all files from QuantumultX directory
console.log(`Reading files from ${inputDir}`);
const files = await fs.readdir(inputDir);
if (files.length === 0) {
console.log('No .js files found in QuantumultX/ directory');
return;
}
for (const file of files) {
if (file.endsWith('.js')) {
const inputPath = path.join(inputDir, file);
const content = await fs.readFile(inputPath, 'utf-8');
console.log(`Processing ${file}:`);
console.log(`Original content (first 100 chars): ${content.substring(0, 100)}...`);
const baseName = path.basename(file, '.js');
// Parse QuantumultX content
const lines = content.split('\n').filter(line => line.trim());
let ruleSection = '';
let rewriteSection = '';
let scriptSection = '';
let mitmSection = '';
let metadata = {
name: baseName,
desc: '',
author: '',
icon: '',
category: ''
};
// Track if rewrite rules are added
let hasRewriteRules = false;
lines.forEach(line => {
// Parse metadata from comments
if (line.startsWith('#!name=')) metadata.name = line.replace('#!name=', '');
else if (line.startsWith('#!desc=')) metadata.desc = line.replace('#!desc=', '');
else if (line.startsWith('#!category=')) metadata.category = line.replace('#!category=', '');
else if (line.startsWith('#!author=')) metadata.author = line.replace('#!author=', '');
else if (line.startsWith('#!icon=')) metadata.icon = line.replace('#!icon=', '');
// Parse sections
else if (line.startsWith('[filter_local]')) ruleSection += '[Rule]\n';
else if (line.startsWith('[rewrite_local]')) {
rewriteSection += '[URL Rewrite]\n';
scriptSection += '[Script]\n';
}
else if (line.startsWith('[Script]')) {
// Reset script section to ensure we don't duplicate the header
scriptSection = '[Script]\n';
}
else if (line.startsWith('[mitm]')) mitmSection += '[MITM]\n';
else if (line.includes('url-regex') && line.includes('reject') && ruleSection) {
// Handle specific URL-REGEX patterns with '^' anchor
if (line.includes('https://m-station2.axs.com.sg/AXSMobile/WebView/MarketPlace')) {
ruleSection += 'URL-REGEX,"^https://m-station2.axs.com.sg/AXSMobile/WebView/MarketPlace",REJECT\n';
} else if (line.includes('https://m-station2.axs.com.sg/AXSMobile/highlight')) {
ruleSection += 'URL-REGEX,"^https://m-station2.axs.com.sg/AXSMobile/highlight",REJECT\n';
} else {
const [, pattern] = line.match(/url-regex,(.*?),reject/) || [];
if (pattern) ruleSection += `URL-REGEX,"${pattern}",REJECT\n`;
}
}
else if (line.includes(' - reject') && rewriteSection) {
const [, pattern] = line.match(/(^.*?)\s+-\s+reject/) || [];
if (pattern) {
rewriteSection += `${pattern} - reject\n`;
hasRewriteRules = true; // Mark that we have rewrite rules
}
}
else if (line.includes('url script-response-body')) {
const [, url, scriptUrl] = line.match(/(^.*?)\s+url\s+script-response-body\s+(.*)/) || [];
if (url && scriptUrl) {
scriptSection += `${baseName} = type=http-response, pattern=${url}, script-path=${scriptUrl}, requires-body=true, max-size=-1, timeout=60\n`;
}
}
else if (line.includes('type=http-response')) {
// Already in Surge format, just add it directly
scriptSection += `${line}\n`;
}
else if (line.startsWith('hostname =')) {
const [, hostname] = line.match(/hostname = (.*)/) || [];
// Check if we've already added [MITM] to the section
if (!mitmSection.includes('[MITM]')) {
mitmSection = '[MITM]\n';
}
if (hostname) mitmSection += `hostname = %APPEND% ${hostname}\n`;
}
});
// Surge .sgmodule format with #! metadata and spacing
const surgeHeader = `#!name=${metadata.name}\n` +
(metadata.desc ? `#!desc=${metadata.desc}\n` : '') +
(metadata.author ? `#!author=${metadata.author}\n` : '') +
(metadata.icon ? `#!icon=${metadata.icon}\n` : '') +
(metadata.category ? `#!category=${metadata.category}\n` : '');
// Only include [URL Rewrite] if there are actual rewrite rules
let surgeRewriteSection = hasRewriteRules ? rewriteSection : '';
// Ensure proper spacing and [MITM] section appears before hostname line
const surgeModule = `${surgeHeader}\n${ruleSection}${surgeRewriteSection}${scriptSection}\n${mitmSection}`.trim();
const surgeOutput = path.join(surgeDir, `${baseName}.sgmodule`);
console.log(`Writing Surge file: ${surgeOutput}`);
await fs.writeFile(surgeOutput, surgeModule, { flag: 'w', encoding: 'utf-8' });
console.log(`Generated ${baseName}.sgmodule for Surge`);
console.log(`Surge file exists: ${await fs.pathExists(surgeOutput)}`);
console.log(`Surge content (first 200 chars): ${surgeModule.substring(0, 200)}...`);
// Loon .plugin format with #! metadata and spacing
const loonHeader = `#!name=${metadata.name}\n` +
(metadata.desc ? `#!desc=${metadata.desc}\n` : '') +
(metadata.author ? `#!author=${metadata.author}\n` : '') +
(metadata.icon ? `#!icon=${metadata.icon}\n` : '') +
(metadata.category ? `#!category=${metadata.category}\n` : '');
// Only include [Rewrite] if there are actual rewrite rules
let loonRewriteSection = hasRewriteRules ? rewriteSection.replace('[URL Rewrite]', '[Rewrite]') : '';
// Rebuild script section for Loon
let loonScriptSection = '[Script]\n';
lines.forEach(line => {
if (line.includes('url script-response-body')) {
const [, url, scriptUrl] = line.match(/(^.*?)\s+url\s+script-response-body\s+(.*)/) || [];
if (url && scriptUrl) {
loonScriptSection += `http-response ${url} script-path=${scriptUrl}, requires-body=true, timeout=60, tag=${baseName}\n`;
}
}
else if (line.includes('type=http-response')) {
// Convert Surge format to Loon format
const pattern = line.match(/pattern=([^,]+)/)?.[1];
const scriptPath = line.match(/script-path=([^,]+)/)?.[1];
const name = line.match(/^([^=]+)=/)?.[1]?.trim();
if (pattern && scriptPath) {
loonScriptSection += `http-response ${pattern} script-path=${scriptPath}, requires-body=true, timeout=60, tag=${name || baseName}\n`;
}
}
});
// Build MITM section for Loon
let loonMitmSection = '[MITM]\n';
lines.forEach(line => {
if (line.startsWith('hostname =')) {
const [, hostname] = line.match(/hostname = (.*)/) || [];
if (hostname) {
// For Loon, we don't need %APPEND%
loonMitmSection += `hostname = ${hostname.replace('%APPEND% ', '')}\n`;
}
}
});
// For Loon format, we need proper spacing between sections
const loonPlugin = `${loonHeader}\n${ruleSection}${loonRewriteSection}${loonScriptSection}${loonMitmSection}`.trim();
const loonOutput = path.join(loonDir, `${baseName}.plugin`);
console.log(`Writing Loon file: ${loonOutput}`);
await fs.writeFile(loonOutput, loonPlugin, { flag: 'w', encoding: 'utf-8' });
console.log(`Generated ${baseName}.plugin for Loon`);
console.log(`Loon file exists: ${await fs.pathExists(loonOutput)}`);
}
}
console.log('All conversions complete!');
} catch (err) {
console.error('Error during conversion:', err);
process.exit(1);
}
}
convertScripts().catch(err => {
console.error('Error during script execution:', err);
process.exit(1);
});