new proxy implementation with Gin framework
- Created a new proxy structure under NewProxy/ using the Gin web framework. - Added main entry point (main.go) to register and run routes. - Implemented new route handlers for: > Status — monitors active downloads and total data transferred. > Forward — handles proxy forwarding logic. > Builds — manages build-related requests.
This commit is contained in:
16
NewProxy/routes/builds.go
Normal file
16
NewProxy/routes/builds.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin" //hellow gin again ;)
|
||||
"newproxy/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func RegisterBuildsRoute(r *gin.Engine) {
|
||||
r.GET("/client/builds.json", buildsHandler)
|
||||
}
|
||||
|
||||
func buildsHandler(c *gin.Context) {
|
||||
utils.FetchLatestRelease()
|
||||
c.JSON(http.StatusOK, utils.LatestRelease)
|
||||
}
|
||||
44
NewProxy/routes/forward.go
Normal file
44
NewProxy/routes/forward.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
) //btw same shitty import gin
|
||||
|
||||
func RegisterForwardRoute(r *gin.Engine) {
|
||||
r.GET("/", forwardHandler)
|
||||
} // main proxy url
|
||||
|
||||
func forwardHandler(c *gin.Context) {
|
||||
targetURL := c.Query("url")
|
||||
if targetURL == "" {
|
||||
c.String(http.StatusBadRequest, "Unsupported request")
|
||||
return
|
||||
}
|
||||
|
||||
ip := c.ClientIP()
|
||||
mu.Lock()
|
||||
activeDownloads[ip] = float64(time.Now().Unix())
|
||||
mu.Unlock()
|
||||
|
||||
resp, err := http.Get(targetURL)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error fetching the requested URL: %v", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
content, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error reading response: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
totalDownloadedBytes += int64(len(content))
|
||||
mu.Unlock()
|
||||
|
||||
c.Data(resp.StatusCode, resp.Header.Get("Content-Type"), content)
|
||||
}
|
||||
50
NewProxy/routes/status.go
Normal file
50
NewProxy/routes/status.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"newproxy/utils"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
activeDownloads = make(map[string]float64)
|
||||
totalDownloadedBytes int64
|
||||
mu sync.Mutex
|
||||
)
|
||||
|
||||
func RegisterStatusRoute(r *gin.Engine) {
|
||||
r.GET("/status", statusHandler)
|
||||
}
|
||||
|
||||
func statusHandler(c *gin.Context) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
timeout := 10.0
|
||||
currentTime := float64(time.Now().Unix())
|
||||
filtered := make(map[string]float64)
|
||||
|
||||
for ip, ts := range activeDownloads {
|
||||
if currentTime-ts < timeout {
|
||||
filtered[ip] = ts
|
||||
}
|
||||
}
|
||||
activeDownloads = filtered
|
||||
|
||||
var totalDownloaded string
|
||||
if totalDownloadedBytes >= 1_000_000_000 {
|
||||
totalDownloaded = fmt.Sprintf("%.2f GB", float64(totalDownloadedBytes)/1_000_000_000)
|
||||
} else {
|
||||
totalDownloaded = fmt.Sprintf("%.2f MB", float64(totalDownloadedBytes)/1_000_000)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"latest_release_version": utils.LatestRelease.ReleaseVersion,
|
||||
"proxy_version": "1.6.2",
|
||||
"active_downloads": len(activeDownloads),
|
||||
"total_downloaded": totalDownloaded,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user