feat(config): sync make default country configurable

This commit is contained in:
iamxvbaba 2026-07-27 20:54:04 +08:00
parent 5807fbad76
commit 4cd055144d
10 changed files with 104 additions and 12 deletions

View file

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

View file

@ -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 去重。

View file

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