Skip to content

Commit

Permalink
add gorilla websocket module, build tran protocol model
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Feb 3, 2022
1 parent b185389 commit d0c6ea6
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/charmbracelet/bubbles v0.10.2
github.com/charmbracelet/lipgloss v0.4.0
github.com/gorilla/websocket v1.4.2
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+Pp
github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5RQr8fzta+xl7BOM=
github.com/containerd/console v1.0.2 h1:Pi6D+aZXM+oUw1czuKgH5IJ+y0jhYcwBJfx5/Ghn9dE=
github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
Expand Down
107 changes: 107 additions & 0 deletions models/protocol/transfer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package protocol

import (
"fmt"
"net"
"strings"
)

// TransferMessageType specifies the message type for the messages in the transfer protocol.
type TransferMessageType int

const (
TransferError TransferMessageType = iota // An error has occured in transferProtocol
ReceiverHandshake // Receiver exchange its IP via the tranx server to the sender
SenderHandshake // Sender exchanges IP, port and payload size to the receiver via the tranx server
ReceiverDirectCommunication
SenderDirectAck // Sender ACKs the request for direct communication
ReceiverRelayCommunication // Receiver has tried to probe the sender but cannot find it on the subnet, relay communication will be used
SenderRelayAck // Sender ACKs the request for relay communication
ReceiverRequestPayload // Receiver request the payload from the sender
SenderPayloadSent // Sender announces that the entire file has been transfered
ReceiverPayloadAck // Receiver ACKs that is has received the payload
SenderClosing // Sender announces that it is closing the connection
ReceiverClosingAck // Receiver ACKs the closing of the connection
)

// TransferMessage specifies a message in the transfer protocol.
type TransferMessage struct {
Type TransferMessageType `json:"type"`
Payload interface{} `json:"payload,omitempty"`
}

func (t TransferMessage) Bytes() []byte {
return []byte(fmt.Sprintf("%v", t))
}

type ReceiverHandshakePayload struct {
IP net.IP `json:"ip"`
}

// SenderHandshakePayload specifies a payload type for announcing the payload size.
type SenderHandshakePayload struct {
IP net.IP `json:"ip"`
Port int `json:"port"`
PayloadSize int64 `json:"payload_size"`
}

type WrongMessageTypeError struct {
expected []TransferMessageType
got TransferMessageType
}

func NewWrongMessageTypeError(expected []TransferMessageType, got TransferMessageType) *WrongMessageTypeError {
return &WrongMessageTypeError{
expected: expected,
got: got,
}
}

func (e *WrongMessageTypeError) Error() string {
var expectedMessageTypes []string

for _, expectedType := range e.expected {
expectedMessageTypes = append(expectedMessageTypes, expectedType.Name())
}

oneOfExpected := strings.Join(expectedMessageTypes, ", ")

return fmt.Sprintf("wrong message type, expected one of: (%s), got: (%s)", oneOfExpected, e.got.Name())
}

func (t TransferMessageType) Name() string {
switch t {
case TransferError:
return "TransferError"

case ReceiverHandshake:
return "ReceiverHandshake"

case SenderHandshake:
return "SenderHandshake"

case ReceiverRelayCommunication:
return "ReceiverRelayCommunication"

case SenderRelayAck:
return "SenderRelayAck"

case ReceiverRequestPayload:
return "ReceiverRequestPayload"

case SenderPayloadSent:
return "SenderPayloadSent"

case ReceiverPayloadAck:
return "ReceiverAckPayload"

case SenderClosing:
return "SenderClosing"

case ReceiverClosingAck:
return "ReceiverClosingAck"

default:
return ""
}
}
60 changes: 60 additions & 0 deletions models/protocol/tranx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package protocol

import (
"net"

"github.com/gorilla/websocket"
)

type TranxMessageType int

const (
TranxToSenderBind TranxMessageType = iota // An ID for this connection is bound and communicated
SenderToTranxEstablish // Sender has generated and hashed password
ReceiverToTranxEstablish // Passsword has been communicated to receiver who has hashed it
TranxToSenderReady // Tranx announces to sender that receiver is connected
SenderToTranxPAKE // Sender sends PAKE information to tranx
TranxToReceiverPAKE // Tranx forwards PAKE information to receiver
ReceiverToTranxPAKE // Receiver sends PAKE information to tranx
TranxToSenderPAKE // Tranx forwards PAKE information to receiver
SenderToTranxSalt // Sender sends cryptographic salt to tranx
TranxToReceiverSalt // Rendevoux forwards cryptographic salt to receiver
ReceiverToTranxClose // Receiver can connect directly to sender, close receiver connection -> close sender connection
SenderToTranxClose // Transit sequence is completed, close sender connection -> close receiver connection
)

type TranxMessage struct {
Type TranxMessageType `json:"type"`
Payload interface{} `json:"payload"`
}

type TranxClient struct {
Conn *websocket.Conn
IP net.IP
}

type TranxSender struct {
TranxClient
Port int
}

type TranxReceiver = TranxClient

/* [💻 Receiver <-> Sender 💻] messages */

type PasswordPayload struct {
Password string `json:"password"`
}
type PakePayload struct {
Bytes []byte `json:"pake_bytes"`
}

type SaltPayload struct {
Salt []byte `json:"salt"`
}

/* [Tranx -> Sender] messages */

type TranxToSenderBindPayload struct {
ID int `json:"id"`
}

0 comments on commit d0c6ea6

Please sign in to comment.