Feat: room must pwd config

pull/21/head
zijiren233 1 year ago
parent 584ed7510d
commit 994b8aa477

@ -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(),
}
}

@ -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,
}
}

@ -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,
},
}))
}

@ -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

@ -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) {

Loading…
Cancel
Save