diff --git a/server/common/enum.go b/server/common/const.go similarity index 64% rename from server/common/enum.go rename to server/common/const.go index 30ab71c4..00a5a551 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]{32}$" ) diff --git a/server/config/config.go b/server/config/config.go index 86422314..f125a5ba 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 fbb00455..1688296d 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 c39b1e2d..28100650 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 = 16 - - 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 { @@ -84,13 +78,13 @@ 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 { - return rerid.Match([]byte(id)) + return rerid.MatchString(id) } type Repository struct { @@ -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 778e5c71..25bf1de1 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" ) @@ -39,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 { @@ -60,6 +63,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 @@ -130,3 +134,17 @@ func TestNewRoomInfo(t *testing.T) { t.Errorf("there were unfulfilled expectations: %s", err) } } + +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(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 320358ce..ce4e82a2 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 {