IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    ejabberd为游戏免除注册限制

    金庆发表于 2016-11-03 04:11:00
    love 0
    20161103_ejabberd为游戏免除注册限制

    (金庆的专栏 2016.11)

    ejabberd聊天服务器默认会限制同一IP注册帐号须间隔600s。
    在游戏中需要为每个角色注册一个聊天帐号,不应该有此限制。
    可以更改服务器代码,为游戏服务器免除这一注册间隔时间。

    假设游戏服用专用的帐号登录ejabberd, 然后为这种帐号免除注册限制。

    在ejabberd.yml配置访问控制列表(ACL)中添加 game_master:

        acl:
          game_master:
            user:
              - "game_master_1@localhost"
              - "game_master_2@localhost"

    game_master 帐号预先创建,供游戏服务器登录ejabberd.

    mod_register的配置中,将 "access_from: deny" 改为 "access_from: all".
    "access_from: deny" 表示任何用户都不能注册帐号,只能是登录前注册。
    "access_from: all" 表示登录用户也能注册帐号。
    这样game_master帐号先登录,然后就可以注册新帐号了。

    添加 access_from_without_time_limit,允许game_master无注册限制。

        mod_register:
            access_from: all
            access: register
        
            ## Allow some user register accounts without registration_timeout limit.
            access_from_without_time_limit:
              - allow: game_master

    实际上也可以这样直接配置:  

        access_from_without_time_limit:
          - allow:
            - user: game_master_1@localhost
            - user: game_master_2@localhost

    配置成 acl 的好处是,可以用

        ejabberdctl reload_config

    重新加载 acl, 而模块配置部分无法重新加载。

    acl 配置还可以在 http admin 界面更改。

    代码需稍加更改,修改mod_register.erl.

    try_register/5 添加 From 参数改为 try_register/6

        try_register(User, Server, Password, From, SourceRaw, Lang)

    并将其中

        case check_timeout(Source) of

    改成

        CheckTimeout = case check_from_without_time_limit(From, Server) of
            allow -> true;
            _ -> check_timeout(Source)
        end,
        case CheckTimeout of

    即如果From是gm帐号时,CheckTimeout直接通过,不再判断600s的间隔。

    check_from_without_time_limit/2 仿照check_from/2,这样实现:

        check_from_without_time_limit(JID, Server) ->
            Access = gen_mod:get_module_opt(Server, ?MODULE, access_from_without_time_limit,
                                            fun(A) -> A end,
                                            none),
            acl:match_rule(Server, Access, JID).
        
    添加:

        mod_opt_type(access_from_without_time_limit) ->
            fun acl:access_rules_validator/1;
        ...
        mod_opt_type(_) ->
            [..., access_from_without_time_limit, ...].
       


    金庆 2016-11-03 12:11 发表评论


沪ICP备19023445号-2号
友情链接