Keep connection active after a POST request

1

I'm trying to create a script to enable the bandwidth control QOS service of my router, but I'm doing something wrong because I can not keep the connection after making a POST request.

local host = '192.168.0.1'
local headers = [[]]

local LuaSocket = require("socket")
client = LuaSocket.connect(host, 80)

headers = [[Host: 192.168.0.1
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 39

Username=admin&checkEn=0&Password=admin]]

client:send("POST /LoginCheck " .. headers)
print("First request")
while 1 do
    r, err = client:receive()
    if err then 
        break
    end
    print(r)
end

headers = [[Host: 192.168.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://192.168.0.1/index.asp
Cookie: language=pt; admin:language=pt
Connection: keep-alive]]

client:send("GET /goform/trafficForm?GO=net_tc.asp&tc_enable=1&up_Band=12800&down_Band=12800&cur_number=2&tc_list_1=80,100,100,1,50,2000,1,1&tc_list_2=80,101,150,1,1,50,1,1 HTTP/1.0" .. headers)
print("Second request")
while 1 do
    r, err = client:receive()
    if err then 
        break
    end
    print(r)
end

client:close()

When running, I have this

First request
HTTP/1.0 302 Redirect
Server: GoAhead-Webs
Date: Wed Aug 03 05:09:46 2016
Set-Cookie: admin:language=pt; path=/
Pragma: no-cache
Cache-Control: no-cache
Content-Type: text/html
Location: index.asp

Second request

By the browser, I have this POST response in / LoginCheck

Cache-Control: no-cache
Content-Type: text/html
Date: Wed Aug 03 05:13:15 2016
Location: index.asp
Pragma: no-cache
Server: GoAhead-Webs
Set-Cookie: admin:language=pt; path=/

From what I saw, it logs in, but I do not know how to maintain the connection to get the GET request that would activate the bandwidth control.

NOTE: In order to get access to any page of the router, I need to log in to the 192.168.0.1/login.asp page, which makes a POST request for 192.168.0.1/LoginCheck , and if successful, redirects to 192.167.0.1/index.asp and then you can access the other pages of it.

    
asked by anonymous 03.08.2016 / 10:15

1 answer

1

To save the data, it is necessary to upload a session, so that the server saves the data in the user session, the header sent to the server must contain the session data.

Cookie: csrftoken=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

This causes the server to save user session data until you close the browser and switch sessions.

GET / HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pt-BR
Accept-Encoding: gzip, deflate
DNT: 1
Cookie: csrftoken=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Connection: keep-alive
Upgrade-Insecure-Requests: 1

This header is from the Firefox browser request, the Cookie property in the header is responsible for saving user session data, having the csrftoken variable that has id user session.

In some cases, csrftoken , is not the only value for the session, also having sessionid with the same format.

    
05.08.2016 / 16:47