This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Windows10User's profile picture

ANOTHER problem with my internet system

Started by Windows10User, 10 May 2018 - 04:58 PM
Windows10User #1
Posted 10 May 2018 - 06:58 PM
I finally got my DNS and browser to work as they should. However, when the browser sends a request to the web server (to fetch the page), it doesn't work. The server receives the request for the page (the domain name + the name of the page) but doesn't seem to react to it and I know that because the browser won't stop waiting for the request!

Browser:

local modem = peripheral.find("modem")

if modem == nil then
   error("No modem found!", 0)
end

if not modem.isWireless() then
   error("Modem isn't wireless!")
end

print("Type the DNS ID:")
local dnschannel = tonumber(read())
print("Type the domain name:")
local domain = read()
print("Sending request. Press any key to cancel.")

modem.open(dnschannel)
modem.transmit(dnschannel, os.getComputerID(), string.match(domain, "[^/]+"))

local event, mSide, sChannel, rChannel, msg, dist = os.pullEvent("modem_message")
if msg == "FAIL" then
   error("DNS error.", 0)
else
   print(domain)
   modem.transmit(tonumber(msg), os.getComputerID(), domain)
   local event, mSide, sChannel, rChannel, msg, dist = os.pullEvent("modem_message")
   shell.run("clear")
   print("Contents:")
   print(msg)
end

Web server:

local domain = "something.com"

local modem = peripheral.find("modem")

if modem == nil then
	error("No modem found!", 0)
end

if not modem.isWireless() then
   error("Modem isn't wireless!")
end

modem.open(os.getComputerID())

while true do
   local event, mSide, sChannel, rChannel, msg, dist = os.pullEvent("modem_message")
   if fs.exists(string.sub(msg, string.len(domain) + 2)) then
	  print(string.sub(msg, string.len(domain) + 2))
	  local fPage = fs.open(string.sub(msg, string.len(domain) + 2), "r")
	  local page = fPage.readAll()
	  fPage.close()
	  modem.transmit(rChannel, os.getComputerID(), page)
   else
	  modem.transmit(rChannel, os.getComputerID(), "404 - Page Not Found")
   end
end

P. S. automatic code indentation WHEN
Edited on 10 May 2018 - 04:59 PM
Bomb Bloke #2
Posted 11 May 2018 - 06:13 AM
Here you're intending to have each computer send to a channel equal to the other's ID, and request that replies be sent back on a channel equal to their own ID.

But in your "browser" script, you have this:

modem.open(dnschannel)

The server doesn't send back to that channel, hence why the browser never sees a response from it. You're wanting both systems to call modem.open() with os.getComputerID() as the parameter.

Note that you don't have to use the computer ID numbers for your transmissions - that's a convention used by the rednet API (which is a wrapper for the modem's API you're using here), but there's nothing stopping you from just picking some random number like 23,456 and then hard-coding your systems to communicate on that channel. It'd save you from needing to enter your "dnschannel" all the time.
Windows10User #3
Posted 11 May 2018 - 06:17 AM
Note that you don't have to use the computer ID numbers for your transmissions - that's a convention used by the rednet API (which is a wrapper for the modem's API you're using here), but there's nothing stopping you from just picking some random number like 23,456 and then hard-coding your systems to communicate on that channel. It'd save you from needing to enter your "dnschannel" all the time.

I know, I can use 65535 if I wanted, but I'm using IDs for security sake, if you understand. :)/> And to prevent conflicts between the 3 systems.
Edited on 11 May 2018 - 04:18 AM
Marc1miner #4
Posted 13 May 2018 - 08:53 AM
Note that you don't have to use the computer ID numbers for your transmissions - that's a convention used by the rednet API (which is a wrapper for the modem's API you're using here), but there's nothing stopping you from just picking some random number like 23,456 and then hard-coding your systems to communicate on that channel. It'd save you from needing to enter your "dnschannel" all the time.

I know, I can use 65535 if I wanted, but I'm using IDs for security sake, if you understand. :)/> And to prevent conflicts between the 3 systems.

If you want to secure at all time you should look at Diffie-Hellman key exchange, https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange
It basically does the folowing:
Both the browser and the website generate a random number
They send their public key over rednet for example.
With their public key they make one private key, and that could be any number form 0 - 65535
and that would be your protocol
It is extremely safe because you generate a new key every time, so it is very hard to guess for someone else to guess what protocol you're using.

How can you implement this?

function keyEchange(g, p)
local g = g -- g is the base, the server and browser have the same
local p = p -- p is the modulo, the server and browser have the same
local a = math.randomseed(os.time())
a = math.random(1,100)
local key = (g^a) % p    -- the server does the same calculation
rednet.open("top")
rednet.send(1, key)
local id, pKey = rednet.receive() -- pKey is the received public key
rednet.close("top")
local sKey = (pKey^a) % p -- sKey is the secret key
return sKey
end


Hopefully you find this useful!

By the way, is there anyway I can contact you more quickly as more as a coop?