Blind implementation

This commit is contained in:
Kumi 2023-01-09 17:18:59 +00:00
parent 01a8f59833
commit 5a1f8533b0
Signed by: kumi
GPG key ID: ECBCC9082395383F
8 changed files with 111 additions and 24 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
settings.ini
*.pyc
__pycache__/
venv/

View file

@ -1,7 +1,7 @@
from configparser import ConfigParser
from .local import Local
from .remote import Remote
from .directory import Directory
from .server import Server
class Config:
def __init__(self):
@ -14,19 +14,11 @@ class Config:
return o
@property
def locals(self):
def directories(self):
for section in self._parser.sections:
if section.startswith("Local "):
yield Local.from_config(section)
if section.startswith("Directory "):
yield Directory.from_config(section)
@property
def remotes(self):
for section in self._parser.sections:
if section.startswith("Remote "):
yield Remote.from_config(section)
@property
def defaults(self):
if "DEFAULT" in self._parser.sections:
return self._parser["DEFAULT"]
return {}
def server(self):
return Server(self._parser["Server"])

11
classes/directory.py Normal file
View 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"))

View file

@ -1,4 +0,0 @@
class Local:
@classmethod
def from_config(cls, section):
pass

View file

@ -1,4 +0,0 @@
class Remote:
@classmethod
def from_config(cls, section):
pass

22
classes/server.py Normal file
View 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

View file

@ -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.")

View file

@ -1 +1,2 @@
python-gnupg
python-gnupg
paramiko