mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Protocol 6: node identities, error codes, Synchronous, schema v8, examples
- Protocol 5: a client authenticates as a node (Node), its pushes go to the section's DefaultTopic unless they name one; old Identities refused at start with what to write instead. - Topics listed by name, ids picked and kept by the server; nodes come from client sections, [BakaShrot.Nodes] optional, AcceptNodes = * by default. - Protocol 6: every refusal carries a code, every client failure too (BakaShrot_ErrorCodes.pbi, BakaShrot_GetLastError(), ErrorCode in Python); nothing reconnects by itself, delivery is at least once. - [BakaShrot.Server] Synchronous = FULL (default) or NORMAL, benchmarked. - Schema v8: browse table and browse_messages view for database tools. - Tools renamed BakaShrot<Name>, wait for Enter when started from Explorer. - Examples/SingleServer: a server and a client with scripts (example keys, public on purpose). - Restart-Test.ps1: servers killed mid-traffic and mid-catch-up. - Docs: one server per node rule, DatabaseError() checked per thread, release notes' Compatibility section, Notes "Right now". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RwFeVQrLqMQPw9AZnsHqQH
This commit is contained in:
@@ -129,9 +129,11 @@ CompilerEndIf
|
||||
Structure BakaShrot_ConnectionMetadata
|
||||
Status.i
|
||||
|
||||
; Identity from the Hello, as the server's own ids (unused on the client
|
||||
; side), only trusted once Status is Authenticated.
|
||||
TopicId.u
|
||||
; Server side only, trusted once Status is Authenticated: the node from
|
||||
; the Hello, and the default topic of its pushes from its client
|
||||
; section's DefaultTopic (-1 without one: pushes must name their topic),
|
||||
; both as the server's own ids.
|
||||
DefaultTopicId.l
|
||||
NodeId.u
|
||||
; The client's Ed25519 public key, on both sides.
|
||||
PublicKey.a[#BakaShrot_Ed25519_PublicKeySize]
|
||||
|
||||
@@ -22,8 +22,8 @@ XIncludeFile "SocketWait.pbi"
|
||||
|
||||
; Who to authenticate as.
|
||||
Structure BakaShrot_OutboundIdentity
|
||||
; Each a name or an id in digits (Names.pbi), resolved by the server.
|
||||
Topic.s
|
||||
; The node it writes as, a name or an id in digits (Names.pbi), resolved
|
||||
; by the server, which must allow it for this key.
|
||||
Node.s
|
||||
|
||||
; Ed25519 public key (Base64), sent in the Hello.
|
||||
@@ -45,6 +45,10 @@ Structure BakaShrot_Outbound
|
||||
; for the server's packets give up right away, as if they timed out.
|
||||
; Lets a server stop its peer links without waiting out their timeouts.
|
||||
*StopRequested.Integer
|
||||
|
||||
; Why BakaShrot_Outbound_Connect() failed, a #BakaShrot_Error_* of
|
||||
; Client/BakaShrot_ErrorCodes.pbi, 0 once it succeeded.
|
||||
LastError.l
|
||||
EndStructure
|
||||
|
||||
|
||||
@@ -158,22 +162,27 @@ Procedure.i BakaShrot_Outbound_Connect(*Outbound.BakaShrot_Outbound, Host.s, Por
|
||||
Protected KeyPair.BakaShrot_SessionKeyPair
|
||||
Protected ChallengeDecoded.i, ServerProved.i
|
||||
|
||||
If *Outbound\ConnectionId Or Not *ServerPublicKey
|
||||
*Outbound\LastError = #BakaShrot_Error_None
|
||||
If *Outbound\ConnectionId
|
||||
*Outbound\LastError = #BakaShrot_Error_AlreadyConnected
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
|
||||
If Port < 1 Or Port > 65535
|
||||
If Not *ServerPublicKey Or Port < 1 Or Port > 65535
|
||||
*Outbound\LastError = #BakaShrot_Error_InvalidArgument
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
|
||||
; Only needed before PureBasic 6.00.
|
||||
CompilerIf #PB_Compiler_Version < 600
|
||||
If Not InitNetwork()
|
||||
*Outbound\LastError = #BakaShrot_Error_Internal
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
CompilerEndIf
|
||||
|
||||
If Not InitCryptography()
|
||||
*Outbound\LastError = #BakaShrot_Error_Internal
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
|
||||
@@ -181,6 +190,7 @@ Procedure.i BakaShrot_Outbound_Connect(*Outbound.BakaShrot_Outbound, Host.s, Por
|
||||
ClearStructure(@*Outbound\Metadata, BakaShrot_ConnectionMetadata)
|
||||
If Not DecodeKeyFromString(*Identity\PublicKey, @*Outbound\Metadata\PublicKey[0], #BakaShrot_Ed25519_PublicKeySize)
|
||||
*Outbound\Metadata\Status = #BakaShrot_ConnectionStatus_Aborted
|
||||
*Outbound\LastError = #BakaShrot_Error_InvalidArgument
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
CopyMemory(*ServerPublicKey, @*Outbound\Metadata\ServerPublicKey[0], #BakaShrot_Ed25519_PublicKeySize)
|
||||
@@ -188,6 +198,7 @@ Procedure.i BakaShrot_Outbound_Connect(*Outbound.BakaShrot_Outbound, Host.s, Por
|
||||
*Outbound\ConnectionId = OpenNetworkConnection(Host, Port, #PB_Network_TCP | #PB_Network_IPv4)
|
||||
If Not *Outbound\ConnectionId
|
||||
*Outbound\Metadata\Status = #BakaShrot_ConnectionStatus_Aborted
|
||||
*Outbound\LastError = #BakaShrot_Error_ConnectFailed
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
*Outbound\Metadata\Status = #BakaShrot_ConnectionStatus_New
|
||||
@@ -198,11 +209,11 @@ Procedure.i BakaShrot_Outbound_Connect(*Outbound.BakaShrot_Outbound, Host.s, Por
|
||||
|
||||
|
||||
; 1. Hello, sealed for the server
|
||||
Hello\ClientTopic = *Identity\Topic
|
||||
Hello\ClientNode = *Identity\Node
|
||||
Hello\ClientPubKeyB64 = *Identity\PublicKey
|
||||
CopyMemory(@KeyPair\PublicKey[0], @Hello\ClientSessionKey[0], #BakaShrot_SessionKeySize)
|
||||
|
||||
*Outbound\LastError = #BakaShrot_Error_Internal
|
||||
*Packet = BakaShrot_HelloPacket_Encode(@Hello, @PacketSize)
|
||||
If Not *Packet
|
||||
Goto ConnectFailed
|
||||
@@ -212,6 +223,7 @@ Procedure.i BakaShrot_Outbound_Connect(*Outbound.BakaShrot_Outbound, Host.s, Por
|
||||
If Not *Sealed
|
||||
Goto ConnectFailed
|
||||
EndIf
|
||||
*Outbound\LastError = #BakaShrot_Error_ConnectFailed
|
||||
Sent = BakaShrot_SendPacket(*Outbound\ConnectionId, *Sealed, SealedSize\i, 0)
|
||||
FreeMemory(*Sealed)
|
||||
If Not Sent
|
||||
@@ -219,12 +231,17 @@ Procedure.i BakaShrot_Outbound_Connect(*Outbound.BakaShrot_Outbound, Host.s, Por
|
||||
EndIf
|
||||
|
||||
|
||||
; 2. Challenge request, in plain text
|
||||
; 2. Challenge request, in plain text. No answer: the server closed the
|
||||
; connection, it couldn't open the Hello or doesn't allow this key and
|
||||
; node (or speaks another protocol version).
|
||||
*Outbound\LastError = #BakaShrot_Error_AuthRejected
|
||||
*Packet = BakaShrot_Outbound_WaitPacket(*Outbound, @PacketSize, #BakaShrot_Outbound_Timeout)
|
||||
If Not *Packet
|
||||
Goto ConnectFailed
|
||||
EndIf
|
||||
|
||||
*Outbound\LastError = #BakaShrot_Error_ProtocolError
|
||||
|
||||
*Request = 0
|
||||
*View = MsgPackWrapMsgBuffer(*Packet, PacketSize\i)
|
||||
If *View
|
||||
@@ -250,10 +267,12 @@ Procedure.i BakaShrot_Outbound_Connect(*Outbound.BakaShrot_Outbound, Host.s, Por
|
||||
BakaShrot_MakeHandshakeTranscript(@*Outbound\Metadata, #BakaShrot_TranscriptServerLabel, @Proof)
|
||||
ServerProved = BakaShrot_VerifyHandshake(@Proof, *ServerPublicKey)
|
||||
If Not ServerProved
|
||||
*Outbound\LastError = #BakaShrot_Error_ServerNotVerified
|
||||
Goto ConnectFailed
|
||||
EndIf
|
||||
|
||||
; Every packet is encrypted from now on.
|
||||
*Outbound\LastError = #BakaShrot_Error_Internal
|
||||
If Not BakaShrot_Session_StartClient(@*Outbound\Metadata\Cipher, @KeyPair, @*Outbound\Metadata\ServerSessionKey[0])
|
||||
Goto ConnectFailed
|
||||
EndIf
|
||||
@@ -274,6 +293,7 @@ Procedure.i BakaShrot_Outbound_Connect(*Outbound.BakaShrot_Outbound, Host.s, Por
|
||||
If Not *Packet
|
||||
Goto ConnectFailed
|
||||
EndIf
|
||||
*Outbound\LastError = #BakaShrot_Error_AuthRejected
|
||||
Sent = BakaShrot_Outbound_Send(*Outbound, *Packet, PacketSize\i)
|
||||
FreeMemory(*Packet)
|
||||
If Not Sent
|
||||
@@ -281,11 +301,12 @@ Procedure.i BakaShrot_Outbound_Connect(*Outbound.BakaShrot_Outbound, Host.s, Por
|
||||
EndIf
|
||||
|
||||
|
||||
; 4. Accepted
|
||||
; 4. Accepted. No answer: the server didn't accept this key's signature.
|
||||
*Packet = BakaShrot_Outbound_WaitPacket(*Outbound, @PacketSize, #BakaShrot_Outbound_Timeout)
|
||||
If Not *Packet
|
||||
Goto ConnectFailed
|
||||
EndIf
|
||||
*Outbound\LastError = #BakaShrot_Error_ProtocolError
|
||||
|
||||
*Accepted = 0
|
||||
*View = MsgPackWrapMsgBuffer(*Packet, PacketSize\i)
|
||||
@@ -302,6 +323,7 @@ Procedure.i BakaShrot_Outbound_Connect(*Outbound.BakaShrot_Outbound, Host.s, Por
|
||||
; The server's answers may be big from now on.
|
||||
BakaShrot_Inbox_Lift(@*Outbound\Inbox)
|
||||
*Outbound\Metadata\Status = #BakaShrot_ConnectionStatus_Authenticated
|
||||
*Outbound\LastError = #BakaShrot_Error_None
|
||||
ProcedureReturn #True
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
|
||||
XIncludeFile "../../Includes/PB-MessagePack/Includes/MessagePack.pbi"
|
||||
XIncludeFile "Names.pbi"
|
||||
; The refusal codes (RequestRefused) and the client's error codes.
|
||||
XIncludeFile "../../Client/BakaShrot_ErrorCodes.pbi"
|
||||
|
||||
|
||||
; Represents the root of all BakaShrot packets.
|
||||
@@ -21,7 +23,10 @@ EndStructure
|
||||
; the ChallengeRequest encrypted (Connections.pbi). Messages carry when
|
||||
; they were pushed (PushedAt).
|
||||
; 4: attachments (Design/Attachments.md).
|
||||
#BakaShrot_ProtocolVersion = 4
|
||||
; 5: the Hello carries the client's node only: its default topic is the
|
||||
; server's to say (DefaultTopic in its client section).
|
||||
; 6: RequestRefused carries a Code (Client/BakaShrot_ErrorCodes.pbi).
|
||||
#BakaShrot_ProtocolVersion = 6
|
||||
|
||||
; Size in bytes of the nonce sent in a challenge request and echoed in its response.
|
||||
; It MUST be filled with CryptRandomData(), never reused, and be checked against the one the
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
XIncludeFile "../Packets.pbi"
|
||||
|
||||
; C2S auth request, sent sealed for the server's public key (Connections.pbi).
|
||||
; ClientTopic, ClientNode: who the client is, each a name or an id in digits
|
||||
; (Commons/Networking/Names.pbi), resolved by the server in its own names.
|
||||
; ClientNode: who the client is, the node it writes as, a name or an id in
|
||||
; digits (Commons/Networking/Names.pbi), resolved by the server in its own
|
||||
; names. Its default topic is the server's to say, not the client's.
|
||||
; ClientSessionKey: the public half of the client's key pair for this
|
||||
; connection (Commons/Cryptography/Session.pbi).
|
||||
Structure BakaShrot_AuthHello Extends BakaShrot_PacketBase
|
||||
ClientTopic.s
|
||||
ClientNode.s
|
||||
ClientPubKeyB64.s
|
||||
ClientSessionKey.a[#BakaShrot_SessionKeySize]
|
||||
@@ -37,10 +37,6 @@ Procedure.i BakaShrot_HelloPacket_Encode(*Hello.BakaShrot_AuthHello, *OutSize.In
|
||||
Goto EncodeFailed
|
||||
EndIf
|
||||
|
||||
If MsgPackWriteString(*MsgPackData, *Hello\ClientTopic) <> #MsgPack_Error_Success
|
||||
Goto EncodeFailed
|
||||
EndIf
|
||||
|
||||
If MsgPackWriteString(*MsgPackData, *Hello\ClientNode) <> #MsgPack_Error_Success
|
||||
Goto EncodeFailed
|
||||
EndIf
|
||||
@@ -84,15 +80,6 @@ Procedure.i BakaShrot_HelloPacket_Decode(*MsgPackData.MsgPackData)
|
||||
Goto DecodeFailed
|
||||
EndIf
|
||||
|
||||
; ClientTopic
|
||||
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
|
||||
Goto DecodeFailed
|
||||
EndIf
|
||||
*Hello\ClientTopic = MsgPackReadString(*MsgPackData)
|
||||
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success
|
||||
Goto DecodeFailed
|
||||
EndIf
|
||||
|
||||
; ClientNode
|
||||
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
|
||||
Goto DecodeFailed
|
||||
|
||||
@@ -6,8 +6,11 @@
|
||||
XIncludeFile "../Packets.pbi"
|
||||
|
||||
; S2C, answers a request the server won't fulfill, the connection stays open.
|
||||
; Reason is only meant for logs and humans.
|
||||
; Code: what kind of refusal, 1-#BakaShrot_Refused_Max, one of the
|
||||
; #BakaShrot_Refused_* in Client/BakaShrot_ErrorCodes.pbi: what clients act
|
||||
; on (protocol 6). Reason is only meant for logs and humans.
|
||||
Structure BakaShrot_RequestRefused Extends BakaShrot_PacketBase
|
||||
Code.l
|
||||
Reason.s
|
||||
EndStructure
|
||||
|
||||
@@ -22,6 +25,10 @@ Procedure.i BakaShrot_RequestRefusedPacket_Encode(*Refused.BakaShrot_RequestRefu
|
||||
*Refused\Version = #BakaShrot_ProtocolVersion
|
||||
*Refused\PacketType = #BakaShrot_PacketType_RequestRefused
|
||||
|
||||
If Not BakaShrot_IsRefusalCode(*Refused\Code)
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
|
||||
*MsgPackData = MsgPackCreate(64)
|
||||
If Not *MsgPackData
|
||||
ProcedureReturn 0
|
||||
@@ -31,6 +38,10 @@ Procedure.i BakaShrot_RequestRefusedPacket_Encode(*Refused.BakaShrot_RequestRefu
|
||||
Goto EncodeFailed
|
||||
EndIf
|
||||
|
||||
If MsgPackWriteInteger(*MsgPackData, *Refused\Code) <> #MsgPack_Error_Success
|
||||
Goto EncodeFailed
|
||||
EndIf
|
||||
|
||||
If MsgPackWriteString(*MsgPackData, *Refused\Reason) <> #MsgPack_Error_Success
|
||||
Goto EncodeFailed
|
||||
EndIf
|
||||
@@ -53,7 +64,7 @@ EndProcedure
|
||||
; *MsgPackData stays owned by the caller, and should be discarded on failure.
|
||||
; Returns 0 on failure, the caller owns the returned struct - free it with FreeStructure.
|
||||
Procedure.i BakaShrot_RequestRefusedPacket_Decode(*MsgPackData.MsgPackData)
|
||||
Protected *Refused.BakaShrot_RequestRefused
|
||||
Protected *Refused.BakaShrot_RequestRefused, Value.q
|
||||
|
||||
*Refused = AllocateStructure(BakaShrot_RequestRefused)
|
||||
If Not *Refused
|
||||
@@ -65,6 +76,16 @@ Procedure.i BakaShrot_RequestRefusedPacket_Decode(*MsgPackData.MsgPackData)
|
||||
Goto DecodeFailed
|
||||
EndIf
|
||||
|
||||
; Code
|
||||
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
|
||||
Goto DecodeFailed
|
||||
EndIf
|
||||
Value = MsgPackReadInteger(*MsgPackData)
|
||||
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not BakaShrot_IsRefusalCode(Value)
|
||||
Goto DecodeFailed
|
||||
EndIf
|
||||
*Refused\Code = Value
|
||||
|
||||
; Reason
|
||||
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
|
||||
Goto DecodeFailed
|
||||
|
||||
Reference in New Issue
Block a user