create RNG in token module

This commit is contained in:
wish
2023-03-12 23:29:50 +11:00
parent cce558db9c
commit ccfd2ac36f
5 changed files with 19 additions and 15 deletions

View File

@@ -1,13 +1,22 @@
package token package token
import "math/rand" import (
"math/rand"
"time"
)
// Generate returns an alphanumeric token of specified length // Generate returns an alphanumeric token of specified length
func Generate(length int) string { func Generate(length int) string {
rng := RNG()
var chars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") var chars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length) b := make([]rune, length)
for i := range b { for i := range b {
b[i] = chars[rand.Intn(len(chars))] b[i] = chars[rng.Intn(len(chars))]
} }
return string(b) return string(b)
} }
// RNG returns a new RNG generator
func RNG() *rand.Rand {
return rand.New(rand.NewSource(time.Now().UnixNano()))
}

View File

@@ -4,13 +4,13 @@ import (
"encoding/hex" "encoding/hex"
"erupe-ce/common/byteframe" "erupe-ce/common/byteframe"
"erupe-ce/common/mhfcourse" "erupe-ce/common/mhfcourse"
"erupe-ce/common/token"
"erupe-ce/config" "erupe-ce/config"
"erupe-ce/network/binpacket" "erupe-ce/network/binpacket"
"erupe-ce/network/mhfpacket" "erupe-ce/network/mhfpacket"
"fmt" "fmt"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"math" "math"
"math/rand"
"strings" "strings"
"time" "time"
@@ -370,8 +370,7 @@ func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) {
roll.SetLE() roll.SetLE()
roll.WriteUint16(4) // Unk roll.WriteUint16(4) // Unk
roll.WriteUint16(authorLen) roll.WriteUint16(authorLen)
rand.Seed(time.Now().UnixNano()) dice := fmt.Sprintf("%d", token.RNG().Intn(100)+1)
dice := fmt.Sprintf("%d", rand.Intn(100)+1)
roll.WriteUint16(uint16(len(dice) + 1)) roll.WriteUint16(uint16(len(dice) + 1))
roll.WriteNullTerminatedBytes([]byte(dice)) roll.WriteNullTerminatedBytes([]byte(dice))
roll.WriteNullTerminatedBytes(tmp.ReadNullTerminatedBytes()) roll.WriteNullTerminatedBytes(tmp.ReadNullTerminatedBytes())

View File

@@ -1,8 +1,8 @@
package channelserver package channelserver
import ( import (
"erupe-ce/common/token"
"math" "math"
"math/rand"
"time" "time"
"erupe-ce/common/byteframe" "erupe-ce/common/byteframe"
@@ -95,9 +95,9 @@ func generateFeatureWeapons(count int) activeFeature {
} }
nums := make([]int, 0) nums := make([]int, 0)
var result int var result int
r := rand.New(rand.NewSource(time.Now().UnixNano())) rng := token.RNG()
for len(nums) < count { for len(nums) < count {
num := r.Intn(14) num := rng.Intn(14)
exist := false exist := false
for _, v := range nums { for _, v := range nums {
if v == num { if v == num {

View File

@@ -4,8 +4,8 @@ import (
"encoding/hex" "encoding/hex"
"erupe-ce/common/byteframe" "erupe-ce/common/byteframe"
ps "erupe-ce/common/pascalstring" ps "erupe-ce/common/pascalstring"
"erupe-ce/common/token"
"erupe-ce/network/mhfpacket" "erupe-ce/network/mhfpacket"
"math/rand"
"sort" "sort"
"time" "time"
) )
@@ -336,8 +336,7 @@ func handleMsgMhfEntryFesta(s *Session, p mhfpacket.MHFPacket) {
doAckSimpleFail(s, pkt.AckHandle, make([]byte, 4)) doAckSimpleFail(s, pkt.AckHandle, make([]byte, 4))
return return
} }
rand.Seed(time.Now().UnixNano()) team := uint32(token.RNG().Intn(2))
team := uint32(rand.Intn(2))
switch team { switch team {
case 0: case 0:
s.server.db.Exec("INSERT INTO festa_registrations VALUES ($1, 'blue')", guild.ID) s.server.db.Exec("INSERT INTO festa_registrations VALUES ($1, 'blue')", guild.ID)

View File

@@ -7,11 +7,9 @@ import (
"erupe-ce/common/token" "erupe-ce/common/token"
"erupe-ce/server/channelserver" "erupe-ce/server/channelserver"
"fmt" "fmt"
"math/rand" "go.uber.org/zap"
"strings" "strings"
"time" "time"
"go.uber.org/zap"
) )
func makeSignInFailureResp(respID RespID) []byte { func makeSignInFailureResp(respID RespID) []byte {
@@ -29,7 +27,6 @@ func (s *Session) makeSignInResp(uid int) []byte {
s.logger.Warn("Error getting characters from DB", zap.Error(err)) s.logger.Warn("Error getting characters from DB", zap.Error(err))
} }
rand.Seed(time.Now().UnixNano())
sessToken := token.Generate(16) sessToken := token.Generate(16)
s.server.registerToken(uid, sessToken) s.server.registerToken(uid, sessToken)