Skip to content

Commit ac5e65b

Browse files
committed
update map.sh patcher's detector
1 parent 9d8ae09 commit ac5e65b

File tree

2 files changed

+56
-84
lines changed

2 files changed

+56
-84
lines changed

htdocs/luci-static/resources/view/fleth.js

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ return view.extend({
1717
L.resolveDefault(fs.exec("/usr/sbin/fleth", ["get_area"]), { stdout: "" }),
1818
L.resolveDefault(fs.exec("/usr/sbin/fleth", ["mape_status"]), { stdout: "" }),
1919
L.resolveDefault(fs.exec("/usr/sbin/fleth", ["get_prefix_length"]), { stdout: "" }),
20-
L.resolveDefault(fs.stat("/lib/netifd/proto/map.sh.flethbak"), null),
21-
L.resolveDefault(fs.exec("sh", ["-c", "cmp -s /lib/netifd/proto/map.sh /lib/netifd/proto/map.sh.flethbak && echo 'same' || echo 'different'"]), { stdout: "" }),
20+
L.resolveDefault(fs.exec("/usr/sbin/fleth", ["mapsh_status"]), { stdout: "" }),
2221
]).then(function (results) {
2322
const area = (results[0].stdout || "").trim();
2423
const mape_status = (results[1].stdout || "").split("\n");
2524
const prefix_length = (results[2].stdout || "").trim();
26-
const mapBackupExists = results[3] !== null;
27-
const filesAreSame = (results[4].stdout || "").trim() === 'same';
28-
25+
const mapsh_status = (results[3].stdout || "").trim();
2926
let areaValue = area || "UNKNOWN";
3027
const mapeIsUnknown = mape_status.length <= 1 || mape_status[0] === "UNKNOWN";
3128

@@ -47,8 +44,7 @@ return view.extend({
4744
mape_status: mape_status,
4845
prefix_length: prefix_length || "UNKNOWN",
4946
isPending: true,
50-
mapBackupExists: mapBackupExists,
51-
mapIsPatched: mapBackupExists && !filesAreSame,
47+
mapIsPatched: mapsh_status === "patched",
5248
};
5349
}
5450
// No pending status, check DS-Lite provider
@@ -61,8 +57,7 @@ return view.extend({
6157
mape_status: mape_status,
6258
prefix_length: prefix_length || "UNKNOWN",
6359
isPending: false,
64-
mapBackupExists: mapBackupExists,
65-
mapIsPatched: mapBackupExists && !filesAreSame,
60+
mapIsPatched: mapsh_status === "patched",
6661
};
6762
});
6863
});
@@ -74,8 +69,7 @@ return view.extend({
7469
mape_status: mape_status,
7570
prefix_length: prefix_length || "UNKNOWN",
7671
isPending: false,
77-
mapBackupExists: mapBackupExists,
78-
mapIsPatched: mapBackupExists && !filesAreSame,
72+
mapIsPatched: mapsh_status === "patched",
7973
};
8074
}
8175
});
@@ -378,32 +372,36 @@ return view.extend({
378372
return this.setupIPv6Config(mapObj, 'pd');
379373
},
380374

381-
replaceMapSh: function (mapObj) {
375+
manageMapSh: function (mapObj, action) {
376+
const actionConfig = {
377+
replace: { verb: _('Replacing'), gerund: _('Downloading...') },
378+
restore: { verb: _('Restoring'), gerund: _('Restoring...') }
379+
};
380+
381+
const config = actionConfig[action];
382+
const actionLower = action.toLowerCase();
383+
382384
return new Promise(function (resolve, reject) {
383-
// First save current configuration
384385
mapObj.save()
385386
.then(function () {
386-
// Show loading message
387-
ui.showModal(_('Replacing'), [
388-
E('p', { 'class': 'spinning' }, _('Downloading...'))
387+
ui.showModal(config.verb, [
388+
E('p', { 'class': 'spinning' }, config.gerund)
389389
]);
390390

391-
// Execute the replace operation via fleth script
392-
return fs.exec('/usr/sbin/fleth', ['replace_mapsh']);
391+
return fs.exec('/usr/sbin/fleth', [action + '_mapsh']);
393392
})
394393
.then(function (result) {
395394
ui.hideModal();
396395

397396
if (result.code === 0 && result.stdout.trim() === 'SUCCESS') {
398-
ui.addNotification(null, E('p', _('Replaced successfully! Please restart the network interface manually.')), 'info');
397+
ui.addNotification(null, E('p', _('Operation completed successfully! Please restart the network interface manually.')), 'info');
399398

400-
// Reload page to update backup status
401399
setTimeout(function() {
402400
window.location.reload();
403-
}, 2000);
401+
}, 5000);
404402
} else {
405403
ui.addNotification(null, E('div', [
406-
E('p', _('Failed to replace:')),
404+
E('p', _('Failed to ' + actionLower + ':')),
407405
E('pre', result.stdout || result.stderr || 'Unknown error')
408406
]), 'error');
409407
}
@@ -413,54 +411,19 @@ return view.extend({
413411
.catch(function (error) {
414412
ui.hideModal();
415413
ui.addNotification(null, E('div', [
416-
E('p', _('Error replacing:')),
414+
E('p', _('Error ' + config.gerund.toLowerCase() + ':')),
417415
E('pre', error.message || error)
418416
]), 'error');
419417
reject(error);
420418
});
421419
});
422420
},
423421

424-
restoreMapSh: function (mapObj) {
425-
return new Promise(function (resolve, reject) {
426-
// First save current configuration
427-
mapObj.save()
428-
.then(function () {
429-
// Show loading message
430-
ui.showModal(_('Restoring'), [
431-
E('p', { 'class': 'spinning' }, _('Restoring...'))
432-
]);
433-
434-
// Execute the restore operation via fleth script
435-
return fs.exec('/usr/sbin/fleth', ['restore_mapsh']);
436-
})
437-
.then(function (result) {
438-
ui.hideModal();
439-
440-
if (result.code === 0 && result.stdout.trim() === 'SUCCESS') {
441-
ui.addNotification(null, E('p', _('Restored successfully! Please restart the network interface manually.')), 'info');
442-
443-
// Reload page to update backup status
444-
setTimeout(function() {
445-
window.location.reload();
446-
}, 2000);
447-
} else {
448-
ui.addNotification(null, E('div', [
449-
E('p', _('Failed to restore:')),
450-
E('pre', result.stdout || result.stderr || 'Unknown error')
451-
]), 'error');
452-
}
422+
replaceMapSh: function (mapObj) {
423+
return this.manageMapSh(mapObj, 'replace');
424+
},
453425

454-
resolve();
455-
})
456-
.catch(function (error) {
457-
ui.hideModal();
458-
ui.addNotification(null, E('div', [
459-
E('p', _('Error restoring:')),
460-
E('pre', error.message || error)
461-
]), 'error');
462-
reject(error);
463-
});
464-
});
426+
restoreMapSh: function (mapObj) {
427+
return this.manageMapSh(mapObj, 'restore');
465428
},
466429
});

root/usr/sbin/fleth

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
DNS_E=2404:1a8:7f01:a::3
55
DNS_W=2001:a7ff:5f01::a
66

7+
# MAP.sh paths
8+
MAPSH_PATH="/lib/netifd/proto/map.sh"
9+
MAPSH_BACKUP_PATH="/lib/netifd/proto/map.sh.flethbak"
10+
711
# Commands that don't require UCI configuration
812
if [ "$1" = "hook_none.js" ] || [ "$1" = "restore_none.js" ]; then
913
# Skip UCI configuration checks for these commands
@@ -402,6 +406,15 @@ elif [ "$1" = "get_prefix_length" ]; then
402406
fi
403407
elif [ "$1" = "auto_setup_ipv6" ]; then
404408
auto_setup_ipv6_mode
409+
elif [ "$1" = "mapsh_status" ]; then
410+
# Check if map.sh is patched or original
411+
if [ ! -f "$MAPSH_BACKUP_PATH" ]; then
412+
echo "original"
413+
elif cmp -s "$MAPSH_PATH" "$MAPSH_BACKUP_PATH"; then
414+
echo "original"
415+
else
416+
echo "patched"
417+
fi
405418
elif [ "$1" = "hook_none.js" ]; then
406419
if ! grep -q 'fleth-hook.js' /www/luci-static/resources/protocol/none.js; then
407420
$0 restore_none.js
@@ -500,56 +513,51 @@ EOF
500513
logger -t fleth "IPv6 PD configuration completed successfully"
501514
echo "SUCCESS"
502515
elif [ "$1" = "replace_mapsh" ]; then
503-
# Replace map.sh with patched version
504-
logger -t fleth "Starting map.sh replacement"
505-
506516
MAPSH_URL="https://raw.githubusercontent.com/fakemanhk/openwrt-jp-ipoe/345b1d9add58cf84c3eebe235f695607a78ebcd9/map.sh.new"
507-
MAPSH_PATH="/lib/netifd/proto/map.sh"
508-
BACKUP_PATH="/lib/netifd/proto/map.sh.flethbak"
509517

510-
# Check if backup already exists, if not create it
511-
if [ ! -f "$BACKUP_PATH" ]; then
512-
cp "$MAPSH_PATH" "$BACKUP_PATH"
518+
if [ ! -f "$MAPSH_BACKUP_PATH" ]; then
519+
cp "$MAPSH_PATH" "$MAPSH_BACKUP_PATH"
513520
if [ $? -ne 0 ]; then
514521
echo "ERROR: Failed to create backup"
515522
exit 1
516523
fi
517524
fi
518-
# --no-check-certificate
519-
wget -q -O /tmp/map.sh.new "$MAPSH_URL"
525+
TEMP_FILE=$(mktemp)
526+
if [ $? -ne 0 ]; then
527+
echo "ERROR: Failed to create temporary file"
528+
exit 1
529+
fi
530+
wget -q -O "$TEMP_FILE" "$MAPSH_URL"
520531
if [ $? -ne 0 ]; then
532+
rm -f "$TEMP_FILE"
521533
echo "ERROR: Failed to download"
522534
exit 1
523535
fi
524-
525-
# Replace the file
526-
mv /tmp/map.sh.new "$MAPSH_PATH"
527-
chmod +x "$MAPSH_PATH"
536+
mv "$TEMP_FILE" "$MAPSH_PATH"
528537
if [ $? -ne 0 ]; then
538+
rm -f "$TEMP_FILE"
529539
echo "ERROR: Failed to replace file"
530540
exit 1
531541
fi
532-
533-
logger -t fleth "map.sh replaced successfully"
542+
chmod +x "$MAPSH_PATH"
534543
echo "SUCCESS"
535544
elif [ "$1" = "restore_mapsh" ]; then
536545
# Restore original map.sh from backup
537-
538-
MAPSH_PATH="/lib/netifd/proto/map.sh"
539-
BACKUP_PATH="/lib/netifd/proto/map.sh.flethbak"
546+
logger -t fleth "Starting map.sh restoration"
540547

541548
# Check if backup exists
542-
if [ ! -f "$BACKUP_PATH" ]; then
549+
if [ ! -f "$MAPSH_BACKUP_PATH" ]; then
543550
echo "ERROR: Backup not found"
544551
exit 1
545552
fi
546553

547-
mv "$BACKUP_PATH" "$MAPSH_PATH"
554+
mv "$MAPSH_BACKUP_PATH" "$MAPSH_PATH"
548555
if [ $? -ne 0 ]; then
549556
echo "ERROR: Failed to restore"
550557
exit 1
551558
fi
552559

560+
logger -t fleth "map.sh restored successfully"
553561
echo "SUCCESS"
554562
else
555563
echo "Flet'h by huggy"
@@ -566,6 +574,7 @@ else
566574
echo " get_dslite_provider Show the current DS-Lite provider details."
567575
echo " mape_status Show the current MAP-E provider details."
568576
echo " get_prefix_length Show the detected IPv6 prefix length (/56 or /64)."
577+
echo " mapsh_status Show if map.sh is patched or original."
569578
echo ""
570579
echo "IPv6 Configuration Commands:"
571580
echo " auto_setup_ipv6 Auto-detect prefix and configure PD (/56) or SLAAC (/64) mode."

0 commit comments

Comments
 (0)