envoy-proxy 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ed79185e2aad87adb4349b67cc291736ba715e0
4
- data.tar.gz: b4c8f976611a606f40cc4cad0b4242f0e1f8b3ee
3
+ metadata.gz: 20acae48638da1f09581622a10b1b742b50828d9
4
+ data.tar.gz: 0238fa0dea61a626f1e262c70e705cd421f2ef14
5
5
  SHA512:
6
- metadata.gz: c0041100f109dbee5cfd0d33a5eb8bffd88acda65fa810bba1c8f3ac1c184d5bcbbae50bc6bf9f535bd9ac9485a4a1a9f28b3e0a886f5b2dced6944aef5bb622
7
- data.tar.gz: d9a0a9017b0dc3457385c9717db2ff3acf72c549cfc76a23eb43b7e8caeca06af98b8ac67ea94b07c139b47ef3379e7e5d48e832c2b27fd8e19348c5e1ab7394
6
+ metadata.gz: 4e24a4507489bdc66f20798b9eeb69a662b3a0856a3cb2eddd0a2004d6d08cc7aa6fa2acb6bb11c404a7c29daf3151e62e443a1d93acbddd1694cbb420479247
7
+ data.tar.gz: a401d0c2b88a85e1a7b1a63a3ba5881b67be7f53bdfffbf0a9044fea06a820a4288558d5fa67a18ecbe160b7cb58443349777991a8fc10a130e0f1eebdab3767
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency('eventmachine', '~> 1.0')
23
23
  spec.add_dependency('bert', '~> 1.1')
24
+ spec.add_dependency('colorize', '~> 0.7.3')
24
25
 
25
26
  spec.add_development_dependency "bundler", "~> 1.3"
26
27
  spec.add_development_dependency "rake"
@@ -12,6 +12,7 @@ module Envoy
12
12
  end
13
13
 
14
14
  def connection_completed
15
+ @client.log TRACE, "connected to upstream service for stream #{@id}"
15
16
  @tried_starting = nil
16
17
  send_data @buffer, true
17
18
  @buffer = nil
@@ -26,18 +27,21 @@ module Envoy
26
27
  end
27
28
 
28
29
  def receive_data data
30
+ @client.log TRACE, "#{data.length} bytes of data send on stream #{@id}"
29
31
  @client.send_object :stream, @id, data
30
32
  end
31
33
 
32
34
  def reconnect
35
+ @client.log TRACE, "reconnecting to upstream service for stream #{@id}"
33
36
  super @client.options[:local_host], @client.options[:local_port]
34
37
  end
35
38
 
36
39
  def unbind e
37
40
  if e == Errno::ECONNREFUSED
41
+ @client.log TRACE, "couldn't connect to upstream service for stream #{@id}"
38
42
  if @tried_starting
39
43
  if Time.now > @tried_starting + @client.options[:delay]
40
- @client.log "Service isn't running, but starting it didn't really work out."
44
+ @client.log ERROR, "Service isn't running, but starting it didn't really work out."
41
45
  @client.send_object :close, @id, 502
42
46
  @tried_starting = false
43
47
  else
@@ -47,7 +51,7 @@ module Envoy
47
51
  end
48
52
  elsif cmd = @client.options[:command]
49
53
  cmd = cmd % @client.options
50
- @client.log "Service doesn't seem to be running. Trying to start it now..."
54
+ @client.log INFO, "Service doesn't seem to be running. Trying to start it now..."
51
55
  @tried_starting = Time.now
52
56
  p @client.options[:dir]
53
57
  Dir.chdir File.expand_path(@client.options[:dir]) do
@@ -64,8 +68,9 @@ module Envoy
64
68
  end
65
69
  end
66
70
  elsif e
67
- @client.log e.inspect
71
+ @client.log ERROR, e.inspect
68
72
  else
73
+ @client.log TRACE, "upstream service closed stream #{@id}"
69
74
  @client.send_object :close, @id
