68 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#include <sol/forward.hpp>
 | 
						|
#include <sol/object.hpp>
 | 
						|
#include <sol/protected_function_result.hpp>
 | 
						|
#include <sol/raii.hpp>
 | 
						|
 | 
						|
#include "lua_processor.h"
 | 
						|
#include "lua_api.h"
 | 
						|
#include "session.h"
 | 
						|
#include "vfs.h"
 | 
						|
 | 
						|
LuaProcessor::LuaProcessor(Session& context) {
 | 
						|
  _lua.open_libraries(sol::lib::base, sol::lib::string, sol::lib::table);
 | 
						|
 | 
						|
  /* Expose vfs_node struct members to Lua. */
 | 
						|
  _lua.new_usertype<vfs_node>("vfs_node",
 | 
						|
                              "name",       &vfs_node::name,
 | 
						|
                              "type",       &vfs_node::type,
 | 
						|
                              "children",   &vfs_node::children,
 | 
						|
                              "content",    &vfs_node::content);
 | 
						|
 | 
						|
  /* Expose CommandProcessor to Lua. DON'T ALLOW SCRIPTS TO CREATE IT THOUGH! */
 | 
						|
  _lua.new_usertype<Session>("Session", sol::no_constructor);
 | 
						|
 | 
						|
  /* Create the 'bettola' API table. */
 | 
						|
  sol::table bettola_api = _lua.create_named_table("bettola");
 | 
						|
  bettola_api.set_function("rm",                &api::rm);
 | 
						|
  bettola_api.set_function("ls",                &api::ls);
 | 
						|
  bettola_api.set_function("write_file",        &api::write_file);
 | 
						|
  bettola_api.set_function("create_executable", &api::create_executable);
 | 
						|
  bettola_api.set_function("get_current_dir",   &api::get_current_dir);
 | 
						|
  bettola_api.set_function("cd",                &api::cd);
 | 
						|
  bettola_api.set_function("scp",               &api::scp);
 | 
						|
  bettola_api.set_function("close_terminal",    &api::close_terminal);
 | 
						|
  bettola_api.set_function("ssh",               &api::ssh);
 | 
						|
  bettola_api.set_function("nmap",              &api::nmap);
 | 
						|
  bettola_api.set_function("disconnect",        &api::disconnect);
 | 
						|
}
 | 
						|
 | 
						|
LuaProcessor::~LuaProcessor(void) {}
 | 
						|
 | 
						|
sol::object LuaProcessor::execute(const std::string& script, Session& context,
 | 
						|
                                  const std::vector<std::string>& args, bool is_remote) {
 | 
						|
  try {
 | 
						|
    /* Pass C++ objects/points into the Lua env. */
 | 
						|
    _lua["is_remote_session"] = is_remote;
 | 
						|
    _lua["context"] = &context;
 | 
						|
 | 
						|
    /* Create and populate the 'arg' table for the script. */
 | 
						|
    sol::table arg_table = _lua.create_table();
 | 
						|
    for(size_t i = 0; i < args.size(); ++i) {
 | 
						|
      arg_table[i+1] = args[i]; /* Lua arrays 1-indexed. */
 | 
						|
    }
 | 
						|
    _lua["arg"] = arg_table;
 | 
						|
 | 
						|
    sol::protected_function_result result = _lua.safe_script(script);
 | 
						|
    if(result.valid()) {
 | 
						|
      return result;
 | 
						|
    } else {
 | 
						|
      sol::error err = result;
 | 
						|
      return sol::make_object(_lua, err.what());
 | 
						|
    }
 | 
						|
 | 
						|
  } catch(const sol::error& e) {
 | 
						|
    /* Return the error message as a string in a sol::object. */
 | 
						|
    return sol::make_object(_lua, e.what());
 | 
						|
  }
 | 
						|
}
 |