putting letters with number inside lua variables - nginx

I have this Nginx lua code which is used to encrypt numbers from 0 to 9 with its equivalent encrypted code ... ( see the example below )
and its working great , but i want to make it also encrypt Letters from A to Z.
I'm not familiar with lua so i don't know what is the best way to make it works !
content_by_lua_block {
local bf = {}
bf[0] = '(((_<<_)<<_)'
bf[1] = '(({}>[])-(()>[]))'
bf[2] = '(({}>[])-(()>[]))])'
bf[3] = '(({}>[])-(()>[]))])*'
bf[4] = '(({}>[])-(()>[]))])*(()>[])'
bf[5] = '(({}>[])-(()>[]))])*(()>[])%'
bf[6] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_'
bf[7] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_'
bf[8] = '(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))'
bf[9] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))'
local cookie = ngx.var.token
for i=0, 9 do
cookie = string.gsub(cookie, i, "'+"..bf[i].."+'")
end
Maybe something like that :
content_by_lua_block {
local bf = {}
bf[0] = '(((_<<_)<<_)'
bf[1] = '(({}>[])-(()>[]))'
bf[2] = '(({}>[])-(()>[]))])'
bf[3] = '(({}>[])-(()>[]))])*'
bf[4] = '(({}>[])-(()>[]))])*(()>[])'
bf[5] = '(({}>[])-(()>[]))])*(()>[])%'
bf[6] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_'
bf[7] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_'
bf[8] = '(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))'
bf[9] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))'
bf[a] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))(()>[])%'
bf[b] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))(()>[])%(()>[])%'
bf[c] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))(()>[])%(()>[])%(()>[])%'
...................
...................
...................
...................
bf[z] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))(()>[])%(()>[])%(()>[])%)%(()>[])%)%(()>[])%'
local cookie = ngx.var.token
for i=0, 9 and a, z do
cookie = string.gsub(cookie, i, "'+"..bf[i].."+'")
end

I tried to rewrite your code:
local bf = {}
bf[0] = '(((_<<_)<<_)'
bf[1] = '(({}>[])-(()>[]))'
bf[2] = '(({}>[])-(()>[]))])'
bf[3] = '(({}>[])-(()>[]))])*'
bf[4] = '(({}>[])-(()>[]))])*(()>[])'
bf[5] = '(({}>[])-(()>[]))])*(()>[])%'
bf[6] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_'
bf[7] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_'
bf[8] = '(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))'
bf[9] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))'
bf['a'] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))(()>[])%'
bf['b'] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))(()>[])%(()>[])%'
bf['c'] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))(()>[])%(()>[])%(()>[])%'
--
--
--
bf['z'] = '(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))(()>[])%(()>[])%(()>[])%)%(()>[])%)%(()>[])%'
local cookie = "01sdz" -- ngx.var.token
-- first example
local cookie = cookie:gsub ( '.', function (x)
local s = tonumber(x) or x
if bf[s] then return "'+".. bf[s] .."+'"
else return x
end
end )
print(cookie)
-- second example
cookie = "01sdz"
for i=0, 9 do
cookie = string.gsub(cookie, i, "'+".. bf[i] .."+'")
-- may be use bf[i]:gsub( '([%%])', '%%%1') with escape escaping symbol %
end
for i= string.byte('a'), string.byte('z') do
local v = bf[string.char(i)]
if v then
cookie = cookie:gsub( string.char(i), "'+".. v:gsub( '([%%])', '%%%1') .."+'")
end
end
print(cookie)
result
'+(((_<<_)<<_)+''+(({}>[])-(()>[]))+'sd'+(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))(()>[])%(()>[])%(()>[])%)%(()>[])%)%(()>[])%+'
'+(((_<<_)<<_)+''+(({}>[])-(()>[]))+'sd'+(({}>[])-(()>[]))])*(()>[])%(((_<<_)<<_)+((_<<_)*_))(()>[])%(()>[])%(()>[])%)%(()>[])%)%(()>[])%+'
I updated my code, anyway the second example will help you

Related

POST don't find