70
75
  end
71
76
  end
@@ -41,6 +41,14 @@ options = parse_options
41
41
 
42
42
  unless EM.reactor_running?
43
43
  EM.run do
44
+ Signal.trap("INT") do
45
+ $exiting = true
46
+ EventMachine.stop
47
+ end
48
+ Signal.trap("TERM") do
49
+ $exiting = true
50
+ EventMachine.stop
51
+ end
44
52
  load_config.each do |config|
45
53
  config = config.merge(options)
46
54
  config["local_port"] ||= config["command"] ? rand(16383) + 49152 : 80
@@ -6,10 +6,13 @@ def default_options
6
6
  "server_port" => "8282",
7
7
  "local_host" => '127.0.0.1',
8
8
  "tls" => false,
9
- "verbose" => false,
9
+ "verbosity" => 3,
10
10
  "version" => Envoy::VERSION,
11
11
  "delay" => 1,
12
- "dir" => "."
12
+ "dir" => ".",
13
+ "timestamps" => false,
14
+ "show_log_level" => true,
15
+ "color_log_level" => true,
13
16
  }
14
17
  end
15
18
 
@@ -32,6 +35,12 @@ def parse_options
32
35
  op.on "-c COMMAND", "Run this command" do |v|
33
36
  options["command"] = v
34
37
  end
38
+ op.on "-v", "--verbose", "Show messages. Repeat to show more." do
39
+ options["verbosity"] += 1
40
+ end
41
+ op.on "-q", "--quiet", "Hide messages. Repeat to hide more." do
42
+ options["verbosity"] -= 1
43
+ end
35
44
  op.on "-h", "--help", "Show this message" do
36
45
  puts op
37
46
  exit
@@ -1,5 +1,6 @@
1
1
  require 'envoy/protocol'
2
2
  require 'envoy/client/channel'
3
+ require 'colorize'
3
4
 
4
5
  module Envoy
5
6
  module Client
@@ -19,6 +20,7 @@ module Envoy
19
20
  if @options.has_key?(:log)
20
21
  @log = @options[:log] && File.open(@options[:log], "a")
21
22
  end
23
+ log DEBUG, "envoy #{Envoy::VERSION} starting up"
22
24
  end
23
25
 
24
26
  def channels
@@ -26,19 +28,23 @@ module Envoy
26
28
  end
27
29
 
28
30
  def receive_start_tls
31
+ log DEBUG, "Securing channel."
29
32
  start_tls
30
33
  end
31
34
 
32
35
  def receive_close id
36
+ log TRACE, "closed stream #{id}"
33
37
  channels[id].close_connection true
34
38
  channels.delete(id)
35
39
  end
36
40
 
37
41
  def receive_stream id, data
42
+ log TRACE, "#{data.length} bytes of data received on stream #{id}"
38
43
  channels[id].send_data data
39
44
  end
40
45
 
41
46
  def receive_connection id
47
+ log TRACE, "New connection request with id `#{id}'"
42
48
  channels[id] = EM.connect(options[:local_host] || '127.0.0.1',
43
49
  options[:local_port], Channel, id, self)
44
50
  end
@@ -46,18 +52,28 @@ module Envoy
46
52
  def receive_keepalive
47
53
  end
48
54
 
49
- def log message, io = @log
55
+ def log (level, text, io = @log)
50
56
  return unless io
51
- t = Time.now.strftime("%F %T")
52
- io.puts t + " " + message.split("\n").join("\n#{t.gsub(/./, ' ')} ")
57
+ return unless level <= verbosity
58
+ message = [
59
+ @options[:timestamps] ? Time.now.strftime("%F %T") : nil,
60
+ @options[:show_log_level] ? "#{VERBOSITIES[level][0]}:" : nil,
61
+ text
62
+ ].compact.join(" ")
63
+ if @options[:color_log_level]
64
+ #FATAL ERROR WARN\ INFO\ DEBUG TRACE
65
+ message = message.colorize(%i"red red yellow green default light_black"[level])
66
+ end
67
+ io.puts message
53
68
  io.flush
