mirror of
https://github.com/neocities/neocities.git
synced 2025-04-25 09:42:36 +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
1
Gemfile
1
Gemfile
|
@ -25,6 +25,7 @@ gem 'filesize'
|
||||||
gem 'thread'
|
gem 'thread'
|
||||||
gem 'scrypt'
|
gem 'scrypt'
|
||||||
gem 'rack-cache'
|
gem 'rack-cache'
|
||||||
|
gem 'rest-client'
|
||||||
|
|
||||||
platform :mri, :rbx do
|
platform :mri, :rbx do
|
||||||
gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic
|
gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic
|
||||||
|
|
15
Gemfile.lock
15
Gemfile.lock
|
@ -8,7 +8,7 @@ GEM
|
||||||
minitest (~> 5.1)
|
minitest (~> 5.1)
|
||||||
thread_safe (~> 0.1)
|
thread_safe (~> 0.1)
|
||||||
tzinfo (~> 1.1)
|
tzinfo (~> 1.1)
|
||||||
addressable (2.3.6)
|
addressable (2.3.7)
|
||||||
ago (0.1.5)
|
ago (0.1.5)
|
||||||
ansi (1.4.3)
|
ansi (1.4.3)
|
||||||
autoparse (0.3.3)
|
autoparse (0.3.3)
|
||||||
|
@ -49,6 +49,8 @@ GEM
|
||||||
uuidtools (~> 2.1.1)
|
uuidtools (~> 2.1.1)
|
||||||
debugger-linecache (1.2.0)
|
debugger-linecache (1.2.0)
|
||||||
docile (1.1.3)
|
docile (1.1.3)
|
||||||
|
domain_name (0.5.23)
|
||||||
|
unf (>= 0.0.5, < 1.0.0)
|
||||||
erubis (2.7.0)
|
erubis (2.7.0)
|
||||||
extlib (0.9.16)
|
extlib (0.9.16)
|
||||||
fabrication (2.11.0)
|
fabrication (2.11.0)
|
||||||
|
@ -74,6 +76,8 @@ GEM
|
||||||
uuidtools (>= 2.1.0)
|
uuidtools (>= 2.1.0)
|
||||||
hashie (2.0.5)
|
hashie (2.0.5)
|
||||||
hiredis (0.5.0)
|
hiredis (0.5.0)
|
||||||
|
http-cookie (1.0.2)
|
||||||
|
domain_name (~> 0.5)
|
||||||
i18n (0.6.9)
|
i18n (0.6.9)
|
||||||
jimson-temp (0.9.5)
|
jimson-temp (0.9.5)
|
||||||
blankslate (>= 3.1.2)
|
blankslate (>= 3.1.2)
|
||||||
|
@ -105,7 +109,7 @@ GEM
|
||||||
metaclass (~> 0.0.1)
|
metaclass (~> 0.0.1)
|
||||||
multi_json (1.10.1)
|
multi_json (1.10.1)
|
||||||
multipart-post (2.0.0)
|
multipart-post (2.0.0)
|
||||||
netrc (0.7.7)
|
netrc (0.10.3)
|
||||||
nokogiri (1.6.3.1)
|
nokogiri (1.6.3.1)
|
||||||
mini_portile (= 0.6.0)
|
mini_portile (= 0.6.0)
|
||||||
pg (0.17.1)
|
pg (0.17.1)
|
||||||
|
@ -149,7 +153,8 @@ GEM
|
||||||
redis (3.0.7)
|
redis (3.0.7)
|
||||||
redis-namespace (1.4.1)
|
redis-namespace (1.4.1)
|
||||||
redis (~> 3.0.4)
|
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)
|
mime-types (>= 1.16, < 3.0)
|
||||||
netrc (~> 0.7)
|
netrc (~> 0.7)
|
||||||
retriable (1.4.1)
|
retriable (1.4.1)
|
||||||
|
@ -209,6 +214,9 @@ GEM
|
||||||
polyglot (>= 0.3.1)
|
polyglot (>= 0.3.1)
|
||||||
tzinfo (1.2.2)
|
tzinfo (1.2.2)
|
||||||
thread_safe (~> 0.1)
|
thread_safe (~> 0.1)
|
||||||
|
unf (0.1.4)
|
||||||
|
unf_ext
|
||||||
|
unf_ext (0.0.6)
|
||||||
unicorn (4.8.2)
|
unicorn (4.8.2)
|
||||||
kgio (~> 2.6)
|
kgio (~> 2.6)
|
||||||
rack
|
rack
|
||||||
|
@ -257,6 +265,7 @@ DEPENDENCIES
|
||||||
rainbows
|
rainbows
|
||||||
rake
|
rake
|
||||||
redis
|
redis
|
||||||
|
rest-client
|
||||||
rmagick
|
rmagick
|
||||||
ruby-debug
|
ruby-debug
|
||||||
sass
|
sass
|
||||||
|
|
42
app/surf.rb
42
app/surf.rb
|
@ -15,3 +15,45 @@ get '/surf/:username' do |username|
|
||||||
not_found if @site.nil?
|
not_found if @site.nil?
|
||||||
erb :'surf', layout: false
|
erb :'surf', layout: false
|
||||||
end
|
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
|
||||||
|
|
|
@ -10,6 +10,8 @@ development:
|
||||||
stripe_api_key: "ENTER KEY HERE"
|
stripe_api_key: "ENTER KEY HERE"
|
||||||
ip_hash_salt: "400$8$1$fc21863da5d531c1"
|
ip_hash_salt: "400$8$1$fc21863da5d531c1"
|
||||||
proxy_pass: 'somethinglongandrandom'
|
proxy_pass: 'somethinglongandrandom'
|
||||||
|
email_unsubscribe_token: 'somethingrandom'
|
||||||
|
surf_proxy_uri: 'http://127.0.0.1:9292'
|
||||||
test:
|
test:
|
||||||
database: 'postgres://neocities@localhost/neocities_test'
|
database: 'postgres://neocities@localhost/neocities_test'
|
||||||
database_pool: 1
|
database_pool: 1
|
||||||
|
@ -22,3 +24,5 @@ test:
|
||||||
stripe_api_key: "ENTER KEY HERE"
|
stripe_api_key: "ENTER KEY HERE"
|
||||||
ip_hash_salt: "400$8$1$fc21863da5d531c1"
|
ip_hash_salt: "400$8$1$fc21863da5d531c1"
|
||||||
proxy_pass: 'somethinglongandrandom'
|
proxy_pass: 'somethinglongandrandom'
|
||||||
|
email_unsubscribe_token: 'somethingrandom'
|
||||||
|
surf_proxy_uri: 'http://127.0.0.1:9292'
|
||||||
|
|
|
@ -989,9 +989,13 @@ class Site < Sequel::Model
|
||||||
!domain.empty? ? domain : "#{username}.neocities.org"
|
!domain.empty? ? domain : "#{username}.neocities.org"
|
||||||
end
|
end
|
||||||
|
|
||||||
def uri
|
def default_schema
|
||||||
# Switch-over for when SSL defaulting is ready
|
# Switch-over for when SSL defaulting is ready
|
||||||
"http://#{host}"
|
'http'
|
||||||
|
end
|
||||||
|
|
||||||
|
def uri
|
||||||
|
"#{default_schema}://#{host}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def title
|
def title
|
||||||
|
|
Loading…
Add table
Reference in a new issue