Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support to use custom rules defined on external node packages #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wandenberg
Copy link

Add similar support to custom rules using an interface close to the one defined by anyproxy rules.

The module needs to export a class to be initialized and respond to some methods depending on if it is to be used as a before_send or an after_body rule (raw specification on the example code bellow).

const fs = require('fs');

module.exports = class RuleModule1 {
  constructor(type, config) {
    this.type = type;
    this.action = {};
    this.trigger = { test: this.test.bind(this) };
    this.config = JSON.parse(config);
  }

  // Define if the rule should be applied or not based on the received parameters.
  // if it returns true means the rule will be applied, otherwise means the rule will be skipped
  test(opt, ctx) {
    opt = opt || {};
    return true;
  }

  // Execute the custom code on the before_send step
  // if it returns null the request will be sent as usual
  // if it returns an object with the format { response: { statusCode: CODE, statusMessage: 'OPTIONAL status Message', headers: {}, body: 'default body' } },
  // the request will not be sent and the custom response will be used to respond the client request
  beforeSendRequest(req, res) {
    const _url = req.ctx.url||req.url_full||req.url;
    if (!_url.match(/svg/)) {
      return null;
    }

    return {
      response: {
        statusCode: 200,
        // statusMessage: 'OK',
        headers: {
          'Cache-Control': 'max-age=691200',
          'Connection': 'Keep-Alive',
          'Date': 'Sun, 01 Sep 2019 08:17:18 GMT',
        },
        body: fs.readFileSync('/images/empty.svg')
      }
    };
  }

  // Execute the custom code on the after_body step
  // if it returns null the default proxy response body will be forwarded
  // if it returns an object with the format { response: { body: 'new body' } }, the new body will be used instead of the original response body
  beforeSendResponse(req, res, body) {
    const _url = req.ctx.url||req.url_full||req.url;
    if (!_url.match(/json/)) {
      return null;
    }

    return {
      response: {
        body: Date.now() + ' HI ' + body
      }
    };
  }
};

UI changes

Creating a custom rule on the UI:
Screenshot 2019-09-11 at 11 54 37

Screenshot 2019-09-11 at 11 54 50

Defining the module name:
Screenshot 2019-09-11 at 11 55 32

Setting custom configuration to the rule in json format:
Screenshot 2019-09-11 at 11 56 02

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant