Giter VIP home page Giter VIP logo

nproxy's Introduction

NProxy

A cli proxy tool specialized in file replacing

Build Status

Why NProxy

Maybe you have such question as why NProxy when we have Fiddler, Charles, Rythem and Tinyproxy. Yes, there is no doubt that they are all great tools, however they don't meet my requirements:

  • Support Mac, Linux and Windows(especially Mac and Linux)
  • Support replacing combo files with separated source files
  • Support directory mapping

This is the main reason why NProxy is here. Besides, NProxy can improve the efficiency of my daily development for enterprise-level product with a bunch of complex building processes, which cost me lots of time.

I've written a post named NProxy: The Mjolnir for UI Developers and a keynote NProxy: A Sharp Weapon for UI Developers to explain my reason for developing NProxy in detail.

Features

  • Support Mac, Linux and Windows
  • Support both single file and combo file replacing
  • Support directory mapping with any files
  • Support both HTTP and HTTPS

Installation

npm install -g nproxy (node >= v0.8.x is required)

If you are not familiar with Node.js and NPM, you can visit the How to install NProxy wiki page to get detail information about installation of NProxy

Usage

nproxy -l replace_rule.js 

Setting your browser's proxy to 127.0.0.1:port(8989 by default)

If you don't know how to set proxy for browser, please read this wiki: How to set brower's proxy

More Options:

Usage: nproxy [options]

Options:

  -h, --help         output usage information
  -V, --version      output the version number
  -l, --list [list]  Specify the replace rule file
  -p, --port [port]  Specify the port nproxy will listen on(8989 by default)
  -t, --timeout [timeout] Specify the request timeout (5 seconds by default)

Template of Replace Rule file(should be a .js file)

module.exports = [

  // 1. replace single file with local one
  {
    pattern: 'homepage.js',      // Match url you wanna replace
    responder:  "/home/goddyzhao/workspace/homepage.js"
  },

  // 2. replace single file with web file
  {
    pattern: 'homepage.js',      // Match url you wanna replace
    responder:  "http://www.anotherwebsite.com/assets/js/homepage2.js"
  },

  // 3. replace combo file with src with absolute file path
  {
    pattern: 'group/homepageTileFramework.*.js', 
    responder: [
      '/home/goddyzhao/workspace/webapp/ui/homepage/js/a.js',
      '/home/goddyzhao/workspace/webapp/ui/homepage/js/b.js',
      '/home/goddyzhao/workspace/webapp/ui/homepage/js/c.js'
    ] 
  },

  // 4. replace combo file with src with relative file path and specified dir
  {
    pattern: 'group/homepageTileFramework.*.js',
    responder: {
      dir: '/home/goddyzhao/workspace/webapp/ui/homepage/js',
      src: [
        'a.js',
        'b.js',
        'c.js'
      ]
    }
  },

  // 5. Map server image directory to local image directory
  {
    pattern: 'ui/homepage/img',  // must be a string
    responder: '/home/goddyzhao/image/' //must be a absolute directory path
  },

  // 6. Write responder with regular expression variables like $1, $2
  {
    pattern: /https?:\/\/[\w\.]*(?::\d+)?\/ui\/(.*)_dev\.(\w+)/,
    reponder: 'http://localhost/proxy/$1.$2'
  },

  // 7. Map server image directory to local image directory with regular expression
  // This simple rule can replace multiple directories to corresponding locale ones
  // For Example, 
  //   http://host:port/ui/a/img/... => /home/a/image/...
  //   http://host:port/ui/b/img/... => /home/b/image/...
  //   http://host:port/ui/c/img/... => /home/c/image/...
  //   ...
  {
    pattern: /ui\/(.*)\/img\//,
    responder: '/home/$1/image/'
  }
];

You can use the template file and replace it with your own configurations.

Quickly setup rule files for SF project

For UI Developers from SuccessFactors, here is a bonus for you guys. You can use the sf-transfer tool to transfer the combo xml file to NProxy rule file automatically!

License

NProxy is available under the terms of the MIT License

nproxy's People

Contributors

goddyzhao avatar jacksontian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nproxy's Issues

nproxy只支持静态url的映射吗

项目中前台所有的调用接口都是相同的URL,是post的,只是在post中有一个标识参数,根据这个不同的参数来返回不同的东西,我如果在前台用URL映射的话就有问题了,nproxy能不能根据post的参数来映射到不同的文件去

Support replacing img

For some projects, static files url will be appended a version control flag like dev-SNAPSHOT or production number. Actually, in source css file, it just use the original image, the url of which doesn't contain flags. Like following:

src: image/a.png

production:  image/a-version.png

So, proxy should support such case

Upstream proxy

Hello,
I'm trying to use nproxy behind a corporate firewall with a proxy of it own. Does nproxy support upstream proxies? Will it apply env.http_proxy to the request agent like the request module?

