Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor types #56

Open
wants to merge 56 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
1dc1755
refactor(types): make primitive types into type aliases
jonbarrow May 22, 2024
a76ede1
refactor(types): make Buffer into a type alias
jonbarrow May 22, 2024
2b0f14b
refactor(types): make QBuffer into a type alias
jonbarrow May 22, 2024
bd05699
refactor(types): make DateTime into a type alias
jonbarrow May 22, 2024
f8b1187
refactor(types): make PID into a type alias
jonbarrow May 22, 2024
8a848c5
refactor(types): make QResult into a type alias
jonbarrow May 22, 2024
2514f63
refactor(types): make String into a type alias
jonbarrow May 22, 2024
44581f1
fix(types): QBuffer.Equals should not be a pointer
jonbarrow May 22, 2024
cc2b19e
refactor(types): make QUUID into a type alias
jonbarrow May 22, 2024
fabfca0
fix(types): fix bad Copy methods
jonbarrow May 22, 2024
7bc3579
refactor(types): make primitive types into type aliases
jonbarrow May 22, 2024
2a775eb
refactor(types): make Buffer into a type alias
jonbarrow May 22, 2024
7998d14
refactor(types): make QBuffer into a type alias
jonbarrow May 22, 2024
d5feb92
refactor(types): make DateTime into a type alias
jonbarrow May 22, 2024
502b601
refactor(types): make PID into a type alias
jonbarrow May 22, 2024
feca72a
refactor(types): make QResult into a type alias
jonbarrow May 22, 2024
3c28fb7
refactor(types): make String into a type alias
jonbarrow May 22, 2024
c1aa55c
fix(types): QBuffer.Equals should not be a pointer
jonbarrow May 22, 2024
885121a
refactor(types): make QUUID into a type alias
jonbarrow May 22, 2024
446a730
fix(types): fix bad Copy methods
jonbarrow May 22, 2024
daceca2
Merge branch 'types-updates' of https://github.com/PretendoNetwork/ne…
jonbarrow Jun 2, 2024
8d306f4
refactor(types): make List into a type alias
jonbarrow Jun 2, 2024
943f141
fix(types): remove recursive String call in String type
jonbarrow Jun 2, 2024
1f8c5ff
fix(types): remove recursive String call in List type
jonbarrow Jun 2, 2024
3a90361
fix: update StationURLs handling in NewPRUDPConnection
jonbarrow Jun 4, 2024
99acae0
refactor(types): make Map into a type alias
jonbarrow Jun 4, 2024
832bd19
refactor(types): remove reliance on pointers
jonbarrow Jun 5, 2024
bfe9c9c
chore(types): remove old Value and LegacyValue methods on PID
jonbarrow Jun 7, 2024
ab4dbfc
chore(types): remove pointer from StationURL.SetPrincipalID
jonbarrow Jun 7, 2024
0e38401
fix(test): make secure endpoint secure again
jonbarrow Jun 10, 2024
7dafc92
fix(types): make StationURL check if map is nil before accessing
jonbarrow Jun 10, 2024
8271f9a
chore(types): rename EncodeToString to Format on StationURL
jonbarrow Jun 11, 2024
d668caa
fix(types): rename FromString to Parse on StationURL and reject unkno…
jonbarrow Jun 11, 2024
2c48000
feat(types): add support for custom parameters in StationURLs
jonbarrow Jun 11, 2024
d19d660
fix(types): make StationURL check if custom map is nil before accessing
jonbarrow Jun 11, 2024
7f5a097
refactor(types): only call ensureFields in StationURL.ExtractFrom
jonbarrow Jun 13, 2024
9373fd8
refactor(types): add StationURL.SetURL and URL, and more accurate imp…
jonbarrow Jun 13, 2024
2c2dd7a
fix(types): add url length checks for StationURL
jonbarrow Jun 13, 2024
88c51d6
fix(types): make StationURL.Format a pointer receiver
jonbarrow Jun 13, 2024
a1ca41e
fix(types): add address and port validation in StationURL
jonbarrow Jun 13, 2024
1bee30d
chore: update test secure server StationURL
jonbarrow Jun 13, 2024
a4b341d
chore(types): update Godoc comment on StationURL.URL
jonbarrow Jun 13, 2024
b57adeb
refactor: remove references to "Primitive" naming conventions
jonbarrow Jun 16, 2024
2309a6d
Merge branch 'master' into types-updates
jonbarrow Jul 4, 2024
182418f
feat: add struct tags to struct types
jonbarrow Jul 4, 2024
a0107a9
fix: AccountDetailsByPID signature to not take in a pointer
jonbarrow Jul 4, 2024
27cdd4c
Merge branch 'master' into types-updates
jonbarrow Jul 5, 2024
4fbbe2d
chore: fix indentation in DeriveKerberosKey from merge
jonbarrow Jul 5, 2024
37d01ad
Merge branch 'master' into types-updates
jonbarrow Jul 7, 2024
b36470e
feat: add CopyRef and Deref methods to RVType(Ptr)
jonbarrow Nov 16, 2024
fb784e4
Merge branch 'types-updates' of https://github.com/PretendoNetwork/ne…
jonbarrow Nov 16, 2024
d3b7a01
chore: make CopyRef do safer, full, copies
jonbarrow Nov 17, 2024
337fdcd
chore: add method comments to RVType and RVTypePtr interfaces
jonbarrow Nov 17, 2024
359eb43
refactor(types): simplify CopyRef() to use Copy() internally
jonbarrow Nov 17, 2024
2797330
chore: fix kerberos.go indentation
jonbarrow Nov 20, 2024
eee4fc9
fix(AnyDataHolder): Use CopyRef to get RVTypePtr of object data
DaniElectra Dec 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import "github.com/PretendoNetwork/nex-go/v2/types"
// guest account, an account which represents the authentication server, and one which represents
// the secure server. See https://nintendo-wiki.pretendo.network/docs/nex/kerberos for more information.
type Account struct {
PID *types.PID // * The PID of the account. PIDs are unique IDs per account. NEX PIDs start at 1800000000 and decrement with each new account.
Username string // * The username for the account. For NEX user accounts this is the same as the accounts PID.
Password string // * The password for the account. For NEX accounts this is always 16 characters long using seemingly any ASCII character
PID types.PID // * The PID of the account. PIDs are unique IDs per account. NEX PIDs start at 1800000000 and decrement with each new account.
Username string // * The username for the account. For NEX user accounts this is the same as the accounts PID.
Password string // * The password for the account. For NEX accounts this is always 16 characters long using seemingly any ASCII character
}

// NewAccount returns a new instance of Account.
// This does not register an account, only creates a new
// struct instance.
func NewAccount(pid *types.PID, username, password string) *Account {
func NewAccount(pid types.PID, username, password string) *Account {
return &Account{
PID: pid,
Username: username,
Expand Down
44 changes: 22 additions & 22 deletions byte_stream_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,98 +68,98 @@ func (bsi *ByteStreamIn) Read(length uint64) ([]byte, error) {
return bsi.ReadBytesNext(int64(length)), nil
}

// ReadPrimitiveUInt8 reads a uint8
func (bsi *ByteStreamIn) ReadPrimitiveUInt8() (uint8, error) {
// ReadUInt8 reads a uint8
func (bsi *ByteStreamIn) ReadUInt8() (uint8, error) {
if bsi.Remaining() < 1 {
return 0, errors.New("Not enough data to read uint8")
}

return uint8(bsi.ReadByteNext()), nil
}

// ReadPrimitiveUInt16LE reads a Little-Endian encoded uint16
func (bsi *ByteStreamIn) ReadPrimitiveUInt16LE() (uint16, error) {
// ReadUInt16LE reads a Little-Endian encoded uint16
func (bsi *ByteStreamIn) ReadUInt16LE() (uint16, error) {
if bsi.Remaining() < 2 {
return 0, errors.New("Not enough data to read uint16")
}

return bsi.ReadU16LENext(1)[0], nil
}

// ReadPrimitiveUInt32LE reads a Little-Endian encoded uint32
func (bsi *ByteStreamIn) ReadPrimitiveUInt32LE() (uint32, error) {
// ReadUInt32LE reads a Little-Endian encoded uint32
func (bsi *ByteStreamIn) ReadUInt32LE() (uint32, error) {
if bsi.Remaining() < 4 {
return 0, errors.New("Not enough data to read uint32")
}

return bsi.ReadU32LENext(1)[0], nil
}

// ReadPrimitiveUInt64LE reads a Little-Endian encoded uint64
func (bsi *ByteStreamIn) ReadPrimitiveUInt64LE() (uint64, error) {
// ReadUInt64LE reads a Little-Endian encoded uint64
func (bsi *ByteStreamIn) ReadUInt64LE() (uint64, error) {
if bsi.Remaining() < 8 {
return 0, errors.New("Not enough data to read uint64")
}

return bsi.ReadU64LENext(1)[0], nil
}

// ReadPrimitiveInt8 reads a uint8
func (bsi *ByteStreamIn) ReadPrimitiveInt8() (int8, error) {
// ReadInt8 reads a uint8
func (bsi *ByteStreamIn) ReadInt8() (int8, error) {
if bsi.Remaining() < 1 {
return 0, errors.New("Not enough data to read int8")
}

return int8(bsi.ReadByteNext()), nil
}

// ReadPrimitiveInt16LE reads a Little-Endian encoded int16
func (bsi *ByteStreamIn) ReadPrimitiveInt16LE() (int16, error) {
// ReadInt16LE reads a Little-Endian encoded int16
func (bsi *ByteStreamIn) ReadInt16LE() (int16, error) {
if bsi.Remaining() < 2 {
return 0, errors.New("Not enough data to read int16")
}

return int16(bsi.ReadU16LENext(1)[0]), nil
}

// ReadPrimitiveInt32LE reads a Little-Endian encoded int32
func (bsi *ByteStreamIn) ReadPrimitiveInt32LE() (int32, error) {
// ReadInt32LE reads a Little-Endian encoded int32
func (bsi *ByteStreamIn) ReadInt32LE() (int32, error) {
if bsi.Remaining() < 4 {
return 0, errors.New("Not enough data to read int32")
}

return int32(bsi.ReadU32LENext(1)[0]), nil
}

// ReadPrimitiveInt64LE reads a Little-Endian encoded int64
func (bsi *ByteStreamIn) ReadPrimitiveInt64LE() (int64, error) {
// ReadInt64LE reads a Little-Endian encoded int64
func (bsi *ByteStreamIn) ReadInt64LE() (int64, error) {
if bsi.Remaining() < 8 {
return 0, errors.New("Not enough data to read int64")
}

return int64(bsi.ReadU64LENext(1)[0]), nil
}

// ReadPrimitiveFloat32LE reads a Little-Endian encoded float32
func (bsi *ByteStreamIn) ReadPrimitiveFloat32LE() (float32, error) {
// ReadFloat32LE reads a Little-Endian encoded float32
func (bsi *ByteStreamIn) ReadFloat32LE() (float32, error) {
if bsi.Remaining() < 4 {
return 0, errors.New("Not enough data to read float32")
}

return bsi.ReadF32LENext(1)[0], nil
}

// ReadPrimitiveFloat64LE reads a Little-Endian encoded float64
func (bsi *ByteStreamIn) ReadPrimitiveFloat64LE() (float64, error) {
// ReadFloat64LE reads a Little-Endian encoded float64
func (bsi *ByteStreamIn) ReadFloat64LE() (float64, error) {
if bsi.Remaining() < 8 {
return 0, errors.New("Not enough data to read float64")
}

return bsi.ReadF64LENext(1)[0], nil
}

// ReadPrimitiveBool reads a bool
func (bsi *ByteStreamIn) ReadPrimitiveBool() (bool, error) {
// ReadBool reads a bool
func (bsi *ByteStreamIn) ReadBool() (bool, error) {
if bsi.Remaining() < 1 {
return false, errors.New("Not enough data to read bool")
}
Expand Down
44 changes: 22 additions & 22 deletions byte_stream_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,68 +56,68 @@ func (bso *ByteStreamOut) Write(data []byte) {
bso.WriteBytesNext(data)
}

// WritePrimitiveUInt8 writes a uint8
func (bso *ByteStreamOut) WritePrimitiveUInt8(u8 uint8) {
// WriteUInt8 writes a uint8
func (bso *ByteStreamOut) WriteUInt8(u8 uint8) {
bso.Grow(1)
bso.WriteByteNext(byte(u8))
}

// WritePrimitiveUInt16LE writes a uint16 as LE
func (bso *ByteStreamOut) WritePrimitiveUInt16LE(u16 uint16) {
// WriteUInt16LE writes a uint16 as LE
func (bso *ByteStreamOut) WriteUInt16LE(u16 uint16) {
bso.Grow(2)
bso.WriteU16LENext([]uint16{u16})
}

// WritePrimitiveUInt32LE writes a uint32 as LE
func (bso *ByteStreamOut) WritePrimitiveUInt32LE(u32 uint32) {
// WriteUInt32LE writes a uint32 as LE
func (bso *ByteStreamOut) WriteUInt32LE(u32 uint32) {
bso.Grow(4)
bso.WriteU32LENext([]uint32{u32})
}

// WritePrimitiveUInt64LE writes a uint64 as LE
func (bso *ByteStreamOut) WritePrimitiveUInt64LE(u64 uint64) {
// WriteUInt64LE writes a uint64 as LE
func (bso *ByteStreamOut) WriteUInt64LE(u64 uint64) {
bso.Grow(8)
bso.WriteU64LENext([]uint64{u64})
}

// WritePrimitiveInt8 writes a int8
func (bso *ByteStreamOut) WritePrimitiveInt8(s8 int8) {
// WriteInt8 writes a int8
func (bso *ByteStreamOut) WriteInt8(s8 int8) {
bso.Grow(1)
bso.WriteByteNext(byte(s8))
}

// WritePrimitiveInt16LE writes a uint16 as LE
func (bso *ByteStreamOut) WritePrimitiveInt16LE(s16 int16) {
// WriteInt16LE writes a uint16 as LE
func (bso *ByteStreamOut) WriteInt16LE(s16 int16) {
bso.Grow(2)
bso.WriteU16LENext([]uint16{uint16(s16)})
}

// WritePrimitiveInt32LE writes a int32 as LE
func (bso *ByteStreamOut) WritePrimitiveInt32LE(s32 int32) {
// WriteInt32LE writes a int32 as LE
func (bso *ByteStreamOut) WriteInt32LE(s32 int32) {
bso.Grow(4)
bso.WriteU32LENext([]uint32{uint32(s32)})
}

// WritePrimitiveInt64LE writes a int64 as LE
func (bso *ByteStreamOut) WritePrimitiveInt64LE(s64 int64) {
// WriteInt64LE writes a int64 as LE
func (bso *ByteStreamOut) WriteInt64LE(s64 int64) {
bso.Grow(8)
bso.WriteU64LENext([]uint64{uint64(s64)})
}

// WritePrimitiveFloat32LE writes a float32 as LE
func (bso *ByteStreamOut) WritePrimitiveFloat32LE(f32 float32) {
// WriteFloat32LE writes a float32 as LE
func (bso *ByteStreamOut) WriteFloat32LE(f32 float32) {
bso.Grow(4)
bso.WriteF32LENext([]float32{f32})
}

// WritePrimitiveFloat64LE writes a float64 as LE
func (bso *ByteStreamOut) WritePrimitiveFloat64LE(f64 float64) {
// WriteFloat64LE writes a float64 as LE
func (bso *ByteStreamOut) WriteFloat64LE(f64 float64) {
bso.Grow(8)
bso.WriteF64LENext([]float64{f64})
}

// WritePrimitiveBool writes a bool
func (bso *ByteStreamOut) WritePrimitiveBool(b bool) {
// WriteBool writes a bool
func (bso *ByteStreamOut) WriteBool(b bool) {
var bVar uint8
if b {
bVar = 1
Expand Down
4 changes: 2 additions & 2 deletions connection_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import (
type ConnectionInterface interface {
Endpoint() EndpointInterface
Address() net.Addr
PID() *types.PID
SetPID(pid *types.PID)
PID() types.PID
SetPID(pid types.PID)
}
6 changes: 3 additions & 3 deletions hpp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type HPPClient struct {
address *net.TCPAddr
endpoint *HPPServer
pid *types.PID
pid types.PID
}

// Endpoint returns the server the client is connecting to
Expand All @@ -24,12 +24,12 @@ func (c *HPPClient) Address() net.Addr {
}

// PID returns the clients NEX PID
func (c *HPPClient) PID() *types.PID {
func (c *HPPClient) PID() types.PID {
return c.pid
}

// SetPID sets the clients NEX PID
func (c *HPPClient) SetPID(pid *types.PID) {
func (c *HPPClient) SetPID(pid types.PID) {
c.pid = pid
}

Expand Down
2 changes: 1 addition & 1 deletion hpp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type HPPServer struct {
dataHandlers []func(packet PacketInterface)
errorEventHandlers []func(err *Error)
byteStreamSettings *ByteStreamSettings
AccountDetailsByPID func(pid *types.PID) (*Account, *Error)
AccountDetailsByPID func(pid types.PID) (*Account, *Error)
AccountDetailsByUsername func(username string) (*Account, *Error)
useVerboseRMC bool
}
Expand Down
8 changes: 4 additions & 4 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ var logger = plogger.NewLogger()
func init() {
initResultCodes()

types.RegisterVariantType(1, types.NewPrimitiveS64(0))
types.RegisterVariantType(2, types.NewPrimitiveF64(0))
types.RegisterVariantType(3, types.NewPrimitiveBool(false))
types.RegisterVariantType(1, types.NewInt64(0))
types.RegisterVariantType(2, types.NewDouble(0))
types.RegisterVariantType(3, types.NewBool(false))
types.RegisterVariantType(4, types.NewString(""))
types.RegisterVariantType(5, types.NewDateTime(0))
types.RegisterVariantType(6, types.NewPrimitiveU64(0))
types.RegisterVariantType(6, types.NewUInt64(0))
}
32 changes: 15 additions & 17 deletions kerberos.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func NewKerberosEncryption(key []byte) *KerberosEncryption {
// KerberosTicket represents a ticket granting a user access to a secure server
type KerberosTicket struct {
SessionKey []byte
TargetPID *types.PID
InternalData *types.Buffer
TargetPID types.PID
InternalData types.Buffer
}

// Encrypt writes the ticket data to the provided stream and returns the encrypted byte slice
Expand All @@ -94,8 +94,8 @@ func NewKerberosTicket() *KerberosTicket {
// KerberosTicketInternalData holds the internal data for a kerberos ticket to be processed by the server
type KerberosTicketInternalData struct {
Server *PRUDPServer // TODO - Remove this dependency and make a settings struct
Issued *types.DateTime
SourcePID *types.PID
Issued types.DateTime
SourcePID types.PID
SessionKey []byte
}

Expand Down Expand Up @@ -152,10 +152,10 @@ func (ti *KerberosTicketInternalData) Decrypt(stream *ByteStreamIn, key []byte)
return fmt.Errorf("Failed to read Kerberos ticket internal data. %s", err.Error())
}

hash := md5.Sum(append(key, ticketKey.Value...))
hash := md5.Sum(append(key, ticketKey...))
key = hash[:]

stream = NewByteStreamIn(data.Value, stream.LibraryVersions, stream.Settings)
stream = NewByteStreamIn(data, stream.LibraryVersions, stream.Settings)
}

encryption := NewKerberosEncryption(key)
Expand Down Expand Up @@ -190,17 +190,15 @@ func NewKerberosTicketInternalData(server *PRUDPServer) *KerberosTicketInternalD
}

// DeriveKerberosKey derives a users kerberos encryption key based on their PID and password
func DeriveKerberosKey(pid *types.PID, password []byte) []byte {
iterationCount := int(65000 + pid.Value()%1024)
key := make([]byte, md5.Size)
copy(key, password)

hash := md5.Sum(password)
copy(key, hash[:])

for i := 1; i < iterationCount; i++ {
hash = md5.Sum(key)
copy(key, hash[:])
func DeriveKerberosKey(pid types.PID, password []byte) []byte {
iterationCount := int(65000 + pid%1024)
key := password
hash := make([]byte, md5.Size)

for i := 0; i < iterationCount; i++ {
sum := md5.Sum(key)
copy(hash, sum[:])
key = hash
}

return key
Expand Down
12 changes: 5 additions & 7 deletions prudp_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type PRUDPConnection struct {
SessionID uint8 // * Random value generated at the start of the session. Client and server IDs do not need to match
ServerSessionID uint8 // * Random value generated at the start of the session. Client and server IDs do not need to match
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
pid types.PID // * PID of the user
DefaultPRUDPVersion int // * The PRUDP version the connection was established with. Used for sending PING packets
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
Expand All @@ -38,7 +38,7 @@ type PRUDPConnection struct {
lastSentPingTime time.Time
heartbeatTimer *time.Timer
pingKickTimer *time.Timer
StationURLs *types.List[*types.StationURL]
StationURLs types.List[types.StationURL]
mutex *sync.Mutex
}

Expand All @@ -53,12 +53,12 @@ func (pc *PRUDPConnection) Address() net.Addr {
}

// PID returns the clients unique PID
func (pc *PRUDPConnection) PID() *types.PID {
func (pc *PRUDPConnection) PID() types.PID {
return pc.pid
}

// SetPID sets the clients unique PID
func (pc *PRUDPConnection) SetPID(pid *types.PID) {
func (pc *PRUDPConnection) SetPID(pid types.PID) {
pc.pid = pid
}

Expand Down Expand Up @@ -299,11 +299,9 @@ func NewPRUDPConnection(socket *SocketConnection) *PRUDPConnection {
outgoingUnreliableSequenceIDCounter: NewCounter[uint16](1),
outgoingPingSequenceIDCounter: NewCounter[uint16](0),
incomingFragmentBuffers: NewMutexMap[uint8, []byte](),
StationURLs: types.NewList[*types.StationURL](),
StationURLs: types.NewList[types.StationURL](),
mutex: &sync.Mutex{},
}

pc.StationURLs.Type = types.NewStationURL("")

return pc
}
Loading
Loading