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,19 +5,23 @@ import sys
|
||||||
import json
|
import json
|
||||||
from urllib import urlopen
|
from urllib import urlopen
|
||||||
|
|
||||||
domain = 'yoyoyo.fr'
|
urls = ['http://dynette-dev.herokuapp.com']
|
||||||
|
lines = []
|
||||||
|
|
||||||
result = str(urlopen('http://dynette-dev.herokuapp.com/all').read())
|
for url in urls:
|
||||||
result = json.loads(result)
|
domains = json.loads(str(urlopen(url +'/domains').read()))
|
||||||
|
|
||||||
lines = [
|
for domain in domains:
|
||||||
|
result = json.loads(str(urlopen(url +'/all').read()))
|
||||||
|
|
||||||
|
lines.extend([
|
||||||
'zone "'+ domain +'" {',
|
'zone "'+ domain +'" {',
|
||||||
' type master;',
|
' type master;',
|
||||||
' file "/var/named/data/yoyoyo.fr.db"; ',
|
' file "/var/named/data/yoyoyo.fr.db"; ',
|
||||||
' update-policy {',
|
' update-policy {',
|
||||||
]
|
])
|
||||||
|
|
||||||
for entry in result:
|
for entry in result:
|
||||||
fqdn = entry['subdomain'] +'.'+ domain +'.'
|
fqdn = entry['subdomain'] +'.'+ domain +'.'
|
||||||
lines.extend([
|
lines.extend([
|
||||||
' grant '+ fqdn +' name '+ fqdn +' A TXT;',
|
' grant '+ fqdn +' name '+ fqdn +' A TXT;',
|
||||||
|
@ -28,12 +32,12 @@ for entry in result:
|
||||||
' grant '+ fqdn +' name _xmpp-server._tcp.'+ fqdn +' SRV;',
|
' grant '+ fqdn +' name _xmpp-server._tcp.'+ fqdn +' SRV;',
|
||||||
])
|
])
|
||||||
|
|
||||||
lines.extend([
|
lines.extend([
|
||||||
' };',
|
' };',
|
||||||
'};',
|
'};',
|
||||||
])
|
])
|
||||||
|
|
||||||
for entry in result:
|
for entry in result:
|
||||||
fqdn = entry['subdomain'] +'.'+ domain +'.'
|
fqdn = entry['subdomain'] +'.'+ domain +'.'
|
||||||
lines.extend([
|
lines.extend([
|
||||||
'key '+ fqdn +' {',
|
'key '+ fqdn +' {',
|
||||||
|
@ -43,8 +47,17 @@ for entry in result:
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
|
os.system('cp /etc/bind/named.conf.local /etc/bind/named.conf.local.back')
|
||||||
|
|
||||||
with open('/etc/bind/named.conf.local', 'w') as zone:
|
with open('/etc/bind/named.conf.local', 'w') as zone:
|
||||||
for line in lines:
|
for line in lines:
|
||||||
zone.write(line + '\n')
|
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'
|
require 'json'
|
||||||
|
|
||||||
DataMapper.setup(:default, ENV['DATABASE_URL'] || "postgres://postgres:yayaya@localhost/dynette")
|
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 = "82.196.13.142"
|
||||||
#ALLOWED_IP = "127.0.0.1"
|
#ALLOWED_IP = "127.0.0.1"
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ before do
|
||||||
if Ipban.first(:ip_addr => request.ip)
|
if Ipban.first(:ip_addr => request.ip)
|
||||||
halt 410, "Your ip is banned from the service"
|
halt 410, "Your ip is banned from the service"
|
||||||
end
|
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 = Iplog.last(:ip_addr => request.ip)
|
||||||
if iplog.visited_at.to_time > Time.now - 30
|
if iplog.visited_at.to_time > Time.now - 30
|
||||||
halt 410, "Please wait 30sec\n"
|
halt 410, "Please wait 30sec\n"
|
||||||
|
@ -57,89 +57,87 @@ before do
|
||||||
else
|
else
|
||||||
Iplog.create(:ip_addr => request.ip, :visited_at => Time.now)
|
Iplog.create(:ip_addr => request.ip, :visited_at => Time.now)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
get '/' do
|
get '/' do
|
||||||
"Wanna play the dynette ?"
|
"Wanna play the dynette ?"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
get '/domains' do
|
||||||
|
DOMAINS.to_json
|
||||||
|
end
|
||||||
|
|
||||||
get '/test/:subdomain' do
|
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])
|
if entry = Entry.first(:subdomain => params[:subdomain])
|
||||||
status 409
|
halt 409, { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" }.to_json
|
||||||
return { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" }.to_json
|
|
||||||
else
|
else
|
||||||
status 200
|
"Domain #{params[:subdomain]}.#{DOMAIN} is available".to_json
|
||||||
return "Domain #{params[:subdomain]}.#{DOMAIN} is available".to_json
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
post '/:public_key' do
|
post '/:public_key' do
|
||||||
content_type :json
|
|
||||||
# Check params
|
# Check params
|
||||||
status 400
|
halt 400, { :error => "Please indicate a subdomain" }.to_json unless params.has_key?("subdomain")
|
||||||
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
|
|
||||||
|
|
||||||
# If already exists
|
# If already exists
|
||||||
status 409
|
|
||||||
if entry = Entry.first(:subdomain => params[:subdomain])
|
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
|
end
|
||||||
if entry = Entry.first(:public_key => params[:public_key])
|
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
|
end
|
||||||
|
|
||||||
# Process
|
# Process
|
||||||
entry = Entry.new(:public_key => params[:public_key], :subdomain => params[:subdomain], :current_ip => request.ip)
|
entry = Entry.new(:public_key => params[:public_key], :subdomain => params[:subdomain], :current_ip => request.ip)
|
||||||
entry.ips << Ip.create(:ip_addr => request.ip)
|
entry.ips << Ip.create(:ip_addr => request.ip)
|
||||||
if entry.save
|
if entry.save
|
||||||
status 201
|
halt 201, { :public_key => entry.public_key, :subdomain => entry.subdomain, :current_ip => entry.current_ip }.to_json
|
||||||
return { :public_key => entry.public_key, :subdomain => entry.subdomain, :current_ip => entry.current_ip }.to_json
|
|
||||||
else
|
else
|
||||||
status 412
|
halt 412, { :error => "A problem occured during DNS registration" }.to_json
|
||||||
return { :error => "A problem occured during DNS registration" }.to_json
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/:public_key' do
|
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])
|
entry = Entry.first(:public_key => params[:public_key])
|
||||||
unless request.ip == entry.current_ip
|
unless request.ip == entry.current_ip
|
||||||
entry.ips << Ip.create(:ip_addr => request.ip)
|
entry.ips << Ip.create(:ip_addr => request.ip)
|
||||||
end
|
end
|
||||||
entry.current_ip = request.ip
|
entry.current_ip = request.ip
|
||||||
if entry.save
|
if entry.save
|
||||||
status 201
|
halt 201, { :public_key => entry.public_key, :subdomain => entry.subdomain, :current_ip => entry.current_ip }.to_json
|
||||||
return { :public_key => entry.public_key, :subdomain => entry.subdomain, :current_ip => entry.current_ip }.to_json
|
|
||||||
else
|
else
|
||||||
status 412
|
halt 412, { :error => "A problem occured during DNS update" }.to_json
|
||||||
return { :error => "A problem occured during DNS update" }.to_json
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
delete '/:public_key' do
|
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])
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -148,7 +146,6 @@ get '/all' do
|
||||||
status 403
|
status 403
|
||||||
return "Access denied"
|
return "Access denied"
|
||||||
end
|
end
|
||||||
content_type :json
|
|
||||||
Entry.all.to_json
|
Entry.all.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -157,11 +154,6 @@ get '/:public_key/ips' do
|
||||||
status 403
|
status 403
|
||||||
return "Access denied"
|
return "Access denied"
|
||||||
end
|
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 = []
|
ips = []
|
||||||
Entry.first(:public_key => params[:public_key]).ips.all.each do |ip|
|
Entry.first(:public_key => params[:public_key]).ips.all.each do |ip|
|
||||||
ips.push(ip.ip_addr)
|
ips.push(ip.ip_addr)
|
||||||
|
@ -169,33 +161,21 @@ get '/:public_key/ips' do
|
||||||
ips.to_json
|
ips.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/ban/:ip_to_ban' do
|
get '/ban/:ip' do
|
||||||
unless request.ip == ALLOWED_IP
|
unless request.ip == ALLOWED_IP
|
||||||
status 403
|
status 403
|
||||||
return "Access denied"
|
return "Access denied"
|
||||||
end
|
end
|
||||||
content_type :json
|
Ipban.create(:ip_addr => params[:ip])
|
||||||
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
|
Ipban.all.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/unban/:ip_to_ub' do
|
get '/unban/:ip' do
|
||||||
unless request.ip == ALLOWED_IP
|
unless request.ip == ALLOWED_IP
|
||||||
status 403
|
status 403
|
||||||
return "Access denied"
|
return "Access denied"
|
||||||
end
|
end
|
||||||
content_type :json
|
Ipban.first(:ip_addr => params[:ip]).destroy
|
||||||
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
|
Ipban.all.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue