Lua LGI unpack GLib.Variant object - lua

I'm trying to get password from keyring for awesome-wm session (using DBus via lgi library).
I'm able to find keyring entry path, open communication session and unlock entry.
Then I call GetSecrets method and store result into secret variable.
According to the documentation it is supposed to be a struct Secret. Seems like lgi cannot handle this type and passes it as userdata type (at least I wasn't able to make it give access to struct fields). Is there a way to get struct Secret value field contents without writing custom C handler?
Here is the code:
local bus = Gio.bus_get_sync(Gio.BusType.SESSION, nil)
local attr = {}
attr[1] = {attribute = "value"} -- attribute-value pair to search for
-- search for secret path
local name = "org.freedesktop.secrets"
local object = "/org/freedesktop/secrets"
local interface = "org.freedesktop.Secret.Service"
local method = "SearchItems"
local message = Gio.DBusMessage.new_method_call(name, object, interface, method)
message:set_body(GLib.Variant("(a{ss})", attr))
local result, err = bus:send_message_with_reply_sync(message, Gio.DBusSendMessageFlags.NONE,
-1, nil)
local location
for _, l in result:get_body():pairs() do
if #l > 0 then location = l[1] end
end
print(location) -- returns "/org/freedesktop/secrets/collection/Default/1"
-- open session
local name = "org.freedesktop.secrets"
local object = "/org/freedesktop/secrets"
local interface = "org.freedesktop.Secret.Service"
local method = "OpenSession"
local message = Gio.DBusMessage.new_method_call(name, object, interface, method)
message:set_body(GLib.Variant("(sv)", {"plain", GLib.Variant("s", "")}))
local result, err = bus:send_message_with_reply_sync(message, Gio.DBusSendMessageFlags.NONE,
-1, nil)
local session = result:get_body()[2]
print(session) -- returns "/org/freedesktop/secrets/session/s4"
-- unlock key
local name = "org.freedesktop.secrets"
local object = "/org/freedesktop/secrets"
local interface = "org.freedesktop.Secret.Service"
local method = "Unlock"
local message = Gio.DBusMessage.new_method_call(name, object, interface, method)
message:set_body(GLib.Variant("(ao)", {{location}}))
local result, err = bus:send_message_with_reply_sync(message, Gio.DBusSendMessageFlags.NONE,
-1, nil)
-- at this point key property "Locked" if false. tested using d-feet
-- get secret
local name = "org.freedesktop.secrets"
local object = "/org/freedesktop/secrets"
local interface = "org.freedesktop.Secret.Service"
local method = "GetSecrets"
local message = Gio.DBusMessage.new_method_call(name, object, interface, method)
message:set_body(GLib.Variant("(aoo)", {{location},session}))
local result, err = bus:send_message_with_reply_sync(message, Gio.DBusSendMessageFlags.NONE,
-1, nil)
local secret = result:get_body()
print(#secret) -- returns "1"
print(secret) -- returns table address
print(type(secret)) -- returns "userdata"
-- lock key
local name = "org.freedesktop.secrets"
local object = "/org/freedesktop/secrets"
local interface = "org.freedesktop.Secret.Service"
local method = "Lock"
local message = Gio.DBusMessage.new_method_call(name, object, interface, method)
message:set_body(GLib.Variant("(ao)", {{location}}))
local result, err = bus:send_message_with_reply_sync(message, Gio.DBusSendMessageFlags.NONE,
-1, nil)
-- close session
local name = "org.freedesktop.secrets"
local object = location
local interface = "org.freedesktop.Secret.Session"
local method = "Close"
local message = Gio.DBusMessage.new_method_call(name, object, interface, method)
local result, err = bus:send_message_with_reply_sync(message, Gio.DBusSendMessageFlags.NONE,
-1, nil)
edit
When I do print(secret), lgi.rec 0x7f57d0014960:GLib.Variant is returned.
So, secret is an object. How can I retrieve value field from GLib.Variant object?
edit 2
secret:get_data_as_bytes():get_data() dumps struct in bytearray form; secret:print() returns formatted string of struct. I wonder if there's a better way.
edit 3
type of secret variable is (a{o(oayays)})
Code to recreate an object of that type:
local lgi = require 'lgi'
local Gio = lgi.require 'Gio'
local GLib = lgi.require 'GLib'
local var = GLib.Variant("(a{o(oayays)})", {{["/path/to/object"]={"/path/to/session","parameters","value","content_type"}}})

(Note that I didn't install this password manager or try any of this at all)
Last time I asked the author of LGI about such issue, the answer was:
I have submitted fix to Variant which restores this functionality. So
an example of use will be:
local function called_from_C(userdata)
local variant = GLib.Variant(userdata)
print(variant)
end
if you have to support older (well, released :) lgi versions, you can
use undocumented way:
local core = require 'lgi.core'
local function called_from_C(userdata)
local variant = core.record.new(GLib.Variant, userdata)
print(variant)
end
Note that there is other ways. To work around another such bugs, I also once created a C Lua plugin and just wrote that code in C. That's actually rather trivial with Lua [2].
Another way, if you use LuaJIT, is to use the built-in FFI to just-in-time compile the struct definition into a Lua object [1].
Finally, if the question is more about how to unpack "working" GVariant values once they are consumed properly by LGI, look at my code for this here https://github.com/Elv13/wirefu/blob/master/proxy.lua
[1] http://luajit.org/ext_ffi_api.html
[2] https://github.com/Elv13/lua_async_binding/blob/master/src/luabridge.c#L407

Finally I've found a solution. To unpack a value of a complex type, for example (a{o(oayays)}), one should use get_child_value function.
secret:get_child_value(0):get_child_value(0):get_child_value(1):get_child_value(2).value
Explanation: index tuple; index array; index dict; index tuple

Related

Webhook discord bug roblox

i have this scripts error below:
attempt to call a boolean value
attempt to call a number value
Here is the script :
--Game Info
local SystemName = "TestSystemname"
local WhitelistedServ = false
local OwnerId = game.CreatorId
local GameId = game.GameId
local MaxPlayers = game.Players.MaxPlayers
local GameUrl = "https://www.roblox.com/games/"..GameId
--Webhook sender and message creator in JS
local WebhookUrlServ = ""
local MessageDataServ = {["content"] = "New game started with the whitelist system !"..SystemName
"The Owner Id= "..OwnerId
"Is Game whitelisted ? "..WhitelistedServ
"How Many Players Max? "..MaxPlayers
"The Game Url= "..GameUrl}
MessageDataServ = HttpServ:JSONEncode(MessageDataServ)
Thanks,
I found the problem, i tryed to send a value i just need replace this
local WhitelistedServ = false
by this
local WhitelistedServ = "false"
and it will be work, value cant be in this script for some reason and make with embed is better.

Unable to assign property Text. string expected, got Instance

I was making random text script (dont mind the sounds thing) and I got this error: Unable to assign property Text. string expected, got Instance.
while thing == true do
wait(math.random(3, 12))
local txts = game.ServerStorage.CallTexts:GetChildren()
local Choices = script:GetChildren()
local RandomIndex = math.random(1, #Choices)
local RandomSound = Choices[RandomIndex]
local RandomTextIndex = math.random(1, #txts)
local RandomText = Choices[RandomTextIndex]
RandomSound:Play()
game.Workspace["Talking Ben's Room"].furniture.desk.phone["De Text"].BilBord.toxt.Text = RandomText
wait(RandomSound.TimeLength)
end
Text should be a string. RandomText is an Instance. Like the error told you. What type are the Choices, e.g. the children of CallTexts? Do they have a getter/toString?
If they are StringValues you will need RandomText.Value
If they are TextLabels (your case), you will need RandomText.ContentText

argument 1 missing or nil

i am making a tower defense game but it keeps saying argument 1 missing or nil
when i try to spawn the tower
this is a module script
(error at line 11)
local PhysicsServive = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PhysicsService = game:GetService("PhysicsService")
local events = ReplicatedStorage:WaitForChild("Events")
local Tower = {}
local SpawnTowerEvent = events:WaitForChild("SpawnTower")
function Tower.Spawn(player, Name, CFrame)
local towerExists = ReplicatedStorage.Towers:FindFirstChild(Name)
if towerExists then
local newTower = towerExists:Clone()
newTower.HumanoidRootPart.CFrame = CFrame
newTower.Parent = workspace.Towers
newTower.HumanoidRootPart:SetNetworkOwner(nil)
for i, object in ipairs(newTower:GetDescendants()) do
if object:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(object, "Tower")
object.Material = Enum.Material.ForceField
end
end
else
warn("Missing:", Name)
end
end
SpawnTowerEvent.OnServerEvent:Connect(Tower.Spawn())
return Tower
SpawnTowerEvent.OnServerEvent:Connect(Tower.Spawn())
Connect expects a function value, not a function call (unless that function call resolves to a function value). Remove the call operator ().
SpawnTowerEvent.OnServerEvent:Connect(Tower.Spawn)
You call Tower.Spawn without any arguments. Therefor you call FindFirstChild(nil) which causes the observed error.
Also it does not return a function value.

Reading artist metadata from DBus in lua script - weird output

I'm trying to reading in DBus metadata from nuvolaplayer in a Lua script. The (track) title and album fields display as expect, but the artist data appears oddly:
lgi.rec 0x7f9ee8005c90:GLib.Variant Underwater Dub Dictionary
Where "Sly & Robbie" is expected in place of "lgi.rec 0x7f9ee8005c90:GLib.Variant".
When I look at the value using d-feet, I can see that the xesam:artist field differs from the others in being surrounded by []s.
Here's the code I'm using:
local lgi = require 'lgi'
local Gio = lgi.require 'Gio'
local core = require 'lgi.core'
local GLib = lgi.require 'GLib'
local type,unpack = type,unpack
local bus = Gio.bus_get_sync(Gio.BusType.SESSION)
local ret,err = bus:call(
"org.mpris.MediaPlayer2.nuvolaplayer",
--"org.gnome.Rhythmbox3",
"/org/mpris/MediaPlayer2",
"org.freedesktop.DBus.Properties",
"GetAll",
GLib.Variant.new_tuple(
{
GLib.Variant("s","org.mpris.MediaPlayer2.Player")
}, 1),
nil,
Gio.DBusConnectionFlags.NONE,
-1, -- Timeout
nil, -- Cancellable
function(conn, res)
local ret, err = bus:call_finish(res)
print("here",err)
local returnValue1, returnValue2 = unpack(ret.value)
if not err then
print("META", returnValue1.Metadata["xesam:artist"],
returnValue1.Metadata["xesam:album"],
returnValue1.Metadata["xesam:title"])
end
end
)
local main_loop = GLib.MainLoop()
main_loop:run()
The field xesam:artist is a list/array of strings (artists). Maybe Lua needs special handling of this type for printing, for example iterate the array and extract plain strings from it.

Wireshark Lua Dissector - How to use a TAP?

I would like to do some analysis on top of my custom protocol that is dissected via my lua dissector. Therefore I tried to do this
myproto_proto = Proto("myproto", "Myproto Protocol")
m_dest = ProtoField.uint16("myproto.dest", "Destination", base.HEX)
m_src = ProtoField.uint16("myproto.src", "Source", base.HEX)
myproto_proto.fields = { sm_dest, sm_src }
dofile(MYPROTO_PROTO_PATH.."parser.lua")
function myproto_proto.dissector(buffer, pinfo, tree)
pinfo.cols.protocol = "MYPROTO"
local subtree = tree:add(myproto_proto, buffer(), "Myproto Protocol Data")
parse_msg(buffer, pinfo, subtree) -- does the actual parsing and sets the fields
end
udp_table = DissectorTable.get("udp.port")
udp_table:add(9000,myproto_proto)
-- LISTENER / TAP
f_test = Field.new("myproto.dest") -- fails because "field does not exist"
local function my_tap()
local window = TextWindow.new("Myproto Tap")
local tap = Listener.new(nil, "myproto")
local counter = 0
function remove()
tap:remove()
end
window:set_atclose(remove)
function tap.packet(pinfo, buffer)
counter = counter + 1
end
function tap.draw(t)
window:append("Counter: \t" .. counter .. "\n")
end
function tap.reset()
window:clear()
counter = 0
end
retap_packets()
end
register_menu("My Tap", my_tap, MENU_TOOLS_UNSORTED)
My problem is, I'm unable to access the dissected data with a field extractor. So how else could I get the dissected data in my lua tap?
Thanks in advance.
It's a known problem that custom Lua Field objects aren't usable in OSX (it apparently works in Windows XP but not Windows 7).
There are a few ways to pass data from your dissector to your tap.
Option 1: Use a shared Lua table
Create a global dictionary that is keyed by the packet number (from pinfo.number, which is visible to both dissector and tap).
-- we omit the 'local' keyword to make `dict` a global variable
dict = {}
In your dissector, add the packet data to the dictionary:
dict[pinfo.number] = { dest = m_dest, src = m_src }
In your tap, you can access the data by a simple lookup.
print('dest', dict[pinfo.number].dest )
XXX: Requires a global; Duplicates storage for a variable that is already held in the protocol tree (and should be accessible from the tap).
Option 2: Use pinfo.private
This was added in the dev build (1.7.0). It's similar to the solution above. pinfo.private is a PrivateTable, which is a hash table that stores only strings.
In your dissector, add your data to the packet's private table:
pinfo.private["src"] = tostring(m_src)
pinfo.private["dest"] = tostring(m_dest)
In your tap, access the data from the pinfo object:
print('dest', pinfo.private["dest"] )
XXX: Can only store string values
Option 3: Reparse the buffer
In your tap, call your parser (i.e., from parser.lua) to reparse the data in buffer, which is passed to the tap.
XXX: Duplicates work already done by dissector (can double processing time for X-large capture files)

Resources