from remote-desktop il y a 4 mois
Parent
commit
4da787b2a0
1 fichiers modifiés avec 20 ajouts et 5 suppressions
  1. 20 5
      src/api/user.go

+ 20 - 5
src/api/user.go

@@ -2,6 +2,7 @@ package api
 
 import (
 	"bytes"
+	"compress/gzip"
 	"encoding/json"
 	"fmt"
 	"io"
@@ -15,7 +16,6 @@ const CASDOOEN_HOST = "https://auth.3dqueen.cloud"
 // forwardRequestWithJWT forwards the request to target URL with JWT token and original request data
 func forwardRequestWithJWT(c *gin.Context, targetURL string) (interface{}, error) {
 	jwtToken := c.GetHeader("Authorization")
-	// fmt.Println(jwtToken)
 
 	// Create new request
 	var req *http.Request
@@ -44,7 +44,6 @@ func forwardRequestWithJWT(c *gin.Context, targetURL string) (interface{}, error
 	}
 
 	// Set JWT token
-
 	req.Header.Set("Authorization", jwtToken)
 
 	// Set content type if it's a POST request
@@ -52,6 +51,9 @@ func forwardRequestWithJWT(c *gin.Context, targetURL string) (interface{}, error
 		req.Header.Set("Content-Type", "application/json")
 	}
 
+	// Accept gzip encoding
+	req.Header.Set("Accept-Encoding", "gzip")
+
 	// Send request
 	client := &http.Client{}
 	resp, err := client.Do(req)
@@ -60,8 +62,21 @@ func forwardRequestWithJWT(c *gin.Context, targetURL string) (interface{}, error
 	}
 	defer resp.Body.Close()
 
+	// Read response body, handling gzip if necessary
+	var reader io.ReadCloser
+	switch resp.Header.Get("Content-Encoding") {
+	case "gzip":
+		reader, err = gzip.NewReader(resp.Body)
+		if err != nil {
+			return nil, fmt.Errorf("create gzip reader error: %v", err)
+		}
+		defer reader.Close()
+	default:
+		reader = resp.Body
+	}
+
 	// Read response
-	respBody, err := io.ReadAll(resp.Body)
+	respBody, err := io.ReadAll(reader)
 	if err != nil {
 		return nil, fmt.Errorf("read response error: %v", err)
 	}
@@ -69,10 +84,10 @@ func forwardRequestWithJWT(c *gin.Context, targetURL string) (interface{}, error
 	// Parse response JSON
 	var result interface{}
 	if err := json.Unmarshal(respBody, &result); err != nil {
-		return nil, fmt.Errorf("parse response error: %v", err)
+		// If JSON parsing fails, return the raw response as string
+		return string(respBody), nil
 	}
 	return result, nil
-
 }
 
 // 转发请求到https://auth.3dqueen.cloud/api/get-account