-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (161 loc) · 4.57 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"embed"
"flag"
"io"
"log"
"net/http"
"os"
"regexp"
"strings"
"github.com/gin-gonic/gin"
)
//go:embed index.html favicon.ico
var staticFiles embed.FS
var (
jsdelivr = 0
// whiteList = []string{}
// blackList = []string{}
passList = []string{}
exp1 = regexp.MustCompile(`^(?:https?://)?github\.com/(?P<author>.+?)/(?P<repo>.+?)/(?:releases|archive)/.*$`)
exp2 = regexp.MustCompile(`^(?:https?://)?github\.com/(?P<author>.+?)/(?P<repo>.+?)/(?:blob|raw)/.*$`)
exp3 = regexp.MustCompile(`^(?:https?://)?github\.com/(?P<author>.+?)/(?P<repo>.+?)/(?:info|git-).*$`)
exp4 = regexp.MustCompile(`^(?:https?://)?raw\.(?:githubusercontent|github)\.com/(?P<author>.+?)/(?P<repo>.+?)/.+?/.+$`)
exp5 = regexp.MustCompile(`^(?:https?://)?gist\.(?:githubusercontent|github)\.com/(?P<author>.+?)/.+?/.+$`)
exp6 = regexp.MustCompile(`(\.com/.*?/.+?)/(.+?/)`)
allowAnyUrl bool
)
func main() {
var host string
var port string
flag.StringVar(&host, "host", "0.0.0.0", "Host address")
flag.StringVar(&port, "port", "80", "Port number")
flag.BoolVar(&allowAnyUrl, "allow-any-url", false, "Do not check if the URL is a GitHub URL")
flag.Parse()
hostRegExp := regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+$`)
if !hostRegExp.MatchString(host) {
log.Println("Invalid host: " + host)
os.Exit(1)
}
portRegExp := regexp.MustCompile(`^\d+$`)
if !portRegExp.MatchString(port) {
log.Println("Invalid port: " + port)
os.Exit(1)
}
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.GET("/", func(c *gin.Context) {
q := c.Query("q")
if q != "" {
c.Redirect(http.StatusFound, "/"+q)
return
}
data, err := staticFiles.ReadFile("index.html")
if err != nil {
c.String(http.StatusInternalServerError, "Failed to read index HTML from embed.FS: %v", err)
return
}
c.Data(http.StatusOK, "text/html; charset=utf-8", data)
})
router.GET("/favicon.ico", func(c *gin.Context) {
data, err := staticFiles.ReadFile("favicon.ico")
if err != nil {
c.String(http.StatusInternalServerError, "Failed to read favicon from embed.FS: %v", err)
return
}
c.Data(http.StatusOK, "image/vnd.microsoft.icon", data)
})
router.NoRoute(func(c *gin.Context) {
path := c.Request.URL.Path[1:]
handler(c, path)
})
if allowAnyUrl {
log.Println("Mode: Allow any URL")
} else {
log.Println("Mode: GitHub URL only")
}
log.Println("Starting server at " + host + ":" + port)
err := router.Run(host + ":" + port)
if err != nil {
log.Println("Failed to start server: ", err)
os.Exit(1)
}
}
func handler(c *gin.Context, u string) {
if !strings.HasPrefix(u, "http") {
u = "https://" + u
}
if !strings.HasPrefix(u, "https://") && strings.HasPrefix(u, "https:/") {
u = strings.Replace(u, "https:/", "https://", 1)
}
passBy := false
match, _ := checkURL(u)
if match {
for _, i := range passList {
if strings.Contains(u, i) {
passBy = true
break
}
}
} else if !allowAnyUrl {
c.String(http.StatusForbidden, "Invalid input.")
return
}
if (jsdelivr > 0 || passBy) && exp2.MatchString(u) {
u = strings.Replace(u, "/blob/", "@", 1)
u = strings.Replace(u, "github.com", "cdn.jsdelivr.net/gh", 1)
c.Redirect(http.StatusFound, u)
} else if (jsdelivr > 0 || passBy) && exp4.MatchString(u) {
u = exp6.ReplaceAllString(u, "$1@$2")
u = strings.Replace(u, "raw.githubusercontent.com", "cdn.jsdelivr.net/gh", 1)
c.Redirect(http.StatusFound, u)
} else {
if exp2.MatchString(u) {
u = strings.Replace(u, "/blob/", "/raw/", 1)
}
proxy(c, u)
}
}
func proxy(c *gin.Context, u string) {
client := &http.Client{}
req, err := http.NewRequest(c.Request.Method, u, nil)
if err != nil {
log.Println("Failed to create request: ", err)
c.String(http.StatusInternalServerError, "Server error: %v", err)
return
}
copyHeader(c.Request.Header, &req.Header)
resp, err := client.Do(req)
if err != nil {
log.Println("Failed to send request: ", err)
c.String(http.StatusInternalServerError, "Server error: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
c.Status(resp.StatusCode)
return
}
header := c.Writer.Header()
copyHeader(resp.Header, &header)
c.Status(resp.StatusCode)
io.Copy(c.Writer, resp.Body)
}
func copyHeader(src http.Header, dest *http.Header) {
for k, vv := range src {
for _, v := range vv {
dest.Add(k, v)
}
}
}
func checkURL(u string) (bool, []string) {
var allExps = []*regexp.Regexp{
exp1, exp2, exp3, exp4, exp5,
}
for _, exp := range allExps {
if exp.MatchString(u) {
return true, exp.FindStringSubmatch(u)
}
}
return false, nil
}