54
69
  end
55
70
 
56
- def receive_message message
57
- log message
71
+ def receive_message text, level = INFO
72
+ log level, text
58
73
  end
59
74
 
60
75
  def receive_ping
76
+ log TRACE, "Server pinged. Ponging back."
61
77
  send_object :pong
62
78
  end
63
79
 
@@ -66,33 +82,42 @@ module Envoy
66
82
  EventMachine.stop_event_loop
67
83
  end
68
84
 
85
+ def receive_confirm (options)
86
+ log DEBUG, "Server confirmed our request. Proxy set up."
87
+ end
88
+
69
89
  def unbind
70
90
  if @halting
71
- STDERR.puts "Server shut us down."
91
+ log DEBUG, "Shutting down because server told us to."
92
+ elsif $exiting
93
+ log DEBUG, "Shutting down because the local system told us to."
72
94
  elsif !@halting && r = @options[:reconnect]
73
- STDERR.write "Lost connection. Retrying... #{r[0]}\r"
95
+ log WARN, "Lost connection. Retrying..." if r == 0
74
96
  EM.add_timer 0.5 do
75
- @options[:reconnect] = r.rotate
97
+ @options[:reconnect] += 1
76
98
  Trunk.start @options
77
99
  end
78
100
  else
79
101
  if options[:did_connect]
80
- STDERR.puts "Connection lost. Not point reconnecting because the host is randomly generated."
102
+ log FATAL, "Connection lost. Not point reconnecting because the host is randomly generated."
81
103
  else
82
- STDERR.puts "Couldn't connect. Abandoning ship."
104
+ log FATAL, "Couldn't connect. Abandoning ship."
83
105
  end
84
106
  EventMachine.stop_event_loop
85
107
  end
86
108
  end
87
109
 
88
110
  def ssl_handshake_completed
111
+ log DEBUG, "Channel is secure, sending options"
89
112
  options[:did_connect] = true
90
- options[:reconnect] = %w"- \\ | /" if options[:hosts]
113
+ options[:reconnect] = 0 if options[:hosts]
91
114
  send_object :options, options
115
+ log DEBUG, "Exporting #{@options[:local_host]}:#{@options[:local_port]}"
92
116
  end
93
117
 
94
118
  def post_init
95
119
  self.comm_inactivity_timeout = 25
120
+ log DEBUG, "Requesting TLS negotiation."
96
121
  send_object :start_tls
97
122
  end
98
123
 
@@ -1,11 +1,24 @@
1
1
  require 'eventmachine'
2
2
  require 'bert'
3
3
 
4
+ TRACE = 5
5
+ DEBUG = 4
6
+ INFO = 3
7
+ WARN = 2
8
+ ERROR = 1
9
+ FATAL = 0
10
+
4
11
  module Envoy
5
12
 
6
13
  module Protocol
7
14
  include EM::P::ObjectProtocol
8
15
 
16
+ VERBOSITIES = %w"FATAL ERROR WARN\ INFO\ DEBUG TRACE"
17
+
18
+ def verbosity
19
+ @verbosity ||= [FATAL, [TRACE, @options[:verbosity] || 3].min].max
20
+ end
21
+
9
22
  module Serializer
10
23
  def self.dump(object)
11
24
  BERT.encode(object)
@@ -5,11 +5,6 @@ module Envoy
5
5
  module Trunk
6
6
  include Protocol
7
7
 
8
- def log message
9
- t = Time.now.strftime("%F %T")
10
- STDERR.puts t + " " + message.split("\n").join("\n#{t.gsub(/./, ' ')} ")
11
- end
12
-
13
8
  def initialize key
14
9
  super
15
10
  @key = key
@@ -33,7 +28,6 @@ module Envoy
33
28
 
