Class: I2P::BOB::Client
- Inherits:
-
Object
- Object
- I2P::BOB::Client
- Defined in:
- lib/i2p/bob/client.rb
Overview
**I2P Basic Open Bridge (BOB) client.**
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
Returns the host name or IP address of the BOB bridge.
-
#port ⇒ Integer
readonly
Returns the port number of the BOB bridge.
-
#socket ⇒ TCPSocket
readonly
Returns the socket connection to the BOB bridge.
Class Method Summary collapse
-
.open(options = {}) {|client| ... } ⇒ void
Establishes a connection to the BOB bridge.
Instance Method Summary collapse
-
#clear ⇒ void
(also: #clear!)
Removes the current tunnel.
-
#connect ⇒ void
(also: #reconnect)
Establishes a connection to the BOB bridge.
-
#connected? ⇒ Boolean
Returns ‘true` if a connection to the BOB bridge has been established and is active.
-
#disconnect ⇒ void
(also: #close)
Closes the connection to the BOB bridge.
-
#getdest ⇒ Destination
Returns the destination for the current tunnel.
-
#getkeys ⇒ KeyPair
Returns the key pair for the current tunnel.
-
#getnick(nickname) ⇒ void
Selects an existing tunnel with the given nickname.
-
#inhost(host) ⇒ void
(also: #inhost=)
Sets the inbound host name or IP address that the current tunnel listens on.
-
#initialize(options = {}) {|client| ... } ⇒ Client
constructor
Initializes a new client instance.
-
#inport(port) ⇒ void
(also: #inport=)
Sets the inbound port number that the current tunnel listens on.
-
#newkeys ⇒ Destination
Generates a new keypair for the current tunnel.
-
#option(key, value) ⇒ void
Sets an I2P Control Protocol (I2CP) option for the current tunnel.
-
#outhost(host) ⇒ void
(also: #outhost=)
Sets the outbound host name or IP address that the current tunnel connects to.
-
#outport(port) ⇒ void
(also: #outport=)
Sets the outbound port number that the current tunnel connects to.
-
#quiet(value = true) ⇒ void
(also: #quiet=)
Toggles whether to send the incoming destination key to listening sockets.
-
#quit ⇒ void
(also: #quit!)
Closes the connection to the BOB bridge cleanly.
-
#setkeys(key_pair) ⇒ void
(also: #keys=)
Sets the key pair for the current tunnel.
-
#setnick(nickname) ⇒ void
(also: #nickname=)
Creates a new tunnel with the given nickname.
-
#start ⇒ void
(also: #start!)
Starts and activates the current tunnel.
-
#stop ⇒ void
(also: #stop!)
Stops and inactivates the current tunnel.
-
#verify(data) ⇒ Boolean
Verifies a Base64-formatted key pair or destination, returning ‘true` for valid input.
Constructor Details
#initialize(options = {}) {|client| ... } ⇒ Client
Initializes a new client instance.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/i2p/bob/client.rb', line 103 def initialize( = {}, &block) = .dup @host = (.delete(:host) || DEFAULT_HOST).to_s @port = (.delete(:port) || DEFAULT_PORT).to_i if block_given? case block.arity when 1 then block.call(self) else instance_eval(&block) end end end |
Instance Attribute Details
#host ⇒ String (readonly)
Returns the host name or IP address of the BOB bridge.
87 88 89 |
# File 'lib/i2p/bob/client.rb', line 87 def host @host end |
#port ⇒ Integer (readonly)
Returns the port number of the BOB bridge.
93 94 95 |
# File 'lib/i2p/bob/client.rb', line 93 def port @port end |
#socket ⇒ TCPSocket (readonly)
Returns the socket connection to the BOB bridge.
81 82 83 |
# File 'lib/i2p/bob/client.rb', line 81 def socket @socket end |
Class Method Details
.open(options = {}) {|client| ... } ⇒ void
This method returns an undefined value.
Establishes a connection to the BOB bridge.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/i2p/bob/client.rb', line 43 def self.open( = {}, &block) client = self.new() client.connect if [:nickname] begin client.getnick([:nickname]) rescue Error => e client.setnick([:nickname]) end end client.setkeys([:keys]) if [:keys] client.quiet([:quiet]) if [:quiet] client.inhost([:inhost]) if [:inhost] client.inport([:inport]) if [:inport] client.outhost([:outhost]) if [:outhost] client.outport([:outport]) if [:outport] unless block_given? client else begin result = case block.arity when 1 then block.call(client) else client.instance_eval(&block) end ensure client.disconnect end result end end |
Instance Method Details
#clear ⇒ void Also known as: clear!
This method returns an undefined value.
Removes the current tunnel. The tunnel must be inactive.
433 434 435 436 437 |
# File 'lib/i2p/bob/client.rb', line 433 def clear send_command(:clear) read_response # "cleared" self end |
#connect ⇒ void Also known as: reconnect
This method returns an undefined value.
Establishes a connection to the BOB bridge.
If called after the connection has already been established, disconnects and then reconnects to the bridge.
140 141 142 143 144 145 146 147 |
# File 'lib/i2p/bob/client.rb', line 140 def connect disconnect if connected? @socket = TCPSocket.new(@host, @port) @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) read_line # "BOB 00.00.0D" read_line # "OK" self end |
#connected? ⇒ Boolean
Returns ‘true` if a connection to the BOB bridge has been established and is active.
126 127 128 |
# File 'lib/i2p/bob/client.rb', line 126 def connected? !!@socket end |
#disconnect ⇒ void Also known as: close
This method returns an undefined value.
Closes the connection to the BOB bridge.
If called after the connection has already been closed, does nothing.
159 160 161 162 163 |
# File 'lib/i2p/bob/client.rb', line 159 def disconnect @socket.close if @socket && !@socket.closed? @socket = nil self end |
#getdest ⇒ Destination
Returns the destination for the current tunnel.
244 245 246 247 |
# File 'lib/i2p/bob/client.rb', line 244 def getdest send_command(:getdest) Destination.parse(read_response) end |
#getkeys ⇒ KeyPair
Returns the key pair for the current tunnel.
257 258 259 260 |
# File 'lib/i2p/bob/client.rb', line 257 def getkeys send_command(:getkeys) KeyPair.parse(read_response) end |
#getnick(nickname) ⇒ void
This method returns an undefined value.
Selects an existing tunnel with the given nickname.
218 219 220 221 222 |
# File 'lib/i2p/bob/client.rb', line 218 def getnick(nickname) send_command(:getnick, [:nickname] = nickname.to_s) read_response # "Nickname set to #{nickname}" self end |
#inhost(host) ⇒ void Also known as: inhost=
This method returns an undefined value.
Sets the inbound host name or IP address that the current tunnel listens on.
The default for new tunnels is ‘inhost(“localhost”)`.
290 291 292 293 294 |
# File 'lib/i2p/bob/client.rb', line 290 def inhost(host) send_command(:inhost, [:inhost] = host.to_s) read_response # "inhost set" self end |
#inport(port) ⇒ void Also known as: inport=
This method returns an undefined value.
Sets the inbound port number that the current tunnel listens on.
306 307 308 309 310 |
# File 'lib/i2p/bob/client.rb', line 306 def inport(port) send_command(:inport, [:inport] = port.to_i) read_response # "inbound port set" self end |
#newkeys ⇒ Destination
Generates a new keypair for the current tunnel.
231 232 233 234 |
# File 'lib/i2p/bob/client.rb', line 231 def newkeys send_command(:newkeys) Destination.parse(read_response) end |
#option(key, value) ⇒ void
This method returns an undefined value.
Sets an I2P Control Protocol (I2CP) option for the current tunnel.
386 387 388 389 390 |
# File 'lib/i2p/bob/client.rb', line 386 def option(key, value) send_command(:option, [key, value].join('=')) read_response # "#{key} set to #{value}" self end |
#outhost(host) ⇒ void Also known as: outhost=
This method returns an undefined value.
Sets the outbound host name or IP address that the current tunnel connects to.
The default for new tunnels is ‘outhost(“localhost”)`.
325 326 327 328 329 |
# File 'lib/i2p/bob/client.rb', line 325 def outhost(host) send_command(:outhost, [:outhost] = host.to_s) read_response # "outhost set" self end |
#outport(port) ⇒ void Also known as: outport=
This method returns an undefined value.
Sets the outbound port number that the current tunnel connects to.
341 342 343 344 345 |
# File 'lib/i2p/bob/client.rb', line 341 def outport(port) send_command(:outport, [:output] = port.to_i) read_response # "outbound port set" self end |
#quiet(value = true) ⇒ void Also known as: quiet=
This method returns an undefined value.
Toggles whether to send the incoming destination key to listening sockets.
This only applies to outbound tunnels and has no effect on inbound tunnels.
The default for new tunnels is ‘quiet(false)`.
369 370 371 372 373 |
# File 'lib/i2p/bob/client.rb', line 369 def quiet(value = true) send_command(:quiet, [:quiet] = value.to_s) read_response # "Quiet set" self end |
#quit ⇒ void Also known as: quit!
This method returns an undefined value.
Closes the connection to the BOB bridge cleanly.
173 174 175 176 177 |
# File 'lib/i2p/bob/client.rb', line 173 def quit send_command(:quit) read_response # "Bye!" disconnect end |
#setkeys(key_pair) ⇒ void Also known as: keys=
This method returns an undefined value.
Sets the key pair for the current tunnel.
271 272 273 274 275 |
# File 'lib/i2p/bob/client.rb', line 271 def setkeys(key_pair) send_command(:setkeys, [:keys] = key_pair.respond_to?(:to_base64) ? key_pair.to_base64 : key_pair.to_s) read_response # the Base64-encoded destination self end |
#setnick(nickname) ⇒ void Also known as: nickname=
This method returns an undefined value.
Creates a new tunnel with the given nickname.
203 204 205 206 207 |
# File 'lib/i2p/bob/client.rb', line 203 def setnick(nickname) send_command(:setnick, [:nickname] = nickname.to_s) read_response # "Nickname set to #{nickname}" self end |
#start ⇒ void Also known as: start!
This method returns an undefined value.
Starts and activates the current tunnel.
401 402 403 404 405 |
# File 'lib/i2p/bob/client.rb', line 401 def start send_command(:start) read_response # "tunnel starting" self end |
#stop ⇒ void Also known as: stop!
This method returns an undefined value.
Stops and inactivates the current tunnel.
417 418 419 420 421 |
# File 'lib/i2p/bob/client.rb', line 417 def stop send_command(:stop) read_response # "tunnel stopping" self end |
#verify(data) ⇒ Boolean
Verifies a Base64-formatted key pair or destination, returning ‘true` for valid input.
190 191 192 193 |
# File 'lib/i2p/bob/client.rb', line 190 def verify(data) send_command(:verify, data.respond_to?(:to_base64) ? data.to_base64 : data.to_s) read_response rescue false end |