2023-11-13 12:34:43 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
#
|
|
|
|
# Copyright (c) 2017 PySSB contributors (see AUTHORS for more details)
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
2023-11-01 05:03:06 +00:00
|
|
|
"""Example SSB Client"""
|
|
|
|
|
2023-11-14 04:00:03 +00:00
|
|
|
from asyncio import ensure_future, gather, get_event_loop
|
|
|
|
import base64
|
|
|
|
import hashlib
|
2017-07-29 09:54:03 +00:00
|
|
|
import logging
|
2017-07-30 08:14:26 +00:00
|
|
|
import struct
|
|
|
|
import time
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
from colorlog import ColoredFormatter
|
2018-02-04 21:18:36 +00:00
|
|
|
from secret_handshake.network import SHSClient
|
2023-11-14 04:00:03 +00:00
|
|
|
|
2017-07-30 12:08:18 +00:00
|
|
|
from ssb.muxrpc import MuxRPCAPI, MuxRPCAPIException
|
2018-02-04 21:18:36 +00:00
|
|
|
from ssb.packet_stream import PacketStream, PSMessageType
|
2017-08-05 10:24:46 +00:00
|
|
|
from ssb.util import load_ssb_secret
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
api = MuxRPCAPI()
|
2017-05-25 10:47:01 +00:00
|
|
|
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
@api.define("createHistoryStream")
|
2023-11-01 05:03:06 +00:00
|
|
|
def create_history_stream(connection, msg): # pylint: disable=unused-argument
|
|
|
|
"""Handle the createHistoryStream RPC call"""
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
print("create_history_stream", msg)
|
2017-07-29 09:54:03 +00:00
|
|
|
# msg = PSMessage(PSMessageType.JSON, True, stream=True, end_err=True, req=-req)
|
|
|
|
# connection.write(msg)
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2017-06-04 22:27:44 +00:00
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
@api.define("blobs.createWants")
|
2023-11-01 05:03:06 +00:00
|
|
|
def create_wants(connection, msg): # pylint: disable=unused-argument
|
|
|
|
"""Handle the createWants RPC call"""
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
print("create_wants", msg)
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
|
2017-08-01 17:38:04 +00:00
|
|
|
async def test_client():
|
2023-11-01 05:03:06 +00:00
|
|
|
"""The actual client implementation"""
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
async for msg in api.call(
|
|
|
|
"createHistoryStream",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519",
|
|
|
|
"seq": 1,
|
|
|
|
"live": False,
|
|
|
|
"keys": False,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source",
|
|
|
|
):
|
|
|
|
print("> RESPONSE:", msg)
|
2017-07-30 08:14:26 +00:00
|
|
|
|
2017-07-30 12:08:18 +00:00
|
|
|
try:
|
2023-11-01 04:04:43 +00:00
|
|
|
print("> RESPONSE:", await api.call("whoami", [], "sync"))
|
2017-07-30 12:08:18 +00:00
|
|
|
except MuxRPCAPIException as e:
|
|
|
|
print(e)
|
2017-07-30 08:14:26 +00:00
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
handler = api.call("gossip.ping", [], "duplex")
|
|
|
|
handler.send(struct.pack("l", int(time.time() * 1000)), msg_type=PSMessageType.BUFFER)
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
async for msg in handler:
|
2023-11-01 04:04:43 +00:00
|
|
|
print("> RESPONSE:", msg)
|
2017-07-30 08:52:04 +00:00
|
|
|
handler.send(True, end=True)
|
|
|
|
break
|
2017-07-29 09:54:03 +00:00
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
img_data = b""
|
|
|
|
async for msg in api.call("blobs.get", ["&kqZ52sDcJSHOx7m4Ww80kK1KIZ65gpGnqwZlfaIVWWM=.sha256"], "source"):
|
|
|
|
if msg.type.name == "BUFFER":
|
2018-05-06 12:55:00 +00:00
|
|
|
img_data += msg.data
|
2023-11-01 04:04:43 +00:00
|
|
|
if msg.type.name == "JSON" and msg.data == b"true":
|
|
|
|
assert (
|
|
|
|
base64.b64encode(hashlib.sha256(img_data).digest()) == b"kqZ52sDcJSHOx7m4Ww80kK1KIZ65gpGnqwZlfaIVWWM="
|
|
|
|
)
|
|
|
|
|
|
|
|
with open("./ub1k.jpg", "wb") as f:
|
2018-05-06 12:55:00 +00:00
|
|
|
f.write(img_data)
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
|
2023-11-01 05:03:06 +00:00
|
|
|
async def main(keypair):
|
|
|
|
"""The main function to run"""
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
client = SHSClient("127.0.0.1", 8008, keypair, bytes(keypair.verify_key))
|
2018-02-04 21:18:36 +00:00
|
|
|
packet_stream = PacketStream(client)
|
|
|
|
await client.open()
|
2017-08-01 17:38:04 +00:00
|
|
|
api.add_connection(packet_stream)
|
|
|
|
await gather(ensure_future(api), test_client())
|
|
|
|
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
if __name__ == "__main__":
|
2017-08-01 17:38:04 +00:00
|
|
|
# create console handler and set level to debug
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
# create formatter
|
2023-11-01 04:04:43 +00:00
|
|
|
formatter = ColoredFormatter(
|
|
|
|
"%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - %(cyan)s%(message)s%(reset)s"
|
|
|
|
)
|
2017-08-01 17:38:04 +00:00
|
|
|
|
|
|
|
# add formatter to ch
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
|
|
|
|
# add ch to logger
|
2023-11-01 04:04:43 +00:00
|
|
|
logger = logging.getLogger("packet_stream")
|
2017-08-01 17:38:04 +00:00
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
logger.addHandler(ch)
|
2017-07-29 09:54:03 +00:00
|
|
|
|
2023-11-01 05:03:06 +00:00
|
|
|
ssb_keypair = load_ssb_secret()["keypair"]
|
2017-07-29 09:54:03 +00:00
|
|
|
|
2017-08-01 17:38:04 +00:00
|
|
|
loop = get_event_loop()
|
2023-11-01 05:03:06 +00:00
|
|
|
loop.run_until_complete(main(ssb_keypair))
|
2017-08-01 17:38:04 +00:00
|
|
|
loop.close()
|