Skip to content

Commit

Permalink
Move IP2Location token to an environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreeeee committed Jul 21, 2023
1 parent a533e95 commit 8d28b17
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 16 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ Check out [DEVELOPMENT_INSTALL.md](https://github.com/Oreeeee/yokkaichi/blob/mas
### Usage
When starting the script for the first time, `yokkaichi.toml` will get created. You will have to adjust it to your preferences. Optionally, you can also pass in `-c` to set a different name or location of the config file.

### How to get geolocating to work?
Yokkaichi uses IP2Location LITE for anything geolocation related. This includes getting the location of the server, and generating the CIDR ranges for scanning. It is a offline, free to use download, so there are no rate limits. However, the database is not redistributed with Yokkaichi, due to IP2Location updating their LITE databases every month. Instead, it will be downloaded and updated automatically everytime you run this script for the first time in the month. To get the downloading to work, you need to have an IP2Location LITE Download Token. To get one, follow these steps.

1. Go to [IP2Location LITE](https://lite.ip2location.com/) website and register (or log into) an account (which is completly free forever).
2. Click on the name in the upper right corner and select "Database Download".
3. Copy your Download Token.
4. Set the `IP2LOCATION_LITE_TOKEN` environment variable to your token.
5. Now, you will be able to use the automatic downloads feature.

### Using the masscan integration
You need to have [masscan](https://github.com/robertdavidgraham/masscan) in your PATH, or in the same directory from which you are running this software. Make sure that the binary is named `masscan` (Unix) or `masscan.exe` (Windows).
1 change: 0 additions & 1 deletion example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ output = "out.json" # IMPORTANT! That's where the servers go!
[ip2location]
enabled = false # Enable getting the location of the server
databases_location = "ip2location_dbs/" # Where are the databases stored
token = "" # Insert your download token here for automatic updates
bin_filename = "IP2LOCATION-LITE-DB11.BIN"
csv_filename = "IP2LOCATION-LITE-DB1.CSV"
bin_code = "DB11LITEBIN" # Avoid changing this
Expand Down
15 changes: 8 additions & 7 deletions yokkaichi/IP2L_Manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
import pathlib
import platform
import time
import urllib
import urllib.request
from uuid import uuid4
from zipfile import ZipFile

from IP2Location import IP2Location

from .constants import console
from .enums import IP2LocDBStatus, IP2LocManagerUserAnswers
from .structs import CFG
from .structs import CFG, EnvVariables


class IP2L_Manager:
def __init__(self, cfg: CFG) -> None:
def __init__(self, cfg: CFG, env: EnvVariables) -> None:
self.cfg = cfg

self.ip2l_dbs: str = self.cfg.ip2location_dbs
self.env: EnvVariables = env

# Try to open the last updated date file
opening_status: IP2LocDBStatus = self.open_last_updated_file()
Expand Down Expand Up @@ -147,9 +148,9 @@ def get_country_cidr(self) -> list:
return ip_list_loc

def download_db(self) -> None:
if self.cfg.ip2location_token == "":
if self.env.ip2location_lite_token == None:
console.print(
"To automatically download IP2Location database, a IP2Location LITE token must be provided! Either provide it in the config, or use manual update.",
"To automatically download IP2Location database, a IP2Location LITE token must be provided! Either set the IP2LOCATION_LITE_TOKEN environment variable, or use manual update.",
style="red",
)
exit(1)
Expand All @@ -161,11 +162,11 @@ def download_db(self) -> None:
)

urllib.request.urlretrieve(
f"https://www.ip2location.com/download/?token={self.cfg.ip2location_token}&file={self.cfg.ip2location_bin_code}",
f"https://www.ip2location.com/download/?token={self.env.ip2location_lite_token}&file={self.cfg.ip2location_bin_code}",
db_zips[0],
)
urllib.request.urlretrieve(
f"https://www.ip2location.com/download/?token={self.cfg.ip2location_token}&file={self.cfg.ip2location_csv_code}",
f"https://www.ip2location.com/download/?token={self.env.ip2location_lite_token}&file={self.cfg.ip2location_csv_code}",
db_zips[1],
)

Expand Down
4 changes: 3 additions & 1 deletion yokkaichi/ServerScan.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def check_server(self, ip: str, port: int, server_platform: Platforms) -> None:
if server_platform == Platforms.JAVA:
server_lookup = JavaServer.lookup(f"{ip}:{port}", timeout=self.cfg.timeout)
if server_platform == Platforms.BEDROCK:
server_lookup = BedrockServer.lookup(f"{ip}:{port}", timeout=self.cfg.timeout)
server_lookup = BedrockServer.lookup(
f"{ip}:{port}", timeout=self.cfg.timeout
)

# Get player list
if self.cfg.query_java:
Expand Down
9 changes: 7 additions & 2 deletions yokkaichi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

from yokkaichi import __version__

from . import config_loader
from . import config_loader, env_loader
from .constants import console
from .IP2L_Manager import IP2L_Manager
from .port_parser import parse_port_range
from .ServerScan import ServerScan
from .structs import EnvVariables


def display_version() -> None:
Expand Down Expand Up @@ -63,6 +64,7 @@ def main():
if args.show_version:
# Show the version and exit
display_version()

# Load the config file
if args.config_file != None:
try:
Expand All @@ -85,6 +87,9 @@ def main():
)
exit(0)

# Load environment variables
env_variables: EnvVariables = env_loader.load_env()

# Check does output file exists
if pathlib.Path(cfg.output).is_file():
console.print(
Expand All @@ -100,7 +105,7 @@ def main():

if cfg.use_ip2location:
# Initialize IP2Location
ip2location: IP2L_Manager = IP2L_Manager(cfg)
ip2location: IP2L_Manager = IP2L_Manager(cfg, env_variables)
else:
ip2location: None = None

Expand Down
2 changes: 0 additions & 2 deletions yokkaichi/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
[ip2location]
enabled = false # Enable getting the location of the server
databases_location = "ip2location_dbs/" # Where are the databases stored
token = "" # Insert your download token here for automatic updates
bin_filename = "IP2LOCATION-LITE-DB11.BIN"
csv_filename = "IP2LOCATION-LITE-DB1.CSV"
bin_code = "DB11LITEBIN" # Avoid changing this
Expand Down Expand Up @@ -115,7 +114,6 @@ def parse_cfg(cfg_location):

cfg.use_ip2location = cfg_file["ip2location"]["enabled"]
cfg.ip2location_dbs = cfg_file["ip2location"]["databases_location"]
cfg.ip2location_token = cfg_file["ip2location"]["token"]
cfg.ip2location_db_bin = cfg_file["ip2location"]["bin_filename"]
cfg.ip2location_db_csv = cfg_file["ip2location"]["csv_filename"]
cfg.ip2location_bin_code = cfg_file["ip2location"]["bin_code"]
Expand Down
11 changes: 11 additions & 0 deletions yokkaichi/env_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os

from .structs import EnvVariables


def load_env() -> EnvVariables:
env_vars: EnvVariables = EnvVariables()

env_vars.ip2location_lite_token = os.environ.get("IP2LOCATION_LITE_TOKEN")

return env_vars
1 change: 0 additions & 1 deletion yokkaichi/structs/_CFG.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class CFG:
output: str = field(default_factory=str)
use_ip2location: bool = field(default_factory=bool)
ip2location_dbs: str = field(default_factory=str)
ip2location_token: str = field(default_factory=str)
ip2location_db_bin: str = field(default_factory=str)
ip2location_db_csv: str = field(default_factory=str)
ip2location_bin_code: str = field(default_factory=str)
Expand Down
6 changes: 6 additions & 0 deletions yokkaichi/structs/_EnvVariables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dataclasses import dataclass, field


@dataclass
class EnvVariables:
ip2location_lite_token: str = field(default_factory=str)
1 change: 1 addition & 0 deletions yokkaichi/structs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from ._CFG import CFG
from ._EnvVariables import EnvVariables
from ._MinecraftServer import MinecraftServer
2 changes: 0 additions & 2 deletions yokkaichi/tests/test_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
[ip2location]
enabled = false
databases_location = "ip2location_dbs/"
token = "dummy"
bin_filename = "IP2LOCATION-LITE-DB11.BIN"
csv_filename = "IP2LOCATION-LITE-DB1.CSV"
bin_code = "DB11LITEBIN"
Expand Down Expand Up @@ -75,7 +74,6 @@ def default_cfg(x):
output="out.json",
use_ip2location=False,
ip2location_dbs="ip2location_dbs/",
ip2location_token="dummy",
ip2location_db_bin="IP2LOCATION-LITE-DB11.BIN",
ip2location_db_csv="IP2LOCATION-LITE-DB1.CSV",
ip2location_bin_code="DB11LITEBIN",
Expand Down
16 changes: 16 additions & 0 deletions yokkaichi/tests/test_env_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import yokkaichi.env_loader
from yokkaichi.structs import EnvVariables

FAKE_TOKEN = "dummy"


def test_getting_variables(monkeypatch):
def mock_os_environ_get(var_name: str):
if var_name == "IP2LOCATION_LITE_TOKEN":
return "dummy"

monkeypatch.setattr("os.environ.get", mock_os_environ_get)

assert yokkaichi.env_loader.load_env() == EnvVariables(
ip2location_lite_token=FAKE_TOKEN
)

0 comments on commit 8d28b17

Please sign in to comment.