feat/561-added autonat service

This commit is contained in:
Winter-Soren
2025-04-20 16:19:47 +05:30
committed by Paul Robinson
parent fd893afba6
commit 9655c88788
10 changed files with 780 additions and 0 deletions

View File

View File

@ -0,0 +1,49 @@
syntax = "proto3";
package autonat.pb;
// AutoNAT service definition
service AutoNAT {
rpc Dial (Message) returns (Message) {}
}
// Message types
enum Type {
UNKNOWN = 0;
DIAL = 1;
DIAL_RESPONSE = 2;
}
// Status codes
enum Status {
OK = 0;
E_DIAL_ERROR = 1;
E_DIAL_REFUSED = 2;
E_DIAL_FAILED = 3;
E_INTERNAL_ERROR = 100;
}
// Main message
message Message {
Type type = 1;
DialRequest dial = 2;
DialResponse dial_response = 3;
}
// Dial request
message DialRequest {
repeated PeerInfo peers = 1;
}
// Dial response
message DialResponse {
Status status = 1;
repeated PeerInfo peers = 2;
}
// Peer information
message PeerInfo {
bytes id = 1;
repeated bytes addrs = 2;
bool success = 3;
}

View File

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# NO CHECKED-IN PROTOBUF GENCODE
# source: libp2p/host/autonat/pb/autonat.proto
# Protobuf Python Version: 5.29.0
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import runtime_version as _runtime_version
from google.protobuf import symbol_database as _symbol_database
from google.protobuf.internal import builder as _builder
_runtime_version.ValidateProtobufRuntimeVersion(
_runtime_version.Domain.PUBLIC,
5,
29,
0,
'',
'libp2p/host/autonat/pb/autonat.proto'
)
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n$libp2p/host/autonat/pb/autonat.proto\x12\nautonat.pb\"\x81\x01\n\x07Message\x12\x1e\n\x04type\x18\x01 \x01(\x0e\x32\x10.autonat.pb.Type\x12%\n\x04\x64ial\x18\x02 \x01(\x0b\x32\x17.autonat.pb.DialRequest\x12/\n\rdial_response\x18\x03 \x01(\x0b\x32\x18.autonat.pb.DialResponse\"2\n\x0b\x44ialRequest\x12#\n\x05peers\x18\x01 \x03(\x0b\x32\x14.autonat.pb.PeerInfo\"W\n\x0c\x44ialResponse\x12\"\n\x06status\x18\x01 \x01(\x0e\x32\x12.autonat.pb.Status\x12#\n\x05peers\x18\x02 \x03(\x0b\x32\x14.autonat.pb.PeerInfo\"6\n\x08PeerInfo\x12\n\n\x02id\x18\x01 \x01(\x0c\x12\r\n\x05\x61\x64\x64rs\x18\x02 \x03(\x0c\x12\x0f\n\x07success\x18\x03 \x01(\x08*0\n\x04Type\x12\x0b\n\x07UNKNOWN\x10\x00\x12\x08\n\x04\x44IAL\x10\x01\x12\x11\n\rDIAL_RESPONSE\x10\x02*_\n\x06Status\x12\x06\n\x02OK\x10\x00\x12\x10\n\x0c\x45_DIAL_ERROR\x10\x01\x12\x12\n\x0e\x45_DIAL_REFUSED\x10\x02\x12\x11\n\rE_DIAL_FAILED\x10\x03\x12\x14\n\x10\x45_INTERNAL_ERROR\x10\x64\x32=\n\x07\x41utoNAT\x12\x32\n\x04\x44ial\x12\x13.autonat.pb.Message\x1a\x13.autonat.pb.Message\"\x00\x62\x06proto3')
_globals = globals()
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'libp2p.host.autonat.pb.autonat_pb2', _globals)
if not _descriptor._USE_C_DESCRIPTORS:
DESCRIPTOR._loaded_options = None
_globals['_TYPE']._serialized_start=381
_globals['_TYPE']._serialized_end=429
_globals['_STATUS']._serialized_start=431
_globals['_STATUS']._serialized_end=526
_globals['_MESSAGE']._serialized_start=53
_globals['_MESSAGE']._serialized_end=182
_globals['_DIALREQUEST']._serialized_start=184
_globals['_DIALREQUEST']._serialized_end=234
_globals['_DIALRESPONSE']._serialized_start=236
_globals['_DIALRESPONSE']._serialized_end=323
_globals['_PEERINFO']._serialized_start=325
_globals['_PEERINFO']._serialized_end=379
_globals['_AUTONAT']._serialized_start=528
_globals['_AUTONAT']._serialized_end=589
# @@protoc_insertion_point(module_scope)

View File

