summaryrefslogtreecommitdiff
path: root/snappy.js
blob: 59ced5158cb4bed59a4d48cecf144c2b26f2fac8 (plain)
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
#!/usr/bin/env node

var argv = require('minimist')(process.argv.slice(2));
const puppeteer = require('puppeteer');
const colors = require('colors');
var PNG = require('png-js');
var fs = require('fs');

// Pretty Setup
const emotes = {'ck':colors.green('[✔ ]'),'x':colors.red('[x]')}

var logo = [
  "        ___            ",
  "       (___)_  Snappy  ",
  "       (_____)_        ",
  "       (_______)       ",
  " .......//(00)\\....... "
]
console.log(colors.cyan(logo.join('\n')));

// Argument Parsing
// Help menu
if(argv.h){
  let arguments = [
    ["-i", "<File with a list of websites>"],
    ["-w", "<Windows Size, Default:1280x720>"],
    ["-v", "<Check if screenshots are blank and output to...>"],
    ["-t", "Add additional delay to screenshots (Seconds)"],
    ["-p", "<SOCKS5 Proxy Port>"]
  ];

  process.stdout.write(colors.green(`Possible Arguments:\n`));
  for(var i = 0;i<arguments.length;i++){
    process.stdout.write(colors.yellow(` ${arguments[i][0]}: ${arguments[i][1]}\n`));
  }
  process.exit()
}

// Get list of ips to scan
if(!argv.i){
  process.stdout.write(colors.red(`\r${emotes['x']} Failed to load sites, you must run with the -i option\n`));
  process.exit(1)
}
var sites = fs.readFileSync(argv.i).toString().split("\n").filter(n => n);

process.stdout.write(colors.yellow(`\r${emotes['ck']} Loaded `) +
  colors.green(`${sites.length}`) +
  colors.yellow(` sites from `) +
  colors.green(`${argv.i}\n`));

// Set browser window size
if(argv.w){
  process.stdout.write(colors.yellow(`\r${emotes['ck']} Loading sites from `) + colors.green(`${argv.i}\n`));
  let windowSize = coolVar.split('x');
  if(windowSize.length != 2){
    process.stdout.write(colors.red(`\r${emotes['x']} Failed to use user defined window size defaulting to -w 1280x720\n`));
    var width = 1280;
    var height = 720;
  } else {
    width = parseInt(windowSize[0]);
    height = parseInt(windowSize[1]);
  }
} else {
  process.stdout.write(colors.yellow(`\r${emotes['ck']} Using default window size -w 1280x720\n`));
  var width = 1280;
  var height = 720;
}

// Check if we are going to use a proxy
if(argv.p){
  process.stdout.write(colors.yellow(`\r${emotes['ck']} Traffic will use SOCKS5 proxy on port `) + colors.green(`${argv.p}\n`));
}

// MODULE LOADING SECTION DONE
console.log(colors.cyan(" ..................... "));

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
};

function validateUrl(url) {
  if (!url.startsWith('http://') && !url.startsWith('https://')) {
    url = 'http://' + url;
  }
  return url;
}

function urlToFilename(url) {
  return url.replace("://","-").replace("/","_") + ".png";
}

function verifyImage(imageName) {
  function isEqual(a, b) {
    return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
  }

  return new Promise((resolve, reject) => {
    PNG.decode(imageName, function(data) {
      const firstPixel = [data[0], data[1], data[2], data[3]];
      var isSameColor = true;

      for (let i = 0; i < data.length; i += 4) {
        const pixel = [data[i], data[i+1], data[i+2], data[i+3]];

        if (!isEqual(firstPixel, pixel)) {
          isSameColor = false;
          break;
        }
      }
      resolve(isSameColor);
    });
  });
}

// Do the actual work now
async function run() {

  var options = {
    headless: 'new'
  }
  if(argv.p){
    options.args = [`--proxy-server=socks5://127.0.0.1:${argv.p}`];
  }

  const browser = await puppeteer.launch(options);
  //const browser = await puppeteer.launch({headless: 'new'});
  const [page] = await browser.pages();

  await page.setViewport({width: width, height: height});

  for(var i = 0;i<sites.length;i++){
    let site = validateUrl(sites[i]);
    let imageName = urlToFilename(site);

    //await page.goto(site, { waitUntil: 'domcontentloaded' });
    //await timeout(5000); //This can be used to REALLY slow down and wait for pages to load
    await page.goto(site, { waitUntil: 'networkidle2' }).then(async () => {
      let userTimeout = parseInt(argv.t);
      await timeout(isNaN(userTimeout)?0:userTimeout*1000);
      await page.screenshot({path: imageName}).then(async () => {
        if(!argv.v)
          return;
        const res = await verifyImage(imageName);
        if(res)
          fs.appendFileSync(argv.v, `${site}\n`);
      });
      console.log(colors.yellow(`${emotes['ck']} Snapped ${site}`));
    }).catch(err => {
      console.log(colors.red(`${emotes['x']} Had an issue with ${site}`));
    });
  }
  await browser.close();
};
run();