Built your own IPV4 geolocation database
Built with ❤ in Rust
Table of Contents generated with mtoc
- Badges
- Introduction
- How works
- Example
- Using the API
- Local development
- Visualize data
- Import from local mongodb to mongodb atlas
- TO DO
- Pending to fix
- Useful Links
- LICENSE
So imagine that you want to start indexing public IPv4 geolocation data. There are paid services, others free but requiring registration, that offer this type of content. By consulting one of these services you will obtain the data you need.
I do not know exactly all the public services that offer this data, in my case I have used the public database of ip-api.com
Without this external service this tool does not work, it would be necessary to refactor and use another API and map the responses.
Other possible solutions (not implemented):
You will make a request to your API endpoint, for example, curl http://127.0.0.1/api/v1/1.1.1.1
, and the API will first check if the data exists in the database. If it does not exist, it will retrieve the information from the external website mentioned above. The next time you query the same IP, the data will be retrieved from MongoDB, avoiding the external query.
API logs can be retrieved executing
docker logs -f iproxy
once the API is running. Plase visit the next step.
The setup is configured to work with docker-compose locally.
With this file located in the root of this repository, you will be able to change some parameters.
Before start the docker-compose, change the directory where you want to save the mongodb data |
Example, from compose.yml:
mongodb:
....other config
volumes:
- /mnt/ssd/iproxy:/data/db # this line!!
In mi case, I'm mapping all the data using an external SSD mounted in /mnt/ssd
Now, launch all the stack:
docker network create iproxy
make compose-up-build
This will starts the iproxy
container and mongodb
.
curl http://127.0.0.1:8000/api/v1/1.1.1.1
curl http://127.0.0.1:8000/api/v1/health
make compose-down
cargo install cargo-watch systemfd
docker network create iproxy
make local-development
make local-development-down
Using mongodb compass you can visualize your data from the collection ips
Import data from localhost to mongodb atlas
Visit the official webpage https://www.mongodb.com/docs/database-tools/installation/installation/.
mongodump --uri="mongodb://admin:admin@localhost:27017/?maxPoolSize=20&w=majority"
This command will create a new dump/
directory with the backup
The uri of this command is the remote uri (mongodb atlas)
mongorestore --uri="mongodb+srv://USERNAME:PASSWORD@XXXXX.XXXX.mongodb.net/?retryWrites=true&w=majority" --db="ipfinder" --collection="ips" dump/iproxy/ips.bson
- Testing and error handling with custom errors (MyErrors)
- Generate possible public ipv4 https://www.criminalip.io/ip-ranges
- Prometheus metrics
- JWT token
- When the ip is private, the process crashes.
iproxy
is distributed under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
.