-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
262 lines (228 loc) · 6.48 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"bytes"
"compress/flate"
"compress/gzip"
"context"
"github.com/andybalholm/brotli"
"golang.org/x/net/html"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"strconv"
"strings"
)
func init() {
log.Default().SetOutput(os.Stdout)
log.Default().SetFlags(log.LstdFlags | log.Lshortfile)
}
const (
// SpringInitializer Spring 官方的地址
SpringInitializer = "https://start.spring.io/"
// BaiduStatistics 百度统计JS代码
BaiduStatistics = `
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?01a8b83d4f38d7c890e8dbcaa8e661d3";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
`
// About 关于我们
About = `
To Pivotal
Hello, due to the firewall blocking in China, most Chinese users are not able to use the official Spring Initializr service, so I created this proxy service, specifically for spring users in mainland China. Please don't block me, thank you very much.
Email: [email protected]
`
)
func main() {
// 代理的地址
targetURL, err := url.Parse(SpringInitializer)
if err != nil {
log.Fatalf("SpringInitializer 地址解析异常: %s\n", err.Error())
}
reverseProxy := httputil.NewSingleHostReverseProxy(targetURL)
reverseProxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, err error) {
log.Printf("ErrorHandler Error: %s\n", err.Error())
}
reverseProxy.ModifyResponse = func(response *http.Response) (err error) {
// 异常响应
if response.StatusCode != http.StatusOK {
return nil
}
// 仅仅修改主页
if response.Request.RequestURI != "/" {
return nil
}
contentType := response.Header.Get("Content-Type")
// 忽略非 html 响应
if contentType == "" || !strings.HasPrefix(strings.ToLower(strings.TrimSpace(contentType)), "text/html") {
return nil
}
//// 忽略空响应
//if response.ContentLength < 1 {
// return nil
//}
contentEncoding := response.Header.Get("Content-Encoding")
var bodyReader io.Reader
switch contentEncoding {
case "br":
bodyReader = brotli.NewReader(response.Body)
case "deflate":
bodyReader = flate.NewReader(response.Body)
case "gzip":
{
bodyReader, err = gzip.NewReader(response.Body)
if err != nil {
return err
}
}
case "":
{
bodyReader = response.Body
}
default:
log.Printf("未知的Content-Encoding,直接返回: %s\n", contentEncoding)
return nil
}
// ---------------- 解析html ----------------
document, err := html.Parse(bodyReader)
if err != nil {
log.Printf("HTML解析异常: %s\n", err.Error())
return err
}
defer func() {
_ = response.Body.Close()
}()
// 删除body前面的3个script标签,官方用于统计的,我这里用不着
//bodyNode := GetNode(document, func(node *html.Node) bool {
// return node.Type == html.ElementNode && node.Data == "body"
//})
//
//var scriptNodes []*html.Node
//for node := bodyNode.LastChild; node != nil; node = node.PrevSibling {
// if node.Type == html.ElementNode && node.Data == "script" {
// scriptNodes = append(scriptNodes, node)
// }
//}
//for _, v := range scriptNodes {
// bodyNode.RemoveChild(v)
//}
// head标签
headNode := GetNode(document, func(node *html.Node) bool {
return node.Type == html.ElementNode && node.Data == "head"
})
// 修改 description
descriptionNode := GetNode(headNode, func(node *html.Node) bool {
if node.Type == html.ElementNode && node.Data == "meta" {
for _, v := range node.Attr {
if strings.EqualFold(v.Key, "name") && strings.EqualFold(v.Val, "description") {
return true
}
}
}
return false
})
if descriptionNode != nil {
for i := range descriptionNode.Attr {
if descriptionNode.Attr[i].Key == "content" {
descriptionNode.Attr[i].Val = "快速构建你的 spring boot 应用。"
}
}
}
// 插入Keywords
headNode.InsertBefore(&html.Node{
Type: html.ElementNode,
Data: "meta",
Attr: []html.Attribute{{
Key: "content",
Val: "Spring Initializr, spring boot 脚手架",
}, {
Key: "name",
Val: "keywords",
}},
}, descriptionNode)
// 自定义脚本
headNode.AppendChild(&html.Node{
Type: html.ElementNode,
DataAtom: 0,
Data: "script",
Attr: []html.Attribute{{
Key: "src",
Val: "https://guohuihui.gitee.io/guohui-blog/spring/spring.js",
}},
})
// 插入百度统计代码
headNode.AppendChild(&html.Node{
FirstChild: &html.Node{
Type: html.TextNode,
Data: BaiduStatistics,
},
Type: html.ElementNode,
Data: "script",
})
// 渲染到内存
buf := &bytes.Buffer{}
if err = html.Render(buf, document); err != nil {
log.Printf("HTML渲染异常: %s\n", err.Error())
return err
}
response.Header.Del("Content-Encoding")
response.Header.Set("Content-Length", strconv.Itoa(buf.Len()))
response.Body = io.NopCloser(buf)
return nil
}
router := http.NewServeMux()
router.HandleFunc("/about", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
_, _ = io.WriteString(writer, About)
})
router.HandleFunc("/robots.txt", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
_, _ = io.WriteString(writer, "User-agent: *")
})
// Proxy
router.HandleFunc("/", reverseProxy.ServeHTTP)
server := &http.Server{
Addr: ":8080",
Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
request.Host = targetURL.Host
request.Header.Set("X-User-Agent", "https://start.springboot.io/about")
router.ServeHTTP(writer, request)
}),
}
go func() {
log.Println("Server Start")
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
<-ctx.Done()
if err := server.Shutdown(context.Background()); err != nil {
log.Printf("Server Shutdown Error: %s\n", err.Error())
}
}()
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server ListenAndServe Error: %s\n", err.Error())
}
log.Println("Bye")
}
func GetNode(node *html.Node, test func(*html.Node) bool) *html.Node {
if test(node) {
return node
}
if node.FirstChild != nil {
if ret := GetNode(node.FirstChild, test); ret != nil {
return ret
}
}
if node.NextSibling != nil {
if ret := GetNode(node.NextSibling, test); ret != nil {
return ret
}
}
return nil
}