mime.lookup is not a function

getting an error on the console output from nproxy after a matched url: [ERROR] uncaughtException: mime.lookup is not a function

Mac os 10.12.4
chrome 67
node v8.10.0

通过 nproxy 中转访问本地的 Node HTTP 服务时,刷新后返回空响应

这个标题看起来有点拗口。我说一下前因后果。

第一步,我在本地起了一个基于 Node 的 HTTP 服务,主要目的是实现本地的 Stylus 编译,通过中间件实现透明自动编译(其实这里的 Stylus 不重要,因为我去掉 Stylus 的部分并不影响最终结果)。主要示意代码如下:

var dir = '/my/local/path'
var app = connect()
app
//  .use(
//      stylus.middleware({
//          src: dir,
//          dest: dir,
//          compile: compile
//      })
//  )
    .use(
        connect.static(dir)
    )
    .listen(3006)

第二步,我为 nproxy 设定中转规则:

{
    pattern: /http:\/\/www\.sample\.com\/(.+)/,
    responder: 'http://127.0.0.1:3006/$1'
}

第三步,在浏览器中通过 nproxy 去访问 http://www.sample.com/path/to/test.js 这个资源(我们期望的结果是浏览器得到 http://127.0.0.1:3006/path/to/test.js 内容)。

在首次访问时可以得到正确的响应,但刷新浏览器(Cmd + R)再次请求时,只会得到空白的结果。(请原谅我暂时还没有分析出确切的状况是否响应体丢失、响应体为空、响应头有问题等。)

如果强制刷新(Cmd + Shift + R)则可以得到正确的响应。如果直接访问 http://127.0.0.1:3006/path/to/test.js 则总是可以得到正确的响应。

在我测到的所有浏览器(Firefox、Chrome、iOS Safari、Android Chrome)上均存在此现象。


系统环境:

  • Mac OS X v10.8.5
  • Node v0.10.26
  • nproxy v1.4.3
  • connect v2.25.7
  • Firefox v31
  • Chrome v36

在Mac下好使吗?

我配置了http代理,其他网站就访问不了了,所以我想这个代理应该生效了;

我安装了nproxy,运行nproxy -V可以拿到版本号1.4.5,所以我猜nproxy的安装应该没有问题;

我运行 nproxy -l code/cellar/web/articles/nproxy/config.js,nproxy也启动了,我的config文件内容是这样:

module.exports = [
    {
        pattern: 'a.js',
        responder:  "/Users/zhaoxiaoqiang/code/cellar/web/b.js"
    }
];

然后我再本地启动了一个网站,访问a.js,发现无法访问,关闭代理可以访问,谁能告诉我这是怎么回事?哪里错了吗?(代理的被动模式开和不开一个样)

代理https时提示证书过期

该证书因为其自签名而不被信任。
该证书仅对 goddy 有效。
证书过期日期为 2012/9/17 12:15。
而当前日期为 2015/8/19 14:15。

(错误码: sec_error_unknown_issuer)

TypeError: first argument must be a string or Buffer

TypeError: first argument must be a string or Buffer
    at ClientRequest.OutgoingMessage.write (http.js:768:11)
    at Object.utils.request (/opt/node-v0.10.4/lib/node_modules/nproxy/lib/utils.js:142:15)
    at Object.respondFromWebFile (/opt/node-v0.10.4/lib/node_modules/nproxy/lib/middlewares/responders/web-file.js:4:10)
    at Object.respond [as handle] (/opt/node-v0.10.4/lib/node_modules/nproxy/lib/middlewares/respond.js:72:24)
    at next (/opt/node-v0.10.4/lib/node_modules/nproxy/node_modules/connect/lib/proto.js:190:15)
    at Function.app.handle (/opt/node-v0.10.4/lib/node_modules/nproxy/node_modules/connect/lib/proto.js:198:3)
    at app (/opt/node-v0.10.4/lib/node_modules/nproxy/node_modules/connect/lib/connect.js:65:37)
    at Server.<anonymous> (/opt/node-v0.10.4/lib/node_modules/nproxy/lib/nproxy.js:49:5)
    at Server.EventEmitter.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2015:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:119:23)

错误发生在使用POST提交表单时,我看了下代码

/opt/node-v0.10.4/lib/node_modules/nproxy/lib/utils.js:47

  /**
   * Simple wrapper for the default http.request
   *
   * @param {Object} options options about url, method and headers
   * @param {Function} callback callback to handle the response object
   */
  request: function(options, callback){
    //.......
    if(requestMethod === 'POST'){
      request.write(options.data);
    }
    //.......
  }

调用来源于/opt/node-v0.10.4/lib/node_modules/nproxy/lib/middlewares/responders/web-file.js:4

var utils = require('../../utils');

function respondFromWebFile(filePath, req, res, next){
   utils.request({
      url: filePath,
      method: req.method,
      headers: req.headers
    }, function(err, data, proxyRes){
      if(err){ throw err; }
      res.writeHead(200, proxyRes.headers);
      res.write(data);
      res.end();

    });
};

module.exports = respondFromWebFile;

对于POST请求没有传data

多条匹配规则

哈哈,希望这个可以增强下。
当多条规则匹配时,如果实际想匹配的是第二条,但是第一条却匹配了(发现匹配即中止查找),响应替换表达式却找不到该路径(实际上的期望值该是第二条替换),在控制台的打印信息中实际上是第一条出错?(用户的潜意识是第二条配置出错了!)
建议:
一:报错情况下 控制台输出pattern或输出实际的responder路径(比如期望值是patternB: X:/file/b.js,但实际上因为规则写的有问题,替换的patternA: X:/file/a.js)

二:嘿嘿。这种需求不知是否广泛(combo的某种形式)。
http://a.b.c/??后跟的文件动态的,或者可能有多重组合,如果为每条匹配情况写一条规则稍显蛋疼.可以这样的话就好了,类似:
{
pattern: /(?=-min)/g,
responder:{
dir: 'x:\aaa\bbb',
src: '$1' //某种形式的分组全部匹配,自动转化成数组
}
}

8080端口映射到9000端口

module.exports = [
    {
        parttern: \http\:\/\/test\.com\/hxm\,
        responder: "http://test.com:9000/hxm"
    }
];

我想通过这个对某个域做端口映射 但是没成功

如何处理websocket的请求

我在使用的时候发现,websocket的请求都是timeout,然后提示msg: socket hang up,如何来设置相关配置,让socket能正常使用呢?

Join forces with magicProxy?

Hello!

I have been working on my own development proxy (https://github.com/fabiosantoscode/magicProxy) for a while now. I did not know of your proxy when I started. I just created it because I had to work on someone else's site and didn't want to have to install the whole thing locally.

My own project uses node-http-proxy, an existing proxy project, as its backend, has a flaky plugin system and includes some plugins which do file replacement like nproxy does, and some which do HTML transforms. One of them uses node-trumpet to change anything with [src] and [href] attributes, and the other uses cheerio to manipulate the DOM before it gets to the browser.

The roadmap of the project includes https://github.com/fabiosantoscode/letmeshowyoutheworldthroughmyeyes (I've opened this repo today), which should allow a developer to share his proxied view with a client or co-worker.

Seeing that we have interesting stuff cooking in different fronts, and have projects similar in a lot of things, why not join forces?

Join the two projects into one, work together on that, and get the best of both worlds. The cool configurability of this project, plus DOM manipulation plugins and shareability through letmeshowyoutheworldthroughmyeyes (although that project should be pretty much proxy agnostic).

To be clear, I intend to shutdown magicProxy and start porting its stuff over to this one.

What do you say?

访问https://www.alipay.com/出现404

npm install nproxy -g
nproxy
[INFO] NProxy started on 8989!
[INFO] connect to https server successfully!
[ERROR] url: /
[ERROR] msg: connect ECONNREFUSED
[INFO] connect to https server successfully!
[ERROR] socket error read ECONNRESET

Windows 7

所有浏览器均打不开HTTPS网址,显示404

似乎是重定向到127.0.0.1

如果我本地127.0.0.1开了80端口,就会从80端口上获取结果,关了80则显示404

可以拿支付宝网址测试:https://www.alipay.com/

Support regular express variable

Support such replacement:

pattern: '?://[\w.](?::\d+)?/ui/(.)_(?:[0-9a-f]{32}|dev-snapshot).(\w+),
responder: 'D:\jboss-eap-4.3\server\main\deploy\main-sfv4.ear\sfv4.war\ui$1_dev-snapshot.$2'

Good job!

It is not an issue. Good job! 👍

method提供DELETE

看到之前有修复过PUT的bug,今天在项目中使用DELETE时,发现不能正常工作,代码修改如下后可以工作:

isContainBodyData: function(method){
    if (!method){
      return false;
    }

    var white_list = ['POST', 'PUT','DELETE'];
    return white_list.some(function(i){
      return i === method;
    });
  }

试着增加了DELETE,请作者看下此处是否应增加更多的method支持?

请问代理时,不能用本地文件代理吗?

代理时,我是配置的本地相对路径的文件。
代理配置文件和要代理的文件放到同一目录下。配置文件中就直接配置文件名了,但代理时报错说不是一个可用的路径

Mobile Device Proxy Support

Goddy, Does it support the Mobile device Proxy?
it is necessary for mobile development

and do you know who I am? :)

pattern在正则中需要转义

pattern = typeof originalPattern === 'string' ? new RegExp(originalPattern) : originalPattern;

originalPattern做为正则的参数,需要把字符串中如 ./这些字符转义处理

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.