mirror of
https://github.com/YunoHost/dynette.git
synced 2024-09-03 20:06:17 +02:00
Multi-domain & multi-url
This commit is contained in:
parent
6252ec6791
commit
9c1119a53a
2 changed files with 89 additions and 96 deletions
|
@ -5,46 +5,59 @@ import sys
|
|||
import json
|
||||
from urllib import urlopen
|
||||
|
||||
domain = 'yoyoyo.fr'
|
||||
urls = ['http://dynette-dev.herokuapp.com']
|
||||
lines = []
|
||||
|
||||
result = str(urlopen('http://dynette-dev.herokuapp.com/all').read())
|
||||
result = json.loads(result)
|
||||
for url in urls:
|
||||
domains = json.loads(str(urlopen(url +'/domains').read()))
|
||||
|
||||
lines = [
|
||||
'zone "'+ domain +'" {',
|
||||
' type master;',
|
||||
' file "/var/named/data/yoyoyo.fr.db"; ',
|
||||
' update-policy {',
|
||||
]
|
||||
for domain in domains:
|
||||
result = json.loads(str(urlopen(url +'/all').read()))
|
||||
|
||||
for entry in result:
|
||||
fqdn = entry['subdomain'] +'.'+ domain +'.'
|
||||
lines.extend([
|
||||
' grant '+ fqdn +' name '+ fqdn +' A TXT;',
|
||||
' grant '+ fqdn +' name pubsub.'+ fqdn +' A;',
|
||||
' grant '+ fqdn +' name muc.'+ fqdn +' A;',
|
||||
' grant '+ fqdn +' name vjud.'+ fqdn +' A;',
|
||||
' grant '+ fqdn +' name _xmpp-client._tcp.'+ fqdn +' SRV;',
|
||||
' grant '+ fqdn +' name _xmpp-server._tcp.'+ fqdn +' SRV;',
|
||||
])
|
||||
lines.extend([
|
||||
'zone "'+ domain +'" {',
|
||||
' type master;',
|
||||
' file "/var/named/data/yoyoyo.fr.db"; ',
|
||||
' update-policy {',
|
||||
])
|
||||
|
||||
lines.extend([
|
||||
' };',
|
||||
'};',
|
||||
])
|
||||
for entry in result:
|
||||
fqdn = entry['subdomain'] +'.'+ domain +'.'
|
||||
lines.extend([
|
||||
' grant '+ fqdn +' name '+ fqdn +' A TXT;',
|
||||
' grant '+ fqdn +' name pubsub.'+ fqdn +' A;',
|
||||
' grant '+ fqdn +' name muc.'+ fqdn +' A;',
|
||||
' grant '+ fqdn +' name vjud.'+ fqdn +' A;',
|
||||
' grant '+ fqdn +' name _xmpp-client._tcp.'+ fqdn +' SRV;',
|
||||
' grant '+ fqdn +' name _xmpp-server._tcp.'+ fqdn +' SRV;',
|
||||
])
|
||||
|
||||
for entry in result:
|
||||
fqdn = entry['subdomain'] +'.'+ domain +'.'
|
||||
lines.extend([
|
||||
'key '+ fqdn +' {',
|
||||
' algorithm hmac-md5;',
|
||||
' secret "'+ entry['public_key'] +'";',
|
||||
'};',
|
||||
])
|
||||
lines.extend([
|
||||
' };',
|
||||
'};',
|
||||
])
|
||||
|
||||
for entry in result:
|
||||
fqdn = entry['subdomain'] +'.'+ domain +'.'
|
||||
lines.extend([
|
||||
'key '+ fqdn +' {',
|
||||
' algorithm hmac-md5;',
|
||||
' secret "'+ entry['public_key'] +'";',
|
||||
'};',
|
||||
])
|
||||
|
||||
|
||||
os.system('cp /etc/bind/named.conf.local /etc/bind/named.conf.local.back')
|
||||
|
||||
with open('/etc/bind/named.conf.local', 'w') as zone:
|
||||
for line in lines:
|
||||
zone.write(line + '\n')
|
||||
|
||||
os.system('rndc reload')
|
||||
if os.system('rndc reload') == 0:
|
||||
exit(0)
|
||||
else:
|
||||
os.system('cp /etc/bind/named.conf.local /etc/bind/named.conf.local.bad')
|
||||
os.system('cp /etc/bind/named.conf.back /etc/bind/named.conf.local')
|
||||
os.system('rndc reload')
|
||||
print("An error occured ! Please check daemon.log and your conf.bad")
|
||||
exit(1)
|
||||
|
|
108
dynette.rb
108
dynette.rb
|
@ -6,7 +6,7 @@ require 'data_mapper'
|
|||
require 'json'
|
||||
|
||||
DataMapper.setup(:default, ENV['DATABASE_URL'] || "postgres://postgres:yayaya@localhost/dynette")
|
||||
DOMAIN = "yoyoyo.fr"
|
||||
DOMAINS = ["yoyoyo.fr", "yayaya.fr"]
|
||||
ALLOWED_IP = "82.196.13.142"
|
||||
#ALLOWED_IP = "127.0.0.1"
|
||||
|
||||
|
@ -47,7 +47,7 @@ before do
|
|||
if Ipban.first(:ip_addr => request.ip)
|
||||
halt 410, "Your ip is banned from the service"
|
||||
end
|
||||
pass if %w[test all ban unban].include? request.path_info.split('/')[1]
|
||||
pass if %w[domains test all ban unban].include? request.path_info.split('/')[1]
|
||||
if iplog = Iplog.last(:ip_addr => request.ip)
|
||||
if iplog.visited_at.to_time > Time.now - 30
|
||||
halt 410, "Please wait 30sec\n"
|
||||
|
@ -57,89 +57,87 @@ before do
|
|||
else
|
||||
Iplog.create(:ip_addr => request.ip, :visited_at => Time.now)
|
||||
end
|
||||
content_type :json
|
||||
|
||||
# Check params
|
||||
if params.has_key?("public_key")
|
||||
unless params[:public_key].match /^[a-z0-9]{22}==$/i
|
||||
halt 400, { :error => "Key is invalid: #{params[:public_key]}" }.to_json
|
||||
end
|
||||
end
|
||||
if params.has_key?("subdomain")
|
||||
unless params[:subdomain].match /^([a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)(\.[a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)*(\.[a-zA-Z]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)$/
|
||||
halt 400, { :error => "Subdomain is invalid: #{params[:subdomain]}" }.to_json
|
||||
end
|
||||
DOMAIN = params[:subdomain].gsub(params[:subdomain].split('.')[0]+'.', '')
|
||||
params[:subdomain] = params[:subdomain].split('.')[0]
|
||||
end
|
||||
if params.has_key?("ip")
|
||||
unless params[:ip].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]?)$/
|
||||
halt 400, { :error => "IP is invalid: #{params[:ip]}" }.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
get '/' do
|
||||
"Wanna play the dynette ?"
|
||||
end
|
||||
|
||||
get '/domains' do
|
||||
DOMAINS.to_json
|
||||
end
|
||||
|
||||
get '/test/:subdomain' do
|
||||
content_type :json
|
||||
unless params[:subdomain].match /^[a-z0-9-]{3,16}$/
|
||||
status 400
|
||||
return { :error => "Subdomain is invalid: #{params[:subdomain]}.#{DOMAIN}" }.to_json
|
||||
end
|
||||
if entry = Entry.first(:subdomain => params[:subdomain])
|
||||
status 409
|
||||
return { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" }.to_json
|
||||
halt 409, { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" }.to_json
|
||||
else
|
||||
status 200
|
||||
return "Domain #{params[:subdomain]}.#{DOMAIN} is available".to_json
|
||||
"Domain #{params[:subdomain]}.#{DOMAIN} is available".to_json
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
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 => "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
|
||||
halt 400, { :error => "Please indicate a subdomain" }.to_json unless params.has_key?("subdomain")
|
||||
|
||||
# If already exists
|
||||
status 409
|
||||
if entry = Entry.first(:subdomain => params[:subdomain])
|
||||
return { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" }.to_json
|
||||
halt 409, { :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}" }.to_json
|
||||
halt 409, { :error => "Key already exists for domain #{entry.subdomain}.#{DOMAIN}" }.to_json
|
||||
end
|
||||
|
||||
# Process
|
||||
entry = Entry.new(:public_key => params[:public_key], :subdomain => params[:subdomain], :current_ip => request.ip)
|
||||
entry.ips << Ip.create(:ip_addr => request.ip)
|
||||
if entry.save
|
||||
status 201
|
||||
return { :public_key => entry.public_key, :subdomain => entry.subdomain, :current_ip => entry.current_ip }.to_json
|
||||
halt 201, { :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" }.to_json
|
||||
halt 412, { :error => "A problem occured during DNS registration" }.to_json
|
||||
end
|
||||
end
|
||||
|
||||
put '/: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
|
||||
|
||||
entry = Entry.first(:public_key => params[:public_key])
|
||||
unless request.ip == entry.current_ip
|
||||
entry.ips << Ip.create(:ip_addr => request.ip)
|
||||
end
|
||||
entry.current_ip = request.ip
|
||||
if entry.save
|
||||
status 201
|
||||
return { :public_key => entry.public_key, :subdomain => entry.subdomain, :current_ip => entry.current_ip }.to_json
|
||||
halt 201, { :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" }.to_json
|
||||
halt 412, { :error => "A problem occured during DNS update" }.to_json
|
||||
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
|
||||
if entry.destroy
|
||||
halt 200, "OK".to_json
|
||||
else
|
||||
halt 412, { :error => "A problem occured during DNS deletion" }.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -148,7 +146,6 @@ get '/all' do
|
|||
status 403
|
||||
return "Access denied"
|
||||
end
|
||||
content_type :json
|
||||
Entry.all.to_json
|
||||
end
|
||||
|
||||
|
@ -157,11 +154,6 @@ get '/:public_key/ips' do
|
|||
status 403
|
||||
return "Access denied"
|
||||
end
|
||||
content_type :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)
|
||||
|
@ -169,33 +161,21 @@ get '/:public_key/ips' do
|
|||
ips.to_json
|
||||
end
|
||||
|
||||
get '/ban/:ip_to_ban' do
|
||||
get '/ban/:ip' do
|
||||
unless request.ip == ALLOWED_IP
|
||||
status 403
|
||||
return "Access denied"
|
||||
end
|
||||
content_type :json
|
||||
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.create(:ip_addr => params[:ip])
|
||||
Ipban.all.to_json
|
||||
end
|
||||
|
||||
get '/unban/:ip_to_ub' do
|
||||
get '/unban/:ip' do
|
||||
unless request.ip == ALLOWED_IP
|
||||
status 403
|
||||
return "Access denied"
|
||||
end
|
||||
content_type :json
|
||||
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.first(:ip_addr => params[:ip]).destroy
|
||||
Ipban.all.to_json
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue