From 994b8aa477a369023b1aedadce3af374ae824d82 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Mon, 16 Oct 2023 17:14:35 +0800 Subject: [PATCH] Feat: room must pwd config --- internal/conf/config.go | 6 ++++++ internal/conf/room.go | 11 +++++++++++ server/handlers/public.go | 3 +++ server/model/room.go | 15 ++++++++++++--- server/model/user.go | 2 +- 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 internal/conf/room.go diff --git a/internal/conf/config.go b/internal/conf/config.go index f433a63..03a6354 100644 --- a/internal/conf/config.go +++ b/internal/conf/config.go @@ -21,6 +21,9 @@ type Config struct { // Proxy Proxy ProxyConfig `yaml:"proxy" hc:"you can use proxy to proxy movie and live when custom headers or network is slow to connect to origin server"` + + // Room + Room RoomConfig `yaml:"room"` } func (c *Config) Save(file string) error { @@ -48,5 +51,8 @@ func DefaultConfig() *Config { // Proxy Proxy: DefaultProxyConfig(), + + // Room + Room: DefaultRoomConfig(), } } diff --git a/internal/conf/room.go b/internal/conf/room.go new file mode 100644 index 0000000..8ceeaaa --- /dev/null +++ b/internal/conf/room.go @@ -0,0 +1,11 @@ +package conf + +type RoomConfig struct { + MustPassword bool `yaml:"must_password" lc:"must input password to create room (default: false)" env:"ROOM_MUST_PASSWORD"` +} + +func DefaultRoomConfig() RoomConfig { + return RoomConfig{ + MustPassword: false, + } +} diff --git a/server/handlers/public.go b/server/handlers/public.go index 1e71f8f..f23896f 100644 --- a/server/handlers/public.go +++ b/server/handlers/public.go @@ -17,5 +17,8 @@ func Settings(ctx *gin.Context) { "movieProxy": conf.Conf.Proxy.MovieProxy, "liveProxy": conf.Conf.Proxy.LiveProxy, }, + "room": gin.H{ + "mustPassword": conf.Conf.Room.MustPassword, + }, })) } diff --git a/server/model/room.go b/server/model/room.go index 8198afc..dabab47 100644 --- a/server/model/room.go +++ b/server/model/room.go @@ -2,9 +2,11 @@ package model import ( "errors" + "fmt" "regexp" json "github.com/json-iterator/go" + "github.com/synctv-org/synctv/internal/conf" "github.com/gin-gonic/gin" ) @@ -14,7 +16,6 @@ var ( ErrRoomIdTooLong = errors.New("room id too long") ErrRoomIdHasInvalidChar = errors.New("room id has invalid char") - ErrEmptyPassword = errors.New("empty password") ErrPasswordTooLong = errors.New("password too long") ErrPasswordHasInvalidChar = errors.New("password has invalid char") @@ -28,6 +29,12 @@ var ( alphaNumChineseReg = regexp.MustCompile(`^[\p{Han}a-zA-Z0-9_\-]+$`) ) +type FormatEmptyPasswordError string + +func (f FormatEmptyPasswordError) Error() string { + return fmt.Sprintf("%s password empty", string(f)) +} + type CreateRoomReq struct { RoomId string `json:"roomId"` Password string `json:"password"` @@ -55,6 +62,8 @@ func (c *CreateRoomReq) Validate() error { } else if !alphaNumReg.MatchString(c.Password) { return ErrPasswordHasInvalidChar } + } else if conf.Conf.Room.MustPassword { + return FormatEmptyPasswordError("room") } if c.Username == "" { @@ -66,7 +75,7 @@ func (c *CreateRoomReq) Validate() error { } if c.UserPassword == "" { - return ErrEmptyPassword + return FormatEmptyPasswordError("user") } else if len(c.UserPassword) > 32 { return ErrPasswordTooLong } else if !alphaNumReg.MatchString(c.UserPassword) { @@ -105,7 +114,7 @@ func (l *LoginRoomReq) Validate() error { } if l.UserPassword == "" { - return ErrEmptyPassword + return FormatEmptyPasswordError("user") } return nil diff --git a/server/model/user.go b/server/model/user.go index ae7a818..6764a22 100644 --- a/server/model/user.go +++ b/server/model/user.go @@ -15,7 +15,7 @@ func (s *SetUserPasswordReq) Decode(ctx *gin.Context) error { func (s *SetUserPasswordReq) Validate() error { if s.Password == "" { - return ErrEmptyPassword + return FormatEmptyPasswordError("user") } else if len(s.Password) > 32 { return ErrPasswordTooLong } else if !alphaNumReg.MatchString(s.Password) {