| 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 |
# File lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb, line 22
22: def initialize(port)
23: @port = port
24: @addresses = []
25: @peers = []
26: @peers_lock = Mutex.new
27: socket = TCPServer.new(port)
28:
29: Thread.new {
30: loop {
31: peer = SOCKS5BytestreamsPeer.new(socket.accept)
32: Thread.new {
33: begin
34: peer.start
35: rescue
36: Jabber::debuglog("SOCKS5 BytestreamsServer: Error accepting peer: #{$!}")
37: end
38: }
39: @peers_lock.synchronize {
40: @peers << peer
41: }
42: }
43: }
44: 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 88
88: def add_address(address)
89: @addresses << address
90: 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 102
102: def each_streamhost(my_jid, &block)
103: @addresses.each { |address|
104: yield SOCKS5BytestreamsServerStreamHost.new(self, my_jid, address, @port)
105: }
106: end
Find the socket a peer is associated to
| addr: | [String] Address like SOCKS5Bytestreams#stream_address |
| result: | [TCPSocker] or [nil] |
# File lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb, line 50
50: def peer_sock(addr)
51: res = nil
52: @peers_lock.synchronize {
53: removes = []
54:
55: @peers.each { |peer|
56: if peer.socket and peer.socket.closed?
57: # Queue peers with closed socket for removal
58: removes << peer
59: elsif peer.address == addr and res.nil?
60: res = peer.socket
61: else
62: # If we sent multiple addresses of our own, clients may
63: # connect multiple times. Close these connections here.
64: removes << peer
65: end
66: }
67:
68: # If we sent multiple addresses of our own, clients may
69: # connect multiple times. Close these connections here.
70: @peers.delete_if { |peer|
71: if removes.include? peer
72: peer.socket.close rescue IOError
73: true
74: else
75: false
76: end
77: }
78: }
79:
80: res
81: end