0

I've been using Lua for many months and I feel like this is a very terrible language that is not easy to solve common problem. I'm using Debian 11 and I see that I already have lua installed like the following:

$ lua

Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio

So, now I'm going ahead trying to run a simple code from ipify.org to fetch my public IP:

I created a file called ip.lua with the following content that I got from ipify.org how to call this using lua:

http.Fetch("https://api.ipify.org", function(body) print("My ip is: " .. body ) end

Then I chmod +x ip.lua

Now, I run it with this command:

lua ip.lua

And I got the following error:

lua: ip.lua:2: ')' expected (to close '(' at line 1) near <eof>

I don't know what does that mean. My aim is to use ipify.org to get public IP from my computer. So this guy has an example code for this here:

So, I use the following code (I was expecting copy and paste the following will work):

local url = require "socket.url"
local http = require "socket.http"
print("----------EXTERNAL IP DIRECT---------------")
local result, status = http.request("http://api.ipify.org/")
print(result, status)
print("---------EXTERNAL IP VIA PROXY-------------")
http.PROXY="http://192.168.102.134:8888/" -- locally hosted http proxy, no name/password
local result1, status1 = http.request("http://api.ipify.org/")
print(result1, status1)

but I got the following error:

lua: ./socket.lua:12: module 'socket.core' not found:
    no field package.preload['socket.core']
    no file '/usr/local/share/lua/5.3/socket/core.lua'
    no file '/usr/local/share/lua/5.3/socket/core/init.lua'
    no file '/usr/local/lib/lua/5.3/socket/core.lua'
    no file '/usr/local/lib/lua/5.3/socket/core/init.lua'
    no file './socket/core.lua'
    no file './socket/core/init.lua'
    no file '/usr/local/lib/lua/5.3/socket/core.so'
    no file '/usr/local/lib/lua/5.3/loadall.so'
    no file './socket/core.so'
    no file '/usr/local/lib/lua/5.3/socket.so'
    no file '/usr/local/lib/lua/5.3/loadall.so'
    no file './socket.so'
stack traceback:
    [C]: in function 'require'
    ./socket.lua:12: in main chunk
    [C]: in function 'require'
    ./socket/url.lua:13: in main chunk
    [C]: in function 'require'
    ip.lua:1: in main chunk
    [C]: in ?

Of course I searched about this error and it said that I need to install luasocket. So I did the following:

apt -y install luarocks

luarocks install luasocket

and rerun the above code, still I got the same error. So, how do you even run a simple curl in lua?

What is more frustrated is, I also tried to install this curl library in order to call simple curl:

luarocks install Lua-cURL

Installing https://luarocks.org/lua-curl-0.3.13-1.src.rock

and got the following error:

Error: Could not find header file for CURL
  No file curl/curl.h in /usr/local/include
  No file curl/curl.h in /usr/include
You may have to install CURL in your system and/or pass CURL_DIR or CURL_INCDIR to the luarocks command.

    Example: luarocks install lua-curl CURL_DIR=/usr/local

I already have curl installed in /usr/bin/curl but it could not find it. So I did following what it said to solve this:

luarocks install Lua-cURL CURL_DIR=/usr/local

But I still got the same error. How would you run the simple API from ipify.org using lua?

Kalib Zen
  • 655
  • 1
  • 5
  • 16

1 Answers1

0

This is really multiple questions in one, but I'll do what I can to answer.

lua: ip.lua:2: ')' expected (to close '(' at line 1) near <eof>

<eof> means End of File, so that means you have an open parenthesis, and the parser hit the end of file without seeing the closing parenthesis. Looking at the code, I see that the http.Fetch call is missing its closing parenthesis.

I don't know what's going on with the luasocket library. I've looked at its source code, and I can't find a core module anywhere, so I guess it must be a bug in luasocket. Its luarocks page shows the latest version as a 2-year old release candidate, so maybe they're not maintaining it that well.

As for the curl issue, you have the curl executable installed, but lua-curl needs a header file. Debian tends to package headers separately from binaries and libraries, which can lead to dependency hell. After running apt search libcurl | less, my best guess for the package you need is libcurl4-openssl-dev. Try sudo apt install libcurl4-openssl-dev.

EDIT: I did a search for curl/curl.h, and I found this article, which suggests libcurl4-nss-dev rather than the openssl version.

EDIT2: I searched the Debian packages for "curl/curl.h" and got this result, which shows where the header is actually installed. Assuming you're on x86_64, you can try this command or some variation of it:

luarocks install lua-curl CURL_INCDIR=/usr/include/x86_64-linux-gnu

These commands I'm giving you are untested, because I don't want to install all this stuff.

luther
  • 5,195
  • 1
  • 14
  • 24
  • But the code was published officially by the `ipify.org`, So how do you fix the code to run the simple command ? So, the bug you mean that I cannot do anything to run that? For the curl thing, actually I already have that installed previously: `libcurl4-openssl-dev is already the newest version (7.74.0-1.3+b1).` So nothing of your answer is working. – Kalib Zen Sep 27 '21 at 02:46
  • What code do you mean? When I go to ipify.org, I see nothing but a short string of hex code. – luther Sep 27 '21 at 04:24
  • When you go to ipfy.org, you type in LUA (in search bar), there are bunch of codes below it. Regarding to your edit answer about installing `libcurl4-nss-dev`. I did install that but I still got error saying ` Could not find header file for CURL`. – Kalib Zen Sep 27 '21 at 14:50
  • Like I said, the page at http://ipify.org/ is completely blank except for the hex numbers, which I guess are some form of my IP address. It's plain text, and not HTML. Can you just post a link to the page where you got whatever code you're referring to? – luther Sep 27 '21 at 16:25
  • That is weird, what browser are you using? This website is not a plaintext site. Something wrong with your browser. You can try using this generated proxy site: https://eu6.proxysite.com/process.php?d=32XCesa8EMENn6LCHMPYdiWq&b=1&f=norefer. The link is the frontpage of the website, it is just `ipify.org`, the code is at the bottom of the page. No other links. The site is using javascript for link. – Kalib Zen Sep 27 '21 at 17:35
  • 1
    I think I understand the issue now. U gotta use `www.ipify.org`. So, you need to include `www`. Weird that my browser Chrome was automatically added `www` when I navigate to `ipify.org` . Sorry about that – Kalib Zen Sep 27 '21 at 17:38
  • Ok, got it. They put some bad Lua code on their website. People make mistakes. You can fix it by adding the `)`, as I explained in my answer. They also don't define `http`. Since it's not in standard Lua, they must want you to find it in Luarocks and `require` it. As for luasocket, I may be missing something since I don't use that library, but as far as I can tell, it's broken. – luther Sep 27 '21 at 17:59
  • I added the `)`, it does not work. It looks like that they don't care about this Lua at all. – Kalib Zen Sep 28 '21 at 09:54
  • The hex numbers are your IPv6 address. You can specify IPv4 or IPv6 with https://api.ident.me, eg https://v4.ident.me should return your IPv4 address. – Pierre Carrier Feb 26 '22 at 17:59