Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef39ee7

Browse files
committed
外网iframe嵌入
1 parent 1f04a6f commit ef39ee7

File tree

7 files changed

+80
-6
lines changed

7 files changed

+80
-6
lines changed

websites/code/studygolang/src/controller/index.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ package controller
99
import (
1010
"math/rand"
1111
"net/http"
12+
"net/url"
13+
"strings"
1214

15+
"config"
1316
"filter"
17+
"logger"
1418
"model"
1519
"service"
20+
"util"
1621
)
1722

1823
// 首页
@@ -52,3 +57,56 @@ func IndexHandler(rw http.ResponseWriter, req *http.Request) {
5257
// 设置模板数据
5358
filter.SetData(req, map[string]interface{}{"topics": newTopics, "articles": recentArticles, "likeflags": likeFlags, "resources": resources[start:end]})
5459
}
60+
61+
// 包装链接
62+
func WRHandler(rw http.ResponseWriter, req *http.Request) {
63+
tUrl := req.FormValue("u")
64+
if tUrl == "" {
65+
util.Redirect(rw, req, "/")
66+
return
67+
}
68+
69+
if pUrl, err := url.Parse(tUrl); err != nil {
70+
util.Redirect(rw, req, tUrl)
71+
return
72+
} else {
73+
if !pUrl.IsAbs() {
74+
util.Redirect(rw, req, tUrl)
75+
return
76+
}
77+
78+
// 本站
79+
if strings.Contains(pUrl.Host, config.Config["domain"]) {
80+
util.Redirect(rw, req, tUrl)
81+
return
82+
}
83+
84+
// 检测是否禁止了 iframe 加载
85+
// 看是否在黑名单中
86+
for _, denyHost := range strings.Split(config.Config["iframe_deny"], ",") {
87+
if strings.Contains(pUrl.Host, denyHost) {
88+
util.Redirect(rw, req, tUrl)
89+
return
90+
}
91+
}
92+
93+
// 检测会比较慢,进行异步检测,记录下来,以后分析再加黑名单
94+
go func() {
95+
resp, err := http.Head(tUrl)
96+
if err != nil {
97+
logger.Errorln("[iframe] head url:", tUrl, "error:", err)
98+
return
99+
}
100+
defer resp.Body.Close()
101+
if resp.Header.Get("X-Frame-Options") != "" {
102+
logger.Errorln("[iframe] deny:", tUrl)
103+
return
104+
}
105+
}()
106+
}
107+
108+
// 设置内容模板
109+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/wr.html")
110+
// 设置模板数据
111+
filter.SetData(req, map[string]interface{}{"url": tUrl})
112+
}

websites/code/studygolang/src/filter/view.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func (this *ViewFilter) PostFilter(rw http.ResponseWriter, req *http.Request) bo
170170
strings.Contains(req.RequestURI, "project") ||
171171
strings.HasPrefix(req.RequestURI, "/p/") ||
172172
strings.Contains(req.RequestURI, "reading") ||
173+
strings.HasPrefix(req.RequestURI, "/wr") ||
173174
req.RequestURI == "/" ||
174175
strings.Contains(req.RequestURI, "search") {
175176
this.commonHtmlFiles = []string{config.ROOT + "/template/common/layout.html"}

websites/code/studygolang/src/server/studygolang/router.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func initRouter() *mux.Router {
3232
router.FilterChain(fontFilterChan)
3333

3434
router.HandleFunc("/", IndexHandler)
35+
router.HandleFunc("/wr", WRHandler)
36+
3537
router.HandleFunc("/topics{view:(|/popular|/no_reply|/last)}", TopicsHandler)
3638
router.HandleFunc("/topics/{tid:[0-9]+}", TopicDetailHandler)
3739
router.HandleFunc("/topics/new{json:(|.json)}", NewTopicHandler).AppendFilterChain(loginFilterChain)

websites/code/studygolang/template/articles/detail.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ <h1>{{.article.Title}}</h1>
5353
</div>
5454
<!-- content END -->
5555
<div class="row orig-info">
56-
<p>本文来自:<a href="http://{{.article.Domain}}" target="_blank" title="{{.article.Name}}">{{.article.Name}}</a></p>
57-
<p>查看原文:<a href="{{.article.Url}}" target="_blank" title="{{.article.Title}}">{{.article.Title}}</a></p>
56+
<p>本文来自:<a href="/wr?u=http://{{.article.Domain}}" target="_blank" title="{{.article.Name}}">{{.article.Name}}</a></p>
57+
<p>查看原文:<a href="/wr?u={{.article.Url}}" target="_blank" title="{{.article.Title}}">{{.article.Title}}</a></p>
5858
</div>
5959
<div class="active row">
6060
<div class="mark-like-btn col-md-8">
@@ -161,6 +161,13 @@ <h3><span class="cmtnum">{{.article.Cmtnum}}</span>条评论</h3>
161161
$('.need-autogrow').autoGrow();
162162

163163
loadComments();
164+
165+
// 文章链接,在本站的 iframe 中打开
166+
$('#wrapper .content').on('mousedown', 'a', function(evt){
167+
var url = $(this).attr('href');
168+
$(this).attr('href', '/wr?u='+url);
169+
$(this).attr('target', '_blank');
170+
});
164171
});
165172
</script>
166173
{{end}}

websites/code/studygolang/template/common/layout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
</div>
9191
</div>
9292
</header>
93-
<div class="wrapper">
93+
<div class="wrapper" id="wrapper">
9494
<div class="container" role="main">
9595
{{template "content" .}}
9696
</div>

websites/code/studygolang/template/readings/list.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
{{define "title"}}网友博文{{end}}
2-
{{define "seo"}}<meta name="keywords" content="Go语言博客文章, Go语言博文, Go语言, Golang, Go中文社区,Golang中文社区, Go语言社区, Go语言学习园地">
3-
<meta name="description" content="文章来自国内知名网站,Go爱好者学习笔记,经验分享;也有爱好者自己的博客文章,已经一些国外精彩文章。">{{end}}
1+
{{define "title"}}技术晨读 {{end}}
2+
{{define "seo"}}<meta name="keywords" content="技术晨读, Go语言技术晨读, Go语言, Golang, Go中文社区,Golang中文社区, Go语言社区, Go语言中文网, Go语言学习园地">
3+
<meta name="description" content="Go语言中文网每天给gopher们分享技术,每天一篇技术晨读,和大家一起进步">{{end}}
44
{{define "content"}}
55
<div class="row banner">
66
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{define "title"}}首页{{end}}
2+
{{define "seo"}}<meta name="keywords" content="Go语言,Golang,Go社区,Go中文社区,Golang中文社区,Go语言社区,Go语言学习,学习Go语言,Go语言学习园地,Golang 中国,Golang中国,Golang China, Go语言论坛">
3+
<meta name="description" content="Go语言中文网,中国 Golang 社区,Go语言学习园地,致力于构建完善的 Golang 中文社区,Go语言爱好者的学习家园。分享 Go 语言知识,交流使用经验">{{end}}
4+
{{define "content"}}
5+
<iframe src="{{.url}}" id="frmEx" width="100%" frameborder="0" framemargin="0" height="12000" scrolling="no" name="frmEx"></iframe>
6+
{{end}}

0 commit comments

Comments
 (0)