Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ LuaSec and LuaSocket (https.request)

LuaSec and LuaSocket (https.request)

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Coloradoderek   (5 posts)  Bio
Date Fri 03 Nov 2017 01:21 AM (UTC)
Message
I am wondering if there is support in the built-in LUA for MushClient for SSL connections over http.

Everything on the internet says I have to install LuaSec and LuaSocket but I'm guessing with MUSHClient its probably slightly unique compared to the normal installs.

I could go into all the things i've tried but in the end they never work.

does MUSHClient have built in SSL (through LUA) connection abilities?

this is a pure example but something like this, only for SSL:

local http=require("socket.http");

local request_body = [[email=<<mydiscordemail>>&password=<<mydiscordpassword>>]]
local response_body = {}

local res, code, response_headers = http.request{
url = "https://discordapp.com/api/",
headers =
{
["Authorization"] = "Bot <<mybottoken>>";
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body),
}


I can actually get non-ssl requests to work, but the SSL ones are not working and despite my many attempts to "install" luasec and luasocket into the mushclient install location, it has not even come close.

any help would be greatly appreciated, or if it isn't supported, that helps to (helps me to stop wasting time!!)

THANK YOU!!!!
Top

Posted by Fiendish   USA  (2,534 posts)  Bio   Global Moderator
Date Reply #1 on Fri 03 Nov 2017 09:37 PM (UTC)

Amended on Fri 03 Nov 2017 09:38 PM (UTC) by Fiendish

Message
It's not built in to MUSHclient, no, but I do it for Aardwolf. I've switched from LuaSec to Lua-openssl though, because LuaSec wasn't and probably still isn't thread-safe, and Lua-openssl has a compatility layer that makes it a drop-in replacement.

https://www.mushclient.com/forum/?id=12520&reply=1#reply1
https://github.com/zhaozg/lua-openssl

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Coloradoderek   (5 posts)  Bio
Date Reply #2 on Mon 06 Nov 2017 05:07 PM (UTC)
Message
thank you for the response.

Here is what I did and what I'm trying and the errors I'm getting:

I first downloaded your aaadwolf package and extracted the 4 files mentioned in your first link (lua/async.lua, llthreads.dll, lua/ssl.lua, lua/ssl/https.lua) and put them in their path's under the mushclient folder.

I then created this function:

function testHTTP(name,lines,wildcards)
async_ok, async = pcall(require,"async")
thread = nil
version_url = "www.google.com"

if async_ok and not thread then
print ("async_ok: "..tostring(async_ok))
thread = async.request(version_url, "HTTPS")
end
end

I then created an alias that when typed, it calls this function.

When I type it the first time. nothing happens. It doesn't show the print "async_ok: true" or anything.

If i type the alias again, it then runs, shows that print and errors out with this error:

async_ok: true
Run-time error
World: Aeroth
Function/Sub: testHTTP called by alias
Reason: processing alias ""
[string "Script file"]:6967: attempt to call field 'request' (a nil value)
stack traceback:
[string "Script file"]:6967: in function <[string "Script file"]:6959>
Error context in script:
6963 : version_url = "www.google.com"
6964 :
6965 : if async_ok and not thread then
6966 : print ("async_ok: "..tostring(async_ok))
6967*: thread = async.request(version_url, "HTTPS")

what am I doing wrong? Probably not including something properly?

All I'm trying to do at this point is get a 200 response from a https call to show it works then i'll worry about sending messages through discord.

Thanks!
Top

Posted by Fiendish   USA  (2,534 posts)  Bio   Global Moderator
Date Reply #3 on Mon 06 Nov 2017 05:37 PM (UTC)
Message
I have edited the post in question to give a more thorough account of the files related to sockets in my repository. Obviously if I'm going to refer back to it, it should be kept updated. See if the new information helps?

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Coloradoderek   (5 posts)  Bio
Date Reply #4 on Mon 06 Nov 2017 06:22 PM (UTC)

Amended on Mon 06 Nov 2017 08:53 PM (UTC) by Nick Gammon

Message
I made sure all those files were present (some were missing) and now when I try to run the function by typing the alias, it crashes MUSHClient.

Is it possible I need to re-build the openssl.dll file from that pages instructions?

If so, can you tell me what the directories should be in the config.win file relative to MUSHClient? Here is what I have atm:


# Installation directories
# System's libraries directory (where binary libraries are installed)
LUA_LIBDIR= "C:\Program Files (x86)\MUSHclient"

# Lua includes and lib
LUA_INC= "C:\Program Files (x86)\MUSHclient"
LUA_LIB= "C:\Program Files (x86)\MUSHclient\lua5.1.lib"

# Openssl include and lib
OPENSSL_INC="c:\openssl-win32\include"
OPENSSL_LIB="c:\openssl-win32\lib\libeay32.lib c:\openssl-win32\lib\ssleay32.lib"

LIBNAME= $T.dll

# Compilation directives
WARN= /O2
INCS= /I$(LUA_INC) /I$(OPENSSL_INC) /Ideps
CFLAGS= /DWIN32_LEAN_AND_MEAN /MD $(WARN) $(INCS)
CC= cl


I believe I have openssl installed properly because I can get to its prompt in the command prompt.

[EDIT] Added code tags.
Top

Posted by Coloradoderek   (5 posts)  Bio
Date Reply #5 on Mon 06 Nov 2017 10:24 PM (UTC)
Message
Hello Again.

I have this working. After all the frustrations, I ended up deleting everything I had done and started over by copying your files and it worked.

Now, I have another question....

How do you add custom headers to your https requests?
My code looks like this now:

Quote:
require "async"
version_url = "https://discordapp.com/api/"

thread = async.request(version_url, "HTTPS")

print("URL:"..version_url)

retval, page, status, headers, full_status = thread:join()
thread = nil
if status == 200 then -- 200 is the HTTP status code for "OK"
print("Status: "..status)
print("Retval: "..tostring(retval))
--print("Page: "..tostring(page))
print("Headers: "..print_r(headers,0))
print("Full_Status: "..full_status)
else
print("Status(F): "..status)
end


and it throws valid errors from the Discord API. I went into https.lua and tried to add this:

Quote:
url.headers["Authorization"] = "Bot Mzc0MzI5MjEwNjk4ODU4NDk2.DOJuvQ.vEPpApkI51qgXQeINp7ywqvJol4"


after line 121, but that didn't do anything.

Ironically, the ONLY call that I need to make will be to Discord so if the headers were "hard coded" that would be fine.

I need to do something like this call: (only through https)
Quote:
--local request_body = json.encode({email="<<myemail>>", password = "<<mypassword>>"})
--local response_body = {}

--local res, code, response_headers, status = http.request{
-- url = "https://discordapp.com/api/auth/login",
-- headers =
-- {
-- ["Content-Type"] = "application/json",
-- ["Content-Length"] = string.len(request_body),
-- ["Authorization"] = "Bot <<bot token>>";
-- },
-- source = ltn12.source.string(request_body),
-- sink = ltn12.sink.table(response_body),
--}


I'm almost there though. The fact that I get a 200 response from https://www.google.com is great progress!!

Thanks again for the help so far! Once I figure out how to add the custom headers I should be real close to solving my problem!
Top

Posted by Fiendish   USA  (2,534 posts)  Bio   Global Moderator
Date Reply #6 on Tue 07 Nov 2017 03:43 AM (UTC)

Amended on Tue 07 Nov 2017 03:54 AM (UTC) by Fiendish

Message
Ah. I don't do anything with custom headers.
But if I were going to, I would change both network_thread_code and request in async.lua to do more than just make a default request.
Right now async's request only passes through the URL to the thread. You'd need to also pass in the headers you want and then push them on through to the socketeer in network_thread_code.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Coloradoderek   (5 posts)  Bio
Date Reply #7 on Tue 07 Nov 2017 04:59 PM (UTC)
Message
I got it working! MAN what a relief. I ended up not needing to do custom headers after all. Here is what I had to add in order for me to be able to send custom POST content through HTTPS using your stuff (I put the changes I made in bold):

Quote:
async.lua
function request(url, protocol, body)
local thread = _llthreads.new(network_thread_code, {url=url, protocol=protocol, body=body})
thread:start()
return thread
end

Inside the network_thread_code = string.dump section:

local page, status, headers, full_status = _socketeer.request(args.url, args.body)

When calling the thread to be created (from my Lua scripts)

url = "https://discordapp.com/api/webhooks/<<discord info>>"
body = "content=ThisIsMessageFour"
if async_ok and not thread then
thread = async.request(url, "HTTPS", body)
end


With those simple additions, I was able to do what I've spent 10-15 hours trying to myself. THANK YOU!
Top

Posted by Fiendish   USA  (2,534 posts)  Bio   Global Moderator
Date Reply #8 on Tue 07 Nov 2017 06:21 PM (UTC)

Amended on Tue 07 Nov 2017 06:31 PM (UTC) by Fiendish

Message
Awesome. :)
Consider using (amending) async.doAsyncRemoteRequest also, instead of directly calling async.request. It tracks request completion for you.

https://github.com/fiendish/aardwolfclientpackage
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


25,718 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.