<commit_before>
// TODO(bwiley): update uTransformers to be compatible with require
// TODO(ldixon): update to a require-style inclusion.
// e.g.
//  import Transformer = require('uproxy-obfuscators/transformer');
/// <reference path='../../../third_party/uTransformers/utransformers.d.ts' />

import logging = require('../logging/logging');
import PacketLengthShaper = require('./packetLengthShaper');

var log :logging.Log = new logging.Log('fancy-transformers');

// TODO(bwiley): Convert /* */ to // as specified in the style guide

/**
 * An obfuscator that only modifies packet length.
 * To start out, this is a very simple (and bad) packet length obfuscator.
 * It follows this logic:
 * Case 1 - buffer length + 2 == target: return length(buffer) + buffer
 * Case 2 - buffer length + 2 > target:  return length(target) + randomBytes(target)
 * Case 3 - buffer length + 2 < target:  return length(target) + buffer + randomBytes(target)
 */
class PacketLengthUniformRandomizer extends PacketLengthShaper implements Transformer {
  /** Length to which packets should be normalized. */
  private targetMinimum_ :number;
  private targetMaximum_ :number;

  public constructor() {
    super();
    log.info('Constructed packet length uniform randomizer');
  }

  /** Get the target minimum and maximum lengths. */
  public configure = (json:string) : void => {
    this.superConfigure(json);

    var dict = JSON.parse(json);
    this.setTargetMinimum_(dict['targetMinimum']);
    this.setTargetMaximum_(dict['targetMaximum']);
    log.info('Configured packet length normalizer %1 %2', this.targetMinimum_, this.targetMaximum_);
  }

  public transform = (buffer:ArrayBuffer) : ArrayBuffer[] => {
    //log.info('Transforming');
    return this.shapePacketLength(buffer, this.nextTargetLength());
  }

  private setTargetMinimum_ = (minimum:number) : void => {
    if(minimum>=1) {
      this.targetMinimum_=minimum;
    } else {
      // throw error
    }
  }

  private setTargetMaximum_ = (maximum:number) : void => {
    if(maximum<=1440) {
      this.targetMaximum_=maximum;
    } else {
      // throw error
    }
  }<commit_msg>Changed the minimum length and added errors for invalid lengths<commit_after>
// TODO(bwiley): update uTransformers to be compatible with require
// TODO(ldixon): update to a require-style inclusion.
// e.g.
//  import Transformer = require('uproxy-obfuscators/transformer');
/// <reference path='../../../third_party/uTransformers/utransformers.d.ts' />

import logging = require('../logging/logging');
import PacketLengthShaper = require('./packetLengthShaper');

var log :logging.Log = new logging.Log('fancy-transformers');

// TODO(bwiley): Convert /* */ to // as specified in the style guide

/**
 * An obfuscator that only modifies packet length.
 * To start out, this is a very simple (and bad) packet length obfuscator.
 * It follows this logic:
 * Case 1 - buffer length + 2 == target: return length(buffer) + buffer
 * Case 2 - buffer length + 2 > target:  return length(target) + randomBytes(target)
 * Case 3 - buffer length + 2 < target:  return length(target) + buffer + randomBytes(target)
 */
class PacketLengthUniformRandomizer extends PacketLengthShaper implements Transformer {
  /** Length to which packets should be normalized. */
  private targetMinimum_ :number;
  private targetMaximum_ :number;

  public constructor() {
    super();
    log.info('Constructed packet length uniform randomizer');
  }

  /** Get the target minimum and maximum lengths. */
  public configure = (json:string) : void => {
    this.superConfigure(json);

    var dict = JSON.parse(json);
    this.setTargetMinimum_(dict['targetMinimum']);
    this.setTargetMaximum_(dict['targetMaximum']);
    log.info('Configured packet length normalizer %1 %2', this.targetMinimum_, this.targetMaximum_);
  }

  public transform = (buffer:ArrayBuffer) : ArrayBuffer[] => {
//    log.info('Transforming');
    return this.shapePacketLength(buffer, this.nextTargetLength());
  }

  private setTargetMinimum_ = (minimum:number) : void => {
    if(minimum>=37) {
      this.targetMinimum_=minimum;
    } else {
      throw new Error('Target packet length is too small %1 / %2', minimum, 37);
    }
  }

  private setTargetMaximum_ = (maximum:number) : void => {
    if(maximum<=1440) {
      this.targetMaximum_=maximum;
    } else {
      throw new Error('Target packet length is too large %1 / %2', maximum, 1440);
    }
  }
{'fraction_non_alphanumeric': 0.12985845877330937, 'fraction_numerical': 0.008312738710402157, 'mean_word_length': 3.766595289079229, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1885753', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1247, 'n_words': 371}