diff --git a/app.rb b/app.rb index ffd2ddab..3c06d906 100644 --- a/app.rb +++ b/app.rb @@ -21,7 +21,13 @@ helpers do end end +set :protection, :frame_options => "ALLOW-FROM #{$config['surf_iframe_source']}" + before do + if request.host.match /\w+\.#{Addressable::URI.parse($config['surf_proxy_uri']).host}/i + surf_proxy + end + if request.path.match /^\/api\//i @api = true content_type :json diff --git a/app/surf.rb b/app/surf.rb index 7be15690..043c0077 100644 --- a/app/surf.rb +++ b/app/surf.rb @@ -16,6 +16,28 @@ get '/surf/:username' do |username| erb :'surf', layout: false end +def surf_proxy + username = request.host.match /^(\w+)/i + resp = RestClient.get "http://#{username}.neocities.org#{request.path}" + content_type resp.headers[:content_type] + + if content_type.match(/^text\/html/) + body = resp.body+erb(:'surf/_surf_iframe_injection', layout: false) + else + body = resp.body + end + + halt body +end + +def surf_proxy_uri(username) + $surf_proxy_uri ||= Addressable::URI.parse $config['surf_proxy_uri'] + new_uri = $surf_proxy_uri.dup + new_uri.host = "#{username}.#{new_uri.host}" + new_uri.to_s +end + +=begin get %r{\/surf\/proxy\/([\w-]+)\/(.+)|\/surf\/proxy\/([\w-]+)\/?} do captures = params[:captures].compact username = captures.first @@ -39,8 +61,10 @@ get %r{\/surf\/proxy\/([\w-]+)\/(.+)|\/surf\/proxy\/([\w-]+)\/?} do site_body.gsub(/(?\b\w+\b)\s*=\s*(?"[^"]*"|'[^']*'|[^"'<>\s]+)/i) do |ele| attributes.each do |attr| - if ele.match attr - uri = ele.match(/\"(.+)\"|\'(.+)\'/).captures.first + if ele.match /#{attr} ?=/ + ele_match = ele.match(/\"(.+)\"|\'(.+)\'/) + next if ele_match.nil? || ele_match.captures.nil? + uri = ele_match.captures.first new_ele = nil @@ -61,3 +85,4 @@ get %r{\/surf\/proxy\/([\w-]+)\/(.+)|\/surf\/proxy\/([\w-]+)\/?} do new_site_body end +=end diff --git a/config.ru b/config.ru index 9b9aaf79..f664a028 100644 --- a/config.ru +++ b/config.ru @@ -77,4 +77,4 @@ map '/sidekiq' do end run Sidekiq::Web -end \ No newline at end of file +end diff --git a/views/browse.erb b/views/browse.erb index 564bb381..4ed9e979 100644 --- a/views/browse.erb +++ b/views/browse.erb @@ -63,13 +63,13 @@ - - + diff --git a/views/surf/_surf_iframe_injection.erb b/views/surf/_surf_iframe_injection.erb new file mode 100644 index 00000000..4b7d0d0e --- /dev/null +++ b/views/surf/_surf_iframe_injection.erb @@ -0,0 +1,10 @@ +