Class: BetterCap::Network::Servers::HTTPD

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/network/servers/httpd.rb

Overview

Simple HTTP server class used to serve static assets when needed.

Instance Method Summary collapse

Constructor Details

#initialize(port = 8081, path = './') ⇒ HTTPD

Initialize the HTTP server with the specified tcp port using path as the document root.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bettercap/network/servers/httpd.rb', line 22

def initialize( port = 8081, path = './' )
  @port = port
  @path = path
  begin
    @server = WEBrick::HTTPServer.new(
      Port: @port,
      DocumentRoot: @path,
      Logger: WEBrick::Log.new("/dev/null"),
      AccessLog: []
    )
  rescue Errno::EADDRINUSE
    raise BetterCap::Error, "[HTTPD] It looks like there's another process listening on port #{@port}, please chose a different port."
  end
end

Instance Method Details

#startObject

Start the server.



38
39
40
41
42
43
# File 'lib/bettercap/network/servers/httpd.rb', line 38

def start
  Logger.info "[#{'HTTPD'.green}] Starting on port #{@port} and path #{@path} ..."
  @thread = Thread.new {
    @server.start
  }
end

#stopObject

Stop the server.



46
47
48
49
50
51
# File 'lib/bettercap/network/servers/httpd.rb', line 46

def stop
  Logger.info 'Stopping HTTPD ...'

  @server.stop
  @thread.join
end