From 3e82942da7ee0bbdc54ac2200687cab537083421 Mon Sep 17 00:00:00 2001 From: Kload Date: Sat, 15 Jun 2013 20:33:45 +0000 Subject: [PATCH] Jsonify --- .dynette.rb.swp | Bin 12288 -> 12288 bytes dynette.rb | 33 ++++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/.dynette.rb.swp b/.dynette.rb.swp index 51fa4ab9803f868c7f860ac8154b460045ebdac9..77d30ff62cd01ccb526c568bcdcbea827721a783 100644 GIT binary patch delta 759 zcmYk(T}YEr7{KvoTF#j|%@*rIdMD^Mx4m1NxCp$6j4+%f#q7)G8yiC#Zp^KVZYD%h zK}8%WgKp}kg1%%IfeHx`-DGuB8B}!FH(e-n(MA13*xP~Mf#-S7dCuXS$&tws$FWen zFQ!HHh%lN&PQ92POwTxe+bv_|=B8zPLw%LV%j(tQm}=I1rHytG2W z2v-qB5IQv6w$TJh4B~Yy2VxeZFffcG7{oTX;Y8Ug(uD|q)s)y+BXSuRkijAB!n3s^ zkMRI04C5dUU^hC@f|WHQ-|-5MFo#*(!5I`0#{i=E$@_o9SA4NC{rixm>X}Q8Rc*aS z!H5~TTq>^{#X>ew%%$^2GUejOQsYU{G)?{J3~kUya}!7y$z)z<+1NyOTwU}y)&BN< z&h7pEdxm!QCi?bDI#tlzK^D8UWLCM_r|Qh2df0xnwzApkeX8Dd{4oimTix;`DuuGu`^Hj<)Q8S%{|Bw=itn76aQUaq;HE!g2CMCNR1)QE;EAQx Zysp-xwQUXS;Vy)_^)2COn~DS%{{q5luKoZ3 delta 617 zcmYk&O-R#m7{Kx8U-Na^_75~Nw9%aNZBtwjLS1wo94HLLF3SA-LI-Uv*$%_TcBn%g zqIiO^ONT%Y5u$%6A)yZH4jM*YG$Om#&1eTb^j!({z=!9@^YS}9xv|_>&3NZ<|A6O8 zFeHRqB(l90omj3pav3q(bRW8^Z=V*aDEZrrsp)CUhFl_kl%fPb&WLQ`B_7~D(nukR zX++SEPMn2_qiT@?e&IX5;xqQ~0k81_Ptc7oRU+4`BrD&XA_vH06?14p0}d)h?jeIa zh@(&;vW^uzM`Y;D;BNP`q&jJ?KUgE~4NNaguk)Yj|onmLKn*a)hm9SU-B}ny2EE z;vxFtNov-Bl}Jp@2CY=`RxFjc@vl@IuKjO#HXXEPitX^9fAnqRUG;XD8u5+VOTJA* zGya diff --git a/dynette.rb b/dynette.rb index e2c8594..aa21d0f 100755 --- a/dynette.rb +++ b/dynette.rb @@ -7,6 +7,7 @@ require 'json' DataMapper.setup(:default, ENV['DATABASE_URL'] || "pgsql://root:yayaya@localhost/dynette") DOMAIN = "yoyoyo.fr" +ALLOWED_IP = "82.242.206.127" class Entry include DataMapper::Resource @@ -36,18 +37,18 @@ post '/' do content_type :json # Check params status 400 - return { :error => "Please indicate a subdomain" } unless params.has_key?("subdomain") - return { :error => "Please indicate a public key" } unless params.has_key?("public_key") - return { :error => "Subdomain is invalid: #{params[:subdomain]}.#{DOMAIN}" } unless params[:subdomain].match /^[a-z0-9-]{3,16}$/ - return { :error => "Key is invalid: #{params[:public_key]}" } unless params[:public_key].match /^[a-z0-9]{22}==$/i + 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 # If already exists status 409 if entry = Entry.first(:subdomain => params[:subdomain]) - return { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" } + return { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" }.to_json end if entry = Entry.first(:public_key => params[:public_key]) - return { :error => "Key already exists for domain #{entry.subdomain}.#{DOMAIN}" } + return { :error => "Key already exists for domain #{entry.subdomain}.#{DOMAIN}" }.to_json end # Process @@ -58,7 +59,7 @@ post '/' do return { :public_key => entry.public_key, :subdomain => entry.subdomain, :current_ip => entry.current_ip }.to_json else status 412 - return { :error => "A problem occured during DNS registration" } + return { :error => "A problem occured during DNS registration" }.to_json end end @@ -66,8 +67,8 @@ put '/' do content_type :json # Check params status 400 - return { :error => "Please indicate a public key" } unless params.has_key?("public_key") - return { :error => "Key is invalid: #{params[:public_key]}" } unless params[:public_key].match /^[a-z0-9]{22}==$/i + 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 entry = Entry.first(:public_key => params[:public_key]) unless request.ip == entry.current_ip @@ -79,12 +80,12 @@ put '/' do return { :public_key => entry.public_key, :subdomain => entry.subdomain, :current_ip => entry.current_ip }.to_json else status 412 - return { :error => "A problem occured during DNS update" } + return { :error => "A problem occured during DNS update" }.to_json end end get '/all' do - unless request.ip == "82.242.206.127" + unless request.ip == ALLOWED_IP status 403 return "Access denied" end @@ -92,4 +93,14 @@ get '/all' do Entry.all.to_json end +get '/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 +end + + DataMapper.auto_upgrade!