-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
233 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ os: | |
- osx | ||
|
||
node_js: | ||
- 11 | ||
- 12 | ||
- 13 | ||
- 14 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,66 @@ | ||
import Ipv4ToRegion from "./ipv4"; | ||
import Ipv4ToRegion, { Ipv4ToRegionResult, Ipv4ToRegionRes } from "./ipv4"; | ||
import Ipv6ToRegion, { Ipv6ToRegionResult, Ipv6ToRegionRes } from "./ipv6"; | ||
import { isIP } from "net"; | ||
|
||
/** IP2Region 配置 */ | ||
export interface IP2RegionOpts { | ||
/** ipv4 数据库地址 */ | ||
ipv4db?: string; | ||
/** ipv6 数据库地址 */ | ||
ipv6db?: string; | ||
/** 关闭 ipv6 */ | ||
disableIpv6?: boolean; | ||
} | ||
|
||
export interface IP2RegionResult { | ||
/** 国家 */ | ||
country: string; | ||
/** 省份 */ | ||
province: string; | ||
/** 城市 */ | ||
city: string; | ||
/** ISP 供应商 */ | ||
isp: string; | ||
} | ||
|
||
export default class IP2Region { | ||
private ipv4: Ipv4ToRegion; | ||
private ipv6: Ipv6ToRegion | null = null; | ||
|
||
constructor(opts: IP2RegionOpts = {}) { | ||
this.ipv4 = new Ipv4ToRegion(opts.ipv4db); | ||
if (!opts.disableIpv6) { | ||
this.ipv6 = new Ipv6ToRegion(opts.ipv6db); | ||
this.ipv6.setIpv4Ins(this.ipv4); | ||
} | ||
} | ||
|
||
search(ipaddr: string, parse = true) { | ||
/** | ||
* 原始搜索 | ||
* @param ipaddr IP 地址 | ||
*/ | ||
searchRaw(ipaddr: string): Ipv6ToRegionResult | Ipv4ToRegionResult | null; | ||
/** | ||
* 原始搜索 | ||
* @param ipaddr IP 地址 | ||
* @param parse 是否解析 | ||
*/ | ||
searchRaw(ipaddr: string, parse: boolean): Ipv4ToRegionRes | Ipv6ToRegionRes | null; | ||
searchRaw(ipaddr: string, parse = true) { | ||
const v = isIP(ipaddr); | ||
if (v === 6 && this.ipv6) { | ||
return this.ipv6.search(ipaddr, parse); | ||
} | ||
return this.ipv4.search(ipaddr, parse); | ||
} | ||
|
||
/** | ||
* 搜索 | ||
* @param ipaddr IP 地址 | ||
*/ | ||
search(ipaddr: string): IP2RegionResult | null { | ||
const res = this.searchRaw(ipaddr); | ||
if (res === null) return res; | ||
return { country: res.country, province: res.province, city: res.city, isp: res.isp }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.