From a2ac84dc429b31133205ec721cfd9ee1f5edcfba Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Wed, 25 Mar 2015 12:54:44 -0700 Subject: [PATCH] first attempt at a working surf bar proxy --- Gemfile | 1 + Gemfile.lock | 15 ++++++++++++--- app/surf.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ config.yml.template | 6 +++++- models/site.rb | 8 ++++++-- 5 files changed, 66 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index 69ed4826..159a70f6 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index fd90fb5b..e74167f8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app/surf.rb b/app/surf.rb index f4639c5c..d8813359 100644 --- a/app/surf.rb +++ b/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(/(?\b\w+\b)\s*=\s*(?"[^"]*"|'[^']*'|[^"'<>\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 diff --git a/config.yml.template b/config.yml.template index 9eaa29a9..dde1db11 100644 --- a/config.yml.template +++ b/config.yml.template @@ -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 @@ -21,4 +23,6 @@ test: stripe_publishable_key: "ENTER KEY HERE" stripe_api_key: "ENTER KEY HERE" ip_hash_salt: "400$8$1$fc21863da5d531c1" - proxy_pass: 'somethinglongandrandom' \ No newline at end of file + proxy_pass: 'somethinglongandrandom' + email_unsubscribe_token: 'somethingrandom' + surf_proxy_uri: 'http://127.0.0.1:9292' diff --git a/models/site.rb b/models/site.rb index 95c9ccb8..39af6d8c 100644 --- a/models/site.rb +++ b/models/site.rb @@ -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