DownloadGeoLite2DB 下载GeoLite2-Country.mmdb文件
(filePath string)
| 426 | |
| 427 | // DownloadGeoLite2DB 下载GeoLite2-Country.mmdb文件 |
| 428 | func 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数据库,失败时不报错 |
| 467 | func TryDownloadGeoLite2DB(filePath string) bool { |
no test coverage detected