-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
143 lines (115 loc) · 3.04 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Package proxy implements a proxy that forward HTTP requests.
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
var startTime time.Time
// GetPathMapping returns a key/value map of the PATH_MAPPING env var.
func GetPathMapping() map[string]string {
pathMappingValue := os.Getenv("PATH_MAPPING")
var pathMapping map[string]string
json.Unmarshal([]byte(pathMappingValue), &pathMapping)
return pathMapping
}
// GetURLPathPrefix returns the first directory if it exists for a given URL.
func GetURLPathPrefix(requestedURL string) string {
urlParts := strings.Split(requestedURL, "/")
if len(urlParts) > 0 {
return urlParts[0]
}
return ""
}
// NormalizeURL adds a scheme to a given URL without scheme.
func NormalizeURL(rawURL string) string {
if !strings.Contains(rawURL, "http") {
return "http://" + rawURL
}
return rawURL
}
// IsValidURL validates URL format and ensure host contains a dot.
func IsValidURL(rawURL string) bool {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return false
}
if !strings.Contains(parsedURL.Host, ".") {
return false
}
return true
}
// Index handler process all the incoming HTTP requests.
func Index(c *gin.Context) {
// Forward to Status page when homepage is requested
if c.Request.URL.Path == "/" {
Status(c)
return
}
// Get the requested URL path and trim the / prefix
requestedURL := strings.TrimPrefix(c.Request.URL.String(), "/")
urlPathPrefix := GetURLPathPrefix(requestedURL)
pathMapping := GetPathMapping()
if host, ok := pathMapping[urlPathPrefix]; ok {
requestedURL = strings.Replace(requestedURL, urlPathPrefix+"/", "", 1)
// Forward to 404 page when URL path prefix matches the requested URL
if urlPathPrefix == requestedURL {
Error(c, http.StatusNotFound)
return
}
requestedURL = host + "/" + requestedURL
}
// Add default HTTP scheme if not provided
requestedURL = NormalizeURL(requestedURL)
// Ensure we forward valid URLs
if !IsValidURL(requestedURL) {
Error(c, http.StatusNotFound)
return
}
resp, err := http.Get(requestedURL)
if err != nil {
Error(c, http.StatusNotFound)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Error(c, http.StatusNotFound, err)
return
}
c.Header("Content-Type", resp.Header.Get("Content-Type"))
c.String(resp.StatusCode, string(body))
}
// Status returns the current time in a JSON object
func Status(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"name": "proxy",
"startedAt": startTime,
"uptime": time.Since(startTime).Seconds(),
})
}
// Error wrapper centralizes the error responses logic.
func Error(c *gin.Context, statusCode int, err ...error) {
c.JSON(statusCode, gin.H{
"code": statusCode,
"message": http.StatusText(statusCode),
"url": c.Request.URL.String(),
})
if err != nil {
log.Print(err)
}
}
func main() {
godotenv.Load()
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.GET("/*path", Index)
router.Run()
}