MCPcopy Create free account
hub / github.com/MengMengCode/GetRealityDomain / DownloadGeoLite2DB

Function DownloadGeoLite2DB

utils.go:428–464  ·  view source on GitHub ↗

DownloadGeoLite2DB 下载GeoLite2-Country.mmdb文件

(filePath string)

Source from the content-addressed store, hash-verified

426
427// DownloadGeoLite2DB 下载GeoLite2-Country.mmdb文件
428func DownloadGeoLite2DB(filePath string) error {
429 // MaxMind的免费GeoLite2数据库下载链接
430 // 注意:这个链接可能需要注册账户才能使用,这里使用一个公开的镜像链接
431 url := "https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-Country.mmdb"
432
433 printInfo("正在下载GeoLite2-Country.mmdb数据库...")
434
435 // 创建HTTP请求
436 resp, err := http.Get(url)
437 if err != nil {
438 return fmt.Errorf("下载请求失败: %v", err)
439 }
440 defer resp.Body.Close()
441
442 // 检查HTTP状态码
443 if resp.StatusCode != http.StatusOK {
444 return fmt.Errorf("下载失败,HTTP状态码: %d", resp.StatusCode)
445 }
446
447 // 创建目标文件
448 file, err := os.Create(filePath)
449 if err != nil {
450 return fmt.Errorf("创建文件失败: %v", err)
451 }
452 defer file.Close()
453
454 // 复制数据到文件
455 _, err = io.Copy(file, resp.Body)
456 if err != nil {
457 // 如果下载失败,删除不完整的文件
458 os.Remove(filePath)
459 return fmt.Errorf("写入文件失败: %v", err)
460 }
461
462 printSuccess(fmt.Sprintf("GeoLite2数据库下载成功: %s", filePath))
463 return nil
464}
465
466// TryDownloadGeoLite2DB 尝试下载GeoLite2数据库,失败时不报错
467func TryDownloadGeoLite2DB(filePath string) bool {

Callers 1

TryDownloadGeoLite2DBFunction · 0.85

Calls 3

printInfoFunction · 0.85
printSuccessFunction · 0.85
CloseMethod · 0.45

Tested by

no test coverage detected