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

io.github.dengchen2020.ip.service.impl.dat.IpDatServiceImpl Maven / Gradle / Ivy

There is a newer version: 0.0.30
Show newest version
package io.github.dengchen2020.ip.service.impl.dat;

import io.github.dengchen2020.ip.model.IpInfo;
import io.github.dengchen2020.ip.service.IpService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;

/**
 * dat查询实现
 *
 * @author dengchen
 * @since 2023/5/5
 */
public class IpDatServiceImpl implements IpService, InitializingBean {

    private IPLocation ipLocation;

    private final String windowsLocation;

    private final String linuxLocation;

    public IpDatServiceImpl(String windowsLocation, String linuxLocation) {
        this.windowsLocation = windowsLocation;
        this.linuxLocation = linuxLocation;
    }

    @Override
    public void afterPropertiesSet() {
        String systemName = System.getProperty("os.name").toLowerCase();
        File file;
        if (systemName.startsWith("windows")) {
            file = new File(windowsLocation);
        } else {
            file = new File(linuxLocation);
        }
        if (file.isFile() && file.exists()) {
            try {
                ipLocation = new IPLocation(Files.readAllBytes(file.toPath()));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return;
        }
        try (InputStream inputStream = getClass().getResourceAsStream("/data_ip.dat")) {
            if (inputStream == null) {
                throw new RuntimeException("未找到ip数据包,请将ip数据包data_ip.dat放置在resources目录下");
            }
            ipLocation = new IPLocation(inputStream.readAllBytes());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public IpInfo getInfo(String ip) {
        if (StringUtils.hasText(ip)) {
            Location location = ipLocation.fetchIPLocation(ip);
            String[] ipInfo1 = location.country.split("\\|");
            String[] ipInfo2 = location.area.split("\\|");
            return new IpInfo()
                    .setIp(ip)
                    .setContinent(getValue(ipInfo1, 0))
                    .setCountry(getValue(ipInfo1, 1))
                    .setProvince(getValue(ipInfo2, 0))
                    .setCity(getValue(ipInfo2, 1))
                    .setDistrict(getValue(ipInfo2, 2))
                    .setZoningCode1(getValue(ipInfo2, 3))
                    .setZoningCode2(getValue(ipInfo2, 4))
                    .setZoningCode3(getValue(ipInfo2, 5))
                    .setNationalEnglish(getValue(ipInfo1, 2))
                    .setCountryAbbreviations(getValue(ipInfo1, 3))
                    .setInternationalAreaCode(getValue(ipInfo1, 4))
                    .setIsp(getValue(ipInfo1, 5))
                    .setLongitude(getValue(ipInfo2, 6))
                    .setLatitude(getValue(ipInfo2, 7))
                    ;
        }
        return defaultInfo();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy