add b32z encode and bt decode for exit auth
This commit is contained in:
parent
d06864f392
commit
eb4f558b56
|
@ -1,10 +1,74 @@
|
||||||
import pylokimq
|
import pylokimq
|
||||||
|
import base64
|
||||||
import subprocess
|
import subprocess
|
||||||
import shlex
|
import shlex
|
||||||
import argparse
|
import argparse
|
||||||
|
import io
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
def decode_str(data):
|
||||||
|
s = ''
|
||||||
|
l = ''
|
||||||
|
while True:
|
||||||
|
ch = data.read(1)
|
||||||
|
if ch == b':':
|
||||||
|
return data.read(int(l))
|
||||||
|
else:
|
||||||
|
l += chr(ch)
|
||||||
|
|
||||||
|
def decode_list(data):
|
||||||
|
l = []
|
||||||
|
ch = data.read(1)
|
||||||
|
assert ch == b'l'
|
||||||
|
while True:
|
||||||
|
ch = data.peek(1)
|
||||||
|
if ch == b'e':
|
||||||
|
data.read(1)
|
||||||
|
return l
|
||||||
|
l += decode_value(data)
|
||||||
|
|
||||||
|
def decode_dict(data):
|
||||||
|
d = dict()
|
||||||
|
ch = data.read(1)
|
||||||
|
assert ch == b'd'
|
||||||
|
while True:
|
||||||
|
ch = data.peek(1)
|
||||||
|
if ch == b'e':
|
||||||
|
data.read(1)
|
||||||
|
return d
|
||||||
|
k = decode_str(data)
|
||||||
|
v = decode_value(data)
|
||||||
|
d[k] = v
|
||||||
|
|
||||||
|
def decode_int(data):
|
||||||
|
ch = data.read(1)
|
||||||
|
i = ''
|
||||||
|
assert ch == b'i'
|
||||||
|
while True:
|
||||||
|
ch = data.read(1)
|
||||||
|
if ch == b'e':
|
||||||
|
return int(i)
|
||||||
|
i += chr(ch)
|
||||||
|
|
||||||
|
def decode_value(data):
|
||||||
|
ch = data.peek(1)
|
||||||
|
if ch in b'0123456789':
|
||||||
|
return decode_string(data)
|
||||||
|
if ch == b'i':
|
||||||
|
return decode_int(data)
|
||||||
|
if ch == b'd':
|
||||||
|
return decode_dict(data)
|
||||||
|
if ch == b'l':
|
||||||
|
return decode_list(data)
|
||||||
|
raise Exception("invalid char: {}".format(ch))
|
||||||
|
|
||||||
|
|
||||||
|
def decode_address(data):
|
||||||
|
return '{}.loki'.format(pylokimq.base32z_encode(decode_dict(data)[b's'][b's']))
|
||||||
|
|
||||||
def handle_auth(args, cmd):
|
def handle_auth(args, cmd):
|
||||||
|
cmd += decode_address(io.BytesIO(args[0]))
|
||||||
|
cmd += base64.b64encode(args[1]).decode('ascii')
|
||||||
result = subprocess.run(args=cmd, check=False)
|
result = subprocess.run(args=cmd, check=False)
|
||||||
if result.returncode == 0:
|
if result.returncode == 0:
|
||||||
return "OK"
|
return "OK"
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
#include "lokimq/bt_serialize.h"
|
#include "lokimq/base32z.h"
|
||||||
|
|
||||||
namespace lokimq
|
namespace lokimq
|
||||||
{
|
{
|
||||||
void
|
void
|
||||||
BEncode_Init(py::module & mod)
|
BEncode_Init(py::module & mod)
|
||||||
{
|
{
|
||||||
auto submod = mod.def_submodule("bencode");
|
mod.def("base32z_encode", [](py::bytes data) {
|
||||||
submod.def("decode", [](py::bytes data) {
|
char * ptr = nullptr;
|
||||||
char * ptr;
|
py::ssize_t sz = 0;
|
||||||
Py_ssize_t len;
|
PyBytes_AsStringAndSize(data.ptr(), &ptr, &sz);
|
||||||
PyBytes_AsStringAndSize(data.ptr(), &ptr, &len);
|
return lokimq::to_base32z(ptr, ptr+sz);
|
||||||
return bt_get(std::string_view(ptr, len));
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue