All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.net.support.ComboIPSearcher Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.net.support;

import com.alibaba.fastjson.JSONObject;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.rx.core.App;
import org.rx.core.Strings;
import org.rx.core.Tasks;
import org.rx.exception.InvalidException;
import org.rx.io.KeyValueStore;
import org.rx.io.KeyValueStoreConfig;
import org.rx.net.Sockets;
import org.rx.net.http.HttpClient;

import java.net.InetAddress;
import java.util.function.Predicate;

import static org.rx.core.Extends.eq;

class ComboIPSearcher implements IPSearcher {
    final KeyValueStore store = new KeyValueStore<>(KeyValueStoreConfig.defaultConfig("./data/ip"));

    @Override
    public IPAddress current() {
        return search(Sockets.loopbackAddress().getHostAddress());
    }

    @SneakyThrows
    @Override
    public IPAddress search(@NonNull String ip) {
        if (ip.equals(Sockets.loopbackAddress().getHostAddress())) {
            return Tasks.randomRetry(() -> ip_Api(ip), () -> ipGeo(ip),
                    () -> ipData(ip), () -> ipApi(ip));
        }

        return store.computeIfAbsent(ip, k -> Tasks.randomRetry(() -> ip_Api(ip), () -> ipGeo(ip),
                () -> ipData(ip), () -> ipApi(ip)));
    }

    private JSONObject getJson(String url, Predicate check) {
        HttpClient client = new HttpClient();
        String text = client.get(url).toString();
        if (Strings.isEmpty(text)) {
            throw new InvalidException("Empty response from {}", url);
        }
        JSONObject json = App.toJsonObject(text);
        if (!check.test(json)) {
            throw new InvalidException("Request:\t{}\n" + "Response:\t{}", url, text);
        }
        return json;
    }

    IPAddress ip_Api(String ip) {
        if (ip.equals(Sockets.loopbackAddress().getHostAddress())) {
            ip = Strings.EMPTY;
        }
        String url = String.format("http://ip-api.com/json/%s", ip);
        JSONObject json = getJson(url, p -> eq(p.getString("status"), "success"));

        return new IPAddress(json.getString("query"), json.getString("country"), json.getString("countryCode"), json.getString("city"),
                json.getString("isp"),
                String.format("%s %s", json.getString("as"), json.getString("org")));
    }

    //1.5k/d
    @SneakyThrows
    IPAddress ipData(String ip) {
        if (ip.equals(Sockets.loopbackAddress().getHostAddress())) {
            ip = Strings.EMPTY;
        } else if (!Sockets.isValidIp(ip)) {
            ip = InetAddress.getByName(ip).getHostAddress();
        }
        String url = String.format("https://api.ipdata.co/%s?api-key=cbf82d3beb9d0591285d73210d518d99920fb7d50dc2e5a24e9c599a", ip);
        JSONObject json = getJson(url, p -> p.getString("country_name") != null);

        String isp = null;
        JSONObject connection = json.getJSONObject("asn");
        if (connection != null) {
            isp = String.format("%s %s", connection.getString("asn"), connection.getString("name"));
        }
        return new IPAddress(json.getString("ip"), json.getString("country_name"), json.getString("country_code"), json.getString("city"),
                isp, null);
    }

    //1k/d
    IPAddress ipGeo(String ip) {
        if (ip.equals(Sockets.loopbackAddress().getHostAddress())) {
            ip = Strings.EMPTY;
        }
        String url = String.format("https://api.ipgeolocation.io/ipgeo?apiKey=e96493e9280e4a4fae1b8744ad688272&ip=%s", ip);
        JSONObject json = getJson(url, p -> p.getString("country_name") != null);

        return new IPAddress(json.getString("ip"), json.getString("country_name"), json.getString("country_code2"), json.getString("city"),
                json.getString("isp"), json.getString("organization"));
    }

    //1k/m
    IPAddress ipApi(String ip) {
        if (ip.equals(Sockets.loopbackAddress().getHostAddress())) {
            ip = "check";
        }
        String url = String.format("http://api.ipapi.com/%s?access_key=8da5fe816dba52150d4c40ba72705954", ip);
        JSONObject json = getJson(url, p -> p.getString("country_name") != null);

        String isp = null;
        JSONObject connection = json.getJSONObject("connection");
        if (connection != null) {
            isp = String.format("%s %s", connection.getString("asn"), connection.getString("isp"));
        }
        return new IPAddress(json.getString("ip"), json.getString("country_name"), json.getString("country_code"), json.getString("city"),
                isp, null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy