- Notifications
You must be signed in to change notification settings - Fork6
http proxy with Elixir. wait request with multi port and forward to each URIs
License
KazuCocoa/http_proxy
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Simple multi HTTP Proxy using Plug. And support record/play requests.
- Record/Play proxied requests
- http_proxy support multi port and multi urls on one execution command
mix proxy.
- http_proxy support multi port and multi urls on one execution command
- Support VCR
http_proxyClient (server client) proxied_server | | | | 1.request | | | ------> | 2.request | | | ------> | | | | | | 3.response | | 4.response | <------ | | <------ | | | | |- The client sends a request to http_proxy, then the http_proxy works as a proxy server.
- When the http_proxy receives the request from the client, then the http_proxy sends the request to a proxied server, e.g.http://google.com, as a client.
- The http_proxy receives responses from the proxied_server, then the http_proxy sets the response into its response to the client.
- The Client receives responses from the http_proxy.
- Elixir over 1.7
mix.exs:loggeris option.:http_proxyis not need if you run http_proxy withHttpProxy.start/0orHttpProxy.stop/0manually.
defapplicationdo[applications:[:logger,:http_proxy]]end...defpdepsdo[{:http_proxy,"~> 1.4.0"}]end
config/config.exs
use Mix.Configconfig :http_proxy, proxies: [ %{port: 8080, to: "http://google.com"}, %{port: 8081, to: "http://yahoo.com"} ]- To manage logger, you should define logger settings like the following.
config :logger, :console, level: :info$ mix deps.get$ mix clean$ mix run --no-halt # start proxy serverIf you would like to start production mode, you should run withMIX_ENV=prod like the following command.
$ MIX_ENV=prod mix run --no-haltLaunch browser and openhttp://localhost:8080 orhttp://localhost:8081.Then,http://localhost:8080 redirect tohttp://google.com andhttp://localhost:8081 do tohttp://yahoo.com.
- Copy
pre-commithookcp hooks/pre-commit ./git/hooks/pre-commit
- You can customize a proxy port. For example, if you change a waiting port from
8080to4000, then you can access tohttp://google.comviahttp://localhost:4000.
use Mix.Configconfig :http_proxy, proxies: [ %{port: 4000, to: "http://google.com"}, %{port: 8081, to: "http://yahoo.com"} ]- You can add a waiting ports in configuration file. For example, the following setting allow you to access to
http://apple.comviahttp://localhost:8082in addition.
use Mix.Configconfig :http_proxy, proxies: [ %{port: 8080, to: "http://google.com"}, %{port: 8081, to: "http://yahoo.com"}, %{port: 8082, to: "http://apple.com"} ]- When
:recordand:playarefalse, then the http_proxy works just multi port proxy. - When
:recordistrue, then the http_proxy works to record request which is proxied. - When
:playistrue, then the http_proxy works to play request between this the http_proxy and clients.- You should set JSON files under
mappingsinplay_path. config.proxies.tomust be available URL to succeed generating http client.
- You should set JSON files under
useMix.Configconfig:http_proxy,proxies:[# MUST%{port:8080,# proxy all request even play or recordto:"http://google.com"},%{port:8081,to:"http://yahoo.com"}]timeout:20_000,# Option, ms to wait http request.record:false,# Option, true: record requests. false: don't record.play:false,# Option, true: play stored requests. false: don't play.export_path:"test/example",# Option, path to export recorded files.play_path:"test/data"# Option, path to read json files as response to.
{"request": {"headers": [],"method":"GET","options": {"aspect":"query_params" },"remote":"127.0.0.1","request_body":"","url":"http://localhost:8080/hoge/inu?email=neko&pass=123" },"response": {"body_file":"path/to/body_file.json","cookies": {},"headers": {"Cache-Control":"public, max-age=2592000","Content-Length":"251","Content-Type":"text/html; charset=UTF-8","Date":"Sat, 21 Nov 2015 00:37:38 GMT","Expires":"Mon, 21 Dec 2015 00:37:38 GMT","Location":"http://www.google.com/hoge/inu?email=neko&pass=123","Server":"sffe","X-Content-Type-Options":"nosniff","X-XSS-Protection":"1; mode=block" },"status_code":301 }}Response body will save in "path/to/body_file.json".
- Example ishttps://github.com/KazuCocoa/http_proxy/tree/master/test/data/mappings
- You can set
pathorpath_patternas attribute underrequest.- If
path, the http_proxy check requests are matched completely. - If
path_pattern, the http_proxy check requests are matched with Regex.
- If
- You can set
bodyorbody_fileas attribute underresponse.- If
body, the http_proxy send the body string. - If
body_file, the http_proxy send the body_file binary as response.
- If
{"request": {"path":"/request/path","port":8080,"method":"GET" },"response": {"body":"<html>hello world</html>","cookies": {},"headers": {"Content-Type":"text/html; charset=UTF-8","Server":"GFE/2.0" },"status_code":200 }}- Pattern match with
Regex.match?(Regex.compile!("\A/request.*neko\z"), request_path) File.read/2viafile/to/path.jsonand respond the binary
{"request": {"path_pattern":"\A/request.*neko\z","port":8080,"method":"GET" },"response": {"body_file":"file/to/path.json","cookies": {},"headers": {"Content-Type":"text/html; charset=UTF-8","Server":"GFE/2.0" },"status_code":200 }}$ mix xref graphlib/http_proxy.ex└── lib/http_proxy/supervisor.ex ├── lib/http_proxy/agent.ex │ ├── lib/http_proxy/play/data.ex │ │ ├── lib/http_proxy/agent.ex │ │ └── lib/http_proxy/play/response.ex │ │ ├── lib/http_proxy/play/data.ex │ │ └── lib/http_proxy/utils/file.ex │ └── lib/http_proxy/play/paths.ex │ ├── lib/http_proxy/agent.ex │ └── lib/http_proxy/play/response.ex └── lib/http_proxy/handle.ex ├── lib/http_proxy/play/body.ex ├── lib/http_proxy/play/data.ex ├── lib/http_proxy/play/paths.ex ├── lib/http_proxy/play/response.ex └── lib/http_proxy/record/response.ex ├── lib/http_proxy/format.ex │ └── lib/http_proxy/data.ex (compile) └── lib/http_proxy/utils/file.ex- record request
- play request
- refactor
- support Regex request path.
- start/stop http_proxy manually
use vcr- integratehttps://github.com/parroty/exvcr
http://elixir.community/styleguide
MIT. Please read LICENSE.
About
http proxy with Elixir. wait request with multi port and forward to each URIs
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.