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.
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 ’::’) |
# 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 { 39: loop { 40: peer = SOCKS5BytestreamsPeer.new(socket.accept) 41: Thread.new { 42: begin 43: peer.start 44: rescue 45: Jabber::debuglog("SOCKS5 BytestreamsServer: Error accepting peer: #{$!}") 46: end 47: } 48: @peers_lock.synchronize { 49: @peers << peer 50: } 51: } 52: } 53: end
Add an external IP address
This is a must-have, as SOCKS5BytestreamsInitiator must inform the target where to connect
# File lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb, line 102 102: def add_address(address) 103: @addresses << address 104: end
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 |
# File lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb, line 116 116: def each_streamhost(my_jid, &block) 117: @addresses.each { |address| 118: yield SOCKS5BytestreamsServerStreamHost.new(self, my_jid, address, @port) 119: } 120: end
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] |
# File lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb, line 62 62: def peer_sock(addr) 63: res = nil 64: @peers_lock.synchronize { 65: removes = [] 66: 67: @peers.each { |peer| 68: if peer.socket and peer.socket.closed? 69: # Queue peers with closed socket for removal 70: removes << peer 71: elsif peer.address == addr and res.nil? 72: res = peer.socket 73: end 74: 75: # If we sent multiple addresses of our own, clients may 76: # connect multiple times. DO NOT close any other connections 77: # here. These may belong to other concurrent bytestreams, 78: # believe that the peer will close any unneeded sockets 79: # which will then be picked up by the next call to peer_sock. 80: } 81: 82: # If we sent multiple addresses of our own, clients may 83: # connect multiple times. Close these connections here. 84: @peers.delete_if { |peer| 85: if removes.include? peer 86: peer.socket.close rescue IOError 87: true 88: else 89: false 90: end 91: } 92: } 93: 94: res 95: end