Skip to content

Commit

Permalink
Merge pull request #4 from dongFangTuring/dev
Browse files Browse the repository at this point in the history
Added verification code function to prevent blasting
  • Loading branch information
yisier authored Oct 27, 2022
2 parents 3f728b2 + 5a16772 commit a403246
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c
github.com/ccding/go-stun v0.0.0-20180726100737-be486d185f3d
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f
github.com/dsnet/compress v0.0.1 // indirect
github.com/golang/snappy v0.0.3
github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76/go.mod h1:vYwsqCOLxGii
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f h1:q/DpyjJjZs94bziQ7YkBmIlpqbVP7yw179rnzoNVX1M=
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f/go.mod h1:QGrK8vMWWHQYQ3QU9bw9Y9OPNfxccGzfb41qjvVeXtY=
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func StopServer(id int) error {
return err
} else {
t.Status = false
logs.Info("close port %d,remark %s,client id %d,task id %d", t.Port, t.Remark, t.Client.Id, t.Id)
file.GetDb().UpdateTask(t)
}
//delete(RunList, id)
Expand Down
17 changes: 17 additions & 0 deletions web/controllers/login.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controllers

import (
"github.com/astaxie/beego/cache"
"github.com/astaxie/beego/utils/captcha"
"math/rand"
"net"
"sync"
Expand All @@ -17,12 +19,19 @@ type LoginController struct {
}

var ipRecord sync.Map
var cpt *captcha.Captcha

type record struct {
hasLoginFailTimes int
lastLoginTime time.Time
}

func init() {
// use beego cache system store the captcha data
store := cache.NewMemoryCache()
cpt = captcha.NewWithFilter("/captcha/", store)
}

func (self *LoginController) Index() {
// Try login implicitly, will succeed if it's configured as no-auth(empty username&password).
webBaseUrl := beego.AppConfig.String("web_base_url")
Expand All @@ -31,12 +40,20 @@ func (self *LoginController) Index() {
}
self.Data["web_base_url"] = webBaseUrl
self.Data["register_allow"], _ = beego.AppConfig.Bool("allow_user_register")
self.Data["captcha_open"], _ = beego.AppConfig.Bool("open_captcha")
self.TplName = "login/index.html"
}

func (self *LoginController) Verify() {
username := self.GetString("username")
password := self.GetString("password")
captchaOpen, _ := beego.AppConfig.Bool("open_captcha")
if captchaOpen {
if !cpt.VerifyReq(self.Ctx.Request) {
self.Data["json"] = map[string]interface{}{"status": 0, "msg": "the verification code is wrong, please get it again and try again"}
self.ServeJSON()
}
}
if self.doLogin(username, password, true) {
self.Data["json"] = map[string]interface{}{"status": 1, "msg": "login success"}
} else {
Expand Down
4 changes: 4 additions & 0 deletions web/static/page/languages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@
<zh-CN>密码</zh-CN>
<en-US>Password</en-US>
</lang>
<lang id="word-captcha">
<zh-CN>验证码</zh-CN>
<en-US>Captcha</en-US>
</lang>
<lang id="word-port">
<zh-CN>端口</zh-CN>
<en-US>Port</en-US>
Expand Down
46 changes: 30 additions & 16 deletions web/views/login/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
</head>

<body class="gray-bg">
<div class="row border-bottom">
<nav class="navbar navbar-static-top navbar-right" role="navigation" style="margin: 20px 40px">
<div></div>
<h1 style="margin:0px" class="navbar-header font-bold" langtag="application"></h1>
<span class="btn-group dropdown">
<button id="languagemenu" class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fa fa-globe-asia fa-lg"></i><span></span></button>
<div class="row border-bottom">
<nav class="navbar navbar-static-top navbar-right" role="navigation" style="margin: 20px 40px">
<div></div>
<h1 style="margin:0px" class="navbar-header font-bold" langtag="application"></h1>
<span class="btn-group dropdown">
<button id="languagemenu" class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false"><i
class="fa fa-globe-asia fa-lg"></i><span></span></button>
<ul class="dropdown-menu"></ul>
</span>
</nav>
</div>
</nav>
</div>
<div class="loginColumns animated fadeInDown mt-3 px-5">
<div class="row">

Expand All @@ -56,16 +58,27 @@ <h3 langtag="info-tagline"></h3>
<div class="ibox-content">
<form class="m-t" onsubmit="return false">
<div class="form-group">
<input name="username" class="form-control" placeholder="username" required="" langtag="word-username">
<input name="username" class="form-control" placeholder="username" required=""
langtag="word-username">
</div>
<div class="form-group">
<input name="password" type="password" class="form-control" placeholder="password" required="" langtag="word-password">
<input name="password" type="password" class="form-control" placeholder="password" required=""
langtag="word-password">
</div>
<button onclick="login()" class="btn btn-primary block full-width m-b" langtag="word-login"></button>
{{if eq true .register_allow}}
<p class="text-muted text-center"><small langtag="info-noaccount"></small></p>
<a class="btn btn-sm btn-white btn-block" href="{{.web_base_url}}/login/register" langtag="word-register"></a>
{{end}}
{{if eq true .captcha_open}}
<div class="form-group">
{{create_captcha}}
<input name="captcha" class="form-control" placeholder="captcha" required=""
langtag="word-captcha">
</div>
{{end}}
<button onclick="login()" class="btn btn-primary block full-width m-b"
langtag="word-login"></button>
{{if eq true .register_allow}}
<p class="text-muted text-center"><small langtag="info-noaccount"></small></p>
<a class="btn btn-sm btn-white btn-block" href="{{.web_base_url}}/login/register"
langtag="word-register"></a>
{{end}}
</form>
</div>
</div>
Expand All @@ -84,7 +97,8 @@ <h3 langtag="info-tagline"></h3>


<script type="text/javascript">
window.nps = { "web_base_url": {{.web_base_url}} }
window.nps = {"web_base_url": {{.web_base_url}} }

// Login Page Flipbox control
function login() {
$.ajax({
Expand Down

0 comments on commit a403246

Please sign in to comment.