forked from TruthHun/http-transfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (56 loc) · 1.22 KB
/
main.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"crypto/tls"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/astaxie/beego/httplib"
"github.com/gin-gonic/gin"
)
type TransferConfig struct {
Port int `json:"port"`
Timeout int `json:"timeout"`
}
const (
timeout = 60 * time.Second
defaultPort = 8080
)
func main() {
args := os.Args[1:]
port := defaultPort
if len(args) == 1 {
port, _ = strconv.Atoi(args[0])
if port <= 0 {
port = defaultPort
}
}
app := gin.Default()
app.GET("/*request", transfer)
app.Run(":" + strconv.Itoa(port))
}
func transfer(ctx *gin.Context) {
if ctx.Request.Method != http.MethodGet {
ctx.AbortWithStatus(http.StatusNotFound)
return
}
requestURL := strings.TrimLeft(ctx.Request.RequestURI, " /")
req := httplib.Get(requestURL).SetTimeout(timeout, timeout*120)
if strings.HasPrefix(requestURL, "https://") {
req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
}
for k, v := range ctx.Request.Header {
req.Header(k, v[0])
}
if u, err := url.Parse(requestURL); err == nil {
req.Header("host", u.Host)
}
resp, err := req.Bytes()
if err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{"error": err.Error(), "request": requestURL})
return
}
ctx.Writer.Write(resp)
}