-
Notifications
You must be signed in to change notification settings - Fork 0
/
htaccess-generator.rb
executable file
·167 lines (144 loc) · 5.28 KB
/
htaccess-generator.rb
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env ruby
#
# See README.md for development, installation and use instructions!
#
require 'resolv'
require 'net/http'
require 'uri'
require 'optparse'
require 'etc'
require 'logger'
require 'fileutils'
require 'resolv'
# Template content written to top of .htaccess file
HTACCESS_TOP = <<-'htaccesstop'
ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 405 default
ErrorDocument 406 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 503 default
# HSTS is better than a mod_rewrite for https security
# See: https://secure.monkeytreehosting.com/index.php/knowledgebase/138/Enable-HSTS-in-cPanel.html
Header set Strict-Transport-Security "max-age=31536000" env=HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
# This is for VBSEO URL rewriting. It keeps thread links from the old VB4
# forum "alive" on the new Xenforo forum.
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule [^/]+/([\d]+)-.+-([\d]+).html showthread.php?t=$1&page=$2 [NC,L]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule [^/]+/([\d]+)-.+.html showthread.php?t=$1 [NC,L]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule [^/]+/.*?/([\d]+)-.+.html showthread.php?t=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
htaccesstop
# Template content written to bottom of .htaccess file
HTACCESS_BOT = <<-'htaccessbot'
htaccessbot
def open_url(url)
Net::HTTP.get(URI.parse(url))
end
options = {
path: '.',
user: 'xenforo',
group: 'xenforo',
site: 'forum.fractalaudio.com',
ports: %w(80 443),
torurl: 'https://check.torproject.org/torbulkexitlist',
debug: false
}
parser = OptionParser.new do |opts|
opts.banner = "Usage: htaccess-generator.rb [options]"
opts.on('-p', '--path path', 'Path') do |path|
options[:path] = path;
end
opts.on('-u', '--user user', 'User') do |user|
options[:user] = user;
end
opts.on('-g', '--group group', 'Group') do |group|
options[:group] = group;
end
opts.on('-s', '--site sitename', 'Site name') do |site|
options[:site] = site
end
opts.on('-t', '--tor-url tor-url', 'Tor URL') do |torurl|
options[:torurl] = torurl
end
opts.on('-d', '--debug', 'Debug messages') do |debug|
options[:debug] = true
end
opts.on('-h', '--help', 'Displays Help') do
puts opts
exit
end
end
parser.parse!
logger = Logger.new(STDOUT)
logger.level = Logger::INFO
logger.level = Logger::DEBUG if options[:debug]
IP = Resolv.getaddress(options[:site])
TMP_FILE = File.join(options[:path], '.htaccess.tmp')
REL_FILE = File.join(options[:path], '.htaccess')
logger.debug("Deleting #{TMP_FILE}") if File.exist?(TMP_FILE)
File.delete(TMP_FILE) if File.exist?(TMP_FILE)
logger.debug("Writing #{TMP_FILE}")
File.open("#{TMP_FILE}", 'w') do |htfile|
htfile.write("# This file was automatically generated on #{Time.now()} by the\n")
htfile.write("# #{File.expand_path(__FILE__)} script.\n")
htfile.write("# Any edits made to this file will be automatically overwritten!\n")
htfile.write(HTACCESS_TOP)
htfile.write("<RequireAll>\n")
htfile.write("\tRequire all granted\n")
options[:ports].each do |port|
url = "#{options[:torurl]}?ip=#{IP}&port=#{port}"
logger.debug("Fetching contents from: #{url}")
uri = URI.parse(url)
response = Net::HTTP.get_response(uri)
unless response.code == '200'
logger.error("Got response #{response.code} -- skipping #{url}\n#{response.body}")
next
end
response.body.each_line do |line|
line = line.strip
if line =~ Resolv::IPv4::Regex
htfile.write("\tRequire not ip #{line}\n")
else
htfile.write("\t# #{line}\n")
end
end
end
htfile.write("</RequireAll>\n")
htfile.write("\n")
htfile.write(HTACCESS_BOT)
end
logger.debug("Done writing #{TMP_FILE}")
FileUtils.chown(options[:user], options[:group], TMP_FILE)
File.chmod(0644, TMP_FILE)
logger.debug("Renaming #{TMP_FILE} -> #{REL_FILE}")
File.rename(TMP_FILE, REL_FILE)
logger.info("New #{REL_FILE} written at #{Time.now()}")
exit(0)