From e8ad5b5c231ff8da6cc3a1582c8f244098e195c0 Mon Sep 17 00:00:00 2001 From: fluo10 Date: Fri, 3 Oct 2025 06:42:36 +0900 Subject: [PATCH] Split protobuf files --- .vscode/settings.json | 4 +- Cargo.toml | 2 +- core/build.rs | 8 +- .../caretta_sync/authorization_request.proto | 77 ---------- .../accept_request.proto | 9 ++ .../accept_response.proto | 8 + .../authorization_request.proto | 23 +++ .../authorization_request/identifier.proto | 12 ++ .../authorization_request/info.proto | 16 ++ .../authorization_request/info_request.proto | 8 + .../authorization_request/info_response.proto | 9 ++ .../authorization_request/list_request.proto | 4 + .../authorization_request/list_response.proto | 8 + .../reject_request.proto | 8 + .../reject_response.proto | 8 + .../authorization_request/send_request.proto | 8 + .../authorization_request/send_response.proto | 9 ++ .../authorization_request/status.proto | 10 ++ core/proto/caretta_sync/authorized_node.proto | 40 ----- .../authorized_node/authorized_node.proto | 14 ++ .../authorized_node/identifier.proto | 14 ++ .../caretta_sync/authorized_node/info.proto | 12 ++ .../authorized_node/info_request.proto | 9 ++ .../authorized_node/info_response.proto | 8 + .../authorized_node/list_request.proto | 5 + .../authorized_node/list_response.proto | 9 ++ core/proto/caretta_sync/common.proto | 41 ----- core/proto/caretta_sync/common/url.proto | 8 + core/proto/caretta_sync/common/uuid.proto | 9 ++ .../caretta_sync/iroh/connection_type.proto | 26 ++++ .../proto/caretta_sync/iroh/control_msg.proto | 18 +++ .../caretta_sync/iroh/direct_addr_info.proto | 30 ++++ core/proto/caretta_sync/iroh/public_key.proto | 8 + .../caretta_sync/iroh/relay_url_info.proto | 12 ++ .../proto/caretta_sync/iroh/remote_info.proto | 18 +++ core/proto/caretta_sync/iroh/source.proto | 23 +++ core/proto/caretta_sync/net/ipv4_addr.proto | 7 + core/proto/caretta_sync/net/ipv6_addr.proto | 7 + core/proto/caretta_sync/net/socket_addr.proto | 19 +++ .../caretta_sync/net/socket_addr_v4.proto | 9 ++ .../caretta_sync/net/socket_addr_v6.proto | 9 ++ core/proto/caretta_sync/remote_node.proto | 107 ------------- .../caretta_sync/remote_node/identifier.proto | 13 ++ .../proto/caretta_sync/remote_node/info.proto | 19 +++ .../remote_node/info_request.proto | 16 ++ .../remote_node/info_response.proto | 8 + .../remote_node/list_request.proto | 4 + .../remote_node/list_response.proto | 8 + .../remote_node/remote_node.proto | 12 ++ core/src/proto/authorization_request.rs | 27 ++++ core/src/proto/authorized_node.rs | 19 +++ core/src/proto/common.rs | 133 ++--------------- core/src/proto/iroh.rs | 120 +++++++++++++++ core/src/proto/mod.rs | 20 +-- core/src/proto/net.rs | 140 ++++++++++++++++++ core/src/proto/remote_node.rs | 74 +-------- 56 files changed, 826 insertions(+), 480 deletions(-) delete mode 100644 core/proto/caretta_sync/authorization_request.proto create mode 100644 core/proto/caretta_sync/authorization_request/accept_request.proto create mode 100644 core/proto/caretta_sync/authorization_request/accept_response.proto create mode 100644 core/proto/caretta_sync/authorization_request/authorization_request.proto create mode 100644 core/proto/caretta_sync/authorization_request/identifier.proto create mode 100644 core/proto/caretta_sync/authorization_request/info.proto create mode 100644 core/proto/caretta_sync/authorization_request/info_request.proto create mode 100644 core/proto/caretta_sync/authorization_request/info_response.proto create mode 100644 core/proto/caretta_sync/authorization_request/list_request.proto create mode 100644 core/proto/caretta_sync/authorization_request/list_response.proto create mode 100644 core/proto/caretta_sync/authorization_request/reject_request.proto create mode 100644 core/proto/caretta_sync/authorization_request/reject_response.proto create mode 100644 core/proto/caretta_sync/authorization_request/send_request.proto create mode 100644 core/proto/caretta_sync/authorization_request/send_response.proto create mode 100644 core/proto/caretta_sync/authorization_request/status.proto delete mode 100644 core/proto/caretta_sync/authorized_node.proto create mode 100644 core/proto/caretta_sync/authorized_node/authorized_node.proto create mode 100644 core/proto/caretta_sync/authorized_node/identifier.proto create mode 100644 core/proto/caretta_sync/authorized_node/info.proto create mode 100644 core/proto/caretta_sync/authorized_node/info_request.proto create mode 100644 core/proto/caretta_sync/authorized_node/info_response.proto create mode 100644 core/proto/caretta_sync/authorized_node/list_request.proto create mode 100644 core/proto/caretta_sync/authorized_node/list_response.proto delete mode 100644 core/proto/caretta_sync/common.proto create mode 100644 core/proto/caretta_sync/common/url.proto create mode 100644 core/proto/caretta_sync/common/uuid.proto create mode 100644 core/proto/caretta_sync/iroh/connection_type.proto create mode 100644 core/proto/caretta_sync/iroh/control_msg.proto create mode 100644 core/proto/caretta_sync/iroh/direct_addr_info.proto create mode 100644 core/proto/caretta_sync/iroh/public_key.proto create mode 100644 core/proto/caretta_sync/iroh/relay_url_info.proto create mode 100644 core/proto/caretta_sync/iroh/remote_info.proto create mode 100644 core/proto/caretta_sync/iroh/source.proto create mode 100644 core/proto/caretta_sync/net/ipv4_addr.proto create mode 100644 core/proto/caretta_sync/net/ipv6_addr.proto create mode 100644 core/proto/caretta_sync/net/socket_addr.proto create mode 100644 core/proto/caretta_sync/net/socket_addr_v4.proto create mode 100644 core/proto/caretta_sync/net/socket_addr_v6.proto delete mode 100644 core/proto/caretta_sync/remote_node.proto create mode 100644 core/proto/caretta_sync/remote_node/identifier.proto create mode 100644 core/proto/caretta_sync/remote_node/info.proto create mode 100644 core/proto/caretta_sync/remote_node/info_request.proto create mode 100644 core/proto/caretta_sync/remote_node/info_response.proto create mode 100644 core/proto/caretta_sync/remote_node/list_request.proto create mode 100644 core/proto/caretta_sync/remote_node/list_response.proto create mode 100644 core/proto/caretta_sync/remote_node/remote_node.proto create mode 100644 core/src/proto/iroh.rs create mode 100644 core/src/proto/net.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index 11a790d..3e21c62 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,10 @@ { "protoc": { + "compile_on_save": true, "options": [ "--proto_path=${workspaceRoot}/tripod-id/proto", - "--proto_path=${workspaceRoot}/core/proto" + "--proto_path=${workspaceRoot}/core/proto", + "--java_out=${workspaceRoot}/.tmp" ] } } \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 7055388..f54ed14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,7 @@ tokio-stream = "0.1.17" tonic = "0.14.0" url = { version = "2.5.7", features = ["serde"] } uuid = { version = "1.17.0", features = ["v7"] } -iroh = { version = "0.91.2", features = ["discovery-local-network", "discovery-pkarr-dht"] } +iroh = { version = "0.92.0", features = ["discovery-local-network", "discovery-pkarr-dht"] } prost = "0.14.1" prost-types = "0.14.1" tonic-prost-build = "0.14.0" diff --git a/core/build.rs b/core/build.rs index 7328d69..ed7aa42 100644 --- a/core/build.rs +++ b/core/build.rs @@ -3,10 +3,10 @@ fn main() -> Result<(), Box> { .extern_path(".tripod_id", "::tripod_id::prost") .compile_protos( &[ - "proto/caretta_sync/authorization_request.proto", - "proto/caretta_sync/authorized_node.proto", - "proto/caretta_sync/remote_node.proto", - "proto/caretta_sync/common.proto" + "proto/caretta_sync/authorization_request/authorization_request.proto", + "proto/caretta_sync/authorized_node/authorized_node.proto", + "proto/caretta_sync/remote_node/remote_node.proto", + ], &["proto", "../tripod-id/proto"] )?; diff --git a/core/proto/caretta_sync/authorization_request.proto b/core/proto/caretta_sync/authorization_request.proto deleted file mode 100644 index 1167df7..0000000 --- a/core/proto/caretta_sync/authorization_request.proto +++ /dev/null @@ -1,77 +0,0 @@ -syntax = "proto3"; -package caretta_sync; - -import "caretta_sync/common.proto"; -import "caretta_sync/authorized_node.proto"; -import "caretta_sync/remote_node.proto"; -import "tripod_id.proto"; -import "google/protobuf/timestamp.proto"; -import "google/protobuf/duration.proto"; - -service AuthorizationRequest { - rpc Send(AuthorizationRequestSendRequest) returns (AuthorizationRequestSendResponse); - rpc Accept(AuthorizationRequestAcceptRequest) returns (AuthorizationRequestAcceptResponse); - rpc Reject(AuthorizationRequestRejectRequest) returns (AuthorizationRequestRejectResponse); - rpc Info(AuthorizationRequestInfoRequest) returns (AuthorizationRequestInfoResponse); - rpc InfoStream(stream AuthorizationRequestInfoStreamRequest) returns (stream AuthorizationRequestInfoStreamResponse); -} - -enum AuthorizationRequestKind { - SENT = 0; - RECEIVED = 1; -} - -message AuthorizationRequestSendRequest { - caretta_sync.RemoteNodeIdentifier remote_node = 1; -} - -message AuthorizationRequestSendResponse { - caretta_sync.RemoteNodeInfo remote_node_info = 1; - string passcode = 2; -} - -message AuthorizationRequestInfo { - tripod_id.Double id = 1; - caretta_sync.PublicKey public_key = 2; - google.protobuf.Timestamp created_at = 3; - AuthorizationRequestKind kind = 4; - string node_info = 5; -} - - -message AuthorizationRequestIdentifier { - oneof identifier { - caretta_sync.Uuid uuid = 1; - tripod_id.Double id = 2; - } -} - -message AuthorizationRequestAcceptRequest { - AuthorizationRequestIdentifier authorization_request = 1; - string passcode = 2; -} - -message AuthorizationRequestAcceptResponse { - caretta_sync.AuthorizedNodeInfo authorized_node_info = 1; -} - -message AuthorizationRequestRejectRequest { - AuthorizationRequestIdentifier authorization_request = 1; -} - -message AuthorizationRequestRejectResponse { - AuthorizationRequestInfo request_info = 1; -} - -message AuthorizationRequestInfoRequest { - AuthorizationRequestIdentifier request = 1; -} -message AuthorizationRequestInfoResponse{ - AuthorizationRequestInfo info = 1; -} - -message AuthorizationRequestInfoStreamRequest {} - -message AuthorizationRequestInfoStreamResponse { - AuthorizationRequestInfo request_info = 1; -} \ No newline at end of file diff --git a/core/proto/caretta_sync/authorization_request/accept_request.proto b/core/proto/caretta_sync/authorization_request/accept_request.proto new file mode 100644 index 0000000..c737fe2 --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/accept_request.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/authorization_request/identifier.proto"; + +message AcceptRequest { + Identifier authorization_request = 1; + string passcode = 2; +} diff --git a/core/proto/caretta_sync/authorization_request/accept_response.proto b/core/proto/caretta_sync/authorization_request/accept_response.proto new file mode 100644 index 0000000..3b7c15a --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/accept_response.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/authorized_node/info.proto"; + +message AcceptResponse { + caretta_sync.authorized_node.Info authorized_node_info = 1; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/authorization_request/authorization_request.proto b/core/proto/caretta_sync/authorization_request/authorization_request.proto new file mode 100644 index 0000000..5b33767 --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/authorization_request.proto @@ -0,0 +1,23 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/authorization_request/accept_request.proto"; +import "caretta_sync/authorization_request/accept_response.proto"; +import "caretta_sync/authorization_request/info_request.proto"; +import "caretta_sync/authorization_request/info_response.proto"; +import "caretta_sync/authorization_request/list_request.proto"; +import "caretta_sync/authorization_request/list_response.proto"; +import "caretta_sync/authorization_request/reject_request.proto"; +import "caretta_sync/authorization_request/reject_response.proto"; +import "caretta_sync/authorization_request/send_request.proto"; +import "caretta_sync/authorization_request/send_response.proto"; + + + +service AuthorizationRequest { + rpc Send(SendRequest) returns (SendResponse); + rpc Accept(AcceptRequest) returns (AcceptResponse); + rpc Reject(RejectRequest) returns (RejectResponse); + rpc Info(InfoRequest) returns (InfoResponse); + rpc List(stream ListRequest) returns (stream ListResponse); +} diff --git a/core/proto/caretta_sync/authorization_request/identifier.proto b/core/proto/caretta_sync/authorization_request/identifier.proto new file mode 100644 index 0000000..52553de --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/identifier.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/common/uuid.proto"; +import "tripod_id/double.proto"; + +message Identifier { + oneof identifier_value { + caretta_sync.common.Uuid uuid = 1; + tripod_id.Double id = 2; + } +} diff --git a/core/proto/caretta_sync/authorization_request/info.proto b/core/proto/caretta_sync/authorization_request/info.proto new file mode 100644 index 0000000..d236d66 --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/info.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/iroh/public_key.proto"; +import "caretta_sync/authorization_request/status.proto"; +import "tripod_id/double.proto"; +import "google/protobuf/timestamp.proto"; + +message Info { + tripod_id.Double id = 1; + caretta_sync.iroh.PublicKey public_key = 2; + google.protobuf.Timestamp created_at = 3; + google.protobuf.Timestamp closed_at = 6; + Status status = 4; +} + diff --git a/core/proto/caretta_sync/authorization_request/info_request.proto b/core/proto/caretta_sync/authorization_request/info_request.proto new file mode 100644 index 0000000..5021b4d --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/info_request.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/authorization_request/identifier.proto"; + +message InfoRequest { + Identifier request = 1; +} diff --git a/core/proto/caretta_sync/authorization_request/info_response.proto b/core/proto/caretta_sync/authorization_request/info_response.proto new file mode 100644 index 0000000..d08cddc --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/info_response.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/authorization_request/info.proto"; + + +message InfoResponse{ + Info info = 1; +} diff --git a/core/proto/caretta_sync/authorization_request/list_request.proto b/core/proto/caretta_sync/authorization_request/list_request.proto new file mode 100644 index 0000000..53a332d --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/list_request.proto @@ -0,0 +1,4 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +message ListRequest {} diff --git a/core/proto/caretta_sync/authorization_request/list_response.proto b/core/proto/caretta_sync/authorization_request/list_response.proto new file mode 100644 index 0000000..21b4a54 --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/list_response.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/authorization_request/info.proto"; + +message ListResponse { + Info request_info = 1; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/authorization_request/reject_request.proto b/core/proto/caretta_sync/authorization_request/reject_request.proto new file mode 100644 index 0000000..cac182c --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/reject_request.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/authorization_request/identifier.proto"; + +message RejectRequest { + Identifier authorization_request = 1; +} diff --git a/core/proto/caretta_sync/authorization_request/reject_response.proto b/core/proto/caretta_sync/authorization_request/reject_response.proto new file mode 100644 index 0000000..8514088 --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/reject_response.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/authorization_request/info.proto"; + +message RejectResponse { + Info request_info = 1; +} diff --git a/core/proto/caretta_sync/authorization_request/send_request.proto b/core/proto/caretta_sync/authorization_request/send_request.proto new file mode 100644 index 0000000..34727fa --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/send_request.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/remote_node/identifier.proto"; + +message SendRequest { + caretta_sync.remote_node.Identifier remote_node = 1; +} diff --git a/core/proto/caretta_sync/authorization_request/send_response.proto b/core/proto/caretta_sync/authorization_request/send_response.proto new file mode 100644 index 0000000..7ce3c89 --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/send_response.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +import "caretta_sync/remote_node/info.proto"; + +message SendResponse { + caretta_sync.remote_node.Info remote_node_info = 1; + string passcode = 2; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/authorization_request/status.proto b/core/proto/caretta_sync/authorization_request/status.proto new file mode 100644 index 0000000..dc54330 --- /dev/null +++ b/core/proto/caretta_sync/authorization_request/status.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; +package caretta_sync.authorization_request; + +enum Status { + UNSPECIFIED = 0; + SENT = 1; + RECEIVED = 2; + ACCEPTED = 3; + REJECTED = 4; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/authorized_node.proto b/core/proto/caretta_sync/authorized_node.proto deleted file mode 100644 index 4c28172..0000000 --- a/core/proto/caretta_sync/authorized_node.proto +++ /dev/null @@ -1,40 +0,0 @@ -syntax = "proto3"; - -// The package contain service and messages about node already authorized. -// The authorized node includes not only remote authorized peers but also local node itself. -package caretta_sync; - -import "tripod_id.proto"; -import "caretta_sync/remote_node.proto"; -import "caretta_sync/common.proto"; - -service AuthorizadNode { - rpc Info(AuthorizedNodeInfoRequest) returns (AuthorizedNodeInfoResponse); - rpc InfoStream(stream AuthorizedNodeInfoStreamRequest) returns (stream AuthorizedNodeInfoStreamResponse); -} - -message AuthorizedNodeIdentifier { - oneof identifier { - tripod_id.Single id = 1; - caretta_sync.PublicKey public_key = 2; - } -} - -message AuthorizedNodeInfo { - tripod_id.Single id = 1; - caretta_sync.PublicKey public_key = 2; - string note = 3; -} - -message AuthorizedNodeInfoRequest { - AuthorizedNodeIdentifier node = 1; -} -message AuthorizedNodeInfoResponse { - AuthorizedNodeInfo node_info = 1; -} - -message AuthorizedNodeInfoStreamRequest{} - -message AuthorizedNodeInfoStreamResponse { - AuthorizedNodeInfo node_info = 1; -} \ No newline at end of file diff --git a/core/proto/caretta_sync/authorized_node/authorized_node.proto b/core/proto/caretta_sync/authorized_node/authorized_node.proto new file mode 100644 index 0000000..9553f52 --- /dev/null +++ b/core/proto/caretta_sync/authorized_node/authorized_node.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package caretta_sync.authorized_node; + +import "caretta_sync/authorized_node/info_request.proto"; +import "caretta_sync/authorized_node/info_response.proto"; +import "caretta_sync/authorized_node/list_request.proto"; +import "caretta_sync/authorized_node/list_response.proto"; + + +service AuthorizedNode { + rpc Info(InfoRequest) returns (InfoResponse); + rpc List(stream ListRequest) returns (stream ListResponse); +} diff --git a/core/proto/caretta_sync/authorized_node/identifier.proto b/core/proto/caretta_sync/authorized_node/identifier.proto new file mode 100644 index 0000000..0060c68 --- /dev/null +++ b/core/proto/caretta_sync/authorized_node/identifier.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package caretta_sync.authorized_node; + +import "tripod_id/single.proto"; +import "caretta_sync/iroh/public_key.proto"; + + +message Identifier { + oneof identifier_value { + tripod_id.Single id = 1; + caretta_sync.iroh.PublicKey public_key = 2; + } +} \ No newline at end of file diff --git a/core/proto/caretta_sync/authorized_node/info.proto b/core/proto/caretta_sync/authorized_node/info.proto new file mode 100644 index 0000000..88b6bb7 --- /dev/null +++ b/core/proto/caretta_sync/authorized_node/info.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; + +package caretta_sync.authorized_node; + +import "tripod_id/single.proto"; +import "caretta_sync/iroh/public_key.proto"; + +message Info { + tripod_id.Single id = 1; + caretta_sync.iroh.PublicKey public_key = 2; + string note = 3; +} diff --git a/core/proto/caretta_sync/authorized_node/info_request.proto b/core/proto/caretta_sync/authorized_node/info_request.proto new file mode 100644 index 0000000..e64ac3f --- /dev/null +++ b/core/proto/caretta_sync/authorized_node/info_request.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +package caretta_sync.authorized_node; + +import "caretta_sync/authorized_node/identifier.proto"; + +message InfoRequest { + Identifier node = 1; +} diff --git a/core/proto/caretta_sync/authorized_node/info_response.proto b/core/proto/caretta_sync/authorized_node/info_response.proto new file mode 100644 index 0000000..640a95e --- /dev/null +++ b/core/proto/caretta_sync/authorized_node/info_response.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +package caretta_sync.authorized_node; +import "caretta_sync/authorized_node/info.proto"; + +message InfoResponse { + Info node_info = 1; +} diff --git a/core/proto/caretta_sync/authorized_node/list_request.proto b/core/proto/caretta_sync/authorized_node/list_request.proto new file mode 100644 index 0000000..a0f0ea0 --- /dev/null +++ b/core/proto/caretta_sync/authorized_node/list_request.proto @@ -0,0 +1,5 @@ +syntax = "proto3"; + +package caretta_sync.authorized_node; + +message ListRequest{} \ No newline at end of file diff --git a/core/proto/caretta_sync/authorized_node/list_response.proto b/core/proto/caretta_sync/authorized_node/list_response.proto new file mode 100644 index 0000000..aaa4f82 --- /dev/null +++ b/core/proto/caretta_sync/authorized_node/list_response.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +package caretta_sync.authorized_node; + +import "caretta_sync/authorized_node/info.proto"; + +message ListResponse { + Info node_info = 1; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/common.proto b/core/proto/caretta_sync/common.proto deleted file mode 100644 index 920fe5b..0000000 --- a/core/proto/caretta_sync/common.proto +++ /dev/null @@ -1,41 +0,0 @@ -syntax = "proto3"; - -package caretta_sync; - -message PublicKey { - bytes key = 1; -} - -message Uuid { - uint64 high_bits = 1; - uint64 low_bits = 2; -} - -message Url { - string url = 1; -} - -message SocketAddr { - oneof socket_addr { - SocketAddrV4 v4 = 1; - SocketAddrV6 v6 = 2; - } -} - -message SocketAddrV4 { - Ipv4Addr ip = 1; - uint32 port = 2; -} - -message Ipv4Addr { - uint32 bits = 1; -} - -message SocketAddrV6 { - Ipv6Addr ip = 1; - uint32 port = 2; -} -message Ipv6Addr { - uint64 high_bits = 1; - uint64 low_bits = 2; -} \ No newline at end of file diff --git a/core/proto/caretta_sync/common/url.proto b/core/proto/caretta_sync/common/url.proto new file mode 100644 index 0000000..f147168 --- /dev/null +++ b/core/proto/caretta_sync/common/url.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +package caretta_sync.common; + +// protobuf message of url::Url. +message Url { + string url = 1; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/common/uuid.proto b/core/proto/caretta_sync/common/uuid.proto new file mode 100644 index 0000000..656ffb3 --- /dev/null +++ b/core/proto/caretta_sync/common/uuid.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +package caretta_sync.common; + +// protobuf message of uuid::Uuid +message Uuid { + uint64 high_bits = 1; + uint64 low_bits = 2; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/iroh/connection_type.proto b/core/proto/caretta_sync/iroh/connection_type.proto new file mode 100644 index 0000000..1d9b2f3 --- /dev/null +++ b/core/proto/caretta_sync/iroh/connection_type.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; +package caretta_sync.iroh; + +import "caretta_sync/common/url.proto"; +import "caretta_sync/net/socket_addr.proto"; + +// A protobuf message of iroh::ConnectionType +message ConnectionType { + message Direct { + caretta_sync.net.SocketAddr direct_value = 1; + } + message Relay { + caretta_sync.common.Url relay_value = 1; + } + message Mixed { + caretta_sync.net.SocketAddr socket_addr = 1; + caretta_sync.common.Url relay_url = 2; + } + message None{} + oneof connection_type_value { + Direct direct = 1; + Relay relay = 2; + Mixed mixed = 3; + None none = 4; + } +} \ No newline at end of file diff --git a/core/proto/caretta_sync/iroh/control_msg.proto b/core/proto/caretta_sync/iroh/control_msg.proto new file mode 100644 index 0000000..27e8c66 --- /dev/null +++ b/core/proto/caretta_sync/iroh/control_msg.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; +package caretta_sync.iroh; + +// The message of iroh::endpoint::ControlMsg. +// To ensure compatiility with irof::endpoint::ControlMsg, +// it's implemented as a message rather than an enum to accommodate the posibility +// that values may be added to the enum in rust in the future. +message ControlMsg { + message Ping {} + message Pong {} + message CallMeMayBe {} + + oneof control_msg_vaue { + Ping ping = 1; + Pong pong = 2; + CallMeMayBe call_me_maybe = 3; + } +} \ No newline at end of file diff --git a/core/proto/caretta_sync/iroh/direct_addr_info.proto b/core/proto/caretta_sync/iroh/direct_addr_info.proto new file mode 100644 index 0000000..5c40425 --- /dev/null +++ b/core/proto/caretta_sync/iroh/direct_addr_info.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; +package caretta_sync.iroh; + +import "google/protobuf/duration.proto"; +import "caretta_sync/iroh/control_msg.proto"; +import "caretta_sync/iroh/source.proto"; +import "caretta_sync/net/socket_addr.proto"; + +// A protobuf message of iroh::endpoint::DirectAddrInfo +message DirectAddrInfo { + // A protobuf message of (Duration, ControlMsg) + message DurationControlMsg { + + google.protobuf.Duration duration = 1; + ControlMsg control_msg = 2; + } + + // A protobuf message of (iroh::Source, Duration) + message SourceDuration { + Source source = 1; + google.protobuf.Duration duration = 2; + } + + caretta_sync.net.SocketAddr addr = 1; + google.protobuf.Duration latency = 2; + DurationControlMsg last_control = 3; + google.protobuf.Duration last_payload = 4; + google.protobuf.Duration last_alive = 5; + repeated SourceDuration sources = 6; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/iroh/public_key.proto b/core/proto/caretta_sync/iroh/public_key.proto new file mode 100644 index 0000000..8302bc4 --- /dev/null +++ b/core/proto/caretta_sync/iroh/public_key.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +package caretta_sync.iroh; + +// protobuf message of iroh::PublicKey. +message PublicKey { + bytes key = 1; +} diff --git a/core/proto/caretta_sync/iroh/relay_url_info.proto b/core/proto/caretta_sync/iroh/relay_url_info.proto new file mode 100644 index 0000000..72e2a39 --- /dev/null +++ b/core/proto/caretta_sync/iroh/relay_url_info.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; +package caretta_sync.iroh; + +import "google/protobuf/duration.proto"; +import "caretta_sync/common/url.proto"; + +// A protobuf message of iroh::RelayUrlInfo +message RelayUrlInfo { + caretta_sync.common.Url relay_url = 1; + google.protobuf.Duration last_alive = 2; + google.protobuf.Duration latency = 3; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/iroh/remote_info.proto b/core/proto/caretta_sync/iroh/remote_info.proto new file mode 100644 index 0000000..356f0d0 --- /dev/null +++ b/core/proto/caretta_sync/iroh/remote_info.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; +package caretta_sync.iroh; + +import "caretta_sync/iroh/public_key.proto"; +import "caretta_sync/iroh/relay_url_info.proto"; +import "caretta_sync/iroh/direct_addr_info.proto"; +import "caretta_sync/iroh/connection_type.proto"; +import "google/protobuf/duration.proto"; + +// A messege of iroh::RemoteInfo. +message RemoteInfo { + PublicKey node_id = 3; + RelayUrlInfo relay_url = 4; + repeated DirectAddrInfo addrs = 5; + ConnectionType conn_type = 6; + google.protobuf.Duration latency = 7; + google.protobuf.Duration last_used = 8; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/iroh/source.proto b/core/proto/caretta_sync/iroh/source.proto new file mode 100644 index 0000000..070602f --- /dev/null +++ b/core/proto/caretta_sync/iroh/source.proto @@ -0,0 +1,23 @@ +syntax = "proto3"; +package caretta_sync.iroh; + +message Source { + message Saved{} + message Udp{} + message Relay{} + message App{} + message Discovery{ + string value = 1; + } + message NamedApp { + string value = 1; + } + oneof source_value { + Saved saved = 1; + Udp udp = 2; + Relay relay = 3; + App app = 4; + Discovery discovery = 5; + NamedApp named_app = 6; + }; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/net/ipv4_addr.proto b/core/proto/caretta_sync/net/ipv4_addr.proto new file mode 100644 index 0000000..df4f212 --- /dev/null +++ b/core/proto/caretta_sync/net/ipv4_addr.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; +package caretta_sync.net; + +message Ipv4Addr { + uint32 bits = 1; +} + diff --git a/core/proto/caretta_sync/net/ipv6_addr.proto b/core/proto/caretta_sync/net/ipv6_addr.proto new file mode 100644 index 0000000..d7b4e7a --- /dev/null +++ b/core/proto/caretta_sync/net/ipv6_addr.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; +package caretta_sync.net; + +message Ipv6Addr { + uint64 high_bits = 1; + uint64 low_bits = 2; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/net/socket_addr.proto b/core/proto/caretta_sync/net/socket_addr.proto new file mode 100644 index 0000000..d3e9c68 --- /dev/null +++ b/core/proto/caretta_sync/net/socket_addr.proto @@ -0,0 +1,19 @@ +syntax = "proto3"; + +package caretta_sync.net; + +import "caretta_sync/net/socket_addr_v4.proto"; +import "caretta_sync/net/socket_addr_v6.proto"; + +// Protobuf message of std::net::SocketAddr. +message SocketAddr { + oneof socket_addr_value { + SocketAddrV4 v4 = 1; + SocketAddrV6 v6 = 2; + } +} + + + + + diff --git a/core/proto/caretta_sync/net/socket_addr_v4.proto b/core/proto/caretta_sync/net/socket_addr_v4.proto new file mode 100644 index 0000000..8633f79 --- /dev/null +++ b/core/proto/caretta_sync/net/socket_addr_v4.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; +package caretta_sync.net; + +import "caretta_sync/net/ipv4_addr.proto"; + +message SocketAddrV4 { + Ipv4Addr ip = 1; + uint32 port = 2; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/net/socket_addr_v6.proto b/core/proto/caretta_sync/net/socket_addr_v6.proto new file mode 100644 index 0000000..680a1a9 --- /dev/null +++ b/core/proto/caretta_sync/net/socket_addr_v6.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; +package caretta_sync.net; + +import "caretta_sync/net/ipv6_addr.proto"; + +message SocketAddrV6 { + Ipv6Addr ip = 1; + uint32 port = 2; +} \ No newline at end of file diff --git a/core/proto/caretta_sync/remote_node.proto b/core/proto/caretta_sync/remote_node.proto deleted file mode 100644 index c9493f4..0000000 --- a/core/proto/caretta_sync/remote_node.proto +++ /dev/null @@ -1,107 +0,0 @@ -syntax = "proto3"; -package caretta_sync; - -import "caretta_sync/common.proto"; -import "tripod_id.proto"; -import "google/protobuf/timestamp.proto"; -import "google/protobuf/duration.proto"; -import "google/protobuf/empty.proto"; - -service RemoteNode { - rpc Info(RemoteNodeInfoRequest) returns (RemoteNodeInfoResponse); - rpc List(stream RemoteNodeListRequest) returns (stream RemoteNodeListResponse); -} - -message RemoteNodeIdentifier { - oneof identifier { - tripod_id.Double id = 1; - caretta_sync.PublicKey public_key = 2; - } -} - -message RemoteNodeInfoRequest { - RemoteNodeIdentifier remote_node = 1; -} - -message RemoteNodeListRequest {} - -message RemoteNodeInfoResponse { - RemoteNodeInfo remote_node_info = 1; -} - -message RemoteNodeListResponse { - RemoteNodeInfo remote_node_info = 1; -} - - -message RemoteNodeInfo { - - // A messege of iroh::RemoteInfo. - message RemoteInfo { - message ConnectionType { - message Mixed { - caretta_sync.SocketAddr socket_addr = 1; - caretta_sync.Url relay_url = 2; - } - oneof conn_type { - caretta_sync.SocketAddr direct = 1; - caretta_sync.Url relay = 2; - Mixed mixed = 3; - google.protobuf.Empty none = 4; - } - } - message DirectAddrInfo { - message LastControl { - enum ControlMsg { - PING = 0; - PONG = 1; - CALL_ME_MAYBE = 2; - } - google.protobuf.Duration duration = 1; - ControlMsg control_msg = 2; - } - message SourceDuration { - oneof source { - google.protobuf.Empty saved = 1; - google.protobuf.Empty udp = 2; - google.protobuf.Empty Relay = 3; - google.protobuf.Empty App = 4; - string discovery = 5; - string named_app = 6; - }; - google.protobuf.Duration duration = 7; - } - caretta_sync.SocketAddr addr = 1; - google.protobuf.Duration latency = 2; - LastControl last_control = 3; - google.protobuf.Duration last_payload = 4; - google.protobuf.Duration last_alive = 5; - repeated SourceDuration sources = 6; - } - - message RelayUrlInfo { - caretta_sync.Url relay_url = 1; - google.protobuf.Duration last_alive = 2; - google.protobuf.Duration latency = 3; - } - - bool authorized = 2; - caretta_sync.PublicKey public_key = 3; - RelayUrlInfo relay_url = 4; - repeated DirectAddrInfo addrs = 5; - ConnectionType conn_type = 6; - google.protobuf.Duration latency = 7; - google.protobuf.Duration last_used = 8; - } - tripod_id.Double public_id = 1; - RemoteInfo remote_info = 2; -} - - - - - - - - - diff --git a/core/proto/caretta_sync/remote_node/identifier.proto b/core/proto/caretta_sync/remote_node/identifier.proto new file mode 100644 index 0000000..3b153d9 --- /dev/null +++ b/core/proto/caretta_sync/remote_node/identifier.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; +package caretta_sync.remote_node; + +import "caretta_sync/iroh/public_key.proto"; +import "tripod_id/double.proto"; + + +message Identifier { + oneof identifier_value { + tripod_id.Double id = 1; + caretta_sync.iroh.PublicKey public_key = 2; + } +} \ No newline at end of file diff --git a/core/proto/caretta_sync/remote_node/info.proto b/core/proto/caretta_sync/remote_node/info.proto new file mode 100644 index 0000000..63351f9 --- /dev/null +++ b/core/proto/caretta_sync/remote_node/info.proto @@ -0,0 +1,19 @@ +syntax = "proto3"; +package caretta_sync.remote_node; + +import "caretta_sync/iroh/remote_info.proto"; +import "tripod_id/double.proto"; + +message Info { + tripod_id.Double public_id = 1; + caretta_sync.iroh.RemoteInfo remote_info = 2; +} + + + + + + + + + diff --git a/core/proto/caretta_sync/remote_node/info_request.proto b/core/proto/caretta_sync/remote_node/info_request.proto new file mode 100644 index 0000000..ad95943 --- /dev/null +++ b/core/proto/caretta_sync/remote_node/info_request.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; +package caretta_sync.remote_node; + +import "caretta_sync/remote_node/identifier.proto"; + +message InfoRequest { + Identifier remote_node = 1; +} + + + + + + + + diff --git a/core/proto/caretta_sync/remote_node/info_response.proto b/core/proto/caretta_sync/remote_node/info_response.proto new file mode 100644 index 0000000..ba9f160 --- /dev/null +++ b/core/proto/caretta_sync/remote_node/info_response.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package caretta_sync.remote_node; + +import "caretta_sync/remote_node/info.proto"; + +message InfoResponse { + Info remote_node_info = 1; +} diff --git a/core/proto/caretta_sync/remote_node/list_request.proto b/core/proto/caretta_sync/remote_node/list_request.proto new file mode 100644 index 0000000..aec884b --- /dev/null +++ b/core/proto/caretta_sync/remote_node/list_request.proto @@ -0,0 +1,4 @@ +syntax = "proto3"; +package caretta_sync.remote_node; + +message ListRequest{} \ No newline at end of file diff --git a/core/proto/caretta_sync/remote_node/list_response.proto b/core/proto/caretta_sync/remote_node/list_response.proto new file mode 100644 index 0000000..c039b29 --- /dev/null +++ b/core/proto/caretta_sync/remote_node/list_response.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package caretta_sync.remote_node; + +import "caretta_sync/remote_node/info.proto"; + +message ListResponse { + Info remote_node_info = 1; +} diff --git a/core/proto/caretta_sync/remote_node/remote_node.proto b/core/proto/caretta_sync/remote_node/remote_node.proto new file mode 100644 index 0000000..055cac1 --- /dev/null +++ b/core/proto/caretta_sync/remote_node/remote_node.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; +package caretta_sync.remote_node; + +import "caretta_sync/remote_node/info_request.proto"; +import "caretta_sync/remote_node/info_response.proto"; +import "caretta_sync/remote_node/list_request.proto"; +import "caretta_sync/remote_node/list_response.proto"; + +service RemoteNode { + rpc Info(InfoRequest) returns (InfoResponse); + rpc List(stream ListRequest) returns (stream ListResponse); +} diff --git a/core/src/proto/authorization_request.rs b/core/src/proto/authorization_request.rs index e69de29..2585c31 100644 --- a/core/src/proto/authorization_request.rs +++ b/core/src/proto/authorization_request.rs @@ -0,0 +1,27 @@ +use std::pin::Pin; + +use futures::Stream; +use tonic::{Request, Response, Streaming}; + +tonic::include_proto!("caretta_sync.authorization_request"); +pub struct AuthorizationRequestService {} + +#[tonic::async_trait] +impl authorization_request_server::AuthorizationRequest for AuthorizationRequestService { + type ListStream = Pin> + Send>>; + async fn send(&self, request: Request) -> Result, tonic::Status> { + todo!() + } + async fn accept(&self, request: Request) -> Result, tonic::Status>{ + todo!() + } + async fn reject(&self, request: Request) -> Result, tonic::Status>{ + todo!() + } + async fn info(&self, request: Request) -> Result, tonic::Status>{ + todo!() + } + async fn list(&self, request: Request>) -> Result, tonic::Status> { + todo!() + } +} \ No newline at end of file diff --git a/core/src/proto/authorized_node.rs b/core/src/proto/authorized_node.rs index e69de29..667fbeb 100644 --- a/core/src/proto/authorized_node.rs +++ b/core/src/proto/authorized_node.rs @@ -0,0 +1,19 @@ +use std::pin::Pin; + +use futures::Stream; +use tonic::{Request, Response, Streaming, Status}; + +tonic::include_proto!("caretta_sync.authorized_node"); +pub struct AuthorizedNodeService {} + +#[tonic::async_trait] +impl authorized_node_server::AuthorizedNode for AuthorizedNodeService { + type ListStream = Pin> + Send>>; + + async fn info(&self, request: Request) -> Result, tonic::Status>{ + todo!() + } + async fn list(&self, request: Request>) -> Result, tonic::Status> { + todo!() + } +} \ No newline at end of file diff --git a/core/src/proto/common.rs b/core/src/proto/common.rs index c957982..176b434 100644 --- a/core/src/proto/common.rs +++ b/core/src/proto/common.rs @@ -1,22 +1,9 @@ use super::*; +tonic::include_proto!("caretta_sync.common"); -use crate::proto::{error::{ProtoDeserializeError, ProtoSerializeError}, socket_addr}; +use crate::proto::{error::{ProtoDeserializeError, ProtoSerializeError}}; -impl From for PublicKeyMessage { - fn from(value: iroh::PublicKey) -> Self { - Self{ key: Vec::from(value.as_bytes()) } - } -} - -impl TryFrom for iroh::PublicKey { - type Error = ProtoDeserializeError; - fn try_from(value: PublicKeyMessage) -> Result { - let slice: [u8; 32] = value.key[0..32].try_into()?; - Ok(iroh::PublicKey::from_bytes(&slice)?) - } -} - -impl From for UuidMessage { +impl From for Uuid { fn from(value: uuid::Uuid) -> Self { let (first_half, second_half) = value.as_u64_pair(); Self { @@ -26,123 +13,34 @@ impl From for UuidMessage { } } -impl From for uuid::Uuid { - fn from(value: UuidMessage) -> Self { +impl From for uuid::Uuid { + fn from(value: Uuid) -> Self { uuid::Uuid::from_u64_pair(value.high_bits, value.low_bits) } } -impl From for UrlMessage { +impl From for Url { fn from(value: url::Url) -> Self { todo!() } } -impl TryFrom for url::Url { +impl TryFrom for url::Url { type Error = ProtoDeserializeError; - fn try_from(value: UrlMessage) -> Result { + fn try_from(value: Url) -> Result { todo!() } } -impl From for SocketAddrMessage { - fn from(value: std::net::SocketAddr) -> Self { - Self{ - socket_addr: Some(match value { - std::net::SocketAddr::V4(x) => socket_addr::SocketAddr::V4(SocketAddrV4Message::from(x)), - std::net::SocketAddr::V6(x) => socket_addr::SocketAddr::V6(SocketAddrV6Message::from(x)), - })} - } -} - -impl TryFrom for std::net::SocketAddr { - type Error = ProtoDeserializeError; - fn try_from(value: SocketAddrMessage) -> Result { - Ok(match value.socket_addr.ok_or(Self::Error::MissingField("SocketAddr.socket_addr"))? { - socket_addr::SocketAddr::V4(x) => std::net::SocketAddr::V4(x.try_into()?), - socket_addr::SocketAddr::V6(x) => std::net::SocketAddr::V6(x.try_into()?), - }) - } -} - -impl From for SocketAddrV4Message { - fn from(value: std::net::SocketAddrV4) -> Self { - Self { - ip : Some(value.ip().clone().into()), - port: value.port().into(), - } - } -} - -impl TryFrom for std::net::SocketAddrV4 { - type Error = ProtoDeserializeError; - fn try_from(value: SocketAddrV4Message) -> Result { - Ok(Self::new(value.ip.ok_or(ProtoDeserializeError::MissingField("SocketAddrV4.ip"))?.into(), value.port.try_into()?)) - } -} - -impl From for Ipv4AddrMessage { - fn from(value: std::net::Ipv4Addr) -> Self { - Self{ - bits: value.to_bits() - } - } -} -impl From for std::net::Ipv4Addr { - fn from(value: Ipv4AddrMessage) -> Self{ - Self::from_bits(value.bits) - } -} - -impl From for SocketAddrV6Message { - fn from(value: std::net::SocketAddrV6) -> Self { - Self{ - ip: Some(value.ip().clone().into()), - port: value.port().into() - } - } -} - -impl TryFrom for std::net::SocketAddrV6 { - type Error = ProtoDeserializeError; - fn try_from(value: SocketAddrV6Message) -> Result { - Ok(Self::new( - value.ip.ok_or(ProtoDeserializeError::MissingField("SocketAddrV6.ip"))?.into(), - value.port.try_into()?, - 0, - 0 - )) - } -} - -impl From for Ipv6AddrMessage { - fn from(value: std::net::Ipv6Addr) -> Self { - let bits = value.to_bits(); - - Self{ - high_bits: (bits >> 64) as u64, - low_bits: bits as u64, - } - } -} -impl From for std::net::Ipv6Addr{ - - fn from(value: Ipv6AddrMessage) -> Self { - Self::from_bits( - ((value.high_bits as u128) << 64) + (value.low_bits as u128) - ) - } -} - #[cfg(test)] mod tests { use std::{net::{self, Ipv4Addr}, u16}; use super::*; fn validate_uuid_conversion(uuid: uuid::Uuid) -> bool{ - let message = UuidMessage::from(uuid); + let message = Uuid::from(uuid); uuid == uuid::Uuid::from(message) } @@ -153,17 +51,4 @@ mod tests { assert!(validate_uuid_conversion(uuid::Uuid::now_v7())); } - fn validate_socket_addr_conversion(socket_addr: net::SocketAddr) -> Result { - let message = SocketAddrMessage::from(socket_addr); - Ok(socket_addr == message.try_into()?) - } - - #[test] - fn socket_addr_conversion() { - assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V4(net::Ipv4Addr::new(0, 0, 0, 0)),u16::MIN)).unwrap()); - assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V4(net::Ipv4Addr::BROADCAST),u16::MAX)).unwrap()); - assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V6(net::Ipv6Addr::new(0,0,0,0,0,0,0,0)), u16::MAX)).unwrap()); - assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V6(net::Ipv6Addr::new(u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX)), u16::MIN)).unwrap()); - } - } \ No newline at end of file diff --git a/core/src/proto/iroh.rs b/core/src/proto/iroh.rs new file mode 100644 index 0000000..c9c7173 --- /dev/null +++ b/core/src/proto/iroh.rs @@ -0,0 +1,120 @@ +use std::pin::Pin; + +use futures::Stream; +use tonic::{async_trait, Request, Response, Status}; + +use crate::proto::{net::SocketAddr, remote_node_server, ProtoDeserializeError, ProtoSerializeError, }; + + +tonic::include_proto!("caretta_sync.iroh"); + +impl From for ConnectionType { + fn from(value: iroh::endpoint::ConnectionType) -> Self { + use connection_type::*; + Self { + connection_type_value: Some(match value { + iroh::endpoint::ConnectionType::Direct(socket_addr) => { + connection_type::ConnectionTypeValue::Direct(connection_type::Direct{direct_value: Some(SocketAddr::from(socket_addr))}) + }, + iroh::endpoint::ConnectionType::Relay(relay_url) => { + connection_type::ConnectionTypeValue::Relay(connection_type::Relay { relay_value: Some(super::common::Url::from((*relay_url).clone()))}) + }, + iroh::endpoint::ConnectionType::Mixed(socket_addr, relay_url) => { + connection_type::ConnectionTypeValue::Mixed(connection_type::Mixed { socket_addr: Some(SocketAddr::from(socket_addr)), relay_url: Some(super::common::Url::from((*relay_url).clone()))}) + }, + iroh::endpoint::ConnectionType::None => { + ConnectionTypeValue::None(None{}) + } + }) + } + } +} + +impl From for ControlMsg { + fn from(value: iroh::endpoint::ControlMsg) -> Self { + use control_msg::*; + Self { control_msg_vaue: Some(match value { + iroh::endpoint::ControlMsg::Ping => ControlMsgVaue::Ping(Ping{}), + iroh::endpoint::ControlMsg::Pong => ControlMsgVaue::Pong(Pong {}), + iroh::endpoint::ControlMsg::CallMeMaybe => ControlMsgVaue::CallMeMaybe(CallMeMayBe { }), + }) } + } +} + +impl TryFrom for DirectAddrInfo { + type Error = ProtoSerializeError; + fn try_from(value: iroh::endpoint::DirectAddrInfo) -> Result { + use direct_addr_info::*; + let last_control: Option = if let Some((duration, control_msg)) = value.last_control { + Some(DurationControlMsg{ + control_msg: Some(control_msg.into()), + duration: Some(duration.try_into()?) + }) + } else { + None + }; + Ok(Self { + addr: Some(value.addr.into()), + latency: value.latency.map(|x| x.try_into()).transpose()?, + last_control: last_control, + last_payload: value.last_payload.map(|x| x.try_into()).transpose()?, + last_alive: value.last_alive.map(|x| x.try_into()).transpose()?, + sources: value.sources.into_iter().map(|(s, d)| { + Ok::(SourceDuration{ + source: Some(s.into()), + duration: Some(d.try_into()?) + }) + }).collect::, ProtoSerializeError>>()?, + }) + } +} + +impl From for PublicKey { + fn from(value: iroh::PublicKey) -> Self { + Self{ key: Vec::from(value.as_bytes()) } + } +} + +impl TryFrom for iroh::PublicKey { + type Error = ProtoDeserializeError; + fn try_from(value: PublicKey) -> Result { + let slice: [u8; 32] = value.key[0..32].try_into()?; + Ok(iroh::PublicKey::from_bytes(&slice)?) + } +} + +impl TryFrom for RemoteInfo { + type Error = ProtoSerializeError; + fn try_from(value: iroh::endpoint::RemoteInfo) -> Result { + Ok(Self { + node_id: Some(value.node_id.into()), + relay_url: value.relay_url.map(|x| { + Ok::(RelayUrlInfo { + relay_url: Some((*x.relay_url).clone().into()), + last_alive: x.last_alive.map(|x| x.try_into()).transpose()?, + latency: x.latency.map(|x| x.try_into()).transpose()? + })}).transpose()?, + addrs: value.addrs.into_iter().map(|x| { + x.try_into() + }).collect::, ProtoSerializeError>>()?, + conn_type: Some(value.conn_type.into()), + latency: value.latency.map(|x| x.try_into()).transpose()?, + last_used:value.last_used.map(|x| x.try_into()).transpose()? + }) + } +} + +impl From for Source { + fn from(value: iroh::endpoint::Source) -> Self { + use source::*; + Self { + source_value:Some(match value { + iroh::endpoint::Source::Saved => SourceValue::Saved(Saved { }), + iroh::endpoint::Source::Udp => SourceValue::Udp(Udp { }), + iroh::endpoint::Source::Relay => SourceValue::Relay(Relay { }), + iroh::endpoint::Source::App => SourceValue::App(App{}), + iroh::endpoint::Source::Discovery { name } => SourceValue::Discovery(Discovery { value: name }), + iroh::endpoint::Source::NamedApp { name } => SourceValue::NamedApp(NamedApp { value: name }), + }) } + } +} \ No newline at end of file diff --git a/core/src/proto/mod.rs b/core/src/proto/mod.rs index 1ad2951..72e35c0 100644 --- a/core/src/proto/mod.rs +++ b/core/src/proto/mod.rs @@ -1,25 +1,11 @@ -pub mod authorization_request; +mod authorization_request; mod authorized_node; mod remote_node; mod common; mod error; +mod iroh; +mod net; -//mod server; -mod generated{ - tonic::include_proto!("caretta_sync"); -} - -pub use generated::{ - *, - PublicKey as PublicKeyMessage, - Uuid as UuidMessage, - Url as UrlMessage, - SocketAddr as SocketAddrMessage, - SocketAddrV6 as SocketAddrV6Message, - SocketAddrV4 as SocketAddrV4Message, - Ipv4Addr as Ipv4AddrMessage, - Ipv6Addr as Ipv6AddrMessage, -}; pub use common::*; pub use error::*; diff --git a/core/src/proto/net.rs b/core/src/proto/net.rs new file mode 100644 index 0000000..32a0f1b --- /dev/null +++ b/core/src/proto/net.rs @@ -0,0 +1,140 @@ +tonic::include_proto!("caretta_sync.net"); + +use crate::proto::{error::{ProtoDeserializeError, ProtoSerializeError}}; + +type Ipv4AddrMessage = Ipv4Addr; +type Ipv6AddrMessage = Ipv6Addr; +type SocketAddrMessage = SocketAddr; +type SocketAddrV4Message = SocketAddrV4; +type SocketAddrV6Message = SocketAddrV6; + +impl From for SocketAddrMessage { + fn from(value: std::net::SocketAddr) -> Self { + Self{ + socket_addr_value: Some(match value { + std::net::SocketAddr::V4(x) => socket_addr::SocketAddrValue::V4(SocketAddrV4Message::from(x)), + std::net::SocketAddr::V6(x) => socket_addr::SocketAddrValue::V6(SocketAddrV6Message::from(x)), + })} + } +} + +impl TryFrom for std::net::SocketAddr { + type Error = ProtoDeserializeError; + fn try_from(value: SocketAddrMessage) -> Result { + Ok(match value.socket_addr_value.ok_or(Self::Error::MissingField("SocketAddr.socket_addr"))? { + socket_addr::SocketAddrValue::V4(x) => std::net::SocketAddr::V4(x.try_into()?), + socket_addr::SocketAddrValue::V6(x) => std::net::SocketAddr::V6(x.try_into()?), + }) + } +} + +impl From for SocketAddrV4Message { + fn from(value: std::net::SocketAddrV4) -> Self { + Self { + ip : Some(value.ip().clone().into()), + port: value.port().into(), + } + } +} + +impl TryFrom for std::net::SocketAddrV4 { + type Error = ProtoDeserializeError; + fn try_from(value: SocketAddrV4Message) -> Result { + Ok(Self::new(value.ip.ok_or(ProtoDeserializeError::MissingField("SocketAddrV4.ip"))?.into(), value.port.try_into()?)) + } +} + +impl From for Ipv4AddrMessage { + fn from(value: std::net::Ipv4Addr) -> Self { + Self{ + bits: value.to_bits() + } + } +} +impl From for std::net::Ipv4Addr { + fn from(value: Ipv4AddrMessage) -> Self{ + Self::from_bits(value.bits) + } +} + +impl From for SocketAddrV6Message { + fn from(value: std::net::SocketAddrV6) -> Self { + Self{ + ip: Some(value.ip().clone().into()), + port: value.port().into() + } + } +} + +impl TryFrom for std::net::SocketAddrV6 { + type Error = ProtoDeserializeError; + fn try_from(value: SocketAddrV6Message) -> Result { + Ok(Self::new( + value.ip.ok_or(ProtoDeserializeError::MissingField("SocketAddrV6.ip"))?.into(), + value.port.try_into()?, + 0, + 0 + )) + } +} + +impl From for Ipv6AddrMessage { + fn from(value: std::net::Ipv6Addr) -> Self { + let bits = value.to_bits(); + + Self{ + high_bits: (bits >> 64) as u64, + low_bits: bits as u64, + } + } +} +impl From for std::net::Ipv6Addr{ + + fn from(value: Ipv6AddrMessage) -> Self { + Self::from_bits( + ((value.high_bits as u128) << 64) + (value.low_bits as u128) + ) + } +} + +#[cfg(test)] +mod tests { + use std::{net::{self, Ipv4Addr}, u16, u8}; + + use rand::random; + + use super::*; + + fn validate_socket_addr_conversion(socket_addr: net::SocketAddr) -> Result { + let message = SocketAddrMessage::from(socket_addr); + Ok(socket_addr == message.try_into()?) + } + + #[test] + fn socket_addr_conversion_ipv4_min() { + assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V4(net::Ipv4Addr::new(0, 0, 0, 0)),u16::MIN)).unwrap()); + } + #[test] + fn socket_addr_conversion_ipv4_max() { + assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V4(net::Ipv4Addr::new(u8::MAX, u8::MAX, u8::MAX, u8::MAX)),u16::MAX)).unwrap()); + } + #[test] + fn socket_addr_conversion_ipv4_random() { + for _ in 0..10 { + assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V4( + net::Ipv4Addr::new(random(), random(), random(), random()) + ), + random() + )).unwrap()) + } + } + #[test] + fn socket_addr_conversion_ipv6_min() { + assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V6(net::Ipv6Addr::new(0,0,0,0,0,0,0,0)), u16::MIN)).unwrap()); + } + #[test] + fn socket_addr_conversion_ipv6_max() { + assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V6(net::Ipv6Addr::new(u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX)), u16::MAX)).unwrap()); + } + +} \ No newline at end of file diff --git a/core/src/proto/remote_node.rs b/core/src/proto/remote_node.rs index d7ed3f7..76e70d9 100644 --- a/core/src/proto/remote_node.rs +++ b/core/src/proto/remote_node.rs @@ -1,4 +1,3 @@ -use super::*; use std::{pin::Pin, time::Duration}; @@ -10,82 +9,21 @@ use tripod_id::Double; use crate::{data::local::{LocalRecordId, RemoteNodeRecord}, error::Error, global::IROH_ENDPOINT, proto::{error::{ProtoDeserializeError, ProtoSerializeError}}}; -impl TryFrom<(iroh::endpoint::Source, Duration)> for remote_node_info::remote_info::direct_addr_info::Source { - type Error = ProtoSerializeError; - fn try_from(src: (iroh::endpoint::Source, Duration)) -> Result { - let (source, duration )= src; - Ok(Self { - source: source.to_string(), - duration: Some(duration.try_into()?), - }) - } -} - -impl TryFrom for RemoteNodeRecord { - type Error = Error; - fn try_from(value: RemoteNodeIdentifier) -> Result { - Ok(match value.identifier.ok_or(ProtoDeserializeError::MissingField("RemoteNodeIdentifier.identifier"))? { - remote_node_identifier::Identifier::PublicKey(x) => Self::get_or_insert_by_public_key(&x.try_into()?)?, - remote_node_identifier::Identifier::Id(x) => Self::get_by_public_id(&x.try_into()?)? - }) - } - -} - - -impl TryFrom<(tripod_id::Double, RemoteInfo)> for RemoteNodeInfo { - type Error = ProtoSerializeError; - fn try_from(value: (tripod_id::Double, RemoteInfo)) -> Result { - Ok(Self { - id: Some(value.0.into()), - public_key: Some(value.1.node_id.into()), - relay_url: value.1.relay_url.map_or(String::from(""), |x| x.relay_url.to_string()), - addrs: value.1.addrs.into_iter() - .map(|x| DirectAddrInfoMessage::try_from(x)) - .collect::,Self::Error>>()?, - conn_type: value.1.conn_type.to_string(), - latency: value.1.latency.map(|x| x.try_into()).transpose()?, - last_used: value.1.last_used.map(|x| x.try_into()).transpose()?, - }) - } -} - -impl TryFrom for RemoteNodeDirectAddrInfo { - type Error = ProtoSerializeError; - fn try_from(value: DirectAddrInfo) -> Result { - Ok(RemoteNodeDirectAddrInfo { - addr: value.addr.to_string(), - latency: value.latency.map(|x| x.try_into()).transpose()?, - last_control: value.last_control.map(|x| LastControlMessage::try_from(x)).transpose()?, - last_payload: value.last_payload.map(|x| x.try_into()).transpose()?, - last_alive: value.last_alive.map(|x| x.try_into()).transpose()?, - sources: value.sources.into_iter().map(|x| SourceMessage::try_from(x)).collect::, Self::Error>>()? - }) - } -} +tonic::include_proto!("caretta_sync.remote_node"); pub struct RemoteNodeServer{} #[tonic::async_trait] impl remote_node_server::RemoteNode for RemoteNodeServer { - type ListStream = Pin> + Send>>; - async fn info(&self, request: Request) -> Result, Status> { - let request = request.into_inner(); - let remote_node: RemoteNodeRecord = request.remote_node.ok_or(Status::invalid_argument("remote_node is required."))?.try_into()?; - let remote_info: RemoteNodeInfo = IROH_ENDPOINT.get_unchecked().remote_info(remote_node.public_key).ok_or(Status::not_found(format!("node {:?} is not found", remote_node)))?.into(); - Ok(Response::new(RemoteNodeInfoResponse{ - remote_node_info: Some(remote_info) - })) + type ListStream = Pin> + Send>>; + async fn info(&self, request: Request) -> Result, Status> { + todo!() } - async fn list(&self, request: Request>) + async fn list(&self, request: Request>) -> Result, Status> { let iter = IROH_ENDPOINT.get_unchecked().remote_info_iter() .map(|x| { - RemoteNodeInfo::try_from(x).map(|x| RemoteNodeListResponse { - remote_node: x.into() - }).or_else(|e| { - Err(Status::from_error(Box::new(e))) - }) + todo!(); }); let stream = futures::stream::iter(iter); Ok(Response::new(Box::pin(stream)))