Skip to content

Commit

Permalink
luackrocks: install lua socket tests: decoder functions:transform
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAppSec committed Sep 6, 2024
1 parent 10bc706 commit c803316
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 124 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A few Snippets to help hacking
Luarocks is required(luasocket mime for encoding decoding):
```bash
sudo apt install luarocks -y
sudo luarocks install luasocket
```
## Using it

Expand Down
198 changes: 77 additions & 121 deletions lua/hacker-helper.lua
Original file line number Diff line number Diff line change
@@ -1,71 +1,15 @@
-- main module file
local module = require("hacker-helper.module")
-- Add LuaRocks paths dynamically
local function add_luarocks_path()
local handle = io.popen("luarocks path --lr-path")
local luarocks_path = handle:read("*a")
handle:close()

local handle_cpath = io.popen("luarocks path --lr-cpath")
local luarocks_cpath = handle_cpath:read("*a")
handle_cpath:close()

-- Add LuaRocks paths
if not string.find(package.path, luarocks_path, 1, true) then
package.path = package.path .. ";" .. luarocks_path
end

if not string.find(package.cpath, luarocks_cpath, 1, true) then
package.cpath = package.cpath .. ";" .. luarocks_cpath
end
end

-- Function to check if LuaRocks is installed
local function is_luarocks_installed()
local result = os.execute("luarocks --version > /dev/null 2>&1")
return result == 0
end

-- Function to check and install luasocket if missing
local function ensure_luasocket_installed()
-- Notify the user if LuaSocket is not installed
local function check_luasocket_installed()
local ok, mime = pcall(require, "mime")
if not ok then
-- Check if LuaRocks is installed
if not is_luarocks_installed() then
-- LuaRocks is not installed, notify the user
vim.notify(
"Hacker Helper Error: LuaRocks is not installed. Please install LuaRocks to use this feature.",
vim.log.levels.ERROR
)
vim.notify("Hacker Helper Error: sudo apt install luarocks", vim.log.levels.ERROR)
return false
end

-- Notify the user that we're installing LuaSocket
vim.notify("LuaSocket not found. Installing LuaSocket (luasocket) via LuaRocks...", vim.log.levels.INFO)

-- Run the LuaRocks install command
local result = os.execute("luarocks install luasocket")
if result == 0 then
-- Installation succeeded
vim.notify("LuaSocket successfully installed!", vim.log.levels.INFO)

-- Add LuaRocks paths dynamically
add_luarocks_path()

-- Reload mime module
local mime_ok, mime_new = pcall(require, "mime")
if not mime_ok then
vim.notify("Error: LuaSocket installed but mime module still not found.", vim.log.levels.ERROR)
return false
else
mime = mime_new
end
else
-- Installation failed, notify the user
vim.notify("Error: Failed to install LuaSocket. Please install it manually.", vim.log.levels.ERROR)
return false
end
vim.notify(
"Hacker Helper Error: LuaSocket (luasocket) is not installed. Please install it using LuaRocks: luarocks install luasocket",
vim.log.levels.ERROR
)
vim.notify("Hacker Helper Error: sudo apt install luarocks", vim.log.levels.ERROR)
return false
end
return true
end
Expand Down Expand Up @@ -96,8 +40,8 @@ M.config = config
-- you can also put some validation here for those.
---@param user_config Config? User-provided configuration
M.setup = function(user_config)
-- Check if luasocket is installed, and attempt to install it if missing
if not ensure_luasocket_installed() then
-- Check if luasocket is installed
if not check_luasocket_installed() then
return
end
-- Merge user configuration with defaults
Expand All @@ -109,6 +53,10 @@ M.setup = function(user_config)
vim.keymap.set("n", M.config.prefix, function() end, { noremap = true, silent = true, desc = "Hacker Helper" })
vim.keymap.set("v", M.config.prefix, function() end, { noremap = true, silent = true, desc = "Hacker Helper" })

-- Run command
vim.keymap.set("v", full_run_exec_mapping, function()
module.exec_line_or_selection_in_term()
end, { noremap = true, silent = true, desc = "Execute Command" })
-- Register the group names for both encoding and decoding
vim.keymap.set(
"n",
Expand All @@ -120,84 +68,92 @@ M.setup = function(user_config)
"n",
M.config.prefix .. M.config.keys.decode_prefix,
function() end,
{ noremap = true, silent = true, desc = "Decode" }
{ noremap = true, silent = true, desc = "Decoder" }
)

-- Encoding key mappings
vim.keymap.set("v", M.config.prefix .. M.config.keys.encode_prefix .. M.config.keys.encode_url, function()
M.encode_selected_text("url")
M.transform_selected_text("url", "encode")
end, { noremap = true, silent = true, desc = "URL Encode" })
vim.keymap.set("v", M.config.prefix .. M.config.keys.encode_prefix .. M.config.keys.encode_base64, function()
M.encode_selected_text("base64")
M.transform_selected_text("base64", "encode")
end, { noremap = true, silent = true, desc = "Base64 Encode" })

-- Decoding key mappings
vim.keymap.set("v", M.config.prefix .. M.config.keys.decode_prefix .. M.config.keys.decode_url, function()
M.decode_selected_text("url")
M.transform_selected_text("url", "decode")
end, { noremap = true, silent = true, desc = "URL Decode" })
vim.keymap.set("v", M.config.prefix .. M.config.keys.decode_prefix .. M.config.keys.decode_base64, function()
M.decode_selected_text("base64")
M.transform_selected_text("base64", "decode")
end, { noremap = true, silent = true, desc = "Base64 Decode" })
-- Set key mappings using vim.keymap.set, including the description
vim.keymap.set("n", full_run_exec_mapping, function()
module.exec_line_or_selection_in_term()
end, { noremap = true, silent = true, desc = "Execute Command" })
vim.keymap.set("v", full_run_exec_mapping, function()
module.exec_line_or_selection_in_term()
end, { noremap = true, silent = true, desc = "Execute Command" })
end
-- Function to handle encoding
M.encode_selected_text = function(type)
local start_line = vim.fn.getpos("'<")[2]
local end_line = vim.fn.getpos("'>")[2]
local selection = vim.fn.getline(start_line, end_line)

if type == "url" then
local encoded = vim.fn.escape(vim.fn.join(selection, "\n"), " ")
encoded = encoded:gsub("\n", ""):gsub(" ", "%%20"):gsub("([^%w%.%-_])", function(c)
return string.format("%%%02X", string.byte(c))
end)
-- If encoded has multiple lines, set them
local lines = vim.split(encoded, "\n")
if #lines == 1 then
vim.fn.setline(start_line, lines[1])
else
vim.fn.setline(start_line, lines)
-- Function to handle encoding/decoding based on selection
M.transform_selected_text = function(type, mode)
local start_pos = vim.fn.getpos("'<")
local end_pos = vim.fn.getpos("'>")
local start_line, start_col = start_pos[2], start_pos[3]
local end_line, end_col = end_pos[2], end_pos[3]
local lines = vim.fn.getline(start_line, end_line)

-- Determine whether to transform entire lines or partial selection
if start_col == 0 and end_col == -1 then
-- Full line(s) selected
if type == "url" then
lines = M.transform_lines(lines, mode, "url")
elseif type == "base64" then
lines = M.transform_lines(lines, mode, "base64")
end
elseif type == "base64" then
local encoded = mime.b64(vim.fn.join(selection, "\n"))
local lines = vim.split(encoded, "\n")
if #lines == 1 then
vim.fn.setline(start_line, lines[1])
else
vim.fn.setline(start_line, lines)
-- Replace all selected lines with the transformed lines
vim.fn.setline(start_line, lines)
else
-- Partial selection within a single line
local selection = string.sub(lines[1], start_col, end_col)
local transformed = ""

if type == "url" then
transformed = M.transform_text(selection, mode, "url")
elseif type == "base64" then
transformed = M.transform_text(selection, mode, "base64")
end

