index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env node
  2. const program = require('commander')
  3. const path = require('path')
  4. const chalk = require('chalk')
  5. const version = require('./util/version_compare')
  6. const iPhoneBackup = require('./util/iphone_backup.js').iPhoneBackup
  7. var base = path.join(process.env.HOME, '/Library/Application Support/MobileSync/Backup/')
  8. var reportTypes = {
  9. 'apps': require('./reports/apps'),
  10. 'calls': require('./reports/calls'),
  11. 'conversations': require('./reports/conversations'),
  12. 'list': require('./reports/list'),
  13. 'manifest': require('./reports/manifest'),
  14. 'messages': require('./reports/messages'),
  15. 'notes': require('./reports/notes'),
  16. 'oldnotes': require('./reports/oldnotes'),
  17. 'photolocations': require('./reports/photolocations'),
  18. 'voicemail-files': require('./reports/voicemail-files'),
  19. 'voicemail': require('./reports/voicemail'),
  20. 'webhistory': require('./reports/webhistory'),
  21. 'wifi': require('./reports/wifi')
  22. }
  23. var formatters = {
  24. 'json': require('./formatters/json'),
  25. 'table': require('./formatters/table'),
  26. 'raw': require('./formatters/raw'),
  27. 'csv': require('./formatters/csv')
  28. }
  29. program
  30. .version('3.0.0')
  31. .option('-l, --list', 'List Backups')
  32. .option(`-b, --backup <backup>`, 'Backup ID')
  33. .option(`-d, --dir <directory>`, `Backup Directory (default: ${base})`)
  34. .option('-r, --report <report_type>', 'Select a report type. see below for a full list.')
  35. .option('-i, --id <id>', 'Specify an ID for filtering certain reports')
  36. .option('-f, --formatter <type>', 'Specify output format. default: table')
  37. .option(`-e, --extract <dir>`, 'Extract data for commands. supported by: voicemail-files')
  38. .option(`-f, --filter <filter>`, 'Filter output for individual reports. See the README for usage.')
  39. .option(`-v, --verbose`, 'Verbose debugging output')
  40. .option(`-x, --no-color`, 'Disable colorized output')
  41. program.on('--help', function () {
  42. console.log('')
  43. console.log('Supported Report Types:')
  44. for (var i in reportTypes) {
  45. if (program.isTTY) {
  46. console.log(' ', reportTypes[i].name, '-', reportTypes[i].description)
  47. } else {
  48. console.log(' ', chalk.green(reportTypes[i].name), '-', reportTypes[i].description)
  49. }
  50. }
  51. console.log('')
  52. console.log("If you're interested to know how this works, check out my post:")
  53. console.log('https://www.richinfante.com/2017/3/16/reverse-engineering-the-ios-backup')
  54. console.log('')
  55. console.log('Issue tracker:')
  56. console.log('https://github.com/richinfante/iphonebackuptools/issues')
  57. console.log('')
  58. })
  59. program.parse(process.argv)
  60. // Global verbose output flag.
  61. // This is bad
  62. global.verbose = program.verbose
  63. // Save the formatter
  64. program.formatter = formatters[program.formatter] || formatters.table
  65. if (!process.stdout.isTTY) { program.color = false }
  66. base = program.dir || base
  67. if (program.verbose) console.log('Using source:', base)
  68. if (program.list) {
  69. // Shortcut for list report
  70. reportTypes.list.func(program, base)
  71. } else if (program.report) {
  72. // If the report is valid
  73. if (reportTypes[program.report]) {
  74. var report = reportTypes[program.report]
  75. // Check if there's a backup specified and one is required.
  76. if (report.requiresBackup) {
  77. if (!program.backup) {
  78. console.log('use -b or --backup <id> to specify backup.')
  79. process.exit(1)
  80. }
  81. }
  82. // Try to use it
  83. if (report.func) {
  84. try {
  85. // New type of reports
  86. var backup = iPhoneBackup.fromID(program.backup, base)
  87. if (report.supportedVersions !== undefined) {
  88. if (version.versionCheck(backup.iOSVersion, report.supportedVersions)) {
  89. if (report.requiresBackup) {
  90. report.func(program, backup)
  91. } else {
  92. report.func(program, base)
  93. }
  94. } else {
  95. console.log('[!] The report generator "' + program.report + '" does not support iOS', backup.iOSVersion)
  96. console.log('')
  97. console.log(' If you think it should, file an issue here:')
  98. console.log(' https://github.com/richinfante/iphonebackuptools/issues')
  99. console.log('')
  100. process.exit(1)
  101. }
  102. } else {
  103. if (report.requiresBackup) {
  104. report.func(program, backup)
  105. } else {
  106. report.func(program, base)
  107. }
  108. }
  109. } catch (e) {
  110. console.log('[!] Encountered an error', e)
  111. }
  112. } else if (report.functions) {
  113. // New type of reports
  114. var backup = iPhoneBackup.fromID(program.backup, base)
  115. var flag = false
  116. // Check for a compatible reporting tool.
  117. for (var key in report.functions) {
  118. if (version.versionCheck(backup.iOSVersion, key)) {
  119. report.functions[key](program, backup)
  120. flag = true
  121. break
  122. }
  123. }
  124. if (!flag) {
  125. console.log('[!] The report generator "', program.report,'" does not support iOS', backup.iOSVersion)
  126. console.log('')
  127. console.log(' If you think it should, file an issue here:')
  128. console.log(' https://github.com/richinfante/iphonebackuptools/issues')
  129. console.log('')
  130. process.exit(1)
  131. }
  132. }
  133. } else {
  134. console.log('')
  135. console.log(' [!] Unknown Option type:', program.report)
  136. console.log(' [!] It\'s possible this tool is out-of date.')
  137. console.log(' https://github.com/richinfante/iphonebackuptools/issues')
  138. console.log('')
  139. program.outputHelp()
  140. }
  141. } else {
  142. program.outputHelp()
  143. }