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

io.github.dengchen2020.ip.service.impl.xdb.IpXdbServiceImpl Maven / Gradle / Ivy

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

import io.github.dengchen2020.ip.model.IpInfo;
import io.github.dengchen2020.ip.service.IpService;
import org.lionsoul.ip2region.xdb.Searcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.StringUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

/**
 * xdb读取实现
 *
 * @author dengchen
 * @since 2023/5/6
 */
public class IpXdbServiceImpl implements IpService, InitializingBean, DisposableBean {

    private static final Logger log = LoggerFactory.getLogger(IpXdbServiceImpl.class);

    private Searcher searcher;

    private final String windowsLocation;

    private final String linuxLocation;

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

    @Override
    public void afterPropertiesSet() {
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        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 {
                searcher = Searcher.newWithBuffer(Files.readAllBytes(file.toPath()));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return;
        }
        try (InputStream inputStream = getClass().getResourceAsStream("/china.xdb")) {
            if (inputStream == null) {
                throw new RuntimeException("未找到ip数据包,请将ip数据包china.xdb放置在resources目录下");
            }
            searcher = Searcher.newWithBuffer(inputStream.readAllBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public IpInfo getInfo(String ip) {
        if (StringUtils.hasText(ip)) {
            try {
                String[] ipInfo = searcher.search(ip).split("\\|");
                if (ipInfo.length > 0) {
                    return new IpInfo()
                            .setIp(ip)
                            .setContinent(getValue(ipInfo, 0))
                            .setCountry(getValue(ipInfo, 1))
                            .setProvince(getValue(ipInfo, 2))
                            .setCity(getValue(ipInfo, 3))
                            .setDistrict(getValue(ipInfo, 4))
                            .setZoningCode1(getValue(ipInfo, 5))
                            .setZoningCode2(getValue(ipInfo, 6))
                            .setZoningCode3(getValue(ipInfo, 7))
                            .setNationalEnglish(getValue(ipInfo, 8))
                            .setCountryAbbreviations(getValue(ipInfo, 9))
                            .setInternationalAreaCode(getValue(ipInfo, 10))
                            .setIsp(getValue(ipInfo, 11))
                            .setLongitude(getValue(ipInfo, 12))
                            .setLatitude(getValue(ipInfo, 13))
                            ;
                }
            } catch (Exception e) {
                log.error("获取ip信息失败:{}", e.getMessage());
            }
        }
        return defaultInfo();
    }

    @Override
    public void destroy() {
        try {
            searcher.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy