<commit_before>/*

# Transmit a completed trace

Run trace, send metadata, and for traceroute type, send submissions

*/
/* jslint node: true, esnext: true */

'use strict';

var request = require('request'),
  net = require('net'),
  dns = require('dns'),
  _ = require('lodash'),
  async = require('async'),
  net = require('net'),
  dns = require('dns');

var socketTrace = require('./socket-trace.js'), trsets = require('./trset');

var platformTraceroute = require('../lib/platform-traceroute.js');
var SUBMIT_URI = 'http://www.ixmaps.ca/application/controller/gather_tr.php';
//var SUBMIT_URI = 'http://zooid.org/post.php';

// start the trace
exports.submitTraceOptions = function(allOptions, send) {
  // submitting hosts from a trset
  if (allOptions.trset) {
    trsets.getTrset(allOptions.trset, function(err, dests) {
    send('info', 'TRSET ' + dests);
      if (err) {
        send('error', 'cannot retrieve trset ' + allOptions.trset + ': ' + err);
      } else {
        var submitter = function(err, res) {
          var dest = dests.shift();
          if (dest) {
            var dest_options = _.assign({}, allOptions, {dest: dest});

            send('info',  'now running ' + dest + ' from ' + allOptions.trset);
            doTrace(dest_options, send, submitter);
          } else {
            send('info', 'finished ' + allOptions.trset);
          }
        };
        submitter();
      }
    });
  // submitting a single host
  } else {
    var dest = allOptions.dest;
    var dest_options = _.assign({}, allOptions, {dest: dest});
    doTrace(dest_options, send, function(err, res) {
      send('info', 'finished host ' + dest);
    });
  }
};

function doTrace(options, send, doneDestCallback) {
  send('info', 'options', JSON.stringify(options, null, 2));

  if (!net.isIP(options.dest)) {
    // resolve a symobolic address
    dns.resolve4(options.dest, function(err, addresses) {
      if (err) {
        send('error', 'cannot resolve host');
      } else {
        options.dest_ip = addresses[0];
        runTrace();
      }
    });
  } else {
    options.dest_ip = options.dest;
    runTrace();
  }

  function runTrace() {
    var traces = [_.partial(socketTrace, options, send)];
    if (options.include_platform_traceroute) {
      traces.push(_.partial(platformTraceroute, options, send));
    }
    async.series(traces, submitTrace);
  }

  // submit hops with options to the server
  function submitTrace(err, results) {
    console.log('RESULTS ' + JSON.stringify(results, null, 2));
    let submission = {
      timeout: options.timeout,
      queries: options.queries,
      dest: options.dest,
      dest_ip: options.dest_ip,
      submitter: options.submitter,
      postal_code: options.postal_code,
      maxhops: options.maxhops,
      os: require('os').type(),
      traceroute_submissions: results
    };
    if (err) {
      submission.error = JSON.stringify(err);
    }

    if (GLOBAL.debug) console.log('submitting', submission);
    if (options.nosubmit) {
      return;
    }
    console.log('SUBMITTRACE post', JSON.stringify(submission, null, 2));
    request.post(SUBMIT_URI, {
      form: submission
    },
      function(err, res, body) {
        if (!err && res.statusCode == 200) {
          send('submitted', JSON.stringify(res));
        } else {
          send('submitted-error', err, res, res);
        }
        doneDestCallback(err, res);
      });
  }
}
<commit_msg>change submit link fixes #15<commit_after>/*

# Transmit a completed trace

Run trace, send metadata, and for traceroute type, send submissions

*/
/* jslint node: true, esnext: true */

'use strict';

var request = require('request'),
  net = require('net'),
  dns = require('dns'),
  _ = require('lodash'),
  async = require('async'),
  net = require('net'),
  dns = require('dns');

var socketTrace = require('./socket-trace.js'), trsets = require('./trset');

var platformTraceroute = require('../lib/platform-traceroute.js');
var SUBMIT_URI = 'https://www.ixmaps.ca/application/controller/gather_tr.php';
//var SUBMIT_URI = 'http://zooid.org/post.php';

// start the trace
exports.submitTraceOptions = function(allOptions, send) {
  // submitting hosts from a trset
  if (allOptions.trset) {
    trsets.getTrset(allOptions.trset, function(err, dests) {
    send('info', 'TRSET ' + dests);
      if (err) {
        send('error', 'cannot retrieve trset ' + allOptions.trset + ': ' + err);
      } else {
        var submitter = function(err, res) {
          var dest = dests.shift();
          if (dest) {
            var dest_options = _.assign({}, allOptions, {dest: dest});

            send('info',  'now running ' + dest + ' from ' + allOptions.trset);
            doTrace(dest_options, send, submitter);
          } else {
            send('info', 'finished ' + allOptions.trset);
          }
        };
        submitter();
      }
    });
  // submitting a single host
  } else {
    var dest = allOptions.dest;
    var dest_options = _.assign({}, allOptions, {dest: dest});
    doTrace(dest_options, send, function(err, res) {
      send('info', 'finished host ' + dest);
    });
  }
};

function doTrace(options, send, doneDestCallback) {
  send('info', 'options', JSON.stringify(options, null, 2));

  if (!net.isIP(options.dest)) {
    // resolve a symobolic address
    dns.resolve4(options.dest, function(err, addresses) {
      if (err) {
        send('error', 'cannot resolve host');
      } else {
        options.dest_ip = addresses[0];
        runTrace();
      }
    });
  } else {
    options.dest_ip = options.dest;
    runTrace();
  }

  function runTrace() {
    var traces = [_.partial(socketTrace, options, send)];
    if (options.include_platform_traceroute) {
      traces.push(_.partial(platformTraceroute, options, send));
    }
    async.series(traces, submitTrace);
  }

  // submit hops with options to the server
  function submitTrace(err, results) {
    console.log('RESULTS ' + JSON.stringify(results, null, 2));
    let submission = {
      timeout: options.timeout,
      queries: options.queries,
      dest: options.dest,
      dest_ip: options.dest_ip,
      submitter: options.submitter,
      postal_code: options.postal_code,
      maxhops: options.maxhops,
      os: require('os').type(),
      traceroute_submissions: results
    };
    if (err) {
      submission.error = JSON.stringify(err);
    }

    if (GLOBAL.debug) console.log('submitting', submission);
    if (options.nosubmit) {
      return;
    }
    console.log('SUBMITTRACE post', JSON.stringify(submission, null, 2));
    request.post(SUBMIT_URI, {
      form: submission
    },
      function(err, res, body) {
        if (!err && res.statusCode == 200) {
          send('submitted', JSON.stringify(res));
        } else {
          send('submitted-error', err, res, res);
        }
        doneDestCallback(err, res);
      });
  }
}

{'fraction_non_alphanumeric': 0.1516690856313498, 'fraction_numerical': 0.0026124818577648768, 'mean_word_length': 2.7028479312197744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5127240', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2157, 'n_words': 496}