sysops.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. require 'base64'
  2. require 'zlib'
  3. require 'rubygems/package'
  4. get '/sysops/proxy/map.txt' do
  5. require_proxy_auth
  6. domains = ''
  7. Site.exclude(domain: nil).
  8. exclude(domain: '').
  9. select(:username,:domain).
  10. all.
  11. collect do |s|
  12. domains << "#{s.domain} #{s.username};\n"
  13. end
  14. content_type :text
  15. domains
  16. end
  17. get '/sysops/proxy/sslcerts.tar.gz' do
  18. require_proxy_auth
  19. sites = Site.ssl_sites
  20. nginx_config = ''
  21. tar = StringIO.new
  22. Gem::Package::TarWriter.new(tar) do |writer|
  23. writer.mkdir 'sslcerts', 0740
  24. writer.mkdir 'sslcerts/certs', 0740
  25. sites.each do |site|
  26. writer.add_file "sslcerts/certs/#{site.username}.key", 0640 do |f|
  27. f.write site.ssl_key
  28. end
  29. writer.add_file "sslcerts/certs/#{site.username}.crt", 0640 do |f|
  30. f.write site.ssl_cert
  31. end
  32. nginx_config << %{
  33. server {
  34. listen 443 ssl;
  35. server_name #{site.domain} *.#{site.domain};
  36. ssl_certificate sslsites/certs/#{site.username}.crt;
  37. ssl_certificate_key sslsites/certs/#{site.username}.key;
  38. location / {
  39. proxy_http_version 1.1;
  40. proxy_set_header Host #{site.username}.neocities.org;
  41. proxy_pass http://127.0.0.1$request_uri;
  42. }
  43. }
  44. }.unindent
  45. end
  46. writer.add_file "sslcerts/sslsites.conf", 0640 do |f|
  47. f.write nginx_config
  48. end
  49. end
  50. tar.rewind
  51. package = StringIO.new 'b'
  52. package.set_encoding 'binary'
  53. gzip = Zlib::GzipWriter.new package
  54. gzip.write tar.read
  55. tar.close
  56. gzip.finish
  57. package.rewind
  58. attachment
  59. package.read
  60. end
  61. class ProxyAccessViolation < StandardError; end
  62. def require_proxy_auth
  63. begin
  64. auth = request.env['HTTP_AUTHORIZATION']
  65. user, pass = Base64.decode64(auth.match(/Basic (.+)/)[1]).split(':')
  66. raise ProxyAccessViolation unless pass == $config['proxy_pass']
  67. rescue
  68. raise ProxyAccessViolation, "Violator: #{request.ip}" unless pass == $config['proxy_pass']
  69. end
  70. end