@ -0,0 +1,56 @@
from typing import Any, List, Optional, Union
class Message:
type: int
dial: Any
dial_response: Any
def ParseFromString(self, data: bytes) -> None: ...
def SerializeToString(self) -> bytes: ...
@staticmethod
def FromString(data: bytes) -> 'Message': ...
class DialRequest:
peers: List[Any]
def ParseFromString(self, data: bytes) -> None: ...
def SerializeToString(self) -> bytes: ...
@staticmethod
def FromString(data: bytes) -> 'DialRequest': ...
class DialResponse:
status: int
peers: List[Any]
def ParseFromString(self, data: bytes) -> None: ...
def SerializeToString(self) -> bytes: ...
@staticmethod
def FromString(data: bytes) -> 'DialResponse': ...
class PeerInfo:
id: bytes
addrs: List[bytes]
success: bool
def ParseFromString(self, data: bytes) -> None: ...
def SerializeToString(self) -> bytes: ...
@staticmethod
def FromString(data: bytes) -> 'PeerInfo': ...
class Type:
UNKNOWN: int
DIAL: int
DIAL_RESPONSE: int
@staticmethod
def Value(name: str) -> int: ...
class Status:
OK: int
E_DIAL_ERROR: int
E_DIAL_REFUSED: int
E_DIAL_FAILED: int
E_INTERNAL_ERROR: int
@staticmethod
def Value(name: str) -> int: ...

View File

@ -0,0 +1,108 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
from typing import Any, Optional
from . import autonat_pb2 as autonat__pb2
GRPC_GENERATED_VERSION = "1.71.0"
GRPC_VERSION = grpc.__version__
_version_not_supported = False
try:
from grpc._utilities import first_version_is_lower
_version_not_supported = first_version_is_lower(
GRPC_VERSION, GRPC_GENERATED_VERSION
)
except ImportError:
_version_not_supported = True
if _version_not_supported:
raise RuntimeError(
f"The grpc package installed is at version {GRPC_VERSION},"
+ f" but the generated code in autonat_pb2_grpc.py depends on"
+ f" grpcio>={GRPC_GENERATED_VERSION}."
+ f" Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}"
+ f" or downgrade your generated code using grpcio-tools<={GRPC_VERSION}."
)
class AutoNATStub:
"""AutoNAT service definition"""
def __init__(self, channel: grpc.Channel) -> None:
"""Initialize the AutoNAT stub.
Args:
----
channel (grpc.Channel): The gRPC channel instance that facilitates
communication for the AutoNAT service, providing the underlying
transport mechanism for RPC calls.
"""
self.Dial = channel.unary_unary(
"/autonat.pb.AutoNAT/Dial",
request_serializer=autonat__pb2.Message.SerializeToString,
response_deserializer=autonat__pb2.Message.FromString,
_registered_method=True,
)
class AutoNATServicer:
"""AutoNAT service definition"""
def Dial(self, request: Any, context: Any) -> Any:
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
def add_AutoNATServicer_to_server(servicer: AutoNATServicer, server: Any) -> None:
rpc_method_handlers = {
"Dial": grpc.unary_unary_rpc_method_handler(
servicer.Dial,
request_deserializer=autonat__pb2.Message.FromString,
response_serializer=autonat__pb2.Message.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
"autonat.pb.AutoNAT", rpc_method_handlers
)
server.add_generic_rpc_handlers((generic_handler,))
server.add_registered_method_handlers("autonat.pb.AutoNAT", rpc_method_handlers)
# This class is part of an EXPERIMENTAL API.
class AutoNAT:
"""AutoNAT service definition"""
@staticmethod
def Dial(
request: Any,
target: str,
options: tuple[Any, ...] = (),
channel_credentials: Optional[Any] = None,
call_credentials: Optional[Any] = None,
insecure: bool = False,
compression: Optional[Any] = None,
wait_for_ready: Optional[bool] = None,
timeout: Optional[float] = None,
metadata: Optional[list[tuple[str, str]]] = None,
) -> Any:
return grpc.experimental.unary_unary(
request,
target,
"/autonat.pb.AutoNAT/Dial",
autonat__pb2.Message.SerializeToString,
autonat__pb2.Message.FromString,
options,
channel_credentials,
insecure,
call_credentials,
compression,
wait_for_ready,
timeout,
metadata,
)

View File

@ -0,0 +1,26 @@
from typing import Any, List, Optional, Tuple, Union
import grpc
class AutoNATStub:
def __init__(self, channel: grpc.Channel) -> None: ...
Dial: Any
class AutoNATServicer:
def Dial(self, request: Any, context: Any) -> Any: ...
def add_AutoNATServicer_to_server(servicer: AutoNATServicer, server: Any) -> None: ...
class AutoNAT:
@staticmethod
def Dial(
request: Any,
target: str,
options: Tuple[Any, ...] = (),
channel_credentials: Optional[Any] = None,
call_credentials: Optional[Any] = None,
insecure: bool = False,
compression: Optional[Any] = None,
wait_for_ready: Optional[bool] = None,
timeout: Optional[float] = None,
metadata: Optional[List[Tuple[str, str]]] = None,
) -> Any: ...

View File

@ -0,0 +1,36 @@
#!/usr/bin/env python3
import subprocess
def generate_proto() -> None:
proto_file = "autonat.proto"
output_dir = "."
# Ensure protoc is installed
try:
subprocess.run(["protoc", "--version"], check=True, capture_output=True)
except subprocess.CalledProcessError:
print("Error: protoc is not installed. Please install protobuf compiler.")
return
except FileNotFoundError:
print("Error: protoc is not found in PATH. Please install protobuf compiler.")
return
# Generate Python code
cmd = [
"protoc",
"--python_out=" + output_dir,
"--grpc_python_out=" + output_dir,
"-I.",
proto_file,
]
try:
subprocess.run(cmd, check=True)
print("Successfully generated protobuf code for " + proto_file)
except subprocess.CalledProcessError as e:
print("Error generating protobuf code: " + str(e))
if __name__ == "__main__":
generate_proto()