-
Notifications
You must be signed in to change notification settings - Fork 338
/
djsd
executable file
·112 lines (94 loc) · 3.57 KB
/
djsd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env ruby
if (%w( -h --help -help help ) & ARGV).length > 0
puts "usage: djsd [-hv]"
puts "starts dotjs server in the foreground. kill with ^C"
exit
end
if ARGV.include?('-v')
puts "djsd 2.0"
exit
end
require 'webrick'
require 'webrick/https'
dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do
def do_GET(request, response)
body = build_body(request.path)
response.status = body.empty? ? 204 : 200
if origin = detect_origin(request)
response['Access-Control-Allow-Origin'] = origin
end
response['Content-Type'] = 'text/javascript'
response.body = body
end
def build_body(path)
files = [File.expand_path("default.js")]
paths = path.gsub('/','').split('.')
until paths.empty?
file = File.expand_path(paths.join('.'))
files << file if File.file?(file)
paths.shift
end
body = "// dotjs is working! //\n"
files.each do |file|
body << File.read(file) + "\n" if File.file?(file)
end
body
end
def detect_origin(req)
path = req.path
origin = req.header['origin']
search = path.gsub('/','').gsub(/\.js$/,'') + '$'
if origin.length == 1 && path.length != 1 && origin[0].match(search)
origin[0]
end
end
end
ssl_info = DATA.read
ssl_cert = ssl_info.scan(/(-----BEGIN CERTIFICATE-----.+?-----END CERTIFICATE-----)/m)[0][0]
ssl_key = ssl_info.scan(/(-----BEGIN RSA PRIVATE KEY-----.+?-----END RSA PRIVATE KEY-----)/m)[0][0]
server_options = {
:BindAddress => "127.0.0.1",
:Port => 3131,
:AccessLog => [],
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLPrivateKey => OpenSSL::PKey::RSA.new(ssl_key),
:SSLCertificate => OpenSSL::X509::Certificate.new(ssl_cert),
:SSLCertName => [["CN", WEBrick::Utils::getservername]],
}
server = WEBrick::HTTPServer.new(server_options)
server.mount('/', dotjs)
%w( INT TERM ).each do |sig|
trap(sig) { server.shutdown }
end
server.start
__END__
-----BEGIN CERTIFICATE-----
MIICHTCCAYYCCQClZE2IvNWbtDANBgkqhkiG9w0BAQUFADBTMQswCQYDVQQGEwJV
UzETMBEGA1UECBMKQ2FsaWZvcm5pYTELMAkGA1UEBxMCTEExDjAMBgNVBAoTBWRv
dGpzMRIwEAYDVQQDEwlsb2NhbGhvc3QwHhcNMTMwMjIwMjMzNzUzWhcNMjIxMTIw
MjMzNzUzWjBTMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTELMAkG
A1UEBxMCTEExDjAMBgNVBAoTBWRvdGpzMRIwEAYDVQQDEwlsb2NhbGhvc3QwgZ8w
DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMNgNijoFmD5TX7NMd2pGEeEmwRifWRc
45jVS1a5kUncfRfgr4ehygPQDS2XrUkd+OYneFIXOcANW9WAWAlfeLs8DiSgs+9m
tuVjZ58RAsRXkW7H3vqQv5sAxmmwwVGN9WfKW+II/xLhpMtVGQH+MOucGbssODzk
0vwXEGSeEqYdAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAgCW2RBZgDMikQenSF3sz
u7KDe8+t8qnEFvrwCEpLUzvedSZxkaKzHrrCnIYlDnXRZBveKngWoejGzqtyIXup
YKzBZaZWH8cV72RdDwgM1owWi3KZBKpxfphYkWSRRx59djHZY/Yjudnb3oT/3c8/
NHsFbLbrZaGriLshIwrjEGs=
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDDYDYo6BZg+U1+zTHdqRhHhJsEYn1kXOOY1UtWuZFJ3H0X4K+H
ocoD0A0tl61JHfjmJ3hSFznADVvVgFgJX3i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