Class: Jabber::Bytestreams::SOCKS5BytestreamsServer

Class | Jabber::Bytestreams::SOCKS5BytestreamsServer
In: | lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb
Parent: | Object

The SOCKS5BytestreamsServer is an implementation of a SOCKS5 server.

You can use it if you‘re reachable by your SOCKS5Bytestreams peers, thus avoiding use of an external proxy.

## Usage:

- Instantiate with an unfirewalled port
- Add your external IP addresses with SOCKS5BytestreamsServer#add_address
- Once you‘ve got an outgoing SOCKS5BytestreamsInitiator, do `SOCKS5BytestreamsInitiator#add_streamhost(my_socks5bytestreamsserver)` before you do `SOCKS5BytestreamsInitiator#open`

### Methods

add_address each_streamhost new peer_sock

### Public Class methods

new(port, listen_on=nil)

Start a local SOCKS5BytestreamsServer

Will start to listen on the given TCP port and accept new peers

port: | [Fixnum] TCP port to listen on
listen_on: | [String] Optional address for the server socket to listen on (i.e. ‘0.0.0.0’ or ’::’)

[Source]

        # File lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb, line 27
    27:       def initialize(port, listen_on=nil)
    28:         @port = port
    29:         @addresses = []
    30:         @peers = []
    31:         @peers_lock = Mutex.new
    32:         if listen_on
    33:           socket = TCPServer.new(listen_on, port)
    34:         else
    35:           socket = TCPServer.new(port)
    36:         end
    37:
    38:         Thread.new do
    39:           Thread.current.abort_on_exception = true
    40:           loop do
    41:             peer = SOCKS5BytestreamsPeer.new(socket.accept)
    42:             Thread.new do
    43:               Thread.current.abort_on_exception = true
    44:               begin
    45:                 peer.start
    46:               rescue
    47:                 Jabber::debuglog("SOCKS5 BytestreamsServer: Error accepting peer: #{$!}")
    48:               end
    49:             end
    50:             @peers_lock.synchronize do
    51:               @peers << peer
    52:             end
    53:           end
    54:         end
    55:       end

### Public Instance methods

add_address(address)

Add an external IP address

This is a must-have, as SOCKS5BytestreamsInitiator must inform the target where to connect

[Source]

         # File lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb, line 104
    104:       def add_address(address)
    105:         @addresses << address
    106:       end

each_streamhost(my_jid) {|SOCKS5BytestreamsServerStreamHost.new(self, my_jid, address, @port)| ...}

Iterate through all configured addresses, yielding SOCKS5BytestreamsServerStreamHost instances, which should be passed to SOCKS5BytestreamsInitiator#add_streamhost

This will be automatically invoked if you pass an instance of SOCKS5BytestreamsServer to SOCKS5BytestreamsInitiator#add_streamhost

my_jid: | [JID] My Jabber-ID

[Source]

         # File lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb, line 118
    118:       def each_streamhost(my_jid, &block)
    119:         @addresses.each { |address|
    120:           yield SOCKS5BytestreamsServerStreamHost.new(self, my_jid, address, @port)
    121:         }
    122:       end

peer_sock(addr)

Find the socket a peer is associated to

This method also performs some housekeeping, ie. removing peers with closed sockets.

addr: | [String] Address like SOCKS5Bytestreams#stream_address
result: | [TCPSocker] or [nil]

[Source]

        # File lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb, line 64
    64:       def peer_sock(addr)
    65:         res = nil
    66:         @peers_lock.synchronize {
    67:           removes = []
    68:
    69:           @peers.each { |peer|
    70:             if peer.socket and peer.socket.closed?
    71:               # Queue peers with closed socket for removal
    72:               removes << peer
    73:             elsif peer.address == addr and res.nil?
    74:               res = peer.socket
    75:             end
    76:
    77:             # If we sent multiple addresses of our own, clients may
    78:             # connect multiple times. DO NOT close any other connections
    79:             # here. These may belong to other concurrent bytestreams,
    80:             # believe that the peer will close any unneeded sockets
    81:             # which will then be picked up by the next call to peer_sock.
    82:           }
    83:
    84:           # If we sent multiple addresses of our own, clients may
    85:           # connect multiple times. Close these connections here.
    86:           @peers.delete_if { |peer|
    87:             if removes.include? peer
    88:               peer.socket.close rescue IOError
    89:               true
    90:             else
    91:               false
    92:             end
    93:           }
    94:         }
    95:
    96:         res
    97:       end

[Validate]