From 55e8bca98cbf295db654aa146fee067e666250c8 Mon Sep 17 00:00:00 2001 From: Kload Date: Sat, 15 Jun 2013 22:13:29 +0000 Subject: [PATCH] Logging SQL o/ --- .dynette.rb.swp | Bin 12288 -> 20480 bytes dynette.rb | 99 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 88 insertions(+), 11 deletions(-) diff --git a/.dynette.rb.swp b/.dynette.rb.swp index 77d30ff62cd01ccb526c568bcdcbea827721a783..04b84a734871d3a8694c8e3381d05cde1e03e985 100644 GIT binary patch literal 20480 zcmeI2dyHIF9mlUlp%#!L#(xk!oyE>>*`1kvl)4kPW$Lc5w%c9U-L{)$oZgvxX6JP8 z+ia zX|sl|tbUgyebh@ZYgx6bm2EhhKG%~~8XC22nla03Jua>b1%v|UtAJxyyWV%H)Z5dY z<4Lrowvj8|K6$>WiRXj@LII(GP(Uak6c7ps1%v`Zf&W1TY~upyR@lACx4qy0d{fKk zoBiJt{%5x3`UU>)9scKSE!X{@;zKAP6c7ps1%v`Z0il3UKqw#-5DEwdgaSf=*Fgap zyY?enz{SWM`~QLe|Keqm^a6MeJP95H_kj~&FX#gA09SyE!0ES1(r>_1US;00zU^o0ndOZ!GqudumGmOB$xm4+eI9%Q^np&$4$|OqaPC4$`Ym__+yzF!5EulT!Kt@M(*58bun1k-l87d&<#N zewRr<>`=?jFoO&Xki!1`qlbnDOT%N*47Eu*n>U=Y$||K<+Q>4)N)ttwq#)BF%{Chu zSyeN(UOH;&nwxM%*KDfUCA(ozk`GS@55drXi={BbwT|cG+5sEP1{olpl~rb1c6;n~ z$iQx5$fjJkrekM|9l^uPbe$)8YMQ#qA586|4Z@^|DIqM6XmFlPW}UOs36{!2{#OF%;RRtG#A-k82uLE$V(DIQ)u= zY@*u5Rdq`&6P@cMdCMs)dR=B(bUi$U%P`2~b&c2F;Q83O71i&_b-UKEs-LMP&w2KF zHpJ2xh&2)SnYduy-Dnu(y6ecWk*SyzdbH=6QIZwKttwFi>u?`WY`WcA|EE!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+m@^FUB_33IG5A delta 712 zcmaLUUr19?9Ki82H(Tm&f*NlqP#qVNtLBdnptNdTafGgoGY?>AQOGz~}J$@#Fl?z2`SRZ5uyZKiCrM z=~W}4R>ATLasBP0nIv5C&()Va9-9~=sVEm&7i6t>bK$&q$}>5=|DNh_yQsXgQ{f~< zb^BP4M~F6T!bVh~qzLgDZ}1d%kj4;B;TU4*MKhXEhc&klKk*UESi);O$08Omiz(d1 zWj9~zBbads5x}DwA@aC|Yv{zMY8v4IBG3_nipNznfdvyE)^j1|kwP5(I1LjW(BMbG z$&WjEtyY3x>o|z}xQjGSq8qO&g?Nc)NT44la2$sagb!;DA->~1ideuro?rqw#Bc-= z{9q2>YT3Tx3(A98vd>K_6|!ruSIK3P*|?PmFs;O}C0D!lI8&K{xMgK!d-R6f8hs&$ zJKeHR+ftZqcxaPHg9l~GP-HRKELV4IkOz#q>P&7hojRuuC9cXdMuYobIzYOuQONv0 zpUfLtWo}f}bXklB%KZv`TFEYdH@g1zav0|Sdze8#7u}Z2hE`bF*=_KU|Z%bTQqb>)DZ!Q8}Ss`2(iIm&O19 diff --git a/dynette.rb b/dynette.rb index aa21d0f..3a48e77 100755 --- a/dynette.rb +++ b/dynette.rb @@ -5,9 +5,9 @@ require 'sinatra' require 'data_mapper' require 'json' -DataMapper.setup(:default, ENV['DATABASE_URL'] || "pgsql://root:yayaya@localhost/dynette") +DataMapper.setup(:default, ENV['DATABASE_URL'] || "postgres://postgres:yayaya@localhost/dynette") DOMAIN = "yoyoyo.fr" -ALLOWED_IP = "82.242.206.127" +ALLOWED_IP = "127.0.0.1" class Entry include DataMapper::Resource @@ -29,16 +29,43 @@ class Ip belongs_to :entry end +class Iplog + include DataMapper::Resource + + property :ip_addr, String, :key => true + property :visited_at, DateTime +end + +class Ipban + include DataMapper::Resource + + property :ip_addr, String, :key => true +end + +before do + if Ipban.first(:ip_addr => request.ip) + halt 410, "Your ip is banned from the service" + end + if iplog = Iplog.last(:ip_addr => request.ip) + if iplog.visited_at.to_time > Time.now - 30 + halt 410, "Please wait 30sec\n" + else + iplog.update(:visited_at => Time.now) + end + else + Iplog.create(:ip_addr => request.ip, :visited_at => Time.now) + end +end + get '/' do `whoami` end -post '/' do +post '/:public_key' do content_type :json # Check params status 400 return { :error => "Please indicate a subdomain" }.to_json unless params.has_key?("subdomain") - return { :error => "Please indicate a public key" }.to_json unless params.has_key?("public_key") return { :error => "Subdomain is invalid: #{params[:subdomain]}.#{DOMAIN}" }.to_json unless params[:subdomain].match /^[a-z0-9-]{3,16}$/ return { :error => "Key is invalid: #{params[:public_key]}" }.to_json unless params[:public_key].match /^[a-z0-9]{22}==$/i @@ -63,12 +90,13 @@ post '/' do end end -put '/' do +put '/:public_key' do content_type :json # Check params - status 400 - return { :error => "Please indicate a public key" }.to_json unless params.has_key?("public_key") - return { :error => "Key is invalid: #{params[:public_key]}" }.to_json unless params[:public_key].match /^[a-z0-9]{22}==$/i + unless params[:public_key].match /^[a-z0-9]{22}==$/i + status 400 + return { :error => "Key is invalid: #{params[:public_key]}" }.to_json + end entry = Entry.first(:public_key => params[:public_key]) unless request.ip == entry.current_ip @@ -84,6 +112,19 @@ put '/' do end end +delete '/:public_key' do + content_type :json + # Check params + unless params[:public_key].match /^[a-z0-9]{22}==$/i + status 400 + return { :error => "Key is invalid: #{params[:public_key]}" }.to_json + end + + if entry = Entry.first(:public_key => params[:public_key]) + return "OK" if entry.destroy + end +end + get '/all' do unless request.ip == ALLOWED_IP status 403 @@ -93,14 +134,50 @@ get '/all' do Entry.all.to_json end -get '/ips' do +get '/:public_key/ips' do unless request.ip == ALLOWED_IP status 403 return "Access denied" end content_type :json - Entry.first(:public_key => params[:public_key]).ips.ip_addr.to_json + unless params[:public_key].match /^[a-z0-9]{22}==$/i + status 400 + return { :error => "Key is invalid: #{params[:public_key]}" }.to_json + end + ips = [] + Entry.first(:public_key => params[:public_key]).ips.all.each do |ip| + ips.push(ip.ip_addr) + end + ips.to_json +end + +get '/ban/:ip_to_ban' do + unless request.ip == ALLOWED_IP + status 403 + return "Access denied" + end + unless params[:ip_to_ban].match /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ + status 400 + return { :error => "IP is invalid: #{params[:ip_to_ban]}" }.to_json + end + + Ipban.create(:ip_addr => params[:ip_to_ban]) + Ipban.all.to_json +end + +get '/unban/:ip_to_ub' do + unless request.ip == ALLOWED_IP + status 403 + return "Access denied" + end + unless params[:ip_to_ub].match /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ + status 400 + return { :error => "IP is invalid: #{params[:ip_to_ub]}" }.to_json + end + + Ipban.first(:ip_addr => params[:ip_to_ub]).destroy + Ipban.all.to_json end -DataMapper.auto_upgrade! +DataMapper.auto_migrate!