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
|
|
|
"""Test SSB server"""
|
|
|
|
|
|
|
|
from asyncio import get_event_loop
|
2017-07-31 21:17:59 +00:00
|
|
|
import logging
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2017-07-31 21:17:59 +00:00
|
|
|
from colorlog import ColoredFormatter
|
2018-02-04 21:18:36 +00:00
|
|
|
from secret_handshake import SHSServer
|
2023-11-01 06:22:29 +00:00
|
|
|
from secret_handshake.network import SHSDuplexStream
|
2023-11-14 04:00:03 +00:00
|
|
|
|
2018-04-20 02:00:22 +00:00
|
|
|
from ssb.muxrpc import MuxRPCAPI
|
2023-11-14 04:00:03 +00:00
|
|
|
from ssb.packet_stream import PacketStream
|
2017-08-05 10:24:46 +00:00
|
|
|
from ssb.util import load_ssb_secret
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2018-04-20 02:00:22 +00:00
|
|
|
api = MuxRPCAPI()
|
|
|
|
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2023-11-01 06:22:29 +00:00
|
|
|
async def on_connect(conn: SHSDuplexStream) -> None:
|
2023-11-01 05:03:06 +00:00
|
|
|
"""Incoming connection handler"""
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
packet_stream = PacketStream(conn)
|
2018-04-20 02:00:22 +00:00
|
|
|
api.add_connection(packet_stream)
|
2017-07-31 21:17:59 +00:00
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
print("connect", conn)
|
2023-11-18 05:50:51 +00:00
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
async for msg in packet_stream:
|
|
|
|
print(msg)
|
2017-07-31 21:17:59 +00:00
|
|
|
|
|
|
|
|
2023-11-01 06:22:29 +00:00
|
|
|
async def main() -> None:
|
2023-11-01 05:03:06 +00:00
|
|
|
"""The main function to run"""
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
server = SHSServer("127.0.0.1", 8008, load_ssb_secret()["keypair"])
|
2018-02-04 21:18:36 +00:00
|
|
|
server.on_connect(on_connect)
|
|
|
|
await server.listen()
|
2017-07-31 21:17:59 +00:00
|
|
|
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
if __name__ == "__main__":
|
2018-02-04 21:18:36 +00:00
|
|
|
# create console handler and set level to debug
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setLevel(logging.DEBUG)
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
# create formatter
|
2023-11-01 04:04:43 +00:00
|
|
|
formatter = ColoredFormatter(
|
2023-11-01 05:03:06 +00:00
|
|
|
"%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - %(cyan)s%(message)s%(reset)s"
|
2023-11-01 04:04:43 +00:00
|
|
|
)
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
# add formatter to ch
|
|
|
|
ch.setFormatter(formatter)
|
2017-06-04 22:27:44 +00:00
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
# add ch to logger
|
2023-11-01 04:04:43 +00:00
|
|
|
logger = logging.getLogger("packet_stream")
|
2018-02-04 21:18:36 +00:00
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(ch)
|
|
|
|
|
|
|
|
loop = get_event_loop()
|
|
|
|
loop.run_until_complete(main())
|
|
|
|
loop.run_forever()
|
|
|
|
loop.close()
|