From af3d334c261fd31e17bcfa7b22c92758e56de4a4 Mon Sep 17 00:00:00 2001 From: Daisuke MAKIUCHI Date: Wed, 3 Apr 2024 20:13:01 +0900 Subject: [PATCH 1/4] Fixed game.RandomHex to return a string of correct length --- server/game/repository.go | 4 ++-- server/game/repository_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/server/game/repository.go b/server/game/repository.go index c39b1e2..3c5eaa7 100644 --- a/server/game/repository.go +++ b/server/game/repository.go @@ -84,9 +84,9 @@ func initQueries() { } func RandomHex(n int) string { - b := make([]byte, n) + b := make([]byte, (n+1)/2) _, _ = randsrc.Read(b) // (*rand.Rand).Read always success. - return hex.EncodeToString(b) + return hex.EncodeToString(b)[:n] } func IsValidRoomId(id string) bool { diff --git a/server/game/repository_test.go b/server/game/repository_test.go index 778e5c7..112e4f9 100644 --- a/server/game/repository_test.go +++ b/server/game/repository_test.go @@ -130,3 +130,16 @@ func TestNewRoomInfo(t *testing.T) { t.Errorf("there were unfulfilled expectations: %s", err) } } + +func TestRandomHexRoomId(t *testing.T) { + rid := RandomHex(lenId) + + if len(rid) != lenId { + t.Errorf("room id len = %v wants %v (%q)", len(rid), lenId, rid) + } + + ok, err := regexp.MatchString(idPattern, rid) + if err != nil || !ok { + t.Errorf("room id pattern missmatch: %v", rid) + } +} From dbc54c58bd5c24a2e5540d078287637d7c5c31bb Mon Sep 17 00:00:00 2001 From: Daisuke MAKIUCHI Date: Wed, 3 Apr 2024 20:25:59 +0900 Subject: [PATCH 2/4] Double the RandomHex() parameter --- server/config/config.go | 4 ++-- server/config/config_test.go | 2 +- server/game/repository.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/server/config/config.go b/server/config/config.go index 8642231..f125a5b 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -187,7 +187,7 @@ func Load(conffile string) (*Config, error) { ClientConf: ClientConf{ EventBufSize: 128, WaitAfterClose: Duration(30 * time.Second), - AuthKeyLen: 32, + AuthKeyLen: 64, }, LogConf: LogConf{ @@ -217,7 +217,7 @@ func Load(conffile string) (*Config, error) { ClientConf: ClientConf{ EventBufSize: 128, WaitAfterClose: Duration(30 * time.Second), - AuthKeyLen: 32, + AuthKeyLen: 64, }, LogConf: LogConf{ diff --git a/server/config/config_test.go b/server/config/config_test.go index fbb0045..1688296 100644 --- a/server/config/config_test.go +++ b/server/config/config_test.go @@ -48,7 +48,7 @@ func TestLoad(t *testing.T) { ClientConf: ClientConf{ EventBufSize: 512, WaitAfterClose: Duration(time.Second * 60), - AuthKeyLen: 32, + AuthKeyLen: 64, }, LogConf: LogConf{ diff --git a/server/game/repository.go b/server/game/repository.go index 3c5eaa7..2b3e535 100644 --- a/server/game/repository.go +++ b/server/game/repository.go @@ -26,7 +26,7 @@ import ( const ( // RoomID文字列長 - lenId = 16 + lenId = 32 idPattern = "^[0-9a-f]+$" ) From ec459c8597ff0f32168e532c3cf072ac301ec6c5 Mon Sep 17 00:00:00 2001 From: Daisuke MAKIUCHI Date: Wed, 3 Apr 2024 20:57:54 +0900 Subject: [PATCH 3/4] refactoring constants --- server/common/{enum.go => const.go} | 3 +++ server/game/repository.go | 12 +++--------- server/game/repository_test.go | 5 ++++- server/lobby/service/api.go | 3 ++- 4 files changed, 12 insertions(+), 11 deletions(-) rename server/common/{enum.go => const.go} (65%) diff --git a/server/common/enum.go b/server/common/const.go similarity index 65% rename from server/common/enum.go rename to server/common/const.go index 30ab71c..19921b7 100644 --- a/server/common/enum.go +++ b/server/common/const.go @@ -4,4 +4,7 @@ const ( HostStatusStarting = 0 HostStatusRunning = 1 HostStatusClosing = 2 + + RoomIdLen = 32 + RoomIdPattern = "^[0-9a-f]+$" ) diff --git a/server/game/repository.go b/server/game/repository.go index 2b3e535..f1c4b80 100644 --- a/server/game/repository.go +++ b/server/game/repository.go @@ -19,18 +19,12 @@ import ( "golang.org/x/xerrors" "google.golang.org/grpc/codes" + "wsnet2/common" "wsnet2/config" "wsnet2/log" "wsnet2/pb" ) -const ( - // RoomID文字列長 - lenId = 32 - - idPattern = "^[0-9a-f]+$" -) - var ( roomInsertQuery string roomUpdateQuery string @@ -46,7 +40,7 @@ func init() { seed, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64)) randsrc = rand.New(rand.NewSource(seed.Int64())) - rerid = regexp.MustCompile(idPattern) + rerid = regexp.MustCompile(common.RoomIdPattern) } func dbCols(t reflect.Type) []string { @@ -309,7 +303,7 @@ func (repo *Repository) newRoomInfo(ctx context.Context, tx *sqlx.Tx, op *pb.Roo default: } - ri.Id = RandomHex(lenId) + ri.Id = RandomHex(common.RoomIdLen) if op.WithNumber { ri.Number.Number = randsrc.Int31n(maxNumber) + 1 // [1..maxNumber] } diff --git a/server/game/repository_test.go b/server/game/repository_test.go index 112e4f9..b657a84 100644 --- a/server/game/repository_test.go +++ b/server/game/repository_test.go @@ -11,6 +11,7 @@ import ( "github.com/jmoiron/sqlx" "golang.org/x/xerrors" + "wsnet2/common" "wsnet2/config" "wsnet2/pb" ) @@ -60,6 +61,7 @@ func newDbMock(t *testing.T) (*sqlx.DB, sqlmock.Sqlmock) { } func TestNewRoomInfo(t *testing.T) { + const lenId = common.RoomIdLen ctx := context.Background() db, mock := newDbMock(t) retryCount := 3 @@ -132,13 +134,14 @@ func TestNewRoomInfo(t *testing.T) { } func TestRandomHexRoomId(t *testing.T) { + const lenId = common.RoomIdLen rid := RandomHex(lenId) if len(rid) != lenId { t.Errorf("room id len = %v wants %v (%q)", len(rid), lenId, rid) } - ok, err := regexp.MatchString(idPattern, rid) + ok, err := regexp.MatchString(common.RoomIdPattern, rid) if err != nil || !ok { t.Errorf("room id pattern missmatch: %v", rid) } diff --git a/server/lobby/service/api.go b/server/lobby/service/api.go index 320358c..ce4e82a 100644 --- a/server/lobby/service/api.go +++ b/server/lobby/service/api.go @@ -18,6 +18,7 @@ import ( "golang.org/x/xerrors" "wsnet2/auth" + "wsnet2/common" "wsnet2/lobby" "wsnet2/log" "wsnet2/pb" @@ -248,7 +249,7 @@ func (sv *LobbyService) handleCreateRoom(w http.ResponseWriter, r *http.Request) } var ( - idRegexp = regexp.MustCompile("^[0-9a-f]+$") + idRegexp = regexp.MustCompile(common.RoomIdPattern) ) type JoinVars struct { From 1ba50d79ffcf85f8cb86c5658c11bed90a5f3f80 Mon Sep 17 00:00:00 2001 From: Daisuke MAKIUCHI Date: Wed, 3 Apr 2024 21:06:56 +0900 Subject: [PATCH 4/4] Make the RoomIdPattern strict --- server/common/const.go | 2 +- server/game/repository.go | 2 +- server/game/repository_test.go | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/server/common/const.go b/server/common/const.go index 19921b7..00a5a55 100644 --- a/server/common/const.go +++ b/server/common/const.go @@ -6,5 +6,5 @@ const ( HostStatusClosing = 2 RoomIdLen = 32 - RoomIdPattern = "^[0-9a-f]+$" + RoomIdPattern = "^[0-9a-f]{32}$" ) diff --git a/server/game/repository.go b/server/game/repository.go index f1c4b80..2810065 100644 --- a/server/game/repository.go +++ b/server/game/repository.go @@ -84,7 +84,7 @@ func RandomHex(n int) string { } func IsValidRoomId(id string) bool { - return rerid.Match([]byte(id)) + return rerid.MatchString(id) } type Repository struct { diff --git a/server/game/repository_test.go b/server/game/repository_test.go index b657a84..25bf1de 100644 --- a/server/game/repository_test.go +++ b/server/game/repository_test.go @@ -40,9 +40,11 @@ func TestQueries(t *testing.T) { func TestIsValidRoomId(t *testing.T) { tests := map[string]bool{ - "123456789abcdef": true, - "123456789ABCDEF": false, - "": false, + "0123456789abcdef0123456789abcdef": true, + "0123456789ABCDEF0123456789ABCDEF": false, + "0123456789abcdef0123456789abcde": false, + "0123456789abcdef0123456789abcdef0": false, + "": false, } for id, valid := range tests {