Skip to content

Commit ab28176

Browse files
authored
Merge pull request #1 from kdssoftware/feat/init
Add LuaObfuscatorCurrent command
2 parents 7ad7933 + 2d47165 commit ab28176

15 files changed

Lines changed: 616 additions & 0 deletions

File tree

.stylua.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
column_width = 100
2+
line_endings = "Unix"
3+
indent_type = "Spaces"
4+
indent_width = 2
5+
quote_style = "AutoPreferSingle"
6+
call_parentheses = "Always"

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# lua-obfuscator.nvim
22
A plugin to use https://luaobfuscator.com/ for Neovim
3+
4+
5+
## Commands
6+
- `LuaObfuscatorCurrent` : obfuscated

doc/nvim-awesome-plugin.txt

Whitespace-only changes.

doc/tags

Whitespace-only changes.

lua/lua-obfuscator.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local M = {}
2+
3+
local api = require('lua-obfuscator.api')
4+
local obf_commands = {LuaObfuscatorCurrent = api.obfuscateScript}
5+
6+
local create_obf_commands = function()
7+
for name, obf_api in pairs(obf_commands) do
8+
vim.api.nvim_create_user_command(name, function(args)
9+
local bufnr = vim.fn.bufnr("%")
10+
local script_content = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
11+
12+
obf_api({
13+
script_content = table.concat(script_content, '\n'),
14+
})
15+
end, {
16+
nargs = '*',
17+
range = true,
18+
complete = function()
19+
local cmdline = vim.fn.getcmdline()
20+
local _, space_count = string.gsub(cmdline, ' ', '')
21+
return space_count == 1 and M.anchor_positions or {}
22+
end
23+
})
24+
end
25+
end
26+
27+
M.setup = function(opts) create_obf_commands() end
28+
29+
return M

lua/lua-obfuscator/api.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
local M = {}
2+
3+
local call = require('lua-obfuscator.call')
4+
5+
---- UTILS ----
6+
local get_range = function(opts)
7+
local range
8+
if opts.range == 2 then range = {start = opts.line1, _end = opts.line2} end
9+
return range
10+
end
11+
12+
local get_opts = function(opts)
13+
opts = opts or {}
14+
opts.range = get_range(opts)
15+
return opts
16+
end
17+
---- END UTILS ----
18+
19+
M.obfuscateScript = function(opts)
20+
local script_content = tostring(opts.script_content)
21+
if script_content == "" then
22+
return vim.api.nvim_err_writeln("No content to obfuscate")
23+
end
24+
25+
local respScriptUpload = call.newScript(script_content)
26+
if respScriptUpload.error and not respScriptUpload.sessionId then
27+
vim.api.nvim_err_writeln("\nError uploading script: " .. tostring(respScriptUpload.error))
28+
return
29+
end
30+
31+
local respObf = call.newObfuscateRequest(respScriptUpload.sessionId)
32+
if respObf.error then
33+
vim.api.nvim_err_writeln("\nError obfuscating: " .. tostring( respObf.error))
34+
return
35+
end
36+
if not respObf.code then
37+
vim.api.nvim_err_writeln("\nError obfuscating: obfuscation failed")
38+
return
39+
end
40+
local obfuscatedCode = respObf.code
41+
local current_bufnr = vim.fn.bufnr()
42+
43+
-- Replace the entire script with obfuscatedCode
44+
vim.api.nvim_buf_set_lines(current_bufnr, 0, -1, true, {obfuscatedCode})
45+
end
46+
47+
return M

lua/lua-obfuscator/call.lua

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)