mirror of
https://github.com/PretendoNetwork/nex-go.git
synced 2025-04-02 11:02:14 -04:00
chore: move constants and enums to constants package
This commit is contained in:
parent
b37dede3c4
commit
a1a590392b
15 changed files with 127 additions and 114 deletions
|
@ -1,4 +1,4 @@
|
|||
package nex
|
||||
package constants
|
||||
|
||||
const (
|
||||
// FlagAck is the ID for the PRUDP Ack Flag
|
|
@ -1,4 +1,4 @@
|
|||
package nex
|
||||
package constants
|
||||
|
||||
const (
|
||||
// SynPacket is the ID for the PRUDP Syn Packet type
|
||||
|
@ -16,11 +16,3 @@ const (
|
|||
// PingPacket is the ID for the PRUDP Ping Packet type
|
||||
PingPacket uint16 = 0x4
|
||||
)
|
||||
|
||||
var validPacketTypes = map[uint16]bool{
|
||||
SynPacket: true,
|
||||
ConnectPacket: true,
|
||||
DataPacket: true,
|
||||
DisconnectPacket: true,
|
||||
PingPacket: true,
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package nex
|
||||
package constants
|
||||
|
||||
// SignatureMethod is an implementation of the nn::nex::PRUDPMessageInterface::SignatureMethod enum.
|
||||
//
|
|
@ -1,4 +1,4 @@
|
|||
package nex
|
||||
package constants
|
||||
|
||||
// TODO - Should this be moved to the types module?
|
||||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/PretendoNetwork/nex-go/constants"
|
||||
"github.com/PretendoNetwork/nex-go/types"
|
||||
)
|
||||
|
||||
|
@ -22,7 +23,7 @@ type PRUDPConnection struct {
|
|||
SessionKey []byte // * Secret key generated at the start of the session. Used for encrypting packets to the secure server
|
||||
pid *types.PID // * PID of the user
|
||||
DefaultPRUDPVersion int // * The PRUDP version the connection was established with. Used for sending PING packets
|
||||
StreamType StreamType // * rdv::Stream::Type used in this connection
|
||||
StreamType constants.StreamType // * rdv::Stream::Type used in this connection
|
||||
StreamID uint8 // * rdv::Stream ID, also called the "port number", used in this connection. 0-15 on PRUDPv0/v1, and 0-31 on PRUDPLite
|
||||
StreamSettings *StreamSettings // * Settings for this virtual connection
|
||||
Signature []byte // * Connection signature for packets coming from the client, as seen by the server
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/PretendoNetwork/nex-go/constants"
|
||||
"github.com/PretendoNetwork/nex-go/types"
|
||||
)
|
||||
|
||||
|
@ -113,21 +114,21 @@ func (pep *PRUDPEndPoint) processPacket(packet PRUDPPacketInterface, socket *Soc
|
|||
|
||||
packet.SetSender(connection)
|
||||
|
||||
if packet.HasFlag(FlagAck) || packet.HasFlag(FlagMultiAck) {
|
||||
if packet.HasFlag(constants.FlagAck) || packet.HasFlag(constants.FlagMultiAck) {
|
||||
pep.handleAcknowledgment(packet)
|
||||
return
|
||||
}
|
||||
|
||||
switch packet.Type() {
|
||||
case SynPacket:
|
||||
case constants.SynPacket:
|
||||
pep.handleSyn(packet)
|
||||
case ConnectPacket:
|
||||
case constants.ConnectPacket:
|
||||
pep.handleConnect(packet)
|
||||
case DataPacket:
|
||||
case constants.DataPacket:
|
||||
pep.handleData(packet)
|
||||
case DisconnectPacket:
|
||||
case constants.DisconnectPacket:
|
||||
pep.handleDisconnect(packet)
|
||||
case PingPacket:
|
||||
case constants.PingPacket:
|
||||
pep.handlePing(packet)
|
||||
}
|
||||
}
|
||||
|
@ -140,7 +141,7 @@ func (pep *PRUDPEndPoint) handleAcknowledgment(packet PRUDPPacketInterface) {
|
|||
return
|
||||
}
|
||||
|
||||
if packet.HasFlag(FlagMultiAck) {
|
||||
if packet.HasFlag(constants.FlagMultiAck) {
|
||||
pep.handleMultiAcknowledgment(packet)
|
||||
return
|
||||
}
|
||||
|
@ -224,9 +225,9 @@ func (pep *PRUDPEndPoint) handleSyn(packet PRUDPPacketInterface) {
|
|||
connection.reset()
|
||||
connection.Signature = connectionSignature
|
||||
|
||||
ack.SetType(SynPacket)
|
||||
ack.AddFlag(FlagAck)
|
||||
ack.AddFlag(FlagHasSize)
|
||||
ack.SetType(constants.SynPacket)
|
||||
ack.AddFlag(constants.FlagAck)
|
||||
ack.AddFlag(constants.FlagHasSize)
|
||||
ack.SetSourceVirtualPortStreamType(packet.DestinationVirtualPortStreamType())
|
||||
ack.SetSourceVirtualPortStreamID(packet.DestinationVirtualPortStreamID())
|
||||
ack.SetDestinationVirtualPortStreamType(packet.SourceVirtualPortStreamType())
|
||||
|
@ -277,9 +278,9 @@ func (pep *PRUDPEndPoint) handleConnect(packet PRUDPPacketInterface) {
|
|||
|
||||
connection.ServerSessionID = packet.SessionID()
|
||||
|
||||
ack.SetType(ConnectPacket)
|
||||
ack.AddFlag(FlagAck)
|
||||
ack.AddFlag(FlagHasSize)
|
||||
ack.SetType(constants.ConnectPacket)
|
||||
ack.AddFlag(constants.FlagAck)
|
||||
ack.AddFlag(constants.FlagHasSize)
|
||||
ack.SetSourceVirtualPortStreamType(packet.DestinationVirtualPortStreamType())
|
||||
ack.SetSourceVirtualPortStreamID(packet.DestinationVirtualPortStreamID())
|
||||
ack.SetDestinationVirtualPortStreamType(packet.SourceVirtualPortStreamType())
|
||||
|
@ -385,7 +386,7 @@ func (pep *PRUDPEndPoint) handleData(packet PRUDPPacketInterface) {
|
|||
|
||||
connection.resetHeartbeat()
|
||||
|
||||
if packet.HasFlag(FlagReliable) {
|
||||
if packet.HasFlag(constants.FlagReliable) {
|
||||
pep.handleReliable(packet)
|
||||
} else {
|
||||
pep.handleUnreliable(packet)
|
||||
|
@ -396,7 +397,7 @@ func (pep *PRUDPEndPoint) handleDisconnect(packet PRUDPPacketInterface) {
|
|||
// TODO - Should we check the state here, or just let the connection disconnect at any time?
|
||||
// TODO - Should we bother to set the connections state here? It's being destroyed anyway
|
||||
|
||||
if packet.HasFlag(FlagNeedsAck) {
|
||||
if packet.HasFlag(constants.FlagNeedsAck) {
|
||||
pep.acknowledgePacket(packet)
|
||||
}
|
||||
|
||||
|
@ -416,7 +417,7 @@ func (pep *PRUDPEndPoint) handlePing(packet PRUDPPacketInterface) {
|
|||
|
||||
connection.resetHeartbeat()
|
||||
|
||||
if packet.HasFlag(FlagNeedsAck) {
|
||||
if packet.HasFlag(constants.FlagNeedsAck) {
|
||||
pep.acknowledgePacket(packet)
|
||||
}
|
||||
}
|
||||
|
@ -499,7 +500,7 @@ func (pep *PRUDPEndPoint) acknowledgePacket(packet PRUDPPacketInterface) {
|
|||
}
|
||||
|
||||
ack.SetType(packet.Type())
|
||||
ack.AddFlag(FlagAck)
|
||||
ack.AddFlag(constants.FlagAck)
|
||||
ack.SetSourceVirtualPortStreamType(packet.DestinationVirtualPortStreamType())
|
||||
ack.SetSourceVirtualPortStreamID(packet.DestinationVirtualPortStreamID())
|
||||
ack.SetDestinationVirtualPortStreamType(packet.SourceVirtualPortStreamType())
|
||||
|
@ -511,14 +512,14 @@ func (pep *PRUDPEndPoint) acknowledgePacket(packet PRUDPPacketInterface) {
|
|||
pep.Server.sendPacket(ack)
|
||||
|
||||
// * Servers send the DISCONNECT ACK 3 times
|
||||
if packet.Type() == DisconnectPacket {
|
||||
if packet.Type() == constants.DisconnectPacket {
|
||||
pep.Server.sendPacket(ack)
|
||||
pep.Server.sendPacket(ack)
|
||||
}
|
||||
}
|
||||
|
||||
func (pep *PRUDPEndPoint) handleReliable(packet PRUDPPacketInterface) {
|
||||
if packet.HasFlag(FlagNeedsAck) {
|
||||
if packet.HasFlag(constants.FlagNeedsAck) {
|
||||
pep.acknowledgePacket(packet)
|
||||
}
|
||||
|
||||
|
@ -527,7 +528,7 @@ func (pep *PRUDPEndPoint) handleReliable(packet PRUDPPacketInterface) {
|
|||
slidingWindow := packet.Sender().(*PRUDPConnection).SlidingWindow(packet.SubstreamID())
|
||||
|
||||
for _, pendingPacket := range slidingWindow.Update(packet) {
|
||||
if packet.Type() == DataPacket {
|
||||
if packet.Type() == constants.DataPacket {
|
||||
var decryptedPayload []byte
|
||||
|
||||
if packet.Version() != 2 {
|
||||
|
@ -563,7 +564,7 @@ func (pep *PRUDPEndPoint) handleReliable(packet PRUDPPacketInterface) {
|
|||
}
|
||||
|
||||
func (pep *PRUDPEndPoint) handleUnreliable(packet PRUDPPacketInterface) {
|
||||
if packet.HasFlag(FlagNeedsAck) {
|
||||
if packet.HasFlag(constants.FlagNeedsAck) {
|
||||
pep.acknowledgePacket(packet)
|
||||
}
|
||||
|
||||
|
@ -635,8 +636,8 @@ func (pep *PRUDPEndPoint) sendPing(connection *PRUDPConnection) {
|
|||
ping, _ = NewPRUDPPacketLite(pep.Server, connection, nil)
|
||||
}
|
||||
|
||||
ping.SetType(PingPacket)
|
||||
ping.AddFlag(FlagNeedsAck)
|
||||
ping.SetType(constants.PingPacket)
|
||||
ping.AddFlag(constants.FlagNeedsAck)
|
||||
ping.SetSourceVirtualPortStreamType(connection.StreamType)
|
||||
ping.SetSourceVirtualPortStreamID(pep.StreamID)
|
||||
ping.SetDestinationVirtualPortStreamType(connection.StreamType)
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package nex
|
||||
|
||||
import "crypto/rc4"
|
||||
import (
|
||||
"crypto/rc4"
|
||||
|
||||
"github.com/PretendoNetwork/nex-go/constants"
|
||||
)
|
||||
|
||||
// PRUDPPacket holds all the fields each packet should have in all PRUDP versions
|
||||
type PRUDPPacket struct {
|
||||
|
@ -58,12 +62,12 @@ func (p *PRUDPPacket) Type() uint16 {
|
|||
}
|
||||
|
||||
// SetSourceVirtualPortStreamType sets the packets source VirtualPort StreamType
|
||||
func (p *PRUDPPacket) SetSourceVirtualPortStreamType(streamType StreamType) {
|
||||
func (p *PRUDPPacket) SetSourceVirtualPortStreamType(streamType constants.StreamType) {
|
||||
p.sourceVirtualPort.SetStreamType(streamType)
|
||||
}
|
||||
|
||||
// SourceVirtualPortStreamType returns the packets source VirtualPort StreamType
|
||||
func (p *PRUDPPacket) SourceVirtualPortStreamType() StreamType {
|
||||
func (p *PRUDPPacket) SourceVirtualPortStreamType() constants.StreamType {
|
||||
return p.sourceVirtualPort.StreamType()
|
||||
}
|
||||
|
||||
|
@ -78,12 +82,12 @@ func (p *PRUDPPacket) SourceVirtualPortStreamID() uint8 {
|
|||
}
|
||||
|
||||
// SetDestinationVirtualPortStreamType sets the packets destination VirtualPort StreamType
|
||||
func (p *PRUDPPacket) SetDestinationVirtualPortStreamType(streamType StreamType) {
|
||||
func (p *PRUDPPacket) SetDestinationVirtualPortStreamType(streamType constants.StreamType) {
|
||||
p.destinationVirtualPort.SetStreamType(streamType)
|
||||
}
|
||||
|
||||
// DestinationVirtualPortStreamType returns the packets destination VirtualPort StreamType
|
||||
func (p *PRUDPPacket) DestinationVirtualPortStreamType() StreamType {
|
||||
func (p *PRUDPPacket) DestinationVirtualPortStreamType() constants.StreamType {
|
||||
return p.destinationVirtualPort.StreamType()
|
||||
}
|
||||
|
||||
|
@ -145,7 +149,7 @@ func (p *PRUDPPacket) decryptPayload() []byte {
|
|||
payload := p.payload
|
||||
|
||||
// TODO - This assumes a reliable DATA packet. Handle unreliable here? Or do that in a different method?
|
||||
if p.packetType == DataPacket {
|
||||
if p.packetType == constants.DataPacket {
|
||||
slidingWindow := p.sender.SlidingWindow(p.SubstreamID())
|
||||
|
||||
payload, _ = slidingWindow.streamSettings.EncryptionAlgorithm.Decrypt(payload)
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package nex
|
||||
|
||||
import "net"
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/PretendoNetwork/nex-go/constants"
|
||||
)
|
||||
|
||||
// PRUDPPacketInterface defines all the methods a PRUDP packet should have
|
||||
type PRUDPPacketInterface interface {
|
||||
|
@ -14,12 +18,12 @@ type PRUDPPacketInterface interface {
|
|||
AddFlag(flag uint16)
|
||||
SetType(packetType uint16)
|
||||
Type() uint16
|
||||
SetSourceVirtualPortStreamType(streamType StreamType)
|
||||
SourceVirtualPortStreamType() StreamType
|
||||
SetSourceVirtualPortStreamType(streamType constants.StreamType)
|
||||
SourceVirtualPortStreamType() constants.StreamType
|
||||
SetSourceVirtualPortStreamID(port uint8)
|
||||
SourceVirtualPortStreamID() uint8
|
||||
SetDestinationVirtualPortStreamType(streamType StreamType)
|
||||
DestinationVirtualPortStreamType() StreamType
|
||||
SetDestinationVirtualPortStreamType(streamType constants.StreamType)
|
||||
DestinationVirtualPortStreamType() constants.StreamType
|
||||
SetDestinationVirtualPortStreamID(port uint8)
|
||||
DestinationVirtualPortStreamID() uint8
|
||||
SessionID() uint8
|
||||
|
|
|
@ -6,14 +6,16 @@ import (
|
|||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/PretendoNetwork/nex-go/constants"
|
||||
)
|
||||
|
||||
// PRUDPPacketLite represents a PRUDPLite packet
|
||||
type PRUDPPacketLite struct {
|
||||
PRUDPPacket
|
||||
sourceVirtualPortStreamType StreamType
|
||||
sourceVirtualPortStreamType constants.StreamType
|
||||
sourceVirtualPortStreamID uint8
|
||||
destinationVirtualPortStreamType StreamType
|
||||
destinationVirtualPortStreamType constants.StreamType
|
||||
destinationVirtualPortStreamID uint8
|
||||
optionsLength uint8
|
||||
minorVersion uint32
|
||||
|
@ -23,13 +25,13 @@ type PRUDPPacketLite struct {
|
|||
liteSignature []byte
|
||||
}
|
||||
|
||||
// SetSourceVirtualPortStreamType sets the packets source VirtualPort StreamType
|
||||
func (p *PRUDPPacketLite) SetSourceVirtualPortStreamType(streamType StreamType) {
|
||||
// SetSourceVirtualPortStreamType sets the packets source VirtualPort StreamType
|
||||
func (p *PRUDPPacketLite) SetSourceVirtualPortStreamType(streamType constants.StreamType) {
|
||||
p.sourceVirtualPortStreamType = streamType
|
||||
}
|
||||
|
||||
// SourceVirtualPortStreamType returns the packets source VirtualPort StreamType
|
||||
func (p *PRUDPPacketLite) SourceVirtualPortStreamType() StreamType {
|
||||
func (p *PRUDPPacketLite) SourceVirtualPortStreamType() constants.StreamType {
|
||||
return p.sourceVirtualPortStreamType
|
||||
}
|
||||
|
||||
|
@ -43,13 +45,13 @@ func (p *PRUDPPacketLite) SourceVirtualPortStreamID() uint8 {
|
|||
return p.sourceVirtualPort.StreamID()
|
||||
}
|
||||
|
||||
// SetDestinationVirtualPortStreamType sets the packets destination VirtualPort StreamType
|
||||
func (p *PRUDPPacketLite) SetDestinationVirtualPortStreamType(streamType StreamType) {
|
||||
// SetDestinationVirtualPortStreamType sets the packets destination VirtualPort constants.StreamType
|
||||
func (p *PRUDPPacketLite) SetDestinationVirtualPortStreamType(streamType constants.StreamType) {
|
||||
p.destinationVirtualPortStreamType = streamType
|
||||
}
|
||||
|
||||
// DestinationVirtualPortStreamType returns the packets destination VirtualPort StreamType
|
||||
func (p *PRUDPPacketLite) DestinationVirtualPortStreamType() StreamType {
|
||||
// DestinationVirtualPortStreamType returns the packets destination VirtualPort constants.StreamType
|
||||
func (p *PRUDPPacketLite) DestinationVirtualPortStreamType() constants.StreamType {
|
||||
return p.destinationVirtualPortStreamType
|
||||
}
|
||||
|
||||
|
@ -139,8 +141,8 @@ func (p *PRUDPPacketLite) decode() error {
|
|||
return fmt.Errorf("Failed to decode PRUDPLite virtual ports stream types. %s", err.Error())
|
||||
}
|
||||
|
||||
p.sourceVirtualPortStreamType = StreamType(streamTypes >> 4)
|
||||
p.destinationVirtualPortStreamType = StreamType(streamTypes & 0xF)
|
||||
p.sourceVirtualPortStreamType = constants.StreamType(streamTypes >> 4)
|
||||
p.destinationVirtualPortStreamType = constants.StreamType(streamTypes & 0xF)
|
||||
|
||||
p.sourceVirtualPortStreamID, err = p.readStream.ReadPrimitiveUInt8()
|
||||
if err != nil {
|
||||
|
@ -220,7 +222,7 @@ func (p *PRUDPPacketLite) decodeOptions() error {
|
|||
return err
|
||||
}
|
||||
|
||||
if p.packetType == SynPacket || p.packetType == ConnectPacket {
|
||||
if p.packetType == constants.SynPacket || p.packetType == constants.ConnectPacket {
|
||||
if optionID == 0 {
|
||||
p.supportedFunctions, err = optionsStream.ReadPrimitiveUInt32LE()
|
||||
|
||||
|
@ -237,19 +239,19 @@ func (p *PRUDPPacketLite) decodeOptions() error {
|
|||
}
|
||||
}
|
||||
|
||||
if p.packetType == ConnectPacket {
|
||||
if p.packetType == constants.ConnectPacket {
|
||||
if optionID == 3 {
|
||||
p.initialUnreliableSequenceID, err = optionsStream.ReadPrimitiveUInt16LE()
|
||||
}
|
||||
}
|
||||
|
||||
if p.packetType == DataPacket {
|
||||
if p.packetType == constants.DataPacket {
|
||||
if optionID == 2 {
|
||||
p.fragmentID, err = optionsStream.ReadPrimitiveUInt8()
|
||||
}
|
||||
}
|
||||
|
||||
if p.packetType == ConnectPacket && !p.HasFlag(FlagAck) {
|
||||
if p.packetType == constants.ConnectPacket && !p.HasFlag(constants.FlagAck) {
|
||||
if optionID == 0x80 {
|
||||
p.liteSignature = optionsStream.ReadBytesNext(int64(optionSize))
|
||||
}
|
||||
|
@ -269,19 +271,19 @@ func (p *PRUDPPacketLite) decodeOptions() error {
|
|||
func (p *PRUDPPacketLite) encodeOptions() []byte {
|
||||
optionsStream := NewByteStreamOut(p.server.LibraryVersions, p.server.ByteStreamSettings)
|
||||
|
||||
if p.packetType == SynPacket || p.packetType == ConnectPacket {
|
||||
if p.packetType == constants.SynPacket || p.packetType == constants.ConnectPacket {
|
||||
optionsStream.WritePrimitiveUInt8(0)
|
||||
optionsStream.WritePrimitiveUInt8(4)
|
||||
optionsStream.WritePrimitiveUInt32LE(p.minorVersion | (p.supportedFunctions << 8))
|
||||
|
||||
if p.packetType == SynPacket && p.HasFlag(FlagAck) {
|
||||
if p.packetType == constants.SynPacket && p.HasFlag(constants.FlagAck) {
|
||||
optionsStream.WritePrimitiveUInt8(1)
|
||||
optionsStream.WritePrimitiveUInt8(16)
|
||||
optionsStream.Grow(16)
|
||||
optionsStream.WriteBytesNext(p.connectionSignature)
|
||||
}
|
||||
|
||||
if p.packetType == ConnectPacket && !p.HasFlag(FlagAck) {
|
||||
if p.packetType == constants.ConnectPacket && !p.HasFlag(constants.FlagAck) {
|
||||
optionsStream.WritePrimitiveUInt8(1)
|
||||
optionsStream.WritePrimitiveUInt8(16)
|
||||
optionsStream.Grow(16)
|
||||
|
|
|
@ -8,6 +8,8 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"slices"
|
||||
|
||||
"github.com/PretendoNetwork/nex-go/constants"
|
||||
)
|
||||
|
||||
// PRUDPPacketV0 represents a PRUDPv0 packet
|
||||
|
@ -98,7 +100,7 @@ func (p *PRUDPPacketV0) decode() error {
|
|||
p.packetType = typeAndFlags & 0xF
|
||||
}
|
||||
|
||||
if _, ok := validPacketTypes[p.packetType]; !ok {
|
||||
if p.packetType > constants.PingPacket {
|
||||
return errors.New("Invalid PRUDPv0 packet type")
|
||||
}
|
||||
|
||||
|
@ -114,7 +116,7 @@ func (p *PRUDPPacketV0) decode() error {
|
|||
return fmt.Errorf("Failed to read PRUDPv0 sequence ID. %s", err.Error())
|
||||
}
|
||||
|
||||
if p.packetType == SynPacket || p.packetType == ConnectPacket {
|
||||
if p.packetType == constants.SynPacket || p.packetType == constants.ConnectPacket {
|
||||
if p.readStream.Remaining() < 4 {
|
||||
return errors.New("Failed to read PRUDPv0 connection signature. Not have enough data")
|
||||
}
|
||||
|
@ -122,7 +124,7 @@ func (p *PRUDPPacketV0) decode() error {
|
|||
p.connectionSignature = p.readStream.ReadBytesNext(4)
|
||||
}
|
||||
|
||||
if p.packetType == DataPacket {
|
||||
if p.packetType == constants.DataPacket {
|
||||
if p.readStream.Remaining() < 1 {
|
||||
return errors.New("Failed to read PRUDPv0 fragment ID. Not have enough data")
|
||||
}
|
||||
|
@ -135,7 +137,7 @@ func (p *PRUDPPacketV0) decode() error {
|
|||
|
||||
var payloadSize uint16
|
||||
|
||||
if p.HasFlag(FlagHasSize) {
|
||||
if p.HasFlag(constants.FlagHasSize) {
|
||||
if p.readStream.Remaining() < 2 {
|
||||
return errors.New("Failed to read PRUDPv0 payload size. Not have enough data")
|
||||
}
|
||||
|
@ -209,16 +211,16 @@ func (p *PRUDPPacketV0) Bytes() []byte {
|
|||
stream.WriteBytesNext(p.signature)
|
||||
stream.WritePrimitiveUInt16LE(p.sequenceID)
|
||||
|
||||
if p.packetType == SynPacket || p.packetType == ConnectPacket {
|
||||
if p.packetType == constants.SynPacket || p.packetType == constants.ConnectPacket {
|
||||
stream.Grow(int64(len(p.connectionSignature)))
|
||||
stream.WriteBytesNext(p.connectionSignature)
|
||||
}
|
||||
|
||||
if p.packetType == DataPacket {
|
||||
if p.packetType == constants.DataPacket {
|
||||
stream.WritePrimitiveUInt8(p.fragmentID)
|
||||
}
|
||||
|
||||
if p.HasFlag(FlagHasSize) {
|
||||
if p.HasFlag(constants.FlagHasSize) {
|
||||
stream.WritePrimitiveUInt16LE(uint16(len(p.payload)))
|
||||
}
|
||||
|
||||
|
@ -310,11 +312,11 @@ func defaultPRUDPv0ConnectionSignature(packet *PRUDPPacketV0, addr net.Addr) ([]
|
|||
|
||||
func defaultPRUDPv0CalculateSignature(packet *PRUDPPacketV0, sessionKey, connectionSignature []byte) []byte {
|
||||
if !packet.server.PRUDPV0Settings.LegacyConnectionSignature {
|
||||
if packet.packetType == DataPacket {
|
||||
if packet.packetType == constants.DataPacket {
|
||||
return packet.server.PRUDPV0Settings.DataSignatureCalculator(packet, sessionKey)
|
||||
}
|
||||
|
||||
if packet.packetType == DisconnectPacket && packet.server.AccessKey != "ridfebb9" {
|
||||
if packet.packetType == constants.DisconnectPacket && packet.server.AccessKey != "ridfebb9" {
|
||||
return packet.server.PRUDPV0Settings.DataSignatureCalculator(packet, sessionKey)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/PretendoNetwork/nex-go/constants"
|
||||
)
|
||||
|
||||
// PRUDPPacketV1 represents a PRUDPv1 packet
|
||||
|
@ -174,7 +176,7 @@ func (p *PRUDPPacketV1) decodeHeader() error {
|
|||
p.flags = typeAndFlags >> 4
|
||||
p.packetType = typeAndFlags & 0xF
|
||||
|
||||
if _, ok := validPacketTypes[p.packetType]; !ok {
|
||||
if p.packetType > constants.PingPacket {
|
||||
return errors.New("Invalid PRUDPv1 packet type")
|
||||
}
|
||||
|
||||
|
@ -227,7 +229,7 @@ func (p *PRUDPPacketV1) decodeOptions() error {
|
|||
return err
|
||||
}
|
||||
|
||||
if p.packetType == SynPacket || p.packetType == ConnectPacket {
|
||||
if p.packetType == constants.SynPacket || p.packetType == constants.ConnectPacket {
|
||||
if optionID == 0 {
|
||||
p.supportedFunctions, err = optionsStream.ReadPrimitiveUInt32LE()
|
||||
|
||||
|
@ -244,13 +246,13 @@ func (p *PRUDPPacketV1) decodeOptions() error {
|
|||
}
|
||||
}
|
||||
|
||||
if p.packetType == ConnectPacket {
|
||||
if p.packetType == constants.ConnectPacket {
|
||||
if optionID == 3 {
|
||||
p.initialUnreliableSequenceID, err = optionsStream.ReadPrimitiveUInt16LE()
|
||||
}
|
||||
}
|
||||
|
||||
if p.packetType == DataPacket {
|
||||
if p.packetType == constants.DataPacket {
|
||||
if optionID == 2 {
|
||||
p.fragmentID, err = optionsStream.ReadPrimitiveUInt8()
|
||||
}
|
||||
|
@ -270,7 +272,7 @@ func (p *PRUDPPacketV1) decodeOptions() error {
|
|||
func (p *PRUDPPacketV1) encodeOptions() []byte {
|
||||
optionsStream := NewByteStreamOut(p.server.LibraryVersions, p.server.ByteStreamSettings)
|
||||
|
||||
if p.packetType == SynPacket || p.packetType == ConnectPacket {
|
||||
if p.packetType == constants.SynPacket || p.packetType == constants.ConnectPacket {
|
||||
optionsStream.WritePrimitiveUInt8(0)
|
||||
optionsStream.WritePrimitiveUInt8(4)
|
||||
optionsStream.WritePrimitiveUInt32LE(p.minorVersion | (p.supportedFunctions << 8))
|
||||
|
@ -288,7 +290,7 @@ func (p *PRUDPPacketV1) encodeOptions() []byte {
|
|||
// * specific order. Due to how this section is
|
||||
// * parsed, though, order REALLY doesn't matter.
|
||||
// * NintendoClients expects option 3 before 4, though
|
||||
if p.packetType == ConnectPacket {
|
||||
if p.packetType == constants.ConnectPacket {
|
||||
optionsStream.WritePrimitiveUInt8(3)
|
||||
optionsStream.WritePrimitiveUInt8(2)
|
||||
optionsStream.WritePrimitiveUInt16LE(p.initialUnreliableSequenceID)
|
||||
|
@ -299,7 +301,7 @@ func (p *PRUDPPacketV1) encodeOptions() []byte {
|
|||
optionsStream.WritePrimitiveUInt8(p.maximumSubstreamID)
|
||||
}
|
||||
|
||||
if p.packetType == DataPacket {
|
||||
if p.packetType == constants.DataPacket {
|
||||
optionsStream.WritePrimitiveUInt8(2)
|
||||
optionsStream.WritePrimitiveUInt8(1)
|
||||
optionsStream.WritePrimitiveUInt8(p.fragmentID)
|
||||
|
@ -388,7 +390,7 @@ func defaultPRUDPv1CalculateSignature(packet *PRUDPPacketV1, sessionKey, connect
|
|||
key := md5.Sum(accessKeyBytes)
|
||||
mac := hmac.New(md5.New, key[:])
|
||||
|
||||
if packet.packetType == ConnectPacket && packet.server.PRUDPV1Settings.LegacyConnectionSignature {
|
||||
if packet.packetType == constants.ConnectPacket && packet.server.PRUDPV1Settings.LegacyConnectionSignature {
|
||||
connectionSignature = make([]byte, 0)
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"net"
|
||||
"runtime"
|
||||
|
||||
"github.com/PretendoNetwork/nex-go/constants"
|
||||
"github.com/lxzan/gws"
|
||||
)
|
||||
|
||||
|
@ -165,12 +166,12 @@ func (ps *PRUDPServer) processPacket(packet PRUDPPacketInterface, address net.Ad
|
|||
return
|
||||
}
|
||||
|
||||
if packet.DestinationVirtualPortStreamType() > StreamTypeRelay {
|
||||
if packet.DestinationVirtualPortStreamType() > constants.StreamTypeRelay {
|
||||
logger.Warningf("Client %s trying to use invalid to destination stream type %d", address.String(), packet.DestinationVirtualPortStreamType())
|
||||
return
|
||||
}
|
||||
|
||||
if packet.SourceVirtualPortStreamType() > StreamTypeRelay {
|
||||
if packet.SourceVirtualPortStreamType() > constants.StreamTypeRelay {
|
||||
logger.Warningf("Client %s trying to use invalid to source stream type %d", address.String(), packet.DestinationVirtualPortStreamType())
|
||||
return
|
||||
}
|
||||
|
@ -233,13 +234,13 @@ func (ps *PRUDPServer) sendPacket(packet PRUDPPacketInterface) {
|
|||
packetCopy := packet.Copy()
|
||||
connection := packetCopy.Sender().(*PRUDPConnection)
|
||||
|
||||
if !packetCopy.HasFlag(FlagAck) && !packetCopy.HasFlag(FlagMultiAck) {
|
||||
if packetCopy.HasFlag(FlagReliable) {
|
||||
if !packetCopy.HasFlag(constants.FlagAck) && !packetCopy.HasFlag(constants.FlagMultiAck) {
|
||||
if packetCopy.HasFlag(constants.FlagReliable) {
|
||||
slidingWindow := connection.SlidingWindow(packetCopy.SubstreamID())
|
||||
packetCopy.SetSequenceID(slidingWindow.NextOutgoingSequenceID())
|
||||
} else if packetCopy.Type() == DataPacket {
|
||||
} else if packetCopy.Type() == constants.DataPacket {
|
||||
packetCopy.SetSequenceID(connection.outgoingUnreliableSequenceIDCounter.Next())
|
||||
} else if packetCopy.Type() == PingPacket {
|
||||
} else if packetCopy.Type() == constants.PingPacket {
|
||||
packetCopy.SetSequenceID(connection.outgoingPingSequenceIDCounter.Next())
|
||||
} else {
|
||||
packetCopy.SetSequenceID(0)
|
||||
|
@ -248,8 +249,8 @@ func (ps *PRUDPServer) sendPacket(packet PRUDPPacketInterface) {
|
|||
|
||||
packetCopy.SetSessionID(connection.ServerSessionID)
|
||||
|
||||
if packetCopy.Type() == DataPacket && !packetCopy.HasFlag(FlagAck) && !packetCopy.HasFlag(FlagMultiAck) {
|
||||
if packetCopy.HasFlag(FlagReliable) {
|
||||
if packetCopy.Type() == constants.DataPacket && !packetCopy.HasFlag(constants.FlagAck) && !packetCopy.HasFlag(constants.FlagMultiAck) {
|
||||
if packetCopy.HasFlag(constants.FlagReliable) {
|
||||
slidingWindow := connection.SlidingWindow(packetCopy.SubstreamID())
|
||||
payload := packetCopy.Payload()
|
||||
|
||||
|
@ -278,7 +279,7 @@ func (ps *PRUDPServer) sendPacket(packet PRUDPPacketInterface) {
|
|||
packetCopy.setSignature(packetCopy.calculateSignature(connection.SessionKey, connection.ServerConnectionSignature))
|
||||
}
|
||||
|
||||
if packetCopy.HasFlag(FlagReliable) && packetCopy.HasFlag(FlagNeedsAck) {
|
||||
if packetCopy.HasFlag(constants.FlagReliable) && packetCopy.HasFlag(constants.FlagNeedsAck) {
|
||||
slidingWindow := connection.SlidingWindow(packetCopy.SubstreamID())
|
||||
slidingWindow.ResendScheduler.AddPacket(packetCopy)
|
||||
}
|
||||
|
|
13
test/auth.go
13
test/auth.go
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
"github.com/PretendoNetwork/nex-go/constants"
|
||||
"github.com/PretendoNetwork/nex-go/types"
|
||||
)
|
||||
|
||||
|
@ -95,9 +96,9 @@ func login(packet nex.PRUDPPacketInterface) {
|
|||
responsePacket, _ := nex.NewPRUDPPacketV0(authServer, packet.Sender().(*nex.PRUDPConnection), nil)
|
||||
|
||||
responsePacket.SetType(packet.Type())
|
||||
responsePacket.AddFlag(nex.FlagHasSize)
|
||||
responsePacket.AddFlag(nex.FlagReliable)
|
||||
responsePacket.AddFlag(nex.FlagNeedsAck)
|
||||
responsePacket.AddFlag(constants.FlagHasSize)
|
||||
responsePacket.AddFlag(constants.FlagReliable)
|
||||
responsePacket.AddFlag(constants.FlagNeedsAck)
|
||||
responsePacket.SetSourceVirtualPortStreamType(packet.DestinationVirtualPortStreamType())
|
||||
responsePacket.SetSourceVirtualPortStreamID(packet.DestinationVirtualPortStreamID())
|
||||
responsePacket.SetDestinationVirtualPortStreamType(packet.SourceVirtualPortStreamType())
|
||||
|
@ -148,9 +149,9 @@ func requestTicket(packet nex.PRUDPPacketInterface) {
|
|||
responsePacket, _ := nex.NewPRUDPPacketV0(authServer, packet.Sender().(*nex.PRUDPConnection), nil)
|
||||
|
||||
responsePacket.SetType(packet.Type())
|
||||
responsePacket.AddFlag(nex.FlagHasSize)
|
||||
responsePacket.AddFlag(nex.FlagReliable)
|
||||
responsePacket.AddFlag(nex.FlagNeedsAck)
|
||||
responsePacket.AddFlag(constants.FlagHasSize)
|
||||
responsePacket.AddFlag(constants.FlagReliable)
|
||||
responsePacket.AddFlag(constants.FlagNeedsAck)
|
||||
responsePacket.SetSourceVirtualPortStreamType(packet.DestinationVirtualPortStreamType())
|
||||
responsePacket.SetSourceVirtualPortStreamID(packet.DestinationVirtualPortStreamID())
|
||||
responsePacket.SetDestinationVirtualPortStreamType(packet.SourceVirtualPortStreamType())
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/PretendoNetwork/nex-go"
|
||||
"github.com/PretendoNetwork/nex-go/constants"
|
||||
"github.com/PretendoNetwork/nex-go/types"
|
||||
)
|
||||
|
||||
|
@ -111,8 +112,8 @@ func registerEx(packet nex.PRUDPPacketInterface) {
|
|||
|
||||
address := packet.Sender().Address().(*net.UDPAddr).IP.String()
|
||||
|
||||
localStation.Fields["address"] = address
|
||||
localStation.Fields["port"] = strconv.Itoa(packet.Sender().Address().(*net.UDPAddr).Port)
|
||||
localStation.Params["address"] = address
|
||||
localStation.Params["port"] = strconv.Itoa(packet.Sender().Address().(*net.UDPAddr).Port)
|
||||
|
||||
retval := types.NewQResultSuccess(0x00010001)
|
||||
localStationURL := types.NewString(localStation.EncodeToString())
|
||||
|
@ -134,9 +135,9 @@ func registerEx(packet nex.PRUDPPacketInterface) {
|
|||
responsePacket, _ := nex.NewPRUDPPacketV0(secureServer, connection, nil)
|
||||
|
||||
responsePacket.SetType(packet.Type())
|
||||
responsePacket.AddFlag(nex.FlagHasSize)
|
||||
responsePacket.AddFlag(nex.FlagReliable)
|
||||
responsePacket.AddFlag(nex.FlagNeedsAck)
|
||||
responsePacket.AddFlag(constants.FlagHasSize)
|
||||
responsePacket.AddFlag(constants.FlagReliable)
|
||||
responsePacket.AddFlag(constants.FlagNeedsAck)
|
||||
responsePacket.SetSourceVirtualPortStreamType(packet.DestinationVirtualPortStreamType())
|
||||
responsePacket.SetSourceVirtualPortStreamID(packet.DestinationVirtualPortStreamID())
|
||||
responsePacket.SetDestinationVirtualPortStreamType(packet.SourceVirtualPortStreamType())
|
||||
|
@ -182,9 +183,9 @@ func updateAndGetAllInformation(packet nex.PRUDPPacketInterface) {
|
|||
responsePacket, _ := nex.NewPRUDPPacketV0(secureServer, packet.Sender().(*nex.PRUDPConnection), nil)
|
||||
|
||||
responsePacket.SetType(packet.Type())
|
||||
responsePacket.AddFlag(nex.FlagHasSize)
|
||||
responsePacket.AddFlag(nex.FlagReliable)
|
||||
responsePacket.AddFlag(nex.FlagNeedsAck)
|
||||
responsePacket.AddFlag(constants.FlagHasSize)
|
||||
responsePacket.AddFlag(constants.FlagReliable)
|
||||
responsePacket.AddFlag(constants.FlagNeedsAck)
|
||||
responsePacket.SetSourceVirtualPortStreamType(packet.DestinationVirtualPortStreamType())
|
||||
responsePacket.SetSourceVirtualPortStreamID(packet.DestinationVirtualPortStreamID())
|
||||
responsePacket.SetDestinationVirtualPortStreamType(packet.SourceVirtualPortStreamType())
|
||||
|
@ -214,9 +215,9 @@ func checkSettingStatus(packet nex.PRUDPPacketInterface) {
|
|||
responsePacket, _ := nex.NewPRUDPPacketV0(secureServer, packet.Sender().(*nex.PRUDPConnection), nil)
|
||||
|
||||
responsePacket.SetType(packet.Type())
|
||||
responsePacket.AddFlag(nex.FlagHasSize)
|
||||
responsePacket.AddFlag(nex.FlagReliable)
|
||||
responsePacket.AddFlag(nex.FlagNeedsAck)
|
||||
responsePacket.AddFlag(constants.FlagHasSize)
|
||||
responsePacket.AddFlag(constants.FlagReliable)
|
||||
responsePacket.AddFlag(constants.FlagNeedsAck)
|
||||
responsePacket.SetSourceVirtualPortStreamType(packet.DestinationVirtualPortStreamType())
|
||||
responsePacket.SetSourceVirtualPortStreamID(packet.DestinationVirtualPortStreamID())
|
||||
responsePacket.SetDestinationVirtualPortStreamType(packet.SourceVirtualPortStreamType())
|
||||
|
@ -241,9 +242,9 @@ func updatePresence(packet nex.PRUDPPacketInterface) {
|
|||
responsePacket, _ := nex.NewPRUDPPacketV0(secureServer, packet.Sender().(*nex.PRUDPConnection), nil)
|
||||
|
||||
responsePacket.SetType(packet.Type())
|
||||
responsePacket.AddFlag(nex.FlagHasSize)
|
||||
responsePacket.AddFlag(nex.FlagReliable)
|
||||
responsePacket.AddFlag(nex.FlagNeedsAck)
|
||||
responsePacket.AddFlag(constants.FlagHasSize)
|
||||
responsePacket.AddFlag(constants.FlagReliable)
|
||||
responsePacket.AddFlag(constants.FlagNeedsAck)
|
||||
responsePacket.SetSourceVirtualPortStreamType(packet.DestinationVirtualPortStreamType())
|
||||
responsePacket.SetSourceVirtualPortStreamID(packet.DestinationVirtualPortStreamID())
|
||||
responsePacket.SetDestinationVirtualPortStreamType(packet.SourceVirtualPortStreamType())
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package nex
|
||||
|
||||
import "github.com/PretendoNetwork/nex-go/constants"
|
||||
|
||||
// VirtualPort in an implementation of rdv::VirtualPort.
|
||||
// PRUDP will reuse a single physical socket connection for many virtual PRUDP connections.
|
||||
// VirtualPorts are a byte which represents a stream for a virtual PRUDP connection.
|
||||
|
@ -9,13 +11,13 @@ package nex
|
|||
type VirtualPort byte
|
||||
|
||||
// SetStreamType sets the VirtualPort stream type
|
||||
func (vp *VirtualPort) SetStreamType(streamType StreamType) {
|
||||
func (vp *VirtualPort) SetStreamType(streamType constants.StreamType) {
|
||||
*vp = VirtualPort((byte(*vp) & 0x0F) | (byte(streamType) << 4))
|
||||
}
|
||||
|
||||
// StreamType returns the VirtualPort stream type
|
||||
func (vp VirtualPort) StreamType() StreamType {
|
||||
return StreamType(vp >> 4)
|
||||
func (vp VirtualPort) StreamType() constants.StreamType {
|
||||
return constants.StreamType(vp >> 4)
|
||||
}
|
||||
|
||||
// SetStreamID sets the VirtualPort stream ID
|
||||
|
|
Loading…
Add table
Reference in a new issue