sslserve 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/sslserve +53 -4
  2. metadata +2 -2
data/bin/sslserve CHANGED
@@ -3,21 +3,24 @@
3
3
  require 'webrick'
4
4
  require 'webrick/https'
5
5
  require 'openssl'
6
+ require 'base64'
6
7
  require 'optparse'
7
8
 
8
- version = "0.0.2"
9
+ version = "0.1.0"
9
10
  options = {
10
11
  :dir => Dir.pwd,
11
12
  :host => '127.0.0.1',
12
13
  :port => '3443',
13
14
  :name => "localhost",
14
- :expire => 1
15
+ :expire => 1,
16
+ :pass => nil,
17
+ :realm => "sslserve realm"
15
18
  }
16
19
 
17
20
  OptionParser.new do |opts|
18
21
  opts.banner = "Usage sslserve [options]"
19
22
 
20
- opts.on("-d", "--dir DIR", "Directory to serve files from. Defaults to pwd") do |d|
23
+ opts.on("-d", "--dir DIR", "Directory to serve files from. Defaults to cwd") do |d|
21
24
  options[:dir] = d
22
25
  end
23
26
 
@@ -37,6 +40,14 @@ OptionParser.new do |opts|
37
40
  options[:expire] = e
38
41
  end
39
42
 
43
+ opts.on("-a", "--auth PASSWORD", "PASSWORD for HTTP Basic Auth. Defaults to disabled. Username is the SHA1 certificate fingerprint") do |p|
44
+ options[:pass] = p
45
+ end
46
+
47
+ opts.on("-r", "--realm REALM", "HTTP Basic Auth REALM. Defaults to #{options[:realm]}") do |r|
48
+ options[:realm] = r
49
+ end
50
+
40
51
  opts.on("-h", "--help", "Show this message") do
41
52
  puts opts
42
53
  exit
@@ -53,16 +64,32 @@ key = OpenSSL::PKey::RSA.new 2048
53
64
 
54
65
  name = OpenSSL::X509::Name.parse "CN=#{options[:name]}"
55
66
 
67
+ expires = Time.now + (options[:expire] * 3600)
68
+
56
69
  cert = OpenSSL::X509::Certificate.new
57
70
  cert.version = 3
58
71
  cert.serial = 0
59
72
  cert.not_before = Time.now
60
- cert.not_after = Time.now + (options[:expire] * 3600)
73
+ cert.not_after = expires
61
74
  cert.public_key = key.public_key
62
75
  cert.subject = name
63
76
  cert.issuer = name
64
77
  cert.sign key, OpenSSL::Digest::SHA1.new
65
78
 
79
+ pem = Base64.decode64(cert.to_pem.split("\n")[1..-1].join(""))
80
+ sha1_fingerprint = OpenSSL::Digest::SHA1.digest(pem).unpack("H*").first.scan(/../).join(":").upcase
81
+ md5_fingerprint = OpenSSL::Digest::MD5.digest(pem).unpack("H*").first.scan(/../).join(":").upcase
82
+
83
+ if options[:pass]
84
+ authenticate = Proc.new do |req, res|
85
+ WEBrick::HTTPAuth.basic_auth(req, res, '') do |user, pass|
86
+ user and user.upcase.gsub(/[^0-9A-F]/,'') == md5_fingerprint.upcase.gsub(/[^0-9A-F]/,'') && pass == options[:pass]
87
+ end
88
+ end
89
+ else
90
+ authenticate = nil
91
+ end
92
+
66
93
  server = WEBrick::HTTPServer.new(
67
94
  :BindAddress => options[:host],
68
95
  :Port => options[:port],
@@ -73,5 +100,27 @@ server = WEBrick::HTTPServer.new(
73
100
  :SSLCertificate => cert,
74
101
  :SSLCertName => [[options[:name], WEBrick::Utils::getservername]],
75
102
  )
103
+
104
+ fh_options = {
105
+ :FancyIndexing => true,
106
+ }
107
+ fh_options[:HandlerCallback] = authenticate if authenticate
108
+
109
+ server.mount('/', WEBrick::HTTPServlet::FileHandler, options[:dir], fh_options)
76
110
  Signal.trap('INT') { server.shutdown }
111
+
112
+ puts "=============================================================================="
113
+ puts " SHA-1 Certificate fingerprint:"
114
+ puts " #{sha1_fingerprint}"
115
+ puts " MD5 Certificate fingerprint:"
116
+ puts " #{md5_fingerprint}"
117
+ puts "\n Certificate expires at #{expires}"
118
+ if authenticate
119
+ puts "\n Basic auth realm: '#{options[:realm]}'"
120
+ puts " Basic auth user: #{md5_fingerprint.upcase.gsub(/[^0-9A-F]/,'')}"
121
+ puts " Basic auth pass: '#{options[:pass]}'"
122
+ end
123
+ puts "\n Further information: https://github.com/zeroXten/sslserve"
124
+ puts "=============================================================================="
125
+
77
126
  server.start
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sslserve
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-14 00:00:00.000000000 Z
12
+ date: 2013-09-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Serve a local directory using this SSL webserver.
15
15
  email: fraser.scott@gmail.com