feat(config): sync make default country configurable
This commit is contained in:
parent
5807fbad76
commit
4cd055144d
10 changed files with 104 additions and 12 deletions
|
|
@ -58,13 +58,3 @@ func BuildConfig(dc int, ip string, port int, now time.Time, publicBaseURL strin
|
|||
config.SetReactionsDefault(&tg.ReactionEmoji{Emoticon: DefaultReactionEmoticon})
|
||||
return config
|
||||
}
|
||||
|
||||
// NearestDC 构造 help.getNearestDc 返回值。
|
||||
func NearestDC(dc int) *tg.NearestDC {
|
||||
return &tg.NearestDC{
|
||||
// 默认国家=中国:DrKLO/TDesktop 登录页据此预选区号(+86)。
|
||||
Country: "CN",
|
||||
ThisDC: dc,
|
||||
NearestDC: dc,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,10 +11,15 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/text/language"
|
||||
|
||||
"telesrv/internal/links"
|
||||
)
|
||||
|
||||
const defaultConfigFile = ".env"
|
||||
const (
|
||||
defaultConfigFile = ".env"
|
||||
defaultCountryCode = "CN"
|
||||
)
|
||||
|
||||
// Config 是 telesrv 的运行配置。
|
||||
type Config struct {
|
||||
|
|
@ -31,6 +36,9 @@ type Config struct {
|
|||
RSAKeyPath string
|
||||
// DC 是本 server 的 DC ID。
|
||||
DC int
|
||||
// DefaultCountryCode 是 help.getNearestDc 返回的 ISO 3166-1 alpha-2 国家码。
|
||||
// 客户端登录页据此预选国家和国际电话区号;例如 CN 对应 +86。
|
||||
DefaultCountryCode string
|
||||
// StrictDCCheck enables the default-off key-exchange DC-label diagnostic.
|
||||
// The normal single-backend mode accepts every wire int32 label without
|
||||
// partitioning auth keys, sessions, or business state. See
|
||||
|
|
@ -461,6 +469,10 @@ func Load() (Config, error) {
|
|||
if err != nil {
|
||||
return Config{}, fmt.Errorf("TELESRV_PUBLIC_APP_NAME: %w", err)
|
||||
}
|
||||
countryCode, err := normalizeDefaultCountryCode(envOr("TELESRV_DEFAULT_COUNTRY_CODE", defaultCountryCode))
|
||||
if err != nil {
|
||||
return Config{}, fmt.Errorf("TELESRV_DEFAULT_COUNTRY_CODE: %w", err)
|
||||
}
|
||||
|
||||
cfg := Config{
|
||||
ListenAddr: envOr("TELESRV_LISTEN", "0.0.0.0:2398"),
|
||||
|
|
@ -475,6 +487,7 @@ func Load() (Config, error) {
|
|||
AdvertiseIP: envOr("TELESRV_ADVERTISE_IP", "127.0.0.1"),
|
||||
RSAKeyPath: envOr("TELESRV_RSA_KEY", "data/server_rsa.pem"),
|
||||
DC: envIntOr("TELESRV_DC", 2),
|
||||
DefaultCountryCode: countryCode,
|
||||
StrictDCCheck: envBoolOr("TELESRV_STRICT_DC_CHECK", false),
|
||||
MTProtoMaxConnections: envIntOr("TELESRV_MTPROTO_MAX_CONNECTIONS", 200000),
|
||||
MTProtoMaxConnectionsPerIP: envIntOr("TELESRV_MTPROTO_MAX_CONNECTIONS_PER_IP", 4096),
|
||||
|
|
@ -690,6 +703,18 @@ func Load() (Config, error) {
|
|||
return cfg, nil
|
||||
}
|
||||
|
||||
func normalizeDefaultCountryCode(raw string) (string, error) {
|
||||
code := strings.ToUpper(strings.TrimSpace(raw))
|
||||
if len(code) != 2 || code[0] < 'A' || code[0] > 'Z' || code[1] < 'A' || code[1] > 'Z' {
|
||||
return "", fmt.Errorf("must be a two-letter ISO 3166-1 alpha-2 code")
|
||||
}
|
||||
region, err := language.ParseRegion(code)
|
||||
if err != nil || !region.IsCountry() {
|
||||
return "", fmt.Errorf("must identify a country or autonomous area")
|
||||
}
|
||||
return region.String(), nil
|
||||
}
|
||||
|
||||
func validateTelegramLoginConfig(cfg Config) error {
|
||||
if !cfg.TelegramLoginEnabled {
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -68,6 +68,47 @@ func TestLoadUsesExplicitAdvertiseIP(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestLoadDefaultCountryCode(t *testing.T) {
|
||||
t.Run("default", func(t *testing.T) {
|
||||
disableDefaultConfigFile(t)
|
||||
t.Setenv("TELESRV_DEFAULT_COUNTRY_CODE", "")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if cfg.DefaultCountryCode != "CN" {
|
||||
t.Fatalf("DefaultCountryCode = %q, want CN", cfg.DefaultCountryCode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("normalized override", func(t *testing.T) {
|
||||
disableDefaultConfigFile(t)
|
||||
t.Setenv("TELESRV_DEFAULT_COUNTRY_CODE", " us ")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if cfg.DefaultCountryCode != "US" {
|
||||
t.Fatalf("DefaultCountryCode = %q, want US", cfg.DefaultCountryCode)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestLoadRejectsInvalidDefaultCountryCode(t *testing.T) {
|
||||
for _, value := range []string{"+86", "CHN", "C1", "中", "ZZ"} {
|
||||
t.Run(value, func(t *testing.T) {
|
||||
disableDefaultConfigFile(t)
|
||||
t.Setenv("TELESRV_DEFAULT_COUNTRY_CODE", value)
|
||||
|
||||
if _, err := Load(); err == nil {
|
||||
t.Fatalf("Load accepted TELESRV_DEFAULT_COUNTRY_CODE=%q", value)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadStrictDCCheck(t *testing.T) {
|
||||
t.Run("defaults off", func(t *testing.T) {
|
||||
disableDefaultConfigFile(t)
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ func (r *Router) registerHelp(d *tlprofile.Dispatcher) {
|
|||
return r.onHelpGetConfig(ctx)
|
||||
})
|
||||
registerRPC[*tg.HelpGetNearestDCRequest](d, tlprofile.SemanticMethodHelpGetNearestDC, func(ctx context.Context, layerRequest *tg.HelpGetNearestDCRequest) (any, error) {
|
||||
return tdesktop.NearestDC(r.cfg.DC), nil
|
||||
return &tg.NearestDC{
|
||||
Country: r.cfg.DefaultCountryCode,
|
||||
ThisDC: r.cfg.DC,
|
||||
NearestDC: r.cfg.DC,
|
||||
}, nil
|
||||
})
|
||||
registerRPC[*tg.HelpGetInviteTextRequest](d, tlprofile.SemanticMethodHelpGetInviteText, func(ctx context.Context, layerRequest *tg.HelpGetInviteTextRequest) (any, error) {
|
||||
return &tg.HelpInviteText{Message: "Join me on " + branding.ProductName + "."}, nil
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ var (
|
|||
// Config 是 Router 所需的服务端信息。
|
||||
type Config struct {
|
||||
DC int
|
||||
DefaultCountryCode string // help.getNearestDc 返回的 ISO 3166-1 alpha-2 国家码。
|
||||
IP string // 对外公布的 DC IP(写入 DCOptions)
|
||||
Port int // 对外公布的 DC 端口
|
||||
InstanceID string // 进程内唯一标识,用于跨实例 ephemeral push 去重。
|
||||
|
|
|
|||
|
|
@ -71,6 +71,31 @@ func TestDispatchUnwrapsWrappers(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDispatchNearestDCUsesConfiguredDefaultCountryCode(t *testing.T) {
|
||||
r := New(Config{
|
||||
DC: 2,
|
||||
DefaultCountryCode: "US",
|
||||
IP: "127.0.0.1",
|
||||
Port: 2398,
|
||||
}, Deps{}, zaptest.NewLogger(t), clock.System)
|
||||
|
||||
var b bin.Buffer
|
||||
if err := (&tg.HelpGetNearestDCRequest{}).Encode(&b); err != nil {
|
||||
t.Fatalf("encode help.getNearestDc: %v", err)
|
||||
}
|
||||
enc, err := r.Dispatch(context.Background(), [8]byte{}, 0, &b)
|
||||
if err != nil {
|
||||
t.Fatalf("dispatch help.getNearestDc: %v", err)
|
||||
}
|
||||
nearest, ok := enc.(*tg.NearestDC)
|
||||
if !ok {
|
||||
t.Fatalf("result type = %T, want *tg.NearestDC", enc)
|
||||
}
|
||||
if nearest.Country != "US" || nearest.ThisDC != 2 || nearest.NearestDC != 2 {
|
||||
t.Fatalf("nearestDc = %+v", nearest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatchRejectsAuthorizedRPCBeforeLogin(t *testing.T) {
|
||||
r := New(Config{DC: 2, IP: "127.0.0.1", Port: 2398}, Deps{
|
||||
Auth: &captureAuthService{},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue