|
| 1 | +package geolite |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/0xJacky/Nginx-UI/internal/geolite" |
| 7 | + "github.com/gin-gonic/gin" |
| 8 | + "github.com/gorilla/websocket" |
| 9 | + "github.com/uozi-tech/cosy/logger" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + StatusInfo = "info" |
| 14 | + StatusError = "error" |
| 15 | + StatusProgress = "progress" |
| 16 | +) |
| 17 | + |
| 18 | +type DownloadProgressResp struct { |
| 19 | + Status string `json:"status"` |
| 20 | + Progress float64 `json:"progress"` |
| 21 | + Message string `json:"message"` |
| 22 | +} |
| 23 | + |
| 24 | +func DownloadGeoLiteDB(c *gin.Context) { |
| 25 | + var upgrader = websocket.Upgrader{ |
| 26 | + CheckOrigin: func(r *http.Request) bool { |
| 27 | + return true |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + // Upgrade HTTP to WebSocket |
| 32 | + ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) |
| 33 | + if err != nil { |
| 34 | + logger.Error(err) |
| 35 | + return |
| 36 | + } |
| 37 | + defer ws.Close() |
| 38 | + |
| 39 | + sendMessage := func(status, message string, progress float64) { |
| 40 | + if err := ws.WriteJSON(DownloadProgressResp{ |
| 41 | + Status: status, |
| 42 | + Progress: progress, |
| 43 | + Message: message, |
| 44 | + }); err != nil { |
| 45 | + logger.Error("Failed to send WebSocket message:", err) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Check if database already exists |
| 50 | + if geolite.DBExists() { |
| 51 | + sendMessage(StatusInfo, "Database already exists, removing old version...", 0) |
| 52 | + // Optionally remove old database here if you want to force re-download |
| 53 | + } |
| 54 | + |
| 55 | + sendMessage(StatusInfo, "Starting download...", 0) |
| 56 | + |
| 57 | + // Download progress channel |
| 58 | + downloadProgressChan := make(chan float64, 100) |
| 59 | + downloadDone := make(chan error, 1) |
| 60 | + |
| 61 | + // Start download in goroutine |
| 62 | + go func() { |
| 63 | + downloadDone <- geolite.DownloadGeoLiteDB(downloadProgressChan) |
| 64 | + }() |
| 65 | + |
| 66 | + // Track download progress (0-50%) |
| 67 | + downloadComplete := false |
| 68 | + for !downloadComplete { |
| 69 | + select { |
| 70 | + case progress := <-downloadProgressChan: |
| 71 | + // Scale download progress to 0-50% |
| 72 | + scaledProgress := progress * 0.5 |
| 73 | + sendMessage(StatusProgress, "Downloading GeoLite2 database...", scaledProgress) |
| 74 | + case err := <-downloadDone: |
| 75 | + if err != nil { |
| 76 | + sendMessage(StatusError, "Download failed: "+err.Error(), 0) |
| 77 | + return |
| 78 | + } |
| 79 | + downloadComplete = true |
| 80 | + sendMessage(StatusInfo, "Download complete", 50) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + sendMessage(StatusInfo, "Decompressing database...", 50) |
| 85 | + |
| 86 | + // Decompress progress channel |
| 87 | + decompressProgressChan := make(chan float64, 100) |
| 88 | + decompressDone := make(chan error, 1) |
| 89 | + |
| 90 | + // Start decompression in goroutine |
| 91 | + go func() { |
| 92 | + decompressDone <- geolite.DecompressGeoLiteDB(decompressProgressChan) |
| 93 | + }() |
| 94 | + |
| 95 | + // Track decompression progress (50-100%) |
| 96 | + decompressComplete := false |
| 97 | + for !decompressComplete { |
| 98 | + select { |
| 99 | + case progress := <-decompressProgressChan: |
| 100 | + // Scale decompress progress to 50-100% |
| 101 | + scaledProgress := 50 + (progress * 0.5) |
| 102 | + sendMessage(StatusProgress, "Decompressing database...", scaledProgress) |
| 103 | + case err := <-decompressDone: |
| 104 | + if err != nil { |
| 105 | + sendMessage(StatusError, "Decompression failed: "+err.Error(), 50) |
| 106 | + return |
| 107 | + } |
| 108 | + decompressComplete = true |
| 109 | + sendMessage(StatusInfo, "Database ready", 100) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + sendMessage(StatusInfo, "GeoLite2 database downloaded and installed successfully", 100) |
| 114 | +} |
0 commit comments