Add exposed address

This commit is contained in:
Zoltan Papp
2024-07-02 11:57:17 +02:00
parent d3785dc1fa
commit 15a7b7629b
15 changed files with 154 additions and 128 deletions

View File

@ -4,6 +4,8 @@ import (
"bytes"
"fmt"
"go.mongodb.org/mongo-driver/bson"
log "github.com/sirupsen/logrus"
)
@ -45,6 +47,10 @@ func (m MsgType) String() string {
}
}
type HelloResponse struct {
DomainAddress string
}
func DetermineClientMsgType(msg []byte) (MsgType, error) {
msgType := MsgType(msg[0])
switch msgType {
@ -97,10 +103,32 @@ func UnmarshalHelloMsg(msg []byte) ([]byte, error) {
return msg[5:], nil
}
func MarshalHelloResponse() []byte {
msg := make([]byte, 1)
func MarshalHelloResponse(DomainAddress string) ([]byte, error) {
payload := HelloResponse{
DomainAddress: DomainAddress,
}
helloResponse, err := bson.Marshal(payload)
if err != nil {
log.Errorf("failed to marshal hello response: %s", err)
return nil, err
}
msg := make([]byte, 1, 1+len(helloResponse))
msg[0] = byte(MsgTypeHelloResponse)
return msg
msg = append(msg, helloResponse...)
return msg, nil
}
func UnmarshalHelloResponse(msg []byte) (string, error) {
if len(msg) < 2 {
return "", fmt.Errorf("invalid 'hello response' message")
}
payload := HelloResponse{}
err := bson.Unmarshal(msg[1:], &payload)
if err != nil {
log.Errorf("failed to unmarshal hello response: %s", err)
return "", err
}
return payload.DomainAddress, nil
}
// Close message