I have a problem with my code. When a I do a post don't find the autor and create a new register in table, but after with the first "try" find 2 with the same autor and launch a error.
Why don't find the try inside the reques.method ='POST'?. I know entry inside the if because in console print the except condition: "Usuario no exite".
Best Regards.
def usuario(request):
user_actual=request.user.id
user = User.objects.get(id=user_actual)
new_form_data = {}
miUsuario = FormularioUsuario(new_form_data)
usuario = {}
print(user)
print(user_actual)
print(Usuario.objects.get(autor= user_actual))
try:
usuario = Usuario.objects.get(autor= user_actual)
new_form_data = {}
new_form_data['irpf'] = usuario.irpf
new_form_data['reduccion'] = usuario.reduccion
new_form_data['guardar_normal'] = usuario.guardar_normal
new_form_data['guardar_ertain'] = usuario.guardar_ertain
new_form_data['guardar_berezi'] = usuario.guardar_berezi
miUsuario = FormularioUsuario(new_form_data)
except:
print("No existe datos usuario")
new_form_data = {}
new_form_data['irpf'] = 0.0
new_form_data['reduccion'] = 0.0
new_form_data['guardar_normal'] = False
new_form_data['guardar_ertain'] = False
new_form_data['guardar_berezi'] = False
miUsuario = FormularioUsuario(new_form_data)
if request.method=='POST': #and ('guardar' in request.POST):
miUsuario=FormularioUsuario(request.POST)
print(user_actual)
try:
usuario = Usuario.objects.get(autor= user_actual)
usuario.irpf = miUsuario.data['irpf']
usuario.reduccion = miUsuario.data['reduccion']
usuario.guardar_normal = miUsuario.data['guardar_normal']
usuario.guardar_ertain = miUsuario.data['guardar_ertain']
usuario.guardar_berezi = miUsuario.data['guardar_berezi']
usuario.save()
print("Guardado datos usuario")
except:
print("Usuario no existe")
usuario = Usuario(autor= user,
irpf= miUsuario.data['irpf'],
reduccion = miUsuario.data['reduccion'],
guardar_normal='guardar_normal' in miUsuario.data,
guardar_ertain='guardar_ertain' in miUsuario.data,
guardar_berezi='guardar_berezi' in miUsuario.data,)
usuario.save()
new_form_data = {}
new_form_data['irpf'] = usuario.irpf
new_form_data['reduccion'] = usuario.reduccion
new_form_data['guardar_normal'] = usuario.guardar_normal
new_form_data['guardar_ertain'] = usuario.guardar_ertain
new_form_data['guardar_berezi'] = usuario.guardar_berezi
miUsuario = FormularioUsuario(new_form_data)
return render(request, "BieleGastosApp/usuario_datos.html", {'usuario': miUsuario})
'''
A good first step would be to find out which line in the try block is giving error. Use except Exception as e: and add print(e) to the except block. That should direct your attention to the specific error that is generated and help you make progress.

Creating instance for non-existing object: <somenumber> | What Did I Do Wrong?

Every time I run my little game, it throws me the following, fatal, error:
ERROR in
action number 2
of Trigger Event:
for object obj_y:
Creating instance for non-existing object: 100012
I have no idea what's causing it. The only time I use obj_y, is in one file. When I comment that part out, it still keeps nagging it's non-existing.
My code:
var randomxnumber;
var randomynumber;
var randomarraynumber;
var array;
var snelheid;
snelheid = 3.5;
array[0] = obj_a;
array[1] = obj_b;
array[2] = obj_c;
array[3] = obj_d;
array[4] = obj_e;
array[5] = obj_f;
array[6] = obj_g;
array[7] = obj_h;
array[8] = obj_i;
array[9] = obj_j;
array[10] = obj_k;
array[11] = obj_l;
array[12] = obj_m;
array[13] = obj_n;
array[14] = obj_o;
array[15] = obj_p;
array[16] = obj_q;
array[17] = obj_r;
array[18] = obj_s;
array[19] = obj_t;
array[20] = obj_u;
array[21] = obj_v;
array[22] = obj_w;
array[23] = obj_x;
array[24] = obj_y;
array[25] = obj_z;
randomxnumber = random_range(0, 50);
randomynumber = random_range(-10, -50);
for (int = 0; int < 20; int += 1){
if(randomxnumber >= window_get_width()){
randomxnumber -= 100;
} else {
randomxnumber += 50;
}
randomynumber -= 50;
array[int] = instance_create(randomxnumber, randomynumber, array[random_range(0, 26)]);
with(array[int]){
vspeed = snelheid;
}
}
My obj_y object:
Quick note, I'm on GameMaker 8.1 Standard. I'm I being stupid and just missing something?
It's very strange code, I don't understand what you trying to do.
Although there are two errors.
array[random_range(0, 26)]
should be
array[irandom_range(0, 25)
GM rounds array's index, so if value is more than 25 (25.1, etc) you will have out of array bounds.
Second.
array[int] = instance_create(...)
There instance's id will be stored into array. Not object's id. But instance_create requires object index. You can get object's index using object_index variable:
array[int] = instance_create(randomxnumber, randomynumber, array[irandom_range(0, 25)].object_index);

what is the pointer to pointer equivalent in lua

I know that you can use tables in a similar way to pointers in lua. That being said, what would pointers to pointers look like? Would they look something like dp = {p = {}}? if so what would the equivalent to the c code below be in lua?
void InsertItem(node **head, node *newp){
node **dp = head;
while((*dp) && (*dp)->value > newp->value
{
dp = &(*dp)->next;
}
newp->next = *dp;
*dp = newp;
}
Yes, double pointer may be translated to Lua as nested table.
local function InsertItem(head, newitem)
while head.next and head.next.value > newitem.value do
head = head.next
end
newitem.next = head.next
head.next = newitem
end
-- Typical Usage:
local head = {}
InsertItem(head, {value = 3.14})
InsertItem(head, {value = 42})
InsertItem(head, {value = 1})
-- Now the data is the following:
-- head = {next = elem1}
-- elem1 = {next = elem2, value = 42 }
-- elem2 = {next = elem3, value = 3.14}
-- elem3 = { value = 1 }
The big difference between C pointers and Lua tables is that in C, you can take the address of a variable and pass it to a function to modify it. You can't do that in Lua, but the function could always return the modified value.
Would they look something like dp = {p = {}}?
Yes, that's about as close as close as you can get to a pointer to a pointer in Lua.
if so what would the equivalent to the c code below be in lua?
Linked lists tend to work more smoothly with recursion:
local function InsertItem(head, newp)
if not head or head.value <= newp.value then
newp.next = head
return newp
end
head.next = InsertItem(head.next, newp)
return head
end

One Esp8266 Client One Esp8266 Server

I did Server-Client Application with lua using one Esp8266. I wanna do this with two Esp8266. I wanna use one of these Esp8266 is Server and the other other one is Client. You can see below first code using for get RSSI from one AP and second code is using for writing these RSSI in a Server. How can i placed these two codes in two Esp8266?
i=5
tmr.alarm(1,10000,1, function()
print(wifi.sta.getap(scan_cfg, 1, listap))
if i>1 then
print(i)
i=i-1
else
tmr.stop(1)
print("Timer Durdu")
end
end
)
function listap(t)
for bssid,v in pairs(t) do
local ssid = string.match(v, "([^,]+)")
l=string.format("%-10s",ssid)
stringtoarray = {}
index = 1
for value in string.gmatch(v,"%w+") do
stringtoarray [index] = value
index = index + 1
end
print(l)
print(stringtoarray[2])
end
end
scan_cfg = {}
scan_cfg.ssid = "VSP250s"
scan_cfg.bssid = "00:09:df:8e:03:b4"
scan_cfg.channel = 0
scan_cfg.show_hidden = 1
Second code:
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
buf = buf.."<!DOCTYPE html><html><div id='container'><font size='5'>"
buf = buf..'<style>body{width:auto;height:auto;background-color:#ffffff;}'
buf = buf..'.button {font-size: 20px;}</style>'
buf = buf.."<head> <meta http-equiv='refresh' content=3> "
buf = buf.."<p><h1>RSSI meter<br> ESP8266</h1>";
--buf = buf.."<p>Refresh : <button class='button'>ON</button>&nbsp</p>";
--buf = buf.."<p>Relay Switch : <button class='button'>ON</button>  "
--buf = buf.."<button class='button'>OFF</button><br>"
buf = buf..'<B>Voltage :<font color=red>'..string.format('%s',l)..' V</font></b><br>'
buf = buf..'<B>Current :<B><font color=blue>'..string.format('%g',stringtoarray[2])..' A</font></b><br>'
--buf = buf..'<B>Power Consumption :<B><font color=DeepSkyBlue>'..'Not Available'..'</font></b><br><BR>'
-- buf = buf..'<p>Function Button :<B><font color=BlueViolet>'..button_status..'</font></b><br></p>';
buf = buf..'</head>'
buf = buf..'<br><br><details><summary><font color=red>BURAK IPEK</font><p>'
buf = buf..'<summary><p>Vestel Electronics </p></details>'
buf = buf.."</body></font></div></html>"
client:send(buf);
client:close();
collectgarbage();
end)
end)
Put each code into a lua file. Include both from init.lua with typing
dofile("client.lua");
dofile("server.lua");
To make things easier, write methods.
Good luck.

Clearing multiple cookies

The code below tries to clear the cookies for all domains once a user logs out of the system. For some reason, only the last domain in the array is cleared. Why does this happen? Am I doing something wrong?
For example, if I change the size of the array to 4 and then change the for loop to only go to 3, then it only logs me out of y.xcv.com.
As a sidenote, I have this loop working on a different server that uses a slightly different function to clear the cookies.
Edit: Code updated per suggestions below. Now it fails on the "as HttpCookie" line. Do I need to include some library?
Dim aDomain(12)
Dim ESidCookie, WIdCookie, EBidCookie, TSidAccessCookie, PSidAccessCookie, SSidCookie As HttpCookie
aDomain(0) = ".x.com"
aDomain(1) = "y.x.com"
aDomain(2) = "y.x.com"
aDomain(3) = "y.xcv.com"
aDomain(4) = "x.com"
aDomain(5) = "y.z.a.com"
aDomain(6) = "y.z.a.com"
aDomain(7) = "z.a.com"
aDomain(8) = ""
aDomain(9) = "y.x.com"
aDomain(10) = "y.x.com"
aDomain(11) = "y.x.com"
for count = 0 to 11
strDomain = aDomain(count)
response.Write count & "/" & strDomain
ESidCookie = New HttpCookie("oneCookie")
ESidCookie.Domain = strDomain
ESidCookie.Path = "/"
ESidCookie = ""
ESidCookie.Expires = now() - 100
Response.Cookies.Add(ESidCookie)
WIdCookie = New HttpCookie("twoCookie")
WIdCookie.Domain = strDomain
WIdCookie.Path = "/"
WIdCookie = ""
WIdCookie.Expires = now() - 100
Response.Cookies.Add(WIdCookie)
EBidCookie = New HttpCookie("threeCookie")
EBidCookie.Domain = strDomain
EBidCookie.Path = "/"
EBidCookie = ""
EBidCookie.Expires = now() - 100
Response.Cookies.Add(EBidCookie)
TSidAccessCookie = New HttpCookie("fourCookie")
TSidAccessCookie.Path = "/"
TSidAccessCookie = "LoggedOut"
Response.Cookies.Add(TSidAccessCookie)
PSidAccessCookie = New HttpCookie("fiveCookie")
PSidAccessCookie.Domain = strDomain
PSidAccessCookie.Path = "/"
PSidAccessCookie = ""
PSidAccessCookie.Expires = now() - 100
Response.Cookies.Add(PSidAccessCookie)
SSidCookie = New HttpCookie("sixCookie")
SSidCookie.Domain = strDomain
SSidCookie.Path = "/"
SSidCookie = ""
SSidCookie.Expires = now() - 100
Response.Cookies.Add(SSidCookie)
next
Any help is appreciated. Thanks!
The Response.Cookies collection is keyed off of the cookie name so you are just changing the domain of the same cookie each time you go through your loop. That's why the last one wins.
You could try creating a new cookie object and adding that to the Response.Cookies collection in your loop instead.
If you want to clear all cookies you will should create all new ones with the same name. Here is a basic example that should get you going:
Dim newCookie As HttpCookie
For i As Integer = 0 To 10
' creating a new cookie each time
newCookie = New HttpCookie(cookieNames(i))
' expire the cookie
newCookie.Expires = DateTime.Now.AddDays(-1)
' storing the new cookie each time
Response.Cookies.Add(newCookie)
Next
It doesn't look like your creating all new cookies and adding them to the response properly.

Resources