vagrant-reverse-proxy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a5afc6f0add3b80d836ec3c2213d457bb11876a3
4
+ data.tar.gz: b95de26303e6bc4f8e5ef010e93935364f92694f
5
+ SHA512:
6
+ metadata.gz: bcc2c346c8fa987eab7bed882a33619b25e72a2fbee9191a7ce75e2f997f4fe841784e173b274d79f7cc365dd5ab3f23dc03da60a797de79ceb4cee056f37bb0
7
+ data.tar.gz: ac6e4a7fd17913ae04119176e6ad9dbc15888f7bf2464968b27943e3ab2e499551269d6e2f69df49494054025557fbe45675c24b0740944ced4a3b3fe71d5b18
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /vendor/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'vagrant', :git => 'git://github.com/mitchellh/vagrant.git', :tag => 'v1.7.4'
5
+ end
6
+
7
+ group :plugins do
8
+ # This gem's dependencies are in vagrant-reverse-proxy.gemspec
9
+ gemspec
10
+
11
+ # Useful when testing
12
+ #gem 'vagrant-libvirt'
13
+ #gem 'vagrant-hostmanager'
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Code Yellow BV
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # Vagrant Reverse Proxy
2
+
3
+ This Vagrant plugin automatically installs a reverse proxy
4
+ configuration for each of your Vagrant machines.
5
+
6
+ This means you can access the HTTP interface of your virtual machines
7
+ by accessing HTTP on your machine's IP address or DNS hostname, with
8
+ a suffix that indicates the VM.
9
+
10
+ In other words, `http://localhost/my-vm` refers to `http://my-vm/` on
11
+ the local machine. This also works if you access it from an external
12
+ machine, even though `my-vm` is a local machine name unknown on the
13
+ network.
14
+
15
+ This plugin currently only supports NGINX, but patches are accepted to
16
+ integrate it with other web servers.
17
+
18
+ ## Installation
19
+
20
+ Install the plugin as usual:
21
+
22
+ $ vagrant plugin install vagrant-reverse-proxy
23
+
24
+ ## Usage
25
+
26
+ First, install NGINX and create a configuration as usual. Then, in
27
+ the `server` configuration block for the host you want to use for
28
+ proxying, simply put `include "vagrant-proxy-config";` in the file.
29
+
30
+ If you don't need anything specific, just put the following in
31
+ `/etc/nginx/sites-enabled/default`:
32
+
33
+ server {
34
+ listen [::]:80 default ipv6only=off;
35
+ # This is the fallback server
36
+ server_name default;
37
+ # Redirect http://localhost/hostname/lalala
38
+ # to http://hostname/lalala
39
+ include "vagrant-proxy-config";
40
+ }
41
+
42
+ This will load the `/etc/nginx/vagrant-proxy-config` file which is
43
+ managed by this plugin. This file contains `location` statements for
44
+ each of your virtual machines, such that `http://localhost/foo` will
45
+ proxy to port 80 on the virtual machine with a `config.vm.hostname`
46
+ value of `foo`. This is only done for virtual machines that have
47
+ `config.reverse_proxy.enabled` set to `true` in their config.
48
+
49
+ Whenever you bring up or halt a machine, the plugin updates the proxy
50
+ config file and invokes `sudo systemctl reload nginx` to make the
51
+ change immediately visible.
52
+
53
+ ### Custom host names
54
+
55
+ Sometimes you want to support several virtual hosts for one VM. To
56
+ set that up, you can override the `vhosts` option:
57
+
58
+ config.reverse_proxy.vhosts = ['foo.test', 'bar.test']
59
+
60
+ This will proxy `http://localhost/foo.test` and
61
+ `http://localhost/bar.test` to this VM, with a matching `Host` header.
62
+
63
+ ## Adding proxy support to your application
64
+
65
+ This plugin will instruct NGINX to pass the following headers to your
66
+ Vagrant box:
67
+
68
+ - `X-Forwarded-For`: This contains the IP address of the client.
69
+ - `X-Forwarded-Host`: This contains the IP address of your hypervisor.
70
+ - `X-Forwarded-Port`: This contains the port number of NGINX on your hypervisor.
71
+ - `X-Base-Url`: This contains the base URL that redirects to this VM.
72
+
73
+ Redirects are transparently rewritten by NGINX, but if your
74
+ application generates links with absolute URLs, you'll need to ensure
75
+ that those links are prefixed with the value of `X-Base-Url`, but only
76
+ if the request originated from the trusted NGINX proxy on your
77
+ hypervisor.
78
+
79
+ Be sure to avoid using these headers when the request originated
80
+ elsewhere, because trusting these headers as sent by arbitrary clients
81
+ is a potential security issue! If you're using Laravel, you could
82
+ consider using the
83
+ [trusted proxies middleware](https://github.com/fideloper/TrustedProxy).
84
+ If you're using Symfony, just use `setTrustedProxies()` on your
85
+ `Request` object, and Symfony takes care of the rest. Note that
86
+ `X-Base-Url` is not supported by either framework, so you'll need to
87
+ add a bit of custom code there if you need to override the base URL.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,83 @@
1
+ module VagrantPlugins
2
+ module ReverseProxy
3
+ module Action
4
+ class WriteNginxConfig
5
+ def initialize(app, env)
6
+ @app = app
7
+ @global_env = env[:machine].env
8
+ @provider = env[:machine].provider_name
9
+ @config = @global_env.vagrantfile.config
10
+ end
11
+
12
+ def call(env)
13
+ @app.call(env)
14
+
15
+ return unless @config.reverse_proxy.enabled?
16
+
17
+ # Determine temp file and target file
18
+ tmp_file = @global_env.tmp_path.join('nginx.vagrant-proxies')
19
+ nginx_site = '/etc/nginx/vagrant-proxy-config'
20
+
21
+ env[:ui].info('Updating nginx configuration. Administrator privileges will be required...')
22
+
23
+ File.open(tmp_file, 'w') do |f|
24
+ get_machines().each do |m|
25
+ f.write(server_block(m))
26
+ end
27
+ end
28
+
29
+ Kernel.system('sudo', 'cp', tmp_file.to_s, nginx_site)
30
+ Kernel.system('sudo', 'systemctl', 'reload', 'nginx')
31
+ end
32
+
33
+ def server_block(machine)
34
+ if @config.reverse_proxy.vhosts
35
+ vhosts = @config.reverse_proxy.vhosts
36
+ else
37
+ vhosts = [machine.config.vm.hostname || machine.name]
38
+ end
39
+ ip = get_ip_address(machine)
40
+ vhosts.collect do |vhost| <<EOF
41
+ location /#{vhost}/ {
42
+ proxy_set_header Host #{vhost};
43
+ proxy_set_header X-Forwarded-For $remote_addr;
44
+ proxy_set_header X-Forwarded-Host $host;
45
+ proxy_set_header X-Forwarded-Port $server_port;
46
+ proxy_set_header X-Base-Url http://$host:$server_port/nl.dr.dev/;
47
+
48
+ proxy_pass http://#{ip}/;
49
+ proxy_redirect http://#{vhost}/ /#{vhost}/;
50
+ }
51
+ EOF
52
+ end.join("\n")
53
+ end
54
+
55
+ # Machine-finding code stolen from vagrant-hostmanager :)
56
+ def get_machines()
57
+ # Collect only machines that exist for the current provider
58
+ @global_env.active_machines.collect do |name, provider|
59
+ if provider == @provider
60
+ begin
61
+ m = @global_env.machine(name, @provider)
62
+ m.state.id == :running ? m : nil
63
+ rescue Vagrant::Errors::MachineNotFound
64
+ nil #ignore
65
+ end
66
+ end
67
+ end.compact
68
+ end
69
+
70
+ # Also from vagrant-hostmanager
71
+ def get_ip_address(machine)
72
+ ip = nil
73
+ machine.config.vm.networks.each do |network|
74
+ key, options = network[0], network[1]
75
+ ip = options[:ip] if key == :private_network
76
+ break if ip
77
+ end
78
+ ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'action/write_nginx_config'
2
+
3
+ module VagrantPlugins
4
+ module ReverseProxy
5
+ module Action
6
+ include Vagrant::Action::Builtin
7
+
8
+ # We (currently) don't distinguish between upping and downing a
9
+ # machine; we always write a complete config with all machines.
10
+ def self.add_machine
11
+ Vagrant::Action::Builder.new.tap do |builder|
12
+ builder.use ConfigValidate
13
+ builder.use WriteNginxConfig
14
+ end
15
+ end
16
+
17
+ def self.remove_machine
18
+ Vagrant::Action::Builder.new.tap do |builder|
19
+ builder.use ConfigValidate
20
+ builder.use WriteNginxConfig
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+ module VagrantPlugins
2
+ module ReverseProxy
3
+ class Plugin
4
+ class Config < Vagrant.plugin(2, :config)
5
+ attr_accessor :enabled, :vhosts
6
+ alias_method :enabled?, :enabled
7
+
8
+ def initialize
9
+ @enabled = UNSET_VALUE
10
+ @vhosts = UNSET_VALUE
11
+ end
12
+
13
+ def finalize!
14
+ @enabled = false if @enabled == UNSET_VALUE
15
+ @vhosts = nil if @vhosts == UNSET_VALUE
16
+ end
17
+
18
+ def validate(machine)
19
+ errors = _detected_errors
20
+
21
+ unless [true, false, UNSET_VALUE].include?(@enabled)
22
+ errors << 'enabled must be a boolean'
23
+ end
24
+
25
+ unless @vhosts.instance_of?(Array) || @vhosts == nil || @vhosts == UNSET_VALUE
26
+ errors << 'vhosts must be an array of hostnames (or nil to use default name)'
27
+ end
28
+
29
+ { 'Reverse proxy configuration' => errors.compact }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,37 @@
1
+ module VagrantPlugins
2
+ module ReverseProxy
3
+ class Plugin < Vagrant.plugin(2)
4
+ name 'Reverse Proxy'
5
+
6
+ description <<-DESC
7
+ This plugin automatically manages a reverse proxy
8
+ configuration in your host machine's web server, which makes
9
+ it easy to access the web servers of your guest machines.
10
+
11
+ This is safer and easier than making the guest machine
12
+ completely available on the outside network.
13
+ DESC
14
+
15
+ config :reverse_proxy do
16
+ require_relative 'config'
17
+ Config
18
+ end
19
+
20
+ action_hook(:reverse_proxy, :machine_action_up) do |hook|
21
+ hook.append(Action.add_machine)
22
+ end
23
+
24
+ action_hook(:reverse_proxy, :machine_action_suspend) do |hook|
25
+ hook.append(Action.remove_machine)
26
+ end
27
+
28
+ action_hook(:reverse_proxy, :machine_action_resume) do |hook|
29
+ hook.append(Action.add_machine)
30
+ end
31
+
32
+ action_hook(:reverse_proxy, :machine_action_halt) do |hook|
33
+ hook.append(Action.remove_machine)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module ReverseProxy
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require "vagrant-reverse-proxy/plugin"
2
+ require "vagrant-reverse-proxy/action"
3
+ require "vagrant-reverse-proxy/version"
4
+
5
+ module VagrantPlugins
6
+ module ReverseProxy
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant-reverse-proxy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-reverse-proxy"
8
+ spec.version = VagrantPlugins::ReverseProxy::VERSION
9
+ spec.authors = ["Peter Bex"]
10
+ spec.email = ["peter@codeyellow.nl"]
11
+ spec.summary = %q{A vagrant plugin that adds reverse proxies for your VMs}
12
+ spec.description = %q{This plugin manages reverse proxy configuration (currently nginx-only) so you can reach your Vagrant VM's web servers externally without having to set up bridge networking.}
13
+ spec.homepage = "https://github.com/CodeYellowBV/vagrant-reverse-proxy"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-reverse-proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Bex
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: This plugin manages reverse proxy configuration (currently nginx-only)
42
+ so you can reach your Vagrant VM's web servers externally without having to set
43
+ up bridge networking.
44
+ email:
45
+ - peter@codeyellow.nl
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/vagrant-reverse-proxy.rb
56
+ - lib/vagrant-reverse-proxy/action.rb
57
+ - lib/vagrant-reverse-proxy/action/write_nginx_config.rb
58
+ - lib/vagrant-reverse-proxy/config.rb
59
+ - lib/vagrant-reverse-proxy/plugin.rb
60
+ - lib/vagrant-reverse-proxy/version.rb
61
+ - vagrant-reverse-proxy.gemspec
62
+ homepage: https://github.com/CodeYellowBV/vagrant-reverse-proxy
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A vagrant plugin that adds reverse proxies for your VMs
86
+ test_files: []