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?