-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.go
46 lines (38 loc) · 1.03 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)
func getImage(c *gin.Context) {
targetURL := c.Query("q")
if targetURL == "" {
c.String(http.StatusBadRequest, "?q=<target url> is required")
return
}
client := &http.Client{}
req, err := http.NewRequest("GET", targetURL, nil)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
c.String(http.StatusBadRequest, fmt.Sprintf("status code is %d", resp.StatusCode))
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
contentType := http.DetectContentType(body)
c.Data(http.StatusOK, contentType, body)
}