surro-gate 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad375981bd8f2b842ba825df243cd373b6a458a6
4
- data.tar.gz: 6b56516f80d5e2c0487f8a6ae694245287f7cf69
3
+ metadata.gz: e7ca2413b4164e0b24ab63bc4045b6032f547a98
4
+ data.tar.gz: 4d962b753c9dc1fd90c6eb4ba1d8f4608fbb36eb
5
5
  SHA512:
6
- metadata.gz: 71e1ea8a327d5ded2f9fef6ba217cb46a473054cdbedafbbde8b7646ab8ea98080cf4e1a6c4c2817143644dc8e47231a9dc6e1cab0ab0078875520d6b36da475
7
- data.tar.gz: c9493e2bf69920c0d4ffe86c1ec2ce1ae27bb85e34c9a7365c2955cbeac24f16c7820537de70efda17b58c31d961c82a2b797d7f27262c096abf2ec20342a7fe
6
+ metadata.gz: d77bfb4c20a74d1551f0946cd600e01c26d5cdae9bcc8ba23788f6503edc78fd4871534a0a74e186b6e8cec29a8e0fcb764ba9c1ad6b6607330da47c80b8e203
7
+ data.tar.gz: a46c65fb6da65c8735dc35fc37f7965081ce4070e0f1fb68044fdce1530f2379893255e7eb50c61c5745e9a4632fe0766663649159a4303aa8cb47534f2b4613
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # SurroGate
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/surro-gate.svg)](https://badge.fury.io/rb/surro-gate)
3
4
  [![Build Status](https://travis-ci.org/skateman/surro-gate.svg?branch=master)](https://travis-ci.org/skateman/surro-gate)
4
5
  [![Dependency Status](https://gemnasium.com/skateman/surro-gate.svg)](https://gemnasium.com/skateman/surro-gate)
5
6
  [![Inline docs](http://inch-ci.org/github/skateman/surro-gate.svg?branch=master)](http://inch-ci.org/github/skateman/surro-gate)
data/codecov.yml ADDED
@@ -0,0 +1 @@
1
+ comment: off
@@ -1,4 +1,5 @@
1
1
  require 'nio'
2
+ require 'logger'
2
3
 
3
4
  module SurroGate
4
5
  # This class is responsible for connecting TCP socket pairs and proxying between them.
@@ -10,9 +11,11 @@ module SurroGate
10
11
  #
11
12
  # The proxy was designed to be highly reusable and it can handle multiple socket pairs.
12
13
  class Proxy
13
- def initialize
14
+ def initialize(logger)
14
15
  @mutex = Mutex.new
15
16
  @selector = NIO::Selector.new
17
+ @log = logger || Logger.new(STDOUT)
18
+ @log.level = Logger::WARN
16
19
  end
17
20
 
18
21
  # Registers a pair of socket for proxying.
@@ -22,12 +25,15 @@ module SurroGate
22
25
  # @raise [ProxyError] when at least one of the pushed sockets is already registered
23
26
  # @param left [TCPSocket]
24
27
  # @param right [TCPSocket]
28
+ # @yield The block responsible for additional cleanup
25
29
  # @return the registered socket pair as an array
26
- def push(left, right)
30
+ def push(left, right, &block)
27
31
  raise ProxyError, 'Socket already handled by the proxy' if includes?(left, right)
28
32
 
33
+ @log.info("Connecting #{left} <-> #{right}")
34
+
29
35
  @mutex.synchronize do
30
- proxy(left, right)
36
+ proxy(left, right, block)
31
37
  end
32
38
 
33
39
  [left, right]
@@ -45,7 +51,7 @@ module SurroGate
45
51
 
46
52
  private
47
53
 
48
- def proxy(left, right)
54
+ def proxy(left, right, block = nil)
49
55
  # Pass boths sockets to the Nio4r selector
50
56
  monitors = [left, right].map { |socket| @selector.register(socket, :rw) }
51
57
 
@@ -56,9 +62,9 @@ module SurroGate
56
62
  # Set up a proc for future transmissions
57
63
  src.value = proc do
58
64
  # Clean up the connection if one of the endpoints gets closed
59
- cleanup(src.io, dst.io) if src.io.closed? || dst.io.closed?
65
+ cleanup(src.io, dst.io, &block) if src.io.closed? || dst.io.closed?
60
66
  # Do the transmission and return with the bytes transferred
61
- transmit(src.io, dst.io) if src.readable? && dst.writable?
67
+ transmit(src.io, dst.io, block) if src.readable? && dst.writable?
62
68
  end
63
69
  end
64
70
 
@@ -66,10 +72,11 @@ module SurroGate
66
72
  thread_start unless @selector.empty?
67
73
  end
68
74
 
69
- def transmit(src, dst)
75
+ def transmit(src, dst, block)
70
76
  dst.write_nonblock(src.read_nonblock(4096))
71
77
  rescue # Clean up both sockets if something bad happens
72
- cleanup(src, dst)
78
+ @log.warn("Transmission failure between #{src} <-> #{dst}")
79
+ cleanup(src, dst, &block)
73
80
  end
74
81
 
75
82
  def cleanup(*sockets)
@@ -79,11 +86,16 @@ module SurroGate
79
86
  socket.close unless socket.closed?
80
87
  end
81
88
 
89
+ @log.info("Disconnecting #{sockets.join(' <-> ')}")
90
+
91
+ yield if block_given?
92
+
82
93
  # Make sure that the internal thread is stopped if no sockets remain
83
94
  thread_stop if @selector.empty?
84
95
  end
85
96
 
86
97
  def thread_start
98
+ @log.debug('Starting the internal thread')
87
99
  @thread ||= Thread.new do
88
100
  loop do
89
101
  reactor
@@ -93,6 +105,7 @@ module SurroGate
93
105
 
94
106
  def thread_stop
95
107
  return if @thread.nil?
108
+ @log.debug('Stopping the internal thread')
96
109
  thread = @thread
97
110
  @thread = nil
98
111
  thread.kill
@@ -1,3 +1,3 @@
1
1
  module SurroGate
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/surro-gate.rb CHANGED
@@ -7,8 +7,8 @@ module SurroGate
7
7
  class << self
8
8
  # Initializes a new Proxy instance
9
9
  # @return [Proxy]
10
- def new
11
- Proxy.new
10
+ def new(logger = nil)
11
+ Proxy.new(logger)
12
12
  end
13
13
  end
14
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surro-gate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dávid Halász
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-24 00:00:00.000000000 Z
11
+ date: 2017-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nio4r
@@ -127,6 +127,7 @@ files:
127
127
  - Rakefile
128
128
  - bin/console
129
129
  - bin/setup
130
+ - codecov.yml
130
131
  - lib/surro-gate.rb
131
132
  - lib/surro-gate/proxy.rb
132
133
  - lib/surro-gate/proxy_error.rb
@@ -152,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
153
  version: '0'
153
154
  requirements: []
154
155
  rubyforge_project:
155
- rubygems_version: 2.5.1
156
+ rubygems_version: 2.5.2
156
157
  signing_key:
157
158
  specification_version: 4
158
159
  summary: A general purrpose TCP-to-TCP proxy written in Ruby