Class: BotNetwork
- Inherits:
-
Object
- Object
- BotNetwork
- Defined in:
- lib/BotnetV2/Network.rb
Overview
A worker. Dial home to receive ask for tasks.
Instance Attribute Summary collapse
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #accept_clients(on_connect_handler, server) ⇒ Object
- #close(socket = @socket) ⇒ Object
- #connect(host, port, cert_file = File.open('cert.pem')) ⇒ Object
- #connect_no_ssl(host, port) ⇒ Object
-
#initialize ⇒ BotNetwork
constructor
Initialize the worker.
- #listen(on_connect_handler, port, cert_pem = File.open('cert.pem'), private_pem = File.open('private.pem')) ⇒ Object
- #listen_no_ssl(on_connect_handler, port) ⇒ Object
- #read(socket = @socket) ⇒ Object
- #send(message, socket = @socket) ⇒ Object
Constructor Details
#initialize ⇒ BotNetwork
Initialize the worker.
49 50 |
# File 'lib/BotnetV2/Network.rb', line 49 def initialize end |
Instance Attribute Details
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
46 47 48 |
# File 'lib/BotnetV2/Network.rb', line 46 def socket @socket end |
Instance Method Details
#accept_clients(on_connect_handler, server) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/BotnetV2/Network.rb', line 94 def accept_clients(on_connect_handler, server) loop do Thread.start(server.accept) do |socket| puts '[' + Time.new.strftime('%Y-%m-%d %H:%M:%S') + '] ' + 'Client connected.!' begin on_connect_handler.call(BotConnection.new socket, self) rescue => e puts e.backtrace unless socket.closed? disconnect socket end end puts '[' + Time.new.strftime('%Y-%m-%d %H:%M:%S') + '] ' + 'Client disconnected!' end end end |
#close(socket = @socket) ⇒ Object
122 123 124 |
# File 'lib/BotnetV2/Network.rb', line 122 def close(socket = @socket) socket.close unless socket.closed? end |
#connect(host, port, cert_file = File.open('cert.pem')) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/BotnetV2/Network.rb', line 52 def connect(host, port, cert_file = File.open('cert.pem')) socket = TCPSocket.open host, port socket.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 expectedCert = OpenSSL::X509::Certificate.new(cert_file) ssl = OpenSSL::SSL::SSLSocket.new(socket) ssl.sync_close = true ssl.connect if ssl.peer_cert.to_s != expectedCert.to_s stderrr.puts 'Unexpected certificate' return nil end @socket = ssl end |
#connect_no_ssl(host, port) ⇒ Object
66 67 68 69 70 |
# File 'lib/BotnetV2/Network.rb', line 66 def connect_no_ssl(host, port) socket = TCPSocket.open host, port socket.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 @socket = socket end |
#listen(on_connect_handler, port, cert_pem = File.open('cert.pem'), private_pem = File.open('private.pem')) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/BotnetV2/Network.rb', line 72 def listen(on_connect_handler, port, cert_pem = File.open('cert.pem'), private_pem = File.open('private.pem')) server = TCPServer.open(port) sslContext = OpenSSL::SSL::SSLContext.new sslContext.cert = OpenSSL::X509::Certificate.new(cert_pem) sslContext.key = OpenSSL::PKey::RSA.new(private_pem) sslServer = OpenSSL::SSL::SSLServer.new(server, sslContext) thread = Thread.start do puts '[' + Time.new.strftime('%Y-%m-%d %H:%M:%S') + '] ' + "Listening secured on port #{port}" accept_clients on_connect_handler, sslServer end thread end |
#listen_no_ssl(on_connect_handler, port) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/BotnetV2/Network.rb', line 85 def listen_no_ssl(on_connect_handler, port) server = TCPServer.open(port) thread = Thread.start do puts '[' + Time.new.strftime('%Y-%m-%d %H:%M:%S') + '] ' + "Listening unsecured on port #{port}" accept_clients on_connect_handler, server end thread end |
#read(socket = @socket) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/BotnetV2/Network.rb', line 115 def read(socket = @socket) res = socket.gets res = res.chomp if res res = JSON.parse(res) if res res end |
#send(message, socket = @socket) ⇒ Object
111 112 113 |
# File 'lib/BotnetV2/Network.rb', line 111 def send(, socket = @socket) socket.puts(JSON.generate()) unless socket.closed? end |