38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Build a Preferences-DataStore protobuf for the vojo proxy app from a ProxyState
|
|
JSON on stdin; emit it base64-encoded on stdout (pipe through `base64 -d` via run-as).
|
|
|
|
Wire layout (androidx.datastore.preferences):
|
|
PreferenceMap.preferences = field 1 (map) -> entry{ key=field1 string, value=field2 Value }
|
|
Value.string = field 5
|
|
"""
|
|
import sys
|
|
import base64
|
|
|
|
|
|
def varint(n):
|
|
out = bytearray()
|
|
while True:
|
|
b = n & 0x7F
|
|
n >>= 7
|
|
if n:
|
|
out.append(b | 0x80)
|
|
else:
|
|
out.append(b)
|
|
return bytes(out)
|
|
|
|
|
|
def ld(field, data): # length-delimited field: tag=(field<<3)|2
|
|
return bytes([(field << 3) | 2]) + varint(len(data)) + data
|
|
|
|
|
|
def main():
|
|
state_json = sys.stdin.read().strip().encode("utf-8")
|
|
value_msg = ld(5, state_json) # Value.string
|
|
entry = ld(1, b"state") + ld(2, value_msg) # entry: key + Value
|
|
pref_map = ld(1, entry) # PreferenceMap.preferences
|
|
sys.stdout.write(base64.b64encode(pref_map).decode())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|