- 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.
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
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)
|
|
} |