30

如何扩展一个OpenResty模块

 4 years ago
source link: https://blog.huoding.com/2020/02/19/801
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

因为 Lua 本身并没有继承之类的语法,所以我们不能通过 OOP 的套路来扩展模块,不过实际上对于 Lua 来说,扩展一个模块有更简单的方法,下面我们以 lua-resty-string 模块中的 aes 加解密功能为例子来说明一下。

在 aes 加解密的过程中,有一个「填充」的过程,相关技术细节可以参考我以前写的「聊聊AES」,当然,不懂也没关系,你只要知道目前的 resty.aes 不支持配置填充的功能即可,因为 OpenSSL 缺省是激活了填充的,所以一旦我们需要自定义填充方法,那么就需要关闭缺省的填充行为,此时 resty.aes 无能为力。

通过查看 resty.aes 源代码,我们知道它是通过 ffi 调用 OpenSSL 来实现相关功能的,所以我们只要照猫画虎扩展 resty.aes 即可,不过不要修改 resty.aes 源代码,否则日后的升级会变得麻烦,最好是新建一个模块,比如本例中的 resty.aes_with_padding:

local aes = require "resty.aes"
local ffi = require "ffi"

local C = ffi.C

ffi.cdef[[
int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int padding);
]]

function aes.set_padding(self, padding)
    local encrypt_ctx, decrypt_ctx = self._encrypt_ctx, self._decrypt_ctx

    if encrypt_ctx == nil or decrypt_ctx == nil then
        return nil, "not initialized"
    end

    C.EVP_CIPHER_CTX_set_padding(encrypt_ctx, padding)
    C.EVP_CIPHER_CTX_set_padding(decrypt_ctx, padding)

    return 1
end

return aes

实际使用的时候,把原本调用 resty.aes 的地方改成 resty.aes_with_padding,然后代码里通过调用新创建的 set_padding 方法来控制开启还是关闭填充。

怎么样,扩展一个OpenResty模块和把大象放冰箱一样简单吧,总共分三步:1)创建一个新模块;2)引入要扩展的原模块;3)直接给原模块附加新方法。

搞定!

ieey22E.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK