KananGamer

[TFM-LUA] Man In The Middle

Jan 5th, 2021 (edited)
1,260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | None | 0 0
  1. do
  2.     local _MITM = {}
  3.  
  4.     local split = function(t, s)
  5.         local a={}
  6.         for i in string.gmatch(t, "[^" .. (s or "%s") .. "]+") do
  7.             a[#a + 1] = i
  8.         end
  9.         return a
  10.     end
  11.  
  12.     setmetatable(_G, {
  13.         __index = function(self, index)
  14.             if _MITM[index] then
  15.                 return function(...)
  16.                     for i, v in next, {"before", "default", "after"} do
  17.                         if _MITM[index][v] then
  18.                             if v == "default" then
  19.                                 _MITM[index][v](...)
  20.                             else
  21.                                 for o, a in next, _MITM[index][v] do
  22.                                     a(...)
  23.                                 end
  24.                             end
  25.                         end
  26.                     end
  27.                 end
  28.             else
  29.                 rawget(self, index)
  30.             end
  31.         end,
  32.         __newindex = function(self, index, value)
  33.             if type(value) == "function" then
  34.                 if not _MITM[index] then
  35.                     _MITM[index] = {}
  36.                 end
  37.  
  38.                 _MITM[index].default = value
  39.             else
  40.                 rawset(self, index, value)
  41.             end
  42.         end,
  43.         __metatable = nil
  44.     });
  45.  
  46.     function injectFunction(positionType, objectToProxy, objectToReplace)
  47.         if type(objectToProxy) == "string" then
  48.             local arg = split(objectToProxy, '.')
  49.  
  50.             if #arg > 1 then
  51.                 local tb = _G
  52.  
  53.                 for i = 1, #arg do
  54.                     if i == #arg then
  55.                         local old = tb[arg[i]]
  56.                         tb[arg[i]] = {}
  57.  
  58.                         if not _MITM[objectToProxy] then
  59.                             _MITM[objectToProxy] = {}
  60.                         end
  61.  
  62.                         _MITM[objectToProxy][positionType] = objectToReplace
  63.  
  64.                         if not getmetatable(tb[arg[i]]) then
  65.                             setmetatable(tb[arg[i]], {
  66.                                 __call = function(self, ...)
  67.                                     for o, a in next, {"before", "default", "after"} do
  68.                                         if a == "default" and positionType ~= "default" then
  69.                                             old(...)
  70.                                         elseif a == positionType then
  71.                                             _MITM[objectToProxy][positionType](old, ...)
  72.                                         end
  73.                                     end
  74.                                 end
  75.                             });
  76.                         end
  77.                     else
  78.                         tb = tb[arg[i]]
  79.                     end
  80.                 end
  81.             elseif arg[1] then
  82.                 if not _MITM[arg[1]] then
  83.                     _MITM[arg[1]] = {}
  84.                 end
  85.  
  86.                 if positionType ~= "default" and not _MITM[arg[1]][positionType] then
  87.                     _MITM[arg[1]][positionType] = {}
  88.                 end
  89.  
  90.                 if positionType == "default" then
  91.                     _MITM[arg[1]][positionType] = objectToReplace
  92.                 else
  93.                     _MITM[arg[1]][positionType][#_MITM[arg[1]][positionType] + 1] = objectToReplace
  94.                 end
  95.             end
  96.         end
  97.     end
  98. end
  99.  
  100. -- Example
  101.  
  102. function eventChatMessage(name, message)
  103.     print(message)
  104. end
  105.  
  106. injectFunction("after", "eventChatMessage", function()
  107.     print("after!")
  108. end)
  109.  
  110. injectFunction("default", "eventChatMessage", function()
  111.     print("default event removed")
  112. end)
Add Comment
Please, Sign In to add comment