-- Replace the partial selection with transformed text
local before_selection = string.sub(lines[1], 1, start_col - 1)
local after_selection = string.sub(lines[1], end_col + 1)
vim.fn.setline(start_line, before_selection .. transformed .. after_selection)
end
end

-- Function to handle decoding
M.decode_selected_text = function(type)
local start_line = vim.fn.getpos("'<")[2]
local end_line = vim.fn.getpos("'>")[2]
local selection = vim.fn.getline(start_line, end_line)
-- Function to transform lines for full-line selection
M.transform_lines = function(lines, mode, type)
local transformed_lines = {}
for _, line in ipairs(lines) do
if type == "url" then
table.insert(transformed_lines, M.transform_text(line, mode, "url"))
elseif type == "base64" then
table.insert(transformed_lines, M.transform_text(line, mode, "base64"))
end
end
return transformed_lines
end

-- Function to transform individual text based on type and mode
M.transform_text = function(text, mode, type)
local mime = require("mime")
if type == "url" then
local decoded = vim.fn.join(selection, "\n"):gsub("%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end)
local lines = vim.split(decoded, "\n")
if #lines == 1 then
vim.fn.setline(start_line, lines[1])
else
vim.fn.setline(start_line, lines)
if mode == "encode" then
return text:gsub("([^%w%.%-_])", function(c)
return string.format("%%%02X", string.byte(c))
end)
else -- decode
return text:gsub("%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end)
end
elseif type == "base64" then
local decoded = mime.unb64(vim.fn.join(selection, "\n"))
local lines = vim.split(decoded, "\n")
if #lines == 1 then
vim.fn.setline(start_line, lines[1])
else
vim.fn.setline(start_line, lines)
if mode == "encode" then
return mime.b64(text)
else -- decode
return mime.unb64(text)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lua/hacker-helper/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function M.exec_line_or_selection_in_term()
size = 50,
})
else
print("toggleterm is not installed or cannot be loaded.")
vim.notify("Hacker Helper: toggleterm is not installed or cannot be loaded", vim.log.levels.WARN)
return
end

Expand Down Expand Up @@ -56,7 +56,7 @@ function M.exec_line_or_selection_in_term()
-- Safely check if the terminal job ID exists
local term_job_id = vim.b.terminal_job_id
if not term_job_id then
print("No terminal job ID found.")
vim.notify("Hacker Helper: toggleterm job id couldn't be found", vim.log.levels.WARN)
return
end

Expand All @@ -68,7 +68,7 @@ function M.exec_line_or_selection_in_term()
-- Switch back to the previous buffer
vim.cmd("b#")
else
print("No terminal buffer found.")
vim.notify("Hacker Helper: No terminal buffer found.", vim.log.levels.WARN)
end
end

Expand Down
28 changes: 28 additions & 0 deletions tests/hacker-helper/hacker_helper_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,31 @@ describe("setup", function()
assert(plugin.hello() == "custom", "my first function with param = custom")
end)
end)

describe("Base64 and URL encoding/decoding", function()
-- Test Base64 encoding and decoding
it("encodes Base64 correctly", function()
local text = "Hello, World!"
local encoded = plugin.transform_text(text, "encode", "base64")
assert(encoded == "SGVsbG8sIFdvcmxkIQ==", "Base64 encoding failed")
end)

it("decodes Base64 correctly", function()
local encoded_text = "SGVsbG8sIFdvcmxkIQ=="
local decoded = plugin.transform_text(encoded_text, "decode", "base64")
assert(decoded == "Hello, World!", "Base64 decoding failed")
end)

-- Test URL encoding and decoding
it("encodes URL correctly", function()
local text = "Hello, World!"
local encoded = plugin.transform_text(text, "encode", "url")
assert(encoded == "Hello%2C%20World%21", "URL encoding failed")
end)

it("decodes URL correctly", function()
local encoded_text = "Hello%2C%20World%21"
local decoded = plugin.transform_text(encoded_text, "decode", "url")
assert(decoded == "Hello, World!", "URL decoding failed")
end)
end)

0 comments on commit c803316

Please sign in to comment.