|
| 1 | +local M = {} |
| 2 | +local json = require("lua-obfuscator.json") |
| 3 | +local utils = require("lua-obfuscator.utils") |
| 4 | + |
| 5 | +-- TODO port to utils |
| 6 | + |
| 7 | +local settings = { |
| 8 | + minifyAll = true, |
| 9 | + virtualize = false, |
| 10 | + customPlugins = { |
| 11 | + SwizzleLookups = 100, |
| 12 | + EncryptStrings = 100, |
| 13 | + RevertAllIfStatements = 50, |
| 14 | + ControlFlowFlattenAllBlocks = 75 |
| 15 | + } |
| 16 | +} |
| 17 | +local settingsData = { |
| 18 | + MinifiyAll = settings.minifyAll, |
| 19 | + Virtualize = settings.virtualize, |
| 20 | + CustomPlugins = { |
| 21 | + SwizzleLookups = {settings.customPlugins.SwizzleLookups}, |
| 22 | + EncryptStrings = {settings.customPlugins.EncryptStrings}, |
| 23 | + RevertAllIfStatements = {settings.customPlugins.RevertAllIfStatements}, |
| 24 | + ControlFlowFlattenAllBlocks = { |
| 25 | + settings.customPlugins.ControlFlowFlattenAllBlocks |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +-- @param script string: Lua script to be obfuscated |
| 31 | +-- @return table: Returns a table with session ID and error message, both of which can be nil or string. |
| 32 | +M.newScript = function(script) |
| 33 | + local endpoint = "https://luaobfuscator.com/api/obfuscator/newscript" |
| 34 | + local headers = "-H 'Content-type: text/plain' -H 'apikey:test'" |
| 35 | + |
| 36 | + local requestBody = json.encode({script = script}) |
| 37 | + |
| 38 | + local command = string.format('curl -X POST %s -s %s --data-raw %q', |
| 39 | + requestBody, endpoint, headers) |
| 40 | + local result = vim.fn.system(command) |
| 41 | + |
| 42 | + if vim.v.shell_error ~= 0 then |
| 43 | + return {code = nil, error = "Error executing the command."} |
| 44 | + end |
| 45 | + |
| 46 | + local response = json.decode(result) |
| 47 | + if not response then |
| 48 | + return {sessionId = nil, error = "Error decoding Lua response."} |
| 49 | + end |
| 50 | + |
| 51 | + if response and response.sessionId then |
| 52 | + local sessionId = tostring(response.sessionId) |
| 53 | + return {sessionId = sessionId, error = nil} |
| 54 | + else |
| 55 | + return {sessionId = nil, error = "Error decoding Lua response."} |
| 56 | + end |
| 57 | +end |
| 58 | + |
| 59 | +-- @param sessionId string: The session ID obtained from script upload. |
| 60 | +-- @return table: Returns a table with obfuscated code and error message, both of which can be nil or string. |
| 61 | +M.newObfuscateRequest = function(sessionId) |
| 62 | + local endpoint = "https://luaobfuscator.com/api/obfuscator/obfuscate/" |
| 63 | + |
| 64 | + local headers = |
| 65 | + "-H 'Content-type: text/plain' -H 'apikey: test' -H 'sessionId: " .. |
| 66 | + sessionId .. "'" |
| 67 | + |
| 68 | + local jsonData = |
| 69 | + '{CustomPlugins:{ControlFlowFlattenAllBlocks:[75],SwizzleLookups:[100],EncryptStrings:[100],RevertAllIfStatements:[50]},MinifiyAll:true,Virtualize:false}' |
| 70 | + |
| 71 | + local command = string.format('curl -X POST %s -s %s --data-raw %q', |
| 72 | + endpoint, headers, json.decode(settingsData)) |
| 73 | + local result = vim.fn.system(command) |
| 74 | + |
| 75 | + if vim.v.shell_error ~= 0 then |
| 76 | + return {code = nil, error = "Error executing the command."} |
| 77 | + end |
| 78 | + |
| 79 | + local response = json.decode(result) |
| 80 | + if response.title == "Unauthorized" then |
| 81 | + return { |
| 82 | + code = nil, |
| 83 | + error = "Looks like the server is down: \n" .. tostring(result) |
| 84 | + } |
| 85 | + end |
| 86 | + |
| 87 | + if string.find(result, '"code":null', 1, true) then -- could try to use response.code |
| 88 | + return {code = nil, error = "failed to obfuscate: " .. tostring(result)} |
| 89 | + end |
| 90 | + |
| 91 | + -- Interpret the Lua code in the response |
| 92 | + if response and type(response) == "table" and response.code then |
| 93 | + return { |
| 94 | + code = response.code, |
| 95 | + error = nil |
| 96 | + } |
| 97 | + else |
| 98 | + return { |
| 99 | + code = nil, |
| 100 | + error = "Error decoding Lua response or missing 'code' field." |
| 101 | + } |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | +return M |
0 commit comments