mirror of https://github.com/cmehay/pytor
Add main and serializer in Onion classes
parent
560fcddead
commit
d7f0be237f
@ -0,0 +1,41 @@
|
|||||||
|
# pytor
|
||||||
|
|
||||||
|
`pytor` is a simple python librairy to create and manager tor hidden services in version 2 and 3.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ pytor new
|
||||||
|
hostname:
|
||||||
|
cljfodghi4w5frc6.onion
|
||||||
|
private_key:
|
||||||
|
-----BEGIN RSA PRIVATE KEY-----
|
||||||
|
MIICXAIBAAKBgQD2Gza8HXzgDGo+YwyhjOdgD0GY7ti5en8YGXtcsBi/JIwHdKZo
|
||||||
|
iLC4e5pWzlmB2ACdTw93ASFTGpPFs7nRxk4NuXo1BnvTsqzzcrsd9HV6xuKO7BkS
|
||||||
|
aTEY3tgrSvB2nQtM1WR9FQoyxV+EWeE0Q9vrBVpEizO4kHqFXRanOJpJbwIDAQAB
|
||||||
|
AoGAA/axPXteGP+qMGIJAIsT6OSmAlAKdoZGCL3UUkxVwbJVfQNAcNuOuRHojPBa
|
||||||
|
2bAAZogw8BI5Fq0NZzg7TGkctazKbvrmIx6o22spx2MOQXEc7lj3R3CJ8B+F1moz
|
||||||
|
9lNxIhNmw4bHeL3Sw5XMTPnOhCy1OKmouWrrcOj7B59YKrkCQQD2pWkZih6Ijl0y
|
||||||
|
xG3vB8w22krpe0YOne94aXwdggkji6Cfne8YRNWU9y8FvxGZgwfXZKwGCOSOgq7r
|
||||||
|
0SP7vEoZAkEA/3CP8BGY1jThrLHLWNPKm5Vu1+YZClL4ibs4cdtxIs0J0l+dQcYW
|
||||||
|
LMSkQpOy1C/nIIYPJpq9x8sCXG2BsRgwxwJAR9NhqONVAvVaZKdZUEuYB71IJXgV
|
||||||
|
rboGe61UTI+Ks8Q8kV7/urSI8imNkwHSUT8cMHiLs/IxBOM/p0KvVOa/OQJAHlXY
|
||||||
|
0jLUysOW9XJb6t2kFxwFAODTonU+DOVOC796zR46h2BRhaknowNrWni96RMTSLqC
|
||||||
|
/BuuZBbI3f8nQsfTqwJBAMX/KjXO/MqcB8TAjyKWHNyVR4T8OJM5lgbk8IGLKE5/
|
||||||
|
w96jWD0AEePqKKdWofLImi074zMSyMKuu6RFrkBSUuI=
|
||||||
|
-----END RSA PRIVATE KEY-----
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
$ mkdir test
|
||||||
|
$ pytor new-hidden-service test --version 3
|
||||||
|
FYI: Binary data is base64 encoded
|
||||||
|
path:
|
||||||
|
test
|
||||||
|
hostname:
|
||||||
|
byb3bkhwi2ccbrctsqkowckpvk3tok36geddzg4l2m6yn6mrw626nqid.onion
|
||||||
|
hs_ed25519_secret_key:
|
||||||
|
PT0gZWQyNTUxOXYxLXNlY3JldDogdHlwZTAgPT0AAAAwIFsWaVtOk8r3RvXnkZcmxwIaDmmOdV8D7KaVf6yBWjVUIUTPpOWNQ9+hEiPKUclJ1RpflZ9FSdPgSj0j0tE3
|
||||||
|
hs_ed25519_public_key:
|
||||||
|
PT0gZWQyNTUxOXYxLXB1YmxpYzogdHlwZTAgPT0AAAAOA7Co9kaEIMRTlBTrCU+qtzcrfjEGPJuL0z2G+ZG3tQ==
|
||||||
|
```
|
||||||
|
|
||||||
|
(more doc soon, I'm tired right mow ~)
|
@ -0,0 +1,7 @@
|
|||||||
|
from .onion import OnionV2
|
||||||
|
from .onion import OnionV3
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
'OnionV2',
|
||||||
|
'OnionV3',
|
||||||
|
]
|
@ -0,0 +1,77 @@
|
|||||||
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import fire
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from .onion import NonEmptyDirException
|
||||||
|
from .onion import OnionV2
|
||||||
|
from .onion import OnionV3
|
||||||
|
|
||||||
|
|
||||||
|
class Format(object):
|
||||||
|
|
||||||
|
def __init__(self, format: str):
|
||||||
|
attr = 'print_{format}'.format(format=format)
|
||||||
|
if not hasattr(self, attr):
|
||||||
|
raise Exception('Output format not valid')
|
||||||
|
self.method = getattr(self, attr)
|
||||||
|
print('FYI: Binary data is base64 encoded', file=sys.stderr)
|
||||||
|
|
||||||
|
def print(self, data: dict):
|
||||||
|
self.method(data)
|
||||||
|
|
||||||
|
def print_plain(self, data: dict):
|
||||||
|
for key, value in data.items():
|
||||||
|
print("{key}:\n{value}".format(key=key, value=value))
|
||||||
|
|
||||||
|
def print_json(self, data: dict):
|
||||||
|
print(json.dumps(data, indent=4))
|
||||||
|
|
||||||
|
def print_yaml(self, data: dict):
|
||||||
|
print(yaml.dump(data))
|
||||||
|
|
||||||
|
|
||||||
|
class Pytor(object):
|
||||||
|
|
||||||
|
_obj = {
|
||||||
|
2: OnionV2,
|
||||||
|
3: OnionV3,
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, version: int = 2, format: str = 'plain'):
|
||||||
|
if version not in self._obj:
|
||||||
|
raise Exception('Onion version not valid')
|
||||||
|
self._version = version
|
||||||
|
self._print = Format(format).print
|
||||||
|
self._stderr: lambda x: print(x, file=sys.stderr)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _cls(self):
|
||||||
|
return self._obj[self._version]
|
||||||
|
|
||||||
|
def new(self):
|
||||||
|
obj = self._cls()
|
||||||
|
self._print(obj.serialize())
|
||||||
|
|
||||||
|
def new_hidden_service(self, path: str, force: bool = False):
|
||||||
|
obj = self._cls()
|
||||||
|
try:
|
||||||
|
obj.write_hidden_service(path=path, force=force)
|
||||||
|
except NonEmptyDirException:
|
||||||
|
s = input(
|
||||||
|
'Dir {path} not empty, override? [Y/n]'.format(path=path)
|
||||||
|
)
|
||||||
|
if not s or s.lower() == 'y':
|
||||||
|
obj.write_hidden_service(path=path, force=True)
|
||||||
|
else:
|
||||||
|
print('Canceled...')
|
||||||
|
self._print({'path': path, **obj.serialize()})
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
fire.Fire(Pytor)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
Reference in New Issue