# 插件使用
終于有時間更新插件使用相關的文章了,這里首先給大家介紹一個**內容替換模塊**中使用的手機號脫敏插件
# 插件編寫
1. 先看這個手機號脫敏插件的源代碼
```
local sub = string.sub
local gsub = ngx.re.gsub
local gmatch = ngx.re.gmatch
local re = "(13[0-9]|14[5,7]|15[0-3,5-9]|17[0,3,5-8]|18[0-9]|166|198|199|147)\\d{8}"
local TB = {}
local tag = false
local function Teltoasterisk(_tel)
-- 中間 4 位
return sub(_tel,1,4).."****"..sub(_tel,9)
end
local function insert_Tb(Stel)
if not TB[Stel] then
TB[Stel] = Teltoasterisk(Stel)
end
end
local function Telgmatch(tel_str)
local it, err = gmatch(tel_str, re, "jo")
if not it then
return
end
while true do
local m,err = it()
if err then
return
end
if not m then
return
end
tag = true
insert_Tb(m[0])
end
end
local _M = { _VERSION = "0.01" }
local function tel_ast(str_all , _tb_args)
Telgmatch(str_all)
if tag then
for k,v in pairs(TB) do
str_all = gsub(str_all,k,v)
end
end
return str_all
end
_M.tel_ast = tel_ast
return _M
```
代碼編寫的非常好理解,當然了還有寫法不規范的地方,如果說您可以調用C代碼編寫的模塊,其工作的效率應該是最高的。(希望有寫好C模塊的同學可以分享一下so文件或者源代碼)
2. 性能測試測試
用于性能測試的代碼如下
```
local optl = require("optl")
local str = [=[xxxxx]=]
local re = "(13[0-9]|14[5,7]|15[0-3,5-9]|17[0,3,5-8]|18[0-9]|166|198|199|147)\\d{8}"
local TB = {}
local tag = false
local function Teltoasterisk(_tel)
-- 中間 4 位
return string.sub(_tel,1,4).."****"..string.sub(_tel,9)
end
local function insert_Tb(Stel)
if not TB[Stel] then
TB[Stel] = Teltoasterisk(Stel)
end
end
local function Telgmatch(tel_str)
local it, err = ngx.re.gmatch(tel_str, re, "jo")
if not it then
return
end
while true do
local m,err = it()
if err then
return
end
if not m then
return
end
tag = true
insert_Tb(m[0])
end
end
local function Telngxfind(tel_str)
local f,t = ngx.re.find(tel_str,re,"jo")
if f then
tag = true
local tmptel = string.sub(tel_str,f,t)
insert_Tb(string.sub(tel_str,f,t))
local tmp_str = string.sub(tel_str,t)
Telngxfind(tmp_str)
end
end
local function Telmatch(tel_str)
local m,e = ngx.re.match(tel_str,re)
if m then
tag = true
insert_Tb(m[0])
local f,t = string.find(tel_str,m[0],1,true)
local tmp_str = string.sub(tel_str, t)
Telmatch(tmp_str)
end
end
local startTime = os.clock()
local num = tonumber(optl.get_paramByName("num")) or 10000
local fuc = optl.get_paramByName("fuc")
local T_fuc = {}
T_fuc.ngxfind = Telngxfind
T_fuc.match = Telmatch
T_fuc.gmatch = Telgmatch
f = T_fuc[fuc] or Telngxfind
for i=1,num do
f(str)
end
if tag then
for k,v in pairs(TB) do
str = ngx.re.gsub(str,k,v)
end
end
local msg = string.format(">> This function cost: %.4f ms", os.clock()-startTime)
optl.sayHtml_ext({code="ok",msg=msg,len=#str,tb=TB,tag=tag})
```
為了和真實的環境接近,找了一篇最佳實踐的頁面,在內容里面添加了一些手機號碼,其分布跨度也是比較大的。
該頁面內容字符串長度:70964

大家也可以對比一下,使用這個 ngx.re.gmatch 效率最高,當然了如果有C模塊的更好了。
完整代碼 分享在[百度網盤 ](https://pan.baidu.com/s/1zIiRhw2zTAA5AMcc6XbF-A)
# replace_Mod --- 插件編寫
閱讀到這里就想著怎么編寫一個自己的插件,那就接著往下看吧。
先看一個最簡單的 內容替換插件
```
local _M = { _VERSION = "0.01" }
local function replace_hello(str_all , _tb_args)
-- 這里可以使用函數完成你想要的替換結果
return ngx.re.gsub(str_all, "hello", "HELLO")
end
_M.replace_hello = replace_hello
return _M
```
首先需要返回一個table,讓后給這個 table 添加一個方法。
其次這個方法需要接收 2 個參數
str_all :這個是需要替換的內容(也就是 整個 Response ,該內容是 openstar 獲取傳遞過來的)
_tb_args:調用參數(使得插件的使用更加靈活,ps 傳遞的就是一個 table ,這里不需要序列化的,減少性能的損耗)
接下來就是編寫你想要的邏輯和內容了,這里和普通編寫lua腳本的一樣的,可以調用其他人的lua模塊,還有調用c代碼等等...,當然不要把自己堵塞了!!!
- kcon 兵器譜
- 演示 1
- 演示 2
- 演示 3
- 演示 4
- 演示 5
- 前言
- 安裝
- 更新
- 登錄后臺
- 授權認證
- 集群配置
- 7層防護 -- 最佳實踐
- 匹配位置說明
- 匹配方式說明
- 規則匹配詳解
- 全局 - CDN規則
- 添加header頭配置
- 限速limit配置
- 緩存proxy_cache配置
- 清除緩存
- 全局 - 獲取真實IP配置
- 全局 - IP黑白名單
- 全局 - 域名方法配置(白名單)
- 全局 - 跳轉規則配置
- 全局 - 高級規則配置
- 全局 - 普通規則配置
- 全局 - 頻率規則配置
- 全局 - 內容替換規則
- 內容替換規則(插件使用)
- 全局 - 攔截信息配置
- 全局 - LOG規則配置
- 平臺配置
- 基本配置
- 高級配置
- 配置文件管理
- 4 層代理
- 轉發配置
- 插件管理
- 防護配置
- 網站管理
- 證書管理
- 域名管理
- 網站規則
- 插件管理
- 插件操作 --- 基本使用
- 插件操作 --- 手機號脫敏插件
- 歸檔
- 更新日志
- 視頻教程目錄