@oxiaom/adoremix 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cli.js +15 -0
- package/src/config/network.js +89 -1
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -132,6 +132,21 @@ function buildProgram() {
|
|
|
132
132
|
process.exitCode = code || 0;
|
|
133
133
|
});
|
|
134
134
|
|
|
135
|
+
config
|
|
136
|
+
.command('preurl')
|
|
137
|
+
.description('配置设备拉资源用的 URL(默认 http://IP:12080/,CDN 加速改成域名)')
|
|
138
|
+
.option('--workdir <path>')
|
|
139
|
+
.option('--url <url>', '直接指定 URL(如 http://cdn.your.com/)')
|
|
140
|
+
.option('--no-interactive', '用本机 IP 自动生成')
|
|
141
|
+
.action(async (opts) => {
|
|
142
|
+
const net = require('./config/network');
|
|
143
|
+
const code = await net.setupPreurl(resolveWorkdir(opts.workdir), {
|
|
144
|
+
url: opts.url,
|
|
145
|
+
interactive: opts.interactive !== false
|
|
146
|
+
});
|
|
147
|
+
process.exitCode = code || 0;
|
|
148
|
+
});
|
|
149
|
+
|
|
135
150
|
const runner = require('./runner');
|
|
136
151
|
const nativeRef = () => {
|
|
137
152
|
try { return paths.loadNative(); }
|
package/src/config/network.js
CHANGED
|
@@ -10,6 +10,23 @@ const NET_KEYS = [
|
|
|
10
10
|
'Settings.Fip'
|
|
11
11
|
];
|
|
12
12
|
|
|
13
|
+
const PREURL_KEY = 'Settings.preurl';
|
|
14
|
+
const HTTP_PORT_KEY = 'listener.port';
|
|
15
|
+
|
|
16
|
+
// 默认 preurl 格式:http://<IP>:<port>/
|
|
17
|
+
// 如果用户改成 CDN 域名(如 http://cdn.xxx.com/),改 IP 时不应该覆盖
|
|
18
|
+
function extractPreurlHost(preurl) {
|
|
19
|
+
if (!preurl) return null;
|
|
20
|
+
const m = String(preurl).match(/^https?:\/\/([^:/]+)/);
|
|
21
|
+
return m ? m[1] : null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isPreurlCdn(preurl) {
|
|
25
|
+
const host = extractPreurlHost(preurl);
|
|
26
|
+
if (!host) return false;
|
|
27
|
+
return !isValidIPv4(host); // 域名 = CDN,IP = 直连
|
|
28
|
+
}
|
|
29
|
+
|
|
13
30
|
function listInterfaces() {
|
|
14
31
|
const ifaces = os.networkInterfaces();
|
|
15
32
|
const list = [];
|
|
@@ -109,6 +126,19 @@ async function setupNetwork(workdir, opts) {
|
|
|
109
126
|
}
|
|
110
127
|
logger.ok(`已设置 ${NET_KEYS.map(k => k.split('.')[1]).join(' / ')} = ${ip}`);
|
|
111
128
|
|
|
129
|
+
// 智能更新 preurl:如果是 IP 格式(直连),同步改;CDN 域名不动
|
|
130
|
+
const currentPreurl = cfg.getConfigValue(workdir, PREURL_KEY);
|
|
131
|
+
const port = cfg.getConfigValue(workdir, HTTP_PORT_KEY) || 12080;
|
|
132
|
+
if (currentPreurl && isPreurlCdn(currentPreurl)) {
|
|
133
|
+
logger.log('');
|
|
134
|
+
logger.log(`preurl 当前是 CDN/域名:${currentPreurl}(保留不覆盖)`);
|
|
135
|
+
logger.log(' 改 CDN 用:adoremix config preurl http://cdn.your.com/');
|
|
136
|
+
} else {
|
|
137
|
+
const newPreurl = `http://${ip}:${port}/`;
|
|
138
|
+
cfg.setConfigValue(workdir, PREURL_KEY, newPreurl);
|
|
139
|
+
logger.ok(`已同步更新 preurl = ${newPreurl}`);
|
|
140
|
+
}
|
|
141
|
+
|
|
112
142
|
// 顺便显示当前端口配置,方便检查
|
|
113
143
|
const ports = [
|
|
114
144
|
['listener.port', 'HTTP 网页'],
|
|
@@ -128,4 +158,62 @@ async function setupNetwork(workdir, opts) {
|
|
|
128
158
|
return 0;
|
|
129
159
|
}
|
|
130
160
|
|
|
131
|
-
|
|
161
|
+
async function setupPreurl(workdir, opts) {
|
|
162
|
+
opts = opts || {};
|
|
163
|
+
const cfg = require('./index');
|
|
164
|
+
const current = cfg.getConfigValue(workdir, PREURL_KEY);
|
|
165
|
+
const port = cfg.getConfigValue(workdir, HTTP_PORT_KEY) || 12080;
|
|
166
|
+
const ip = cfg.getConfigValue(workdir, 'Settings.LocalIP') || '127.0.0.1';
|
|
167
|
+
|
|
168
|
+
let url;
|
|
169
|
+
if (opts.url) {
|
|
170
|
+
url = opts.url;
|
|
171
|
+
} else if (opts.interactive === false) {
|
|
172
|
+
url = `http://${ip}:${port}/`;
|
|
173
|
+
} else {
|
|
174
|
+
logger.log('');
|
|
175
|
+
logger.log('=== preurl 配置(设备拉资源用的 URL)===');
|
|
176
|
+
logger.log('说明:');
|
|
177
|
+
logger.log(' • 直连:http://<本机IP>:<端口>/(默认)');
|
|
178
|
+
logger.log(' • CDN 加速:http://cdn.your.com/(设备从 CDN 拉资源)');
|
|
179
|
+
logger.log(` • 当前值:${current}`);
|
|
180
|
+
logger.log('');
|
|
181
|
+
const resp = await prompts({
|
|
182
|
+
type: 'select',
|
|
183
|
+
name: 'choice',
|
|
184
|
+
message: '选择 preurl 来源',
|
|
185
|
+
choices: [
|
|
186
|
+
{ title: `用本机直连:http://${ip}:${port}/`, value: 'direct' },
|
|
187
|
+
{ title: '🌐 手动输入 CDN/外网地址', value: 'custom' }
|
|
188
|
+
]
|
|
189
|
+
});
|
|
190
|
+
if (resp.choice === 'direct') {
|
|
191
|
+
url = `http://${ip}:${port}/`;
|
|
192
|
+
} else if (resp.choice === 'custom') {
|
|
193
|
+
const r2 = await prompts({
|
|
194
|
+
type: 'text',
|
|
195
|
+
name: 'url',
|
|
196
|
+
message: '输入完整 URL(含 http(s):// 和末尾 /)',
|
|
197
|
+
initial: 'http://cdn.example.com/',
|
|
198
|
+
validate: v => /^https?:\/\/[^/]+\//.test(v) ? true : '格式:http(s)://域名或IP/'
|
|
199
|
+
});
|
|
200
|
+
url = r2.url;
|
|
201
|
+
} else {
|
|
202
|
+
return 1;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
cfg.setConfigValue(workdir, PREURL_KEY, url);
|
|
206
|
+
logger.ok(`preurl = ${url}`);
|
|
207
|
+
logger.log('改完后记得:adoremix restart');
|
|
208
|
+
return 0;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
module.exports = {
|
|
212
|
+
setupNetwork,
|
|
213
|
+
setupPreurl,
|
|
214
|
+
listInterfaces,
|
|
215
|
+
isValidIPv4,
|
|
216
|
+
isPreurlCdn,
|
|
217
|
+
NET_KEYS,
|
|
218
|
+
PREURL_KEY
|
|
219
|
+
};
|