I dare you to add comments to a ruby file.

This commit is contained in:
opi 2016-04-26 12:18:16 +02:00
parent 87acdfaf80
commit 44e231a0dd

View file

@ -6,10 +6,20 @@ require 'data_mapper'
require 'json' require 'json'
require 'base64' require 'base64'
######################
###  Configuration ###
######################
DataMapper.setup(:default, ENV['DATABASE_URL'] || "postgres://dynette:myPassword@localhost/dynette") DataMapper.setup(:default, ENV['DATABASE_URL'] || "postgres://dynette:myPassword@localhost/dynette")
DOMAINS = ["nohost.me", "noho.st"] DOMAINS = ["nohost.me", "noho.st"]
ALLOWED_IP = ["127.0.0.1"] ALLOWED_IP = ["127.0.0.1"]
###############
### Classes ###
###############
# Dynette Entry class
class Entry class Entry
include DataMapper::Resource include DataMapper::Resource
@ -22,6 +32,7 @@ class Entry
has n, :ips has n, :ips
end end
# IP class
class Ip class Ip
include DataMapper::Resource include DataMapper::Resource
@ -31,6 +42,7 @@ class Ip
belongs_to :entry belongs_to :entry
end end
# IP Log class
class Iplog class Iplog
include DataMapper::Resource include DataMapper::Resource
@ -38,17 +50,29 @@ class Iplog
property :visited_at, DateTime property :visited_at, DateTime
end end
# IP ban class
class Ipban class Ipban
include DataMapper::Resource include DataMapper::Resource
property :ip_addr, String, :key => true property :ip_addr, String, :key => true
end end
################
### Handlers ###
################
# 404 Error handler
not_found do not_found do
content_type :json content_type :json
halt 404, { :error => "Not found" }.to_json halt 404, { :error => "Not found" }.to_json
end end
##############
### Routes ###
##############
# Common tasks and settings for every route # Common tasks and settings for every route
before do before do
# Always return json # Always return json
@ -100,15 +124,18 @@ end
end end
end end
# Main page, return some basic text
get '/' do get '/' do
content_type 'text/html' content_type 'text/html'
"Wanna play the dynette ?" "Wanna play the dynette ?"
end end
# Get availables DynDNS domains
get '/domains' do get '/domains' do
DOMAINS.to_json DOMAINS.to_json
end end
# Check for sub-domain vailability
get '/test/:subdomain' do get '/test/:subdomain' do
if entry = Entry.first(:subdomain => params[:subdomain]) if entry = Entry.first(:subdomain => params[:subdomain])
halt 409, { :error => "Subdomain already taken: #{entry.subdomain}" }.to_json halt 409, { :error => "Subdomain already taken: #{entry.subdomain}" }.to_json
@ -117,7 +144,7 @@ get '/test/:subdomain' do
end end
end end
# Register a sub-domain
post '/key/:public_key' do post '/key/:public_key' do
params[:public_key] = Base64.decode64(params[:public_key].encode('ascii-8bit')) params[:public_key] = Base64.decode64(params[:public_key].encode('ascii-8bit'))
# Check params # Check params
@ -141,6 +168,7 @@ post '/key/:public_key' do
end end
end end
# Update a sub-domain
put '/key/:public_key' do put '/key/:public_key' do
params[:public_key] = Base64.decode64(params[:public_key].encode('ascii-8bit')) params[:public_key] = Base64.decode64(params[:public_key].encode('ascii-8bit'))
entry = Entry.first(:public_key => params[:public_key]) entry = Entry.first(:public_key => params[:public_key])
@ -155,6 +183,7 @@ put '/key/:public_key' do
end end
end end
# Delete a sub-domain from key
delete '/key/:public_key' do delete '/key/:public_key' do
unless ALLOWED_IP.include? request.ip unless ALLOWED_IP.include? request.ip
halt 403, { :error => "Access denied"}.to_json halt 403, { :error => "Access denied"}.to_json
@ -170,6 +199,7 @@ delete '/key/:public_key' do
end end
end end
# Delete a sub-domain
delete '/domains/:subdomain' do delete '/domains/:subdomain' do
unless ALLOWED_IP.include? request.ip unless ALLOWED_IP.include? request.ip
halt 403, { :error => "Access denied"}.to_json halt 403, { :error => "Access denied"}.to_json
@ -184,6 +214,7 @@ delete '/domains/:subdomain' do
end end
end end
# Get all registered sub-domains
get '/all' do get '/all' do
unless ALLOWED_IP.include? request.ip unless ALLOWED_IP.include? request.ip
halt 403, { :error => "Access denied"}.to_json halt 403, { :error => "Access denied"}.to_json
@ -191,6 +222,7 @@ get '/all' do
Entry.all.to_json Entry.all.to_json
end end
# Get all registered sub-domains for a specific DynDNS domain
get '/all/:domain' do get '/all/:domain' do
unless ALLOWED_IP.include? request.ip unless ALLOWED_IP.include? request.ip
halt 403, { :error => "Access denied"}.to_json halt 403, { :error => "Access denied"}.to_json
@ -202,6 +234,7 @@ get '/all/:domain' do
halt 200, result.to_json halt 200, result.to_json
end end
# ?
get '/ips/:public_key' do get '/ips/:public_key' do
params[:public_key] = Base64.decode64(params[:public_key].encode('ascii-8bit')) params[:public_key] = Base64.decode64(params[:public_key].encode('ascii-8bit'))
unless ALLOWED_IP.include? request.ip unless ALLOWED_IP.include? request.ip
@ -214,6 +247,7 @@ get '/ips/:public_key' do
ips.to_json ips.to_json
end end
# Ban an IP address for 30 seconds
get '/ban/:ip' do get '/ban/:ip' do
unless ALLOWED_IP.include? request.ip unless ALLOWED_IP.include? request.ip
halt 403, { :error => "Access denied"}.to_json halt 403, { :error => "Access denied"}.to_json
@ -222,6 +256,7 @@ get '/ban/:ip' do
Ipban.all.to_json Ipban.all.to_json
end end
# Unban an IP address
get '/unban/:ip' do get '/unban/:ip' do
unless ALLOWED_IP.include? request.ip unless ALLOWED_IP.include? request.ip
halt 403, { :error => "Access denied"}.to_json halt 403, { :error => "Access denied"}.to_json
@ -230,5 +265,6 @@ get '/unban/:ip' do
Ipban.all.to_json Ipban.all.to_json
end end
#DataMapper.auto_migrate! # Destroy db content #DataMapper.auto_migrate! # Destroy db content
DataMapper.auto_upgrade! DataMapper.auto_upgrade!