-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add gorilla websocket module, build tran protocol model
- Loading branch information
Showing
4 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |