Initial commit
This commit is contained in:
68
backend/hetzner.py
Normal file
68
backend/hetzner.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import configparser
|
||||
import requests
|
||||
|
||||
|
||||
def reverse_ip_to_standard(reverse_ip):
|
||||
parts = reverse_ip.split(".")
|
||||
parts = parts[:4]
|
||||
standard_ip = ".".join(reversed(parts))
|
||||
|
||||
return standard_ip
|
||||
|
||||
|
||||
class HetznerBackend:
|
||||
def __init__(self, config_file='config.ini'):
|
||||
self.config_file = config_file
|
||||
self.username = None
|
||||
self.password = None
|
||||
|
||||
self.load_credentials()
|
||||
|
||||
def load_credentials(self):
|
||||
config = configparser.ConfigParser()
|
||||
config.read(self.config_file)
|
||||
|
||||
if "Hetzner" not in config:
|
||||
raise ValueError("Hetzner section not found in config.ini")
|
||||
|
||||
self.username = config["Hetzner"].get("username")
|
||||
self.password = config["Hetzner"].get("password")
|
||||
|
||||
if not self.username or not self.password:
|
||||
raise ValueError("Hetzner username or password not provided in config.ini")
|
||||
|
||||
def update_record(self, reverse_ip, record):
|
||||
ip = reverse_ip_to_standard(reverse_ip)
|
||||
|
||||
# Check if the record contains the necessary data
|
||||
if not record or "content" not in record[0]:
|
||||
print("Invalid record data. 'content' field is missing.")
|
||||
return None
|
||||
|
||||
domain = record[0]["content"].strip(".")
|
||||
|
||||
data = {"ptr": domain}
|
||||
response = requests.post(f"https://robot-ws.your-server.de/rdns/{ip}",
|
||||
data=data, auth=(self.username, self.password))
|
||||
|
||||
if response.status_code == 200:
|
||||
print("Updated RDNS record for: " + ip + " to: " + record[0]["content"])
|
||||
return response.json()
|
||||
else:
|
||||
print(f"Failed to fetch rDNS data. Status code: {response.status_code}")
|
||||
return None
|
||||
|
||||
def create_record(self, reverse_ip, record):
|
||||
# Hetzner's API treats creation and updates the same way so just call update_record instead
|
||||
self.update_record(reverse_ip, record)
|
||||
pass
|
||||
|
||||
def delete_record(self, reverse_ip):
|
||||
ip = reverse_ip_to_standard(reverse_ip)
|
||||
response = requests.delete(f"https://robot-ws.your-server.de/rdns/{ip}", auth=(self.username, self.password))
|
||||
|
||||
if response.status_code == 200:
|
||||
print("Deleted RDNS record for: " + ip)
|
||||
else:
|
||||
print(f"Failed to delete rDNS record. Status code: {response.status_code}")
|
||||
return None
|
||||
Reference in New Issue
Block a user