1
无
This commit is contained in:
251
modules/ImportDataBase.go
Normal file
251
modules/ImportDataBase.go
Normal file
@@ -0,0 +1,251 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func ImportDatabaseToString() {
|
||||
mysql.RegisterLocalFile("C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Blocks-IPv4-string.csv")
|
||||
db, err := sql.Open("mysql", "root:990821@tcp(192.168.2.20:3306)/checkip")
|
||||
if err != nil {
|
||||
fmt.Println("数据库连接错误:", err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
//创建表
|
||||
CreateTable := `
|
||||
CREATE TABLE geolite2_city_ipv4_string (
|
||||
network_start_ip VARCHAR(16) NOT NULL,
|
||||
network_last_ip VARCHAR(16) NOT NULL,
|
||||
geoname_id INT,
|
||||
registered_country_geoname_id INT,
|
||||
represented_country_geoname_id INT,
|
||||
is_anonymous_proxy BOOL,
|
||||
is_satellite_provider BOOL,
|
||||
postal_code TEXT,
|
||||
latitude FLOAT,
|
||||
longitude FLOAT,
|
||||
accuracy_radius INT,
|
||||
INDEX (network_start_ip),
|
||||
INDEX (network_last_ip)
|
||||
);
|
||||
`
|
||||
//导入数据
|
||||
sqlStatement := `
|
||||
LOAD DATA LOCAL INFILE 'C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Blocks-IPv4-string.csv'
|
||||
INTO TABLE geolite2_city_ipv4_string
|
||||
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS
|
||||
(@network_start_ip, @network_last_ip, @geoname_id, @registered_country_geoname_id, @represented_country_geoname_id,
|
||||
@is_anonymous_proxy, @is_satellite_provider, @postal_code, @latitude, @longitude, @accuracy_radius)
|
||||
SET network_start_ip = (@network_start_ip),
|
||||
network_last_ip = (@network_last_ip),
|
||||
geoname_id = NULLIF(@geoname_id, ''),
|
||||
registered_country_geoname_id = NULLIF(@registered_country_geoname_id, ''),
|
||||
represented_country_geoname_id = NULLIF(@represented_country_geoname_id, ''),
|
||||
is_anonymous_proxy = NULLIF(@is_anonymous_proxy, ''),
|
||||
is_satellite_provider = NULLIF(@is_satellite_provider, ''),
|
||||
postal_code = NULLIF(@postal_code, ''),
|
||||
latitude = NULLIF(@latitude, ''),
|
||||
longitude = NULLIF(@longitude, ''),
|
||||
accuracy_radius = NULLIF(@accuracy_radius, '')
|
||||
`
|
||||
// 执行SQL语句
|
||||
_, err = db.Exec(CreateTable)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("创建表geolite2_city_ipv4_string完成")
|
||||
_, err = db.Exec(sqlStatement)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("表geolite2_city_ipv4_string数据加载完成")
|
||||
}
|
||||
|
||||
func ImportDatabaseToHex() {
|
||||
mysql.RegisterLocalFile("C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Blocks-IPv4-hex.csv")
|
||||
db, err := sql.Open("mysql", "root:990821@tcp(192.168.2.20:3306)/checkip")
|
||||
if err != nil {
|
||||
fmt.Println("数据库连接错误:", err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
//创建表geolite2_city_ipv4_hex
|
||||
CreateTable := `
|
||||
CREATE TABLE geolite2_city_ipv4_hex (
|
||||
network_start_hex varbinary(16) not null,
|
||||
network_last_hex varbinary(16) not null,
|
||||
geoname_id INT,
|
||||
registered_country_geoname_id INT,
|
||||
represented_country_geoname_id INT,
|
||||
is_anonymous_proxy BOOL,
|
||||
is_satellite_provider BOOL,
|
||||
postal_code TEXT,
|
||||
latitude FLOAT,
|
||||
longitude FLOAT,
|
||||
accuracy_radius INT,
|
||||
INDEX (network_start_hex),
|
||||
INDEX (network_last_hex)
|
||||
);
|
||||
`
|
||||
//导入十六进制数据
|
||||
sqlStatement := `
|
||||
LOAD DATA LOCAL INFILE 'C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Blocks-IPv4-hex.csv'
|
||||
INTO TABLE geolite2_city_ipv4_hex
|
||||
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS
|
||||
(@network_start_hex, @network_last_hex, @geoname_id, @registered_country_geoname_id, @represented_country_geoname_id,
|
||||
@is_anonymous_proxy, @is_satellite_provider, @postal_code, @latitude, @longitude, @accuracy_radius)
|
||||
SET network_start_hex = unhex(@network_start_hex),
|
||||
network_last_hex = unhex(@network_last_hex),
|
||||
geoname_id = NULLIF(@geoname_id, ''),
|
||||
registered_country_geoname_id = NULLIF(@registered_country_geoname_id, ''),
|
||||
represented_country_geoname_id = NULLIF(@represented_country_geoname_id, ''),
|
||||
is_anonymous_proxy = NULLIF(@is_anonymous_proxy, ''),
|
||||
is_satellite_provider = NULLIF(@is_satellite_provider, ''),
|
||||
postal_code = NULLIF(@postal_code, ''),
|
||||
latitude = NULLIF(@latitude, ''),
|
||||
longitude = NULLIF(@longitude, ''),
|
||||
accuracy_radius = NULLIF(@accuracy_radius, '')
|
||||
`
|
||||
// 执行SQL语句
|
||||
_, err = db.Exec(CreateTable)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("创建表geolite2_city_ipv4_hex完成")
|
||||
_, err = db.Exec(sqlStatement)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("表geolite2_city_ipv4_hex数据加载完成")
|
||||
}
|
||||
|
||||
func ImportLocation_zhCN() {
|
||||
mysql.RegisterLocalFile("C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Locations-zh-CN.csv")
|
||||
db, err := sql.Open("mysql", "root:990821@tcp(192.168.2.20:3306)/checkip")
|
||||
if err != nil {
|
||||
fmt.Println("数据库连接错误:", err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
//创建表
|
||||
CreateTable := `
|
||||
CREATE TABLE geolite2_city_locations_zh_cn (
|
||||
geoname_id int not null,
|
||||
locale_code text not null,
|
||||
continent_code text not null,
|
||||
continent_name text not null,
|
||||
country_iso_code text,
|
||||
country_name text,
|
||||
subdivision_1_iso_code text,
|
||||
subdivision_1_name text,
|
||||
subdivision_2_iso_code text,
|
||||
subdivision_2_name text,
|
||||
city_name text,
|
||||
metro_code int,
|
||||
time_zone text,
|
||||
is_in_european_union bool,
|
||||
primary key (geoname_id, locale_code(5))
|
||||
);
|
||||
`
|
||||
//导入geolite2-city-locations_zh_cn位置信息数据
|
||||
sqlStatement := `LOAD DATA LOCAL INFILE 'C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Locations-zh-CN.csv'
|
||||
into table geolite2_city_locations_zh_cn
|
||||
fields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows (
|
||||
geoname_id, locale_code, continent_code, continent_name,
|
||||
@country_iso_code, @country_name, @subdivision_1_iso_code, @subdivision_1_name,
|
||||
@subdivision_2_iso_code, @subdivision_2_name, @city_name, @metro_code, @time_zone,
|
||||
is_in_european_union
|
||||
)
|
||||
set country_iso_code = nullif(@country_iso_code, ''),
|
||||
country_name = nullif(@country_name, ''),
|
||||
subdivision_1_iso_code = nullif(@subdivision_1_iso_code, ''),
|
||||
subdivision_1_name = nullif(@subdivision_1_name, ''),
|
||||
subdivision_2_iso_code = nullif(@subdivision_2_iso_code, ''),
|
||||
subdivision_2_name = nullif(@subdivision_2_name, ''),
|
||||
city_name = nullif(@city_name, ''),
|
||||
metro_code = nullif(@metro_code, ''),
|
||||
time_zone = nullif(@time_zone, '');
|
||||
`
|
||||
// 执行SQL语句
|
||||
_, err = db.Exec(CreateTable)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("创建表geolite2_city_locations_zh_cn完成")
|
||||
_, err = db.Exec(sqlStatement)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("表geolite2_city_locations_zh_cn数据加载完成")
|
||||
}
|
||||
|
||||
func ImportLocation_en() {
|
||||
mysql.RegisterLocalFile("C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Locations-en.csv")
|
||||
db, err := sql.Open("mysql", "root:990821@tcp(192.168.2.20:3306)/checkip")
|
||||
if err != nil {
|
||||
fmt.Println("数据库连接错误:", err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
//创建表
|
||||
CreateTable := `
|
||||
CREATE TABLE geolite2_city_locations_en (
|
||||
geoname_id int not null,
|
||||
locale_code text not null,
|
||||
continent_code text not null,
|
||||
continent_name text not null,
|
||||
country_iso_code text,
|
||||
country_name text,
|
||||
subdivision_1_iso_code text,
|
||||
subdivision_1_name text,
|
||||
subdivision_2_iso_code text,
|
||||
subdivision_2_name text,
|
||||
city_name text,
|
||||
metro_code int,
|
||||
time_zone text,
|
||||
is_in_european_union bool,
|
||||
primary key (geoname_id, locale_code(5))
|
||||
);
|
||||
`
|
||||
//导入geolite2_city_locations_en位置信息数据
|
||||
sqlStatement := `LOAD DATA LOCAL INFILE 'C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Locations-en.csv'
|
||||
into table geolite2_city_locations_en
|
||||
fields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows (
|
||||
geoname_id, locale_code, continent_code, continent_name,
|
||||
@country_iso_code, @country_name, @subdivision_1_iso_code, @subdivision_1_name,
|
||||
@subdivision_2_iso_code, @subdivision_2_name, @city_name, @metro_code, @time_zone,
|
||||
is_in_european_union
|
||||
)
|
||||
set country_iso_code = nullif(@country_iso_code, ''),
|
||||
country_name = nullif(@country_name, ''),
|
||||
subdivision_1_iso_code = nullif(@subdivision_1_iso_code, ''),
|
||||
subdivision_1_name = nullif(@subdivision_1_name, ''),
|
||||
subdivision_2_iso_code = nullif(@subdivision_2_iso_code, ''),
|
||||
subdivision_2_name = nullif(@subdivision_2_name, ''),
|
||||
city_name = nullif(@city_name, ''),
|
||||
metro_code = nullif(@metro_code, ''),
|
||||
time_zone = nullif(@time_zone, '');
|
||||
`
|
||||
// 执行SQL语句
|
||||
_, err = db.Exec(CreateTable)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("创建表geolite2_city_locations_en完成")
|
||||
_, err = db.Exec(sqlStatement)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("表geolite2_city_locations_en数据加载完成")
|
||||
}
|
||||
33
modules/MmdbQuery.go
Normal file
33
modules/MmdbQuery.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"github.com/IncSW/geoip2"
|
||||
"net"
|
||||
)
|
||||
|
||||
type IpInfo struct {
|
||||
Ip string `json:"ip"`
|
||||
CountryIsoCode string `json:"Country_Iso_Code"`
|
||||
CountryName string `json:"Country_Name"`
|
||||
City string `json:"City"`
|
||||
TimeZone string `json:"Time_Zone"`
|
||||
}
|
||||
|
||||
func MmdbQueryIP(ip string) IpInfo {
|
||||
reader, err := geoip2.NewCityReaderFromFile("./db/GeoLite2-City.mmdb")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
record, err := reader.Lookup(net.ParseIP(ip))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
GetIpInfo := IpInfo{
|
||||
Ip: ip,
|
||||
CountryIsoCode: record.Country.ISOCode,
|
||||
CountryName: record.Country.Names["en"],
|
||||
City: record.City.Names["en"],
|
||||
TimeZone: record.Location.TimeZone,
|
||||
}
|
||||
return GetIpInfo
|
||||
}
|
||||
73
modules/MysqlQuery.go
Normal file
73
modules/MysqlQuery.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
)
|
||||
|
||||
type IpInformation struct {
|
||||
Ip string `json:"ip"`
|
||||
CountryIsoCode string `json:"Country_Iso_Code"`
|
||||
CountryName string `json:"Country_Name"`
|
||||
City string `json:"City"`
|
||||
TimeZone string `json:"Time_Zone"`
|
||||
}
|
||||
|
||||
func MysqlQuery(ip string) IpInformation {
|
||||
// 连接到MySQL数据库
|
||||
db, err := sql.Open("mysql", "root:990821@tcp(192.168.2.20:3306)/checkip")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// 执行查询语句
|
||||
query := `
|
||||
SELECT country_iso_code, country_name, city_name,time_zone
|
||||
FROM (
|
||||
SELECT *
|
||||
FROM geolite2_city_ipv4_string
|
||||
WHERE ? between network_start_ip and network_last_ip
|
||||
LIMIT 1
|
||||
) net
|
||||
LEFT JOIN geolite2_city_locations_en ON net.geoname_id = geolite2_city_locations_en.geoname_id;
|
||||
`
|
||||
rows, err := db.Query(query, ip)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var IpInformatin IpInformation
|
||||
// 处理查询结果
|
||||
for rows.Next() {
|
||||
var (
|
||||
country_iso_code string
|
||||
country_name string
|
||||
city_name string
|
||||
time_zone string
|
||||
)
|
||||
err := rows.Scan(&country_iso_code, &country_name, &city_name, &time_zone)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
IpInformatin = IpInformation{
|
||||
Ip: ip,
|
||||
CountryIsoCode: country_iso_code,
|
||||
CountryName: country_name,
|
||||
City: city_name,
|
||||
TimeZone: time_zone,
|
||||
}
|
||||
// 打印结果
|
||||
//fmt.Println("country_iso_code=", country_iso_code)
|
||||
//fmt.Println("country_name=", country_name)
|
||||
//fmt.Println("city_name=", city_name)
|
||||
//fmt.Println("time_zone=", time_zone)
|
||||
}
|
||||
// 检查是否有错误发生
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Print("tag:mysqlquery=", IpInformatin)
|
||||
return IpInformatin
|
||||
}
|
||||
Reference in New Issue
Block a user