mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 16:21:11 +08:00
24w06b
This commit is contained in:
parent
93c3175206
commit
a8d524739b
1 changed files with 14 additions and 14 deletions
|
|
@ -35,7 +35,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||||
|
|
||||||
rawPath = "https://" + matches[2]
|
rawPath = "https://" + matches[2]
|
||||||
|
|
||||||
matches = checkURL(rawPath)
|
matches = CheckURL(rawPath)
|
||||||
if matches == nil {
|
if matches == nil {
|
||||||
c.String(http.StatusForbidden, "Invalid input.")
|
c.String(http.StatusForbidden, "Invalid input.")
|
||||||
return
|
return
|
||||||
|
|
@ -57,10 +57,10 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||||
switch {
|
switch {
|
||||||
case exps[0].MatchString(rawPath), exps[1].MatchString(rawPath), exps[3].MatchString(rawPath), exps[4].MatchString(rawPath):
|
case exps[0].MatchString(rawPath), exps[1].MatchString(rawPath), exps[3].MatchString(rawPath), exps[4].MatchString(rawPath):
|
||||||
logw("%s Matched - USE proxy-chrome", rawPath)
|
logw("%s Matched - USE proxy-chrome", rawPath)
|
||||||
proxyRequest(c, rawPath, cfg, "chrome")
|
ProxyRequest(c, rawPath, cfg, "chrome")
|
||||||
case exps[2].MatchString(rawPath):
|
case exps[2].MatchString(rawPath):
|
||||||
logw("%s Matched - USE proxy-git", rawPath)
|
logw("%s Matched - USE proxy-git", rawPath)
|
||||||
proxyRequest(c, rawPath, cfg, "git")
|
ProxyRequest(c, rawPath, cfg, "git")
|
||||||
default:
|
default:
|
||||||
c.String(http.StatusForbidden, "Invalid input.")
|
c.String(http.StatusForbidden, "Invalid input.")
|
||||||
return
|
return
|
||||||
|
|
@ -68,7 +68,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func proxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
|
func ProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
|
||||||
method := c.Request.Method
|
method := c.Request.Method
|
||||||
logw("%s Method: %s", u, method)
|
logw("%s Method: %s", u, method)
|
||||||
|
|
||||||
|
|
@ -85,7 +85,7 @@ func proxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
|
||||||
|
|
||||||
body, err := io.ReadAll(c.Request.Body)
|
body, err := io.ReadAll(c.Request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(c, fmt.Sprintf("Failed to read request body: %v", err))
|
HandleError(c, fmt.Sprintf("Failed to read request body: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer c.Request.Body.Close()
|
defer c.Request.Body.Close()
|
||||||
|
|
@ -98,26 +98,26 @@ func proxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := sendRequest(req, method, u)
|
resp, err := SendRequest(req, method, u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if err := handleResponseSize(resp, cfg, c); err != nil {
|
if err := HandleResponseSize(resp, cfg, c); err != nil {
|
||||||
logw("Error handling response size: %v", err)
|
logw("Error handling response size: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
copyResponseHeaders(resp, c, cfg)
|
CopyResponseHeaders(resp, c, cfg)
|
||||||
c.Status(resp.StatusCode)
|
c.Status(resp.StatusCode)
|
||||||
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
|
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
|
||||||
logw("Failed to copy response body: %v", err)
|
logw("Failed to copy response body: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendRequest(req *req.Request, method, url string) (*req.Response, error) {
|
func SendRequest(req *req.Request, method, url string) (*req.Response, error) {
|
||||||
switch method {
|
switch method {
|
||||||
case "GET":
|
case "GET":
|
||||||
return req.Get(url)
|
return req.Get(url)
|
||||||
|
|
@ -132,7 +132,7 @@ func sendRequest(req *req.Request, method, url string) (*req.Response, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleResponseSize(resp *req.Response, cfg *config.Config, c *gin.Context) error {
|
func HandleResponseSize(resp *req.Response, cfg *config.Config, c *gin.Context) error {
|
||||||
contentLength := resp.Header.Get("Content-Length")
|
contentLength := resp.Header.Get("Content-Length")
|
||||||
if contentLength != "" {
|
if contentLength != "" {
|
||||||
size, err := strconv.Atoi(contentLength)
|
size, err := strconv.Atoi(contentLength)
|
||||||
|
|
@ -146,7 +146,7 @@ func handleResponseSize(resp *req.Response, cfg *config.Config, c *gin.Context)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyResponseHeaders(resp *req.Response, c *gin.Context, cfg *config.Config) {
|
func CopyResponseHeaders(resp *req.Response, c *gin.Context, cfg *config.Config) {
|
||||||
headersToRemove := []string{"Content-Security-Policy", "Referrer-Policy", "Strict-Transport-Security"}
|
headersToRemove := []string{"Content-Security-Policy", "Referrer-Policy", "Strict-Transport-Security"}
|
||||||
|
|
||||||
for _, header := range headersToRemove {
|
for _, header := range headersToRemove {
|
||||||
|
|
@ -166,12 +166,12 @@ func copyResponseHeaders(resp *req.Response, c *gin.Context, cfg *config.Config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleError(c *gin.Context, message string) {
|
func HandleError(c *gin.Context, message string) {
|
||||||
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
|
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
|
||||||
logw(message)
|
logw(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkURL(u string) []string {
|
func CheckURL(u string) []string {
|
||||||
for _, exp := range exps {
|
for _, exp := range exps {
|
||||||
if matches := exp.FindStringSubmatch(u); matches != nil {
|
if matches := exp.FindStringSubmatch(u); matches != nil {
|
||||||
logw("URL matched: %s, Matches: %v", u, matches[1:])
|
logw("URL matched: %s, Matches: %v", u, matches[1:])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue