Created
September 6, 2019 12:26
-
-
Save KINGSABRI/7d4bdc7c4762b6d8c95283024e7830cd to your computer and use it in GitHub Desktop.
A simple HTTP Server in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Author: Sabri (@KINGSABRI) | |
# | |
require 'webrick' | |
require 'webrick/https' | |
require 'optparse' | |
options = { | |
ip: '0.0.0.0', | |
port: 8080, | |
log: File::NULL, | |
https: false, | |
rlookup: true | |
} | |
opts = OptionParser.new do |opts| | |
opts.banner = "Usage: #{__FILE__} [options]" | |
opts.on("-i", "--ip [IP_ADDR]", "IP address to bind to.", " (default: #{options[:ip]})") {|v| options[:ip] = v} | |
opts.on("-p", "--port [NUM]", "Port number to listen on.", Integer, " (default: #{options[:port]})") {|v| options[:port] = v} | |
opts.on("-f", "--file [PATH]", "Root File or directory to share.") {|v| options[:root] = v} | |
opts.on("-l", "--log [LOG_FILE]", "Log server logs to a file.", " (default: #{options[:log]})") {|v| options[:log] = v} | |
opts.on("-s", "--https", "Enable HTTPs instead of HTTP.", " (default: #{options[:https]})") {|v| options[:https] = v} | |
opts.on("-r", "--reverse-lookup", "Enable reverse lookup.", " (default: false)") {|v| options[:rlookup] = v} | |
opts.on('-h', '--help', 'Display this screen.') do | |
puts opts | |
exit! | |
end | |
opts.on_tail "Examples:" | |
opts.on_tail " #{__FILE__} -f /var/www/ -p 8080 --https -l /var/log/httpd.log" | |
end | |
opts.parse!(ARGV) | |
config = {:ServerName => 'BB'} | |
if options[:ip] | |
config[:BindAddress] = options[:ip] | |
end | |
if options[:port] | |
config[:Port] = options[:port] | |
end | |
if options[:root] | |
config[:DocumentRoot] = options[:root] | |
else | |
puts "[!] Error: Missing '--file' option." | |
puts opts | |
exit! | |
end | |
if options[:https] | |
cert = [ %w[CN localhost] ] | |
config[:SSLEnable] = true | |
config[:SSLCertName] = cert | |
end | |
if options[:log] | |
config[:AccessLog] = [] | |
config[:Logger] = WEBrick::Log.new(File.open(options[:log], 'w')) | |
end | |
if options[:rlookup] | |
config[:DoNotReverseLookup] = options[:rlookup] | |
end | |
begin | |
WEBrick::HTTPServer.new(config).start | |
rescue Exception => e | |
puts e | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment