mirror of
https://github.com/neocities/neocities.git
synced 2025-08-06 01:24:56 +02:00
first attempt at a working surf bar proxy
This commit is contained in:
parent
5640f02ee8
commit
a2ac84dc42
5 changed files with 66 additions and 6 deletions
42
app/surf.rb
42
app/surf.rb
|
@ -15,3 +15,45 @@ get '/surf/:username' do |username|
|
|||
not_found if @site.nil?
|
||||
erb :'surf', layout: false
|
||||
end
|
||||
|
||||
get %r{\/surf\/proxy\/([\w-]+)\/(.+)|\/surf\/proxy\/([\w-]+)\/?} do
|
||||
captures = params[:captures].compact
|
||||
username = captures.first
|
||||
path = captures.length == 2 ? captures.last : ''
|
||||
|
||||
site = Site.where(username: username).select(:id, :username, :title, :domain).first
|
||||
not_found if site.nil?
|
||||
|
||||
resp = RestClient.get "http://#{site.username}.neocities.org/#{path}"
|
||||
|
||||
content_type resp.headers[:content_type]
|
||||
site_body = resp.body
|
||||
|
||||
unless path == '/' || path == '' || path.match(/\.html?$/i)
|
||||
return site_body
|
||||
end
|
||||
|
||||
attributes = ['src', 'href', 'background']
|
||||
|
||||
new_site_body = site_body.dup
|
||||
|
||||
site_body.gsub(/(?<name>\b\w+\b)\s*=\s*(?<value>"[^"]*"|'[^']*'|[^"'<>\s]+)/i) do |ele|
|
||||
attributes.each do |attr|
|
||||
if ele.match attr
|
||||
uri = ele.match(/\"(.+)\"|\'(.+)\'/).captures.first
|
||||
|
||||
new_ele = nil
|
||||
|
||||
if uri.match /^\//
|
||||
new_ele = ele.gsub(uri, "#{$config['surf_proxy_uri']}/surf/proxy/#{site.username}#{uri}")
|
||||
elsif !uri.match /^\w+:\/\//
|
||||
new_ele = ele.gsub(uri, "#{$config['surf_proxy_uri']}/surf/proxy/#{site.username}/#{uri}")
|
||||
end
|
||||
|
||||
new_site_body.gsub! ele, new_ele if new_ele
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
new_site_body
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue