I know that there are many online tools available, but I want to write in a home tool, links, or how to do this? Or can this even be done, or is it a gigantic task?
You need a database of locations for all IP addresses. Thus, you create the database yourself (truly a mammouth job) or use an external service.
Some IP location services have an API, so you can call them from within your code.
This is pretty easy to do.
check this resource
http://www.ip2location.com/html5geolocationapi.aspx