punycode support for domains, validate for registered domains

This commit is contained in:
Kyle Drake 2016-02-17 23:15:12 -08:00
parent 19638ff712
commit fda95b7f07
6 changed files with 22 additions and 7 deletions

View file

@ -36,6 +36,7 @@ gem 'base32'
gem 'coveralls', require: false gem 'coveralls', require: false
gem 'sanitize' gem 'sanitize'
gem 'will_paginate' gem 'will_paginate'
gem 'simpleidn'
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

View file

@ -175,6 +175,7 @@ GEM
json (~> 1.8) json (~> 1.8)
simplecov-html (~> 0.10.0) simplecov-html (~> 0.10.0)
simplecov-html (0.10.0) simplecov-html (0.10.0)
simpleidn (0.0.6)
sinatra (1.4.6) sinatra (1.4.6)
rack (~> 1.4) rack (~> 1.4)
rack-protection (~> 1.4) rack-protection (~> 1.4)
@ -277,6 +278,7 @@ DEPENDENCIES
shotgun shotgun
sidekiq sidekiq
simplecov simplecov
simpleidn
sinatra sinatra
sinatra-flash sinatra-flash
sinatra-xsendfile sinatra-xsendfile

View file

@ -69,7 +69,7 @@ desc 'Compile domain map for nginx'
task :compile_domain_map => [:environment] do task :compile_domain_map => [:environment] do
File.open('./files/map.txt', 'w') do |file| File.open('./files/map.txt', 'w') do |file|
Site.exclude(domain: nil).exclude(domain: '').select(:username,:domain).all.collect do |site| Site.exclude(domain: nil).exclude(domain: '').select(:username,:domain).all.collect do |site|
file.write ".#{site.domain} #{site.username};\n" file.write ".#{site.values[:domain]} #{site.username};\n"
end end
end end
end end

View file

@ -102,7 +102,6 @@ end
get '/api/info' do get '/api/info' do
if params[:sitename] if params[:sitename]
site = Site[username: params[:sitename]] site = Site[username: params[:sitename]]
api_error 400, 'site_not_found', "could not find site #{params[:sitename]}" if site.nil? || site.is_banned api_error 400, 'site_not_found', "could not find site #{params[:sitename]}" if site.nil? || site.is_banned
api_success api_info_for(site) api_success api_info_for(site)
else else

View file

@ -225,6 +225,16 @@ post '/settings/:username/custom_domain' do
@site.domain = params[:domain] @site.domain = params[:domain]
begin
Socket.gethostbyname @site.values[:domain]
rescue SocketError => e
if e.message =~ /name or service not known/i
flash[:error] = 'Domain needs to be valid and already registered.'
redirect "/settings/#{@site.username}#custom_domain"
end
raise e
end
if @site.valid? if @site.valid?
@site.save_changes @site.save_changes
flash[:success] = 'The domain has been successfully updated.' flash[:success] = 'The domain has been successfully updated.'

View file

@ -812,6 +812,14 @@ class Site < Sequel::Model
# super # super
# end # end
def domain=(domain)
super SimpleIDN.to_ascii(domain)
end
def domain
SimpleIDN.to_unicode values[:domain]
end
def validate def validate
super super
@ -864,15 +872,10 @@ class Site < Sequel::Model
end end
if !values[:domain].nil? && !values[:domain].empty? if !values[:domain].nil? && !values[:domain].empty?
if values[:domain] =~ /neocities\.org/ || values[:domain] =~ /neocitiesops\.net/ if values[:domain] =~ /neocities\.org/ || values[:domain] =~ /neocitiesops\.net/
errors.add :domain, "Domain is already being used.. by Neocities." errors.add :domain, "Domain is already being used.. by Neocities."
end end
if !(values[:domain] =~ /^[a-zA-Z0-9.-]+\.[a-zA-Z0-9]+$/i) || values[:domain].length > 90
errors.add :domain, "Domain provided is not valid. Must take the form of domain.com"
end
site = Site[domain: values[:domain]] site = Site[domain: values[:domain]]
if !site.nil? && site.id != self.id if !site.nil? && site.id != self.id
errors.add :domain, "Domain provided is already being used by another site, please choose another." errors.add :domain, "Domain provided is already being used by another site, please choose another."