first attempt at a working surf bar proxy

This commit is contained in:
Kyle Drake 2015-03-25 12:54:44 -07:00
parent 5640f02ee8
commit a2ac84dc42
5 changed files with 66 additions and 6 deletions

View file

@ -25,6 +25,7 @@ gem 'filesize'
gem 'thread'
gem 'scrypt'
gem 'rack-cache'
gem 'rest-client'
platform :mri, :rbx do
gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic

View file

@ -8,7 +8,7 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.1)
tzinfo (~> 1.1)
addressable (2.3.6)
addressable (2.3.7)
ago (0.1.5)
ansi (1.4.3)
autoparse (0.3.3)
@ -49,6 +49,8 @@ GEM
uuidtools (~> 2.1.1)
debugger-linecache (1.2.0)
docile (1.1.3)
domain_name (0.5.23)
unf (>= 0.0.5, < 1.0.0)
erubis (2.7.0)
extlib (0.9.16)
fabrication (2.11.0)
@ -74,6 +76,8 @@ GEM
uuidtools (>= 2.1.0)
hashie (2.0.5)
hiredis (0.5.0)
http-cookie (1.0.2)
domain_name (~> 0.5)
i18n (0.6.9)
jimson-temp (0.9.5)
blankslate (>= 3.1.2)
@ -105,7 +109,7 @@ GEM
metaclass (~> 0.0.1)
multi_json (1.10.1)
multipart-post (2.0.0)
netrc (0.7.7)
netrc (0.10.3)
nokogiri (1.6.3.1)
mini_portile (= 0.6.0)
pg (0.17.1)
@ -149,7 +153,8 @@ GEM
redis (3.0.7)
redis-namespace (1.4.1)
redis (~> 3.0.4)
rest-client (1.7.2)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
retriable (1.4.1)
@ -209,6 +214,9 @@ GEM
polyglot (>= 0.3.1)
tzinfo (1.2.2)
thread_safe (~> 0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.6)
unicorn (4.8.2)
kgio (~> 2.6)
rack
@ -257,6 +265,7 @@ DEPENDENCIES
rainbows
rake
redis
rest-client
rmagick
ruby-debug
sass

View file

@ -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

View file

@ -10,6 +10,8 @@ development:
stripe_api_key: "ENTER KEY HERE"
ip_hash_salt: "400$8$1$fc21863da5d531c1"
proxy_pass: 'somethinglongandrandom'
email_unsubscribe_token: 'somethingrandom'
surf_proxy_uri: 'http://127.0.0.1:9292'
test:
database: 'postgres://neocities@localhost/neocities_test'
database_pool: 1
@ -22,3 +24,5 @@ test:
stripe_api_key: "ENTER KEY HERE"
ip_hash_salt: "400$8$1$fc21863da5d531c1"
proxy_pass: 'somethinglongandrandom'
email_unsubscribe_token: 'somethingrandom'
surf_proxy_uri: 'http://127.0.0.1:9292'

View file

@ -989,9 +989,13 @@ class Site < Sequel::Model
!domain.empty? ? domain : "#{username}.neocities.org"
end
def uri
def default_schema
# Switch-over for when SSL defaulting is ready
"http://#{host}"
'http'
end
def uri
"#{default_schema}://#{host}"
end
def title