GijsTimmers / kotnetcli

An easy automated way to log in on Kotnet.
GNU General Public License v3.0
5 stars 4 forks source link

Find a way to determine if kotnetcli is connected to the KotNet network. If this is True, login. It this is False: exit gracefully #20

Closed jovanbulck closed 9 years ago

jovanbulck commented 9 years ago

When netlogin.kuleuven.be server is not responding, it means the user is not on a KotNet network and therefore kotnetcli should exit gracefully with an exit code and information message (if not --quiet) to inform the user

GijsTimmers commented 9 years ago

Done. https://github.com/GijsTimmers/kotnetcli/commit/1b92df571e4b36dc1b47b09af8fc353077c310d2

GijsTimmers commented 9 years ago

Although netlogin.kuleuven.be can't be reached from home via a web browser, it does respond to ping requests. So we need another technique to figure out if the user is on KotNet or not. This may also explain the problems you were having running kotnetcli on-campus.

Some alternatives:

jovanbulck commented 9 years ago

I found something interesting you should test: https://admin.kuleuven.be/icts/english/kotnet/diagnosis

screenshot from 2014-12-06 15 10 06

Note the bold note at the bottom ;-)

GijsTimmers commented 9 years ago

Very interesting, we should check if this works both at home and at kot.

At home:

The server does respond to ping -s 1472 -c 100 134.58.126.3

GijsTimmers commented 9 years ago

KotNet's broadcast addres is 10.92.39.255. Maybe we can use that to determine if we're on the KU Leuven network.

GijsTimmers commented 9 years ago

https://github.com/GijsTimmers/kotnetcli/commit/d3759145e09e272b0da83f10d5c168bad8223564

jovanbulck commented 9 years ago

I can't test now in dev branch since dev still crashes on communicator factory pattern:

Traceback (most recent call last): File "./kotnetcli.py", line 317, in aanstuurderObvArgumenten(argumentenParser()) File "./kotnetcli.py", line 247, in aanstuurderObvArgumenten fab = LoginCommunicatorFabriek() NameError: global name 'LoginCommunicatorFabriek' is not defined

I could create a patch from your commit and apply it to master locally to see if it works, but looking at the code I wonder whether it will because gateway is defined as xx.xx.xx.xx

Then again, ideally we would find a better way as the standard gatway address is that of the NAT router inside kot I think. So it wont be the same on all kots and may clash likely with another home default gateway...

GijsTimmers commented 9 years ago

Hi, could you run this code snippet? (First install netifaces using pip)

import netifaces
print netifaces.gateways()["default"][netifaces.AF_INET][0]

What IP does it return? We need to check this on different kots.

GijsTimmers commented 9 years ago

@Wouter92 could you do the same?

jovanbulck commented 9 years ago

I'm pretty sure it's a bad strategy since one has control over once own default gateway on network setup (so even if most of kotnets setups have the same default gateway its still ugly.) I'll think about something better...

I'll try this evening on kot. If helpful, this is the default gateway in the library (non-kotnet):

10.43.127.254

jovanbulck commented 9 years ago

It's probably easy: instead of pinging netlogin.kuleuven.be we should see if we can open a https connection

nmap -p 443 netlogin.kuleuven.be or so will probably do the job...

GijsTimmers commented 9 years ago

I'm pretty sure it's a bad strategy since one has control over once own default gateway on network setup

I'm not so sure about that, actually. IIRC, KotNet manages the DHCP and thus hands out the IP's . The routers on-kot merely act as a switch.

It's probably easy: instead of pinging netlogin.kuleuven.be we should see if we can open a https connection

Good idea.

jovanbulck commented 9 years ago

I'm not so sure about that, actually. IIRC, KotNet manages the DHCP and thus hands out the IP's . The routers on-kot merely act as a switch.

Yep indeed, I'm not sure neither. I think however the default gateway is the first router in line to send requests to; it can subsequently forward if needed. I"m back in doubt however. You could very well be right...

Anyway: here's the result

10.92.32.1

and the nmap trick above seems to work on kot and not in library :-) So maybe this is the way to go (there will surely be some python support to try set up a TCP connection on port 433 which is https)

Wanimatrix commented 9 years ago

This is my result:

10.92.80.1

GijsTimmers commented 9 years ago

Thank you both for testing this. It seems like Kotnet hands out 10.92.xx.x IP's. Regardless, I think that Jo's solution is better.

From my home:

[gijs@xiphos ~]$ nmap -p 443 netlogin.kuleuven.be

Starting Nmap 6.46 ( http://nmap.org ) at 2015-01-24 13:45 CET
Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn
Nmap done: 1 IP address (0 hosts up) scanned in 3.09 seconds

So, we'll execute a HTTPS request to ensure that we're on the kotnet network.

jovanbulck commented 9 years ago

Try the following code in a python interpreter. It works fine here :-)

import socket

def test():
    print "================ KOTNET CONNECTION TEST ================"
    ## try to open a TCP connection on port 443 with a timeout of 1.5 second
    try:
        sock = socket.create_connection(("netlogin.kuleuven.be", 443), 1.5)
        sock.close()
        print "I could successfully connect to netlogin.kuleuven.be"
    except:
        print "Connection attempt to netlogin.kuleuven.be timed out. Are you on the kotnet network?"

test()
GijsTimmers commented 9 years ago
In [1]: import socket

In [2]: def test():
    print "================ KOTNET CONNECTION TEST ================"
    try:
        sock = socket.create_connection(("netlogin.kuleuven.be", 443), 1.5)
        sock.close()
        print "I could successfully connect to netlogin.kuleuven.be"
    except:
        print "Connection attempt to netlogin.kuleuven.be timed out. Are you on the kotnet network?"

In [3]: test()
================ KOTNET CONNECTION TEST ================
Connection attempt to netlogin.kuleuven.be timed out. Are you on the kotnet network?

Nice code, I really like the lightweight socket import.

jovanbulck commented 9 years ago

ok,

I implemented this in the dev branch, as linked above. I moved the functionality from tools/pinger to browser as it fits more cohesively I think. Moreover this allows us to override the behavior in the DummyBrowser :-)

Should work nicely. If not we reopen this issue