Blind implementation
This commit is contained in:
parent
01a8f59833
commit
5a1f8533b0
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
settings.ini
|
settings.ini
|
||||||
*.pyc
|
*.pyc
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
venv/
|
|
@ -1,7 +1,7 @@
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
|
|
||||||
from .local import Local
|
from .directory import Directory
|
||||||
from .remote import Remote
|
from .server import Server
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -14,19 +14,11 @@ class Config:
|
||||||
return o
|
return o
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def locals(self):
|
def directories(self):
|
||||||
for section in self._parser.sections:
|
for section in self._parser.sections:
|
||||||
if section.startswith("Local "):
|
if section.startswith("Directory "):
|
||||||
yield Local.from_config(section)
|
yield Directory.from_config(section)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def remotes(self):
|
def server(self):
|
||||||
for section in self._parser.sections:
|
return Server(self._parser["Server"])
|
||||||
if section.startswith("Remote "):
|
|
||||||
yield Remote.from_config(section)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def defaults(self):
|
|
||||||
if "DEFAULT" in self._parser.sections:
|
|
||||||
return self._parser["DEFAULT"]
|
|
||||||
return {}
|
|
||||||
|
|
11
classes/directory.py
Normal file
11
classes/directory.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
class Directory:
|
||||||
|
def __init__(self, name, source, destination, sourcebackup=None, destinationbackup=None):
|
||||||
|
self.name = name
|
||||||
|
self.source = source
|
||||||
|
self.destination = destination
|
||||||
|
self.sourcebackup = sourcebackup
|
||||||
|
self.destinationbackup = destinationbackup
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_config(cls, section):
|
||||||
|
return cls(section.name.split()[1], section["SourceDirectory"], section["DestinationDirectory"], section.get("SourceBackup"), section.get("DestinationBackup"))
|
|
@ -1,4 +0,0 @@
|
||||||
class Local:
|
|
||||||
@classmethod
|
|
||||||
def from_config(cls, section):
|
|
||||||
pass
|
|
|
@ -1,4 +0,0 @@
|
||||||
class Remote:
|
|
||||||
@classmethod
|
|
||||||
def from_config(cls, section):
|
|
||||||
pass
|
|
22
classes/server.py
Normal file
22
classes/server.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import paramiko
|
||||||
|
|
||||||
|
class Server:
|
||||||
|
def __init__(self, host, username, password, inpath, outpath, ourkey, theirkey):
|
||||||
|
self.host = host
|
||||||
|
self.username = username
|
||||||
|
self.password = password
|
||||||
|
self.inpath = inpath
|
||||||
|
self.outpath = outpath
|
||||||
|
self.ourkey = ourkey
|
||||||
|
self.theirkey = theirkey
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_config(cls, section):
|
||||||
|
return cls(section["Host"], section["Username"], section["Password"], section["InPath"], section["OutPath"], section["OurKey"], section["TheirKey"])
|
||||||
|
|
||||||
|
def get_sftp_client(self):
|
||||||
|
ssh_client = paramiko.SSHClient()
|
||||||
|
ssh_client.load_system_host_keys()
|
||||||
|
ssh_client.connect(self.host, username=self.username, password=self.password)
|
||||||
|
sftp_client = ssh_client.open_sftp()
|
||||||
|
return sftp_client
|
68
daemon.py
68
daemon.py
|
@ -0,0 +1,68 @@
|
||||||
|
from classes.config import Config
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
from gnupg import GPG
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
config = Config.from_file(Path(__file__).parent / "settings.ini")
|
||||||
|
|
||||||
|
for directory in config.directories:
|
||||||
|
source = Path(directory.source)
|
||||||
|
|
||||||
|
for newfile in source.iterdir():
|
||||||
|
if newfile.is_file() and (time.time() - newfile.stat().st_mtime > 60):
|
||||||
|
try:
|
||||||
|
raw = newfile.read_text()
|
||||||
|
encrypted: str = GPG().encrypt(raw, config.server.theirkey, sign=config.server.ourkey)
|
||||||
|
upfl = BytesIO(encrypted.encode())
|
||||||
|
uppath = Path(config.server.inpath) / f"{directory.name}_{newfile.name}"
|
||||||
|
|
||||||
|
with config.server.get_sftp_client() as sftp:
|
||||||
|
sftp.putfo(upfl, uppath)
|
||||||
|
|
||||||
|
if directory.sourcebackup:
|
||||||
|
newfile.rename(Path(directory.sourcebackup) / newfile.name)
|
||||||
|
else:
|
||||||
|
newfile.unlink()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Something went wrong uploading file {newfile.name} from {directory.name}: {e}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
with config.server.get_sftp_client() as sftp:
|
||||||
|
for response in sftp.listdir(config.server.outpath):
|
||||||
|
if (time.time() - sftp.stat(response).st_mtime < 60):
|
||||||
|
continue
|
||||||
|
|
||||||
|
encrypted: bytes = sftp.open(response).read()
|
||||||
|
decrypted = GPG().decrypt(encrypted)
|
||||||
|
|
||||||
|
dirname = Path(outpath).name.split("_")[0]
|
||||||
|
|
||||||
|
founddir = None
|
||||||
|
|
||||||
|
for directory in config.directories:
|
||||||
|
if directory.name == dirname:
|
||||||
|
founddir = directory
|
||||||
|
break
|
||||||
|
|
||||||
|
if not founddir:
|
||||||
|
founddir = directory
|
||||||
|
|
||||||
|
outfile = Path("_".join(Path(outpath).name.split("_")[1:]))
|
||||||
|
|
||||||
|
assert not outfile.exists()
|
||||||
|
|
||||||
|
outfile.write_text(decrypted)
|
||||||
|
|
||||||
|
if founddir.destinationbackup:
|
||||||
|
(Path(founddir.destinationbackup) / outfile.name).write_text(decrypted)
|
||||||
|
|
||||||
|
sftp.remove(response)
|
||||||
|
|
||||||
|
except:
|
||||||
|
print(f"Something went wrong downloading files from the server.")
|
|
@ -1 +1,2 @@
|
||||||
python-gnupg
|
python-gnupg
|
||||||
|
paramiko
|
Loading…
Reference in a new issue