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

Skip to content

Commit a30ae40

Browse files
committed
https 对 wr 的处理
1 parent 14883fa commit a30ae40

File tree

6 files changed

+61
-43
lines changed

6 files changed

+61
-43
lines changed

src/http/controller/account.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (self AccountController) Register(ctx echo.Context) error {
121121
</div>`),
122122
}
123123

124-
isHttps := goutils.MustBool(ctx.Request().Header().Get("X-Https"))
124+
isHttps := CheckIsHttps(ctx)
125125
// 需要检验邮箱的正确性
126126
go logic.DefaultEmail.SendActivateMail(email, uuid, isHttps)
127127

@@ -137,7 +137,7 @@ func (self AccountController) Register(ctx echo.Context) error {
137137

138138
// SendActivateEmail 发送注册激活邮件
139139
func (self AccountController) SendActivateEmail(ctx echo.Context) error {
140-
isHttps := goutils.MustBool(ctx.Request().Header().Get("X-Https"))
140+
isHttps := CheckIsHttps(ctx)
141141

142142
uuid := ctx.FormValue("uuid")
143143
if uuid != "" {
@@ -271,7 +271,7 @@ func (self AccountController) Edit(ctx echo.Context) error {
271271

272272
email := ctx.FormValue("email")
273273
if me.Email != email {
274-
isHttps := goutils.MustBool(ctx.Request().Header().Get("X-Https"))
274+
isHttps := CheckIsHttps(ctx)
275275
go logic.DefaultEmail.SendActivateMail(email, RegActivateCode.GenUUID(email), isHttps)
276276
}
277277

@@ -345,7 +345,7 @@ func (AccountController) ForgetPasswd(ctx echo.Context) error {
345345
emailUrl = "http://mail." + email[pos+1:]
346346
}
347347

348-
isHttps := goutils.MustBool(ctx.Request().Header().Get("X-Https"))
348+
isHttps := CheckIsHttps(ctx)
349349
data["success"] = template.HTML(`一封包含了重设密码链接的邮件已经发送到您的注册邮箱,按照邮件中的提示,即可重设您的密码。<a href="` + emailUrl + `" target="_blank">立即前往邮箱</a>`)
350350
go logic.DefaultEmail.SendResetpwdMail(email, uuid, isHttps)
351351
} else {

src/http/controller/app/user.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"model"
1212

1313
"github.com/labstack/echo"
14-
"github.com/polaris1119/goutils"
1514

1615
. "http"
1716
. "http/internal/helper"
@@ -91,7 +90,7 @@ func (UserController) Modify(ctx echo.Context) error {
9190

9291
email := ctx.FormValue("email")
9392
if me.Email != email {
94-
isHttps := goutils.MustBool(ctx.Request().Header().Get("X-Https"))
93+
isHttps := checkIsHttps(ctx)
9594
go logic.DefaultEmail.SendActivateMail(email, RegActivateCode.GenUUID(email), isHttps)
9695
}
9796

src/http/controller/comment.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"net/http"
1414
"strconv"
1515

16+
. "http"
17+
1618
"github.com/labstack/echo"
1719
"github.com/polaris1119/goutils"
1820
"github.com/polaris1119/slices"
@@ -29,7 +31,7 @@ func (self CommentController) RegisterRoute(g *echo.Group) {
2931
// AtUsers 评论或回复 @ 某人 suggest
3032
func (CommentController) AtUsers(ctx echo.Context) error {
3133
term := ctx.QueryParam("term")
32-
isHttps := goutils.MustBool(ctx.Request().Header().Get("X-Https"), false)
34+
isHttps := CheckIsHttps(ctx)
3335
users := logic.DefaultUser.GetUserMentions(term, 10, isHttps)
3436
return ctx.JSON(http.StatusOK, users)
3537
}

src/http/controller/image.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (self ImageController) QuickUpload(ctx echo.Context) error {
6767
}
6868

6969
cdnDomain := global.App.CDNHttp
70-
if goutils.MustBool(ctx.Request().Header().Get("X-Https")) {
70+
if CheckIsHttps(ctx) {
7171
cdnDomain = global.App.CDNHttps
7272
}
7373

@@ -128,7 +128,7 @@ func (ImageController) Upload(ctx echo.Context) error {
128128
}
129129

130130
cdnDomain := global.App.CDNHttp
131-
if goutils.MustBool(ctx.Request().Header().Get("X-Https")) {
131+
if CheckIsHttps(ctx) {
132132
cdnDomain = global.App.CDNHttps
133133
}
134134
if !strings.HasSuffix(cdnDomain, "/") {
@@ -157,7 +157,7 @@ func (ImageController) Transfer(ctx echo.Context) error {
157157
}
158158

159159
cdnDomain := global.App.CDNHttp
160-
if goutils.MustBool(ctx.Request().Header().Get("X-Https")) {
160+
if CheckIsHttps(ctx) {
161161
cdnDomain = global.App.CDNHttps
162162
}
163163
if !strings.HasSuffix(cdnDomain, "/") {

src/http/controller/index.go

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,41 +108,53 @@ func (IndexController) WrapUrl(ctx echo.Context) error {
108108
return ctx.Redirect(http.StatusSeeOther, "/")
109109
}
110110

111-
if pUrl, err := url.Parse(tUrl); err != nil {
111+
// 本站
112+
if strings.Contains(tUrl, logic.WebsiteSetting.Domain) {
112113
return ctx.Redirect(http.StatusSeeOther, tUrl)
114+
}
115+
116+
if strings.Contains(tUrl, "?") {
117+
tUrl += "&"
113118
} else {
114-
if !pUrl.IsAbs() {
115-
return ctx.Redirect(http.StatusSeeOther, tUrl)
116-
}
119+
tUrl += "?"
120+
}
121+
tUrl += "hmsr=studygolang.com&utm_medium=studygolang.com&utm_source=studygolang.com"
122+
123+
if CheckIsHttps(ctx) {
124+
return ctx.Redirect(http.StatusSeeOther, tUrl)
125+
}
117126

118-
// 本站
119-
if strings.Contains(pUrl.Host, logic.WebsiteSetting.Domain) {
127+
var (
128+
pUrl *url.URL
129+
err error
130+
)
131+
132+
if pUrl, err = url.Parse(tUrl); err != nil {
133+
return ctx.Redirect(http.StatusSeeOther, tUrl)
134+
}
135+
136+
iframeDeny := config.ConfigFile.MustValue("crawl", "iframe_deny")
137+
// 检测是否禁止了 iframe 加载
138+
// 看是否在黑名单中
139+
for _, denyHost := range strings.Split(iframeDeny, ",") {
140+
if strings.Contains(pUrl.Host, denyHost) {
120141
return ctx.Redirect(http.StatusSeeOther, tUrl)
121142
}
143+
}
122144

123-
iframeDeny := config.ConfigFile.MustValue("crawl", "iframe_deny")
124-
// 检测是否禁止了 iframe 加载
125-
// 看是否在黑名单中
126-
for _, denyHost := range strings.Split(iframeDeny, ",") {
127-
if strings.Contains(pUrl.Host, denyHost) {
128-
return ctx.Redirect(http.StatusSeeOther, tUrl)
129-
}
145+
// 检测会比较慢,进行异步检测,记录下来,以后分析再加黑名单
146+
go func() {
147+
resp, err := http.Head(tUrl)
148+
if err != nil {
149+
logger.Errorln("[iframe] head url:", tUrl, "error:", err)
150+
return
130151
}
131-
132-
// 检测会比较慢,进行异步检测,记录下来,以后分析再加黑名单
133-
go func() {
134-
resp, err := http.Head(tUrl)
135-
if err != nil {
136-
logger.Errorln("[iframe] head url:", tUrl, "error:", err)
137-
return
138-
}
139-
defer resp.Body.Close()
140-
if resp.Header.Get("X-Frame-Options") != "" {
141-
logger.Errorln("[iframe] deny:", tUrl)
142-
return
143-
}
144-
}()
145-
}
152+
defer resp.Body.Close()
153+
if resp.Header.Get("X-Frame-Options") != "" {
154+
logger.Errorln("[iframe] deny:", tUrl)
155+
return
156+
}
157+
}()
146158

147159
return render(ctx, "wr.html", map[string]interface{}{"url": tUrl})
148160
}

src/http/http.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,7 @@ func executeTpl(ctx echo.Context, tpl *template.Template, data map[string]interf
305305
global.App.SetUptime()
306306
global.App.SetCopyright()
307307

308-
isHttps := goutils.MustBool(ctx.Request().Header().Get("X-Https"))
309-
if logic.WebsiteSetting.OnlyHttps {
310-
isHttps = true
311-
}
312-
308+
isHttps := CheckIsHttps(ctx)
313309
cdnDomain := global.App.CDNHttp
314310
if isHttps {
315311
cdnDomain = global.App.CDNHttps
@@ -335,6 +331,15 @@ func executeTpl(ctx echo.Context, tpl *template.Template, data map[string]interf
335331
return ctx.HTML(http.StatusOK, buf.String())
336332
}
337333

334+
func CheckIsHttps(ctx echo.Context) bool {
335+
isHttps := goutils.MustBool(ctx.Request().Header().Get("X-Https"))
336+
if logic.WebsiteSetting.OnlyHttps {
337+
isHttps = true
338+
}
339+
340+
return isHttps
341+
}
342+
338343
///////////////////////////////// APP 相关 //////////////////////////////
339344

340345
const (

0 commit comments

Comments
 (0)