| 12 | from astroprint.software import softwareManager |
| 13 | |
| 14 | class SslManager(object): |
| 15 | def __init__(self): |
| 16 | self._settings = settings() |
| 17 | self.sslCertPath = roConfig('network.ssl.certPath') |
| 18 | |
| 19 | def isSslActive(self): |
| 20 | return os.path.isfile(self.sslCertPath) |
| 21 | |
| 22 | def disable(self): |
| 23 | if self.isSslActive(): |
| 24 | dirname = os.path.dirname(self.sslCertPath) |
| 25 | os.rename(dirname, dirname.replace('ssl', 'ssl_disabled')) |
| 26 | softwareManager().restartServer() |
| 27 | |
| 28 | def enable(self): |
| 29 | if not self.isSslActive(): |
| 30 | disabledPath = self.sslCertPath.replace('ssl', 'ssl_disabled') |
| 31 | if os.path.isfile(disabledPath): |
| 32 | os.rename(os.path.dirname(disabledPath), os.path.dirname(self.sslCertPath)) |
| 33 | |
| 34 | else: |
| 35 | from astroprint.cloud import astroprintCloud |
| 36 | |
| 37 | astroprint = astroprintCloud() |
| 38 | cert = astroprint.get_local_certificate() |
| 39 | if cert is not None: |
| 40 | mkpath(os.path.dirname(self.sslCertPath)) |
| 41 | with open(self.sslCertPath, 'w') as f: |
| 42 | f.write(cert) |
| 43 | else: |
| 44 | raise NoCertException() |
| 45 | |
| 46 | softwareManager().restartServer() |
| 47 | |
| 48 | def setDomain(self, domain): |
| 49 | self._settings.set(['network', 'ssl', 'domain'], domain) |
| 50 | self._settings.save() |
| 51 | |
| 52 | def getDomain(self): |
| 53 | return self._settings.get(['network', 'ssl', 'domain']) |
| 54 | |
| 55 | # Exceptions |
| 56 | |