34
29
  def receive_pong
35
30
  EM.add_timer 5 do
36
- log "ping"
37
31
  send_object :ping
38
32
  end
39
33
  end
@@ -60,8 +54,21 @@ module Envoy
60
54
  @options[:key]
61
55
  end
62
56
 
57
+ def log message
58
+ t = Time.now.strftime("%F %T")
59
+ STDERR.puts t + " " + message.split("\n").join("\n#{t.gsub(/./, ' ')} ")
60
+ end
61
+
62
+ def message (level, message)
63
+ if @options[:verbosity]
64
+ send_object :message, message, level
65
+ else
66
+ send_object :message, message
67
+ end
68
+ end
69
+
63
70
  def halt message = nil
64
- send_object :message, message if message
71
+ message FATAL, message if message
65
72
  send_object :halt
66
73
  close_connection(true)
67
74
  end
@@ -72,13 +79,11 @@ module Envoy
72
79
 
73
80
  def receive_options options
74
81
  @options = options
75
- if version? "~> 0.1"
76
- receive_pong
77
- end
82
+ receive_pong if version? "> 0.1"
78
83
  if version? "< #{Envoy::VERSION}"
79
- send_object :message, "Your client is out of date. Please upgrade to #{Envoy::VERSION}."
84
+ message WARN, "Your client is out of date. Please upgrade to #{Envoy::VERSION}."
80
85
  elsif version? "> #{Envoy::VERSION}"
81
- send_object :message, "Your client is from the future. The server is expecting #{Envoy::VERSION}."
86
+ message WARN, "Your client is from the future. The server is expecting #{Envoy::VERSION}."
82
87
  end
83
88
  if @key and @key != @options[:key]
84
89
  halt "Key is invalid"
@@ -87,28 +92,32 @@ module Envoy
87
92
  hosts = @options[:hosts] || []
88
93
  hosts.any? do |label|
89
94
  if label == "s"
90
- send_object :message, "label is reserved: `#{label}'"
95
+ message FATAL, "label is reserved: `#{label}'"
91
96
  true
92
97
  elsif label =~ /\./
93
- send_object :message, "label is invalid: `#{label}'"
98
+ message FATAL, "label is invalid: `#{label}'"
94
99
  true
95
100
  elsif other_trunk = Trunk.trunks[label][0]
96
101
  unless other_trunk.key == key
97
- send_object :message, "label is protected with a key: `#{label}'"
102
+ message FATAL, "label is protected with a key: `#{label}'"
98
103
  true
99
104
  end
100
105
  end
101
106
  end && halt
102
- hosts << SecureRandom.random_number(36 ** 4).to_s(36) if hosts.empty?
103
- m = ["#{options[:local_host]}:#{options[:local_port]} now available at:"]
104
- @hosts = hosts.each do |host|
105
- Trunk.trunks[host] << self
106
- send_object :message, "host: #{host}.#{$zone}"
107
+ if hosts.empty?
108
+ hosts = [SecureRandom.random_number(36 ** 4).to_s(36)]
109
+ message INFO, "Service accessible at http://#{hosts[0]}.#{$zone}/"
110
+ else
111
+ @hosts = hosts.each do |host|
112
+ Trunk.trunks[host] << self
113
+ message INFO, "Service accessible at http://#{host}.#{$zone}/"
114
+ end
107
115
  end
108
116
  unless @options[:key]
109
- @options[:key] ||= SecureRandom.hex(8)
110
- send_object :message, "key: #{@options[:key]}"
117
+ @options[:key] = SecureRandom.hex(8)
118
+ message INFO, "Service access key is `#{@options[:key]}'"
111
119
  end
120
+ send_object :confirm, @options if version? ">= 0.2.2"
112
121
  end
113
122
 
114
123
  def unbind
@@ -1,3 +1,3 @@
1
1
  module Envoy
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: envoy-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Baum
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.7.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.7.3
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement