Beispiel #1
0
func (this *LoginRouter) Get() {
	// Set const
	this.Data["App_Name"] = App_Name
	// xsrf
	this.Data["xsrf"] = template.HTML(this.XsrfFormHtml())
	// set token in case twice submit
	Token := models.Token()
	this.SetSession("Token", Token)
	// set cookie not bot in case bots
	Cookies := models.RandString(20)
	this.SetSession("Cookie", Cookies)

	this.Data["Token"] = Token
	this.Data["Cookie"] = Cookies

	// Get referral
	Refer := this.Input().Get("username")
	if models.UserExist(Refer) {
		this.Data["Refer"] = Refer
	} else {
		models.Log(models.Log_Struct{"error", "Login:"******"No such referer")})
	}

	// Get User Session
	var user Session_User
	u := this.GetSession("_User")
	if u != nil {
		user = u.(Session_User)
		this.Data["User"] = user
	} else {
		this.Data["User"] = false
	}

	// Show the login page or Error page
	var showLogin bool
	v := this.GetSession("ShowLogin")
	if v != nil {
		showLogin = v.(bool)
		this.DelSession("ShowLogin")
	} else {
		showLogin = true
	}
	this.Data["ShowLogin"] = showLogin

	// Errors
	if !showLogin {
		e := this.GetSession("Error")
		if e != nil {
			this.Data["Error"] = GetError(e)
			this.DelSession("Error")
		}
	}

	this.TplNames = "login.html"
}
Beispiel #2
0
func (this *AdminRouter) postData(sec string) {
	inputs := this.Input()
	switch sec {
	case "Game_New":
		teamA := inputs.Get("TeamA")
		teamB := inputs.Get("TeamB")
		oddsa := inputs.Get("Oddsa")
		oddsb := inputs.Get("Oddsb")
		concede := inputs.Get("Concede")
		scoresum := inputs.Get("ScoreSum")
		starttime := inputs.Get("TimeStart")
		Type := inputs.Get("Type")
		slice := []interface{}{"Football", "Basketball"}

		// Check if input matches
		if models.ValidOdds(oddsa) && models.ValidOdds(oddsb) && models.ValidScore(concede) && models.ValidScore(scoresum) && models.ValidStarttime(starttime) && In_slice(Type, slice) {
			oddsa_float64, _ := strconv.ParseFloat(oddsa, 64)
			oddsb_float64, _ := strconv.ParseFloat(oddsb, 64)
			concede_float64, _ := strconv.ParseFloat(concede, 64)
			scoresum_float64, _ := strconv.ParseFloat(scoresum, 64)
			var isfootball int
			if Type == "Football" {
				isfootball = 1
			}

			if !models.NewGame(models.Game_Detail{Isfootball: isfootball, Teama: teamA, Teamb: teamB, Oddsa: oddsa_float64, Oddsb: oddsb_float64, Concede: concede_float64, Scoresum: scoresum_float64, Timestarted: starttime, Timecreated: time.Now().Format(layout)}) {
				models.Log(models.Log_Struct{"error", "Create New Game:", errors.New("Failed to create new game")})
			}
		}
	case "Game_Result":
		gid := inputs.Get("gid")
		result := inputs.Get("result")

		// Check if input matches
		if models.ValidResult(result) && models.ValidGid(gid) {
			r := strings.Split(result, ":")
			score_a, _ := strconv.Atoi(r[0])
			score_b, _ := strconv.Atoi(r[1])
			score_sum := score_a + score_b

			id, _ := strconv.Atoi(gid)

			// Update the game
			if !models.UpdateGameById(score_a, score_b, id) {
				panic("Cant Update Result")
			}

			// Get the game by id, get game scoresum, odds, concede
			game := models.GameById(id)
			sum := game.Scoresum
			oddsa := game.Oddsa
			oddsb := game.Oddsb
			concede := game.Concede

			// Calculate the result, A_Win, B_Win, Odd, Even, Large, Small
			odds := oddsa
			a_or_b := "A_Win"
			if score_a-int(concede) <= score_b {
				a_or_b = "B_Win"
				odds = oddsb
			}
			oddeven := "Odd"
			if score_sum%2 == 0 {
				oddeven = "Even"
			}
			largesmall := "Large"
			if score_sum <= int(sum) {
				largesmall = "Small"
			}

			g_result := []interface{}{a_or_b, oddeven, largesmall}
			// Update_Distribution, TODO:
			if !models.CalculateResult(id, odds, ProfitAddr, g_result) {
				panic("Cant Calculate Result")
			}
		}
	}
	this.Ctx.Redirect(302, "/admin")
}
Beispiel #3
0
func (this *RegisterRouter) Post() {
	// Get user inputs
	inputs := this.Input()

	// Check token in case twice submit
	var token string
	token_sess := this.GetSession("Token")
	if token_sess != nil {
		this.DelSession("Token")
		token = fmt.Sprintf("%d", token_sess.(int64))
	}
	if token != inputs.Get("token") {
		this.SetSession("Error", ERROR_TWICESUBMIT)
		models.Log(models.Log_Struct{"error", "Register:", errors.New("Submit twice")})
		this.fail()
		return
	}

	// Check cookie in case bots
	cookie_sess := this.GetSession("Cookie")
	if cookie_sess != nil {
		this.DelSession("Cookie")
		cookie := cookie_sess.(string)
		if cookie != this.Ctx.GetCookie("nobot") {
			this.SetSession("Error", ERROR_CAPTCHA)
			models.Log(models.Log_Struct{"error", "Register:", errors.New("No bot is allowed")})
			this.fail()
			return
		}
	}

	// Validate user inputs
	username := strings.TrimSpace(inputs.Get("username"))
	password := strings.TrimSpace(inputs.Get("password"))
	re_password := strings.TrimSpace(inputs.Get("re-password"))
	fundpass := strings.TrimSpace(inputs.Get("fundpassword"))
	re_fundpass := strings.TrimSpace(inputs.Get("re-fundpassword"))
	email := strings.TrimSpace(inputs.Get("email"))
	dateofbirth := strings.TrimSpace(inputs.Get("birth"))

	// Check referral
	refer := strings.TrimSpace(inputs.Get("refer"))
	if refer != "" && models.ValidString(refer) {
		if !models.UserExist(refer) {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("Referral user not exist.")})
			this.SetSession("Error", ERROR_REFERNOTEXIST)
			this.fail()
			return
		}
	}

	// Validate user inputs, set sessions and redirect
	if models.ValidString(username) && models.ValidString(password) && models.ValidString(re_password) && models.ValidString(fundpass) && models.ValidString(re_fundpass) && password == re_password && fundpass == re_fundpass && models.ValidEmail(email) && models.ValidBirth(dateofbirth) {
		// Check if user exist
		if models.UserExist(username) {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("User already exist.")})
			this.SetSession("Error", ERROR_USEREXIST)
			this.fail()
			return
		}

		// Check if email exist
		if models.EmailExist(email) {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("Email already exist.")})
			this.SetSession("Error", ERROR_EMAILEXIST)
			this.fail()
			return
		}

		// Generate new address for new user
		address, err := models.NewAddress(username)
		if err != nil {
			models.Log(models.Log_Struct{"info", "Register:", err})
			this.fail()
			this.SetSession("Error", ERROR_ADDRESS)
			return
		}

		// Insert new user to DB
		ok := models.NewUser(models.User{Username: username, Password: models.EncodePass(password), Fundpassword: models.EncodePass(fundpass), Email: email, Btcaddress: address, Birth: dateofbirth, Referral: refer})
		if !ok {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("Unable to insert user, need to delete from wallet.")})
			err = models.Archive(address)
			if err != nil {
				models.Log(models.Log_Struct{"warn", "Register:", errors.New("Unable to archive.")})
			} else {
				models.Log(models.Log_Struct{"info", "Register:", errors.New("Succeed archiving address.")})
			}
			this.fail()
			this.SetSession("Error", ERROR_DB)
			return
		}

		this.succ()
		return
	}

	models.Log(models.Log_Struct{"info", "Register:", errors.New("Failed, invalid data")})
	this.SetSession("Error", ERROR_INVALIDINPUT)
	this.fail()
}
Beispiel #4
0
func (this *ForgetRouter) Post() {
	// Get inputs and validate
	inputs := this.Input()

	// Check token in case twice submit
	var token string
	token_sess := this.GetSession("Token")
	if token_sess != nil {
		this.DelSession("Token")
		token = fmt.Sprintf("%d", token_sess.(int64))
	}
	if token != inputs.Get("token") {
		this.SetSession("Error", ERROR_TWICESUBMIT)
		models.Log(models.Log_Struct{"error", "Forget:", errors.New("Submit twice")})
		this.fail()
		return
	}

	// Check cookie in case bots
	cookie_sess := this.GetSession("Cookie")
	if cookie_sess != nil {
		this.DelSession("Cookie")
		cookie := cookie_sess.(string)
		if cookie != this.Ctx.GetCookie("nobot") {
			this.SetSession("Error", ERROR_CAPTCHA)
			models.Log(models.Log_Struct{"error", "Forget:", errors.New("No bot is allowed")})
			this.fail()
			return
		}
	}

	// Validate inputs
	username := strings.TrimSpace(inputs.Get("username"))
	dateofbirth := strings.TrimSpace(inputs.Get("birth"))
	email := strings.TrimSpace(inputs.Get("email"))

	if models.ValidString(username) && models.ValidEmail(email) && models.ValidBirth(dateofbirth) {
		// Check if user exist
		if !models.UserExist(username) {
			models.Log(models.Log_Struct{"info", "Forget:", errors.New("User not exist.")})
			this.SetSession("Error", ERROR_USERNOTEXIST)
			this.fail()
			return
		}
		// Check if birth matches
		if !models.BirthMatch(username, dateofbirth) {
			models.Log(models.Log_Struct{"info", "Forget:", errors.New("Birth not match.")})
			this.SetSession("Error", ERROR_BIRTHNOTMATCH)
			this.fail()
			return
		}
		// Check if email mathces
		if !models.EmailMatch(username, email) {
			models.Log(models.Log_Struct{"info", "Forget:", errors.New("Email not match.")})
			this.SetSession("Error", ERROR_EMAILNOTMATCH)
			this.fail()
			return
		}

		// Send Email to authenticate
		authen := models.RandString(8)
		if !models.SendEmail(email, "重设密码", username+":  请复制验证码,以完成重设密码操作---->", authen) {
			models.Log(models.Log_Struct{"warn", "Forget:", errors.New("Cant send email to authen password reset.")})
			this.SetSession("Error", ERROR_EMAILNOTSENT)
			this.fail()
			return
		}

		this.SetSession("Username", username)
		this.SetSession("Authen", authen)
		this.succ()
		return
	}

	models.Log(models.Log_Struct{"info", "Forget:", errors.New("Failed, invalid data.")})
	this.SetSession("Error", ERROR_INVALIDINPUT)
	this.fail()
}
Beispiel #5
0
func (this *ResetRouter) Post() {
	// Get inputs and validate
	inputs := this.Input()

	// Check token in case twice submit
	var token string
	token_sess := this.GetSession("Token")
	if token_sess != nil {
		this.DelSession("Token")
		token = fmt.Sprintf("%d", token_sess.(int64))
	}
	if token != inputs.Get("token") {
		this.SetSession("Error", ERROR_TWICESUBMIT)
		models.Log(models.Log_Struct{"error", "Reset:", errors.New("Submit twice")})
		this.fail()
		return
	}

	// Check cookie in case bots
	cookie_sess := this.GetSession("Cookie")
	if cookie_sess != nil {
		this.DelSession("Cookie")
		cookie := cookie_sess.(string)
		if cookie != this.Ctx.GetCookie("nobot") {
			this.SetSession("Error", ERROR_CAPTCHA)
			models.Log(models.Log_Struct{"error", "Reset:", errors.New("No bot is allowed")})
			this.fail()
			return
		}
	}

	// Get the email code
	var code string
	code_sess := this.GetSession("Authen")
	if code_sess != nil {
		this.DelSession("Authen")
		code = code_sess.(string)
	}
	// Get the username
	var username string
	user_sess := this.GetSession("Username")
	if user_sess != nil {
		this.DelSession("Username")
		username = user_sess.(string)
	}

	// Get user inputs
	authen := strings.TrimSpace(inputs.Get("authen"))
	password := strings.TrimSpace(inputs.Get("password"))
	re_password := strings.TrimSpace(inputs.Get("re-password"))
	fundpass := strings.TrimSpace(inputs.Get("fundpassword"))
	re_fundpass := strings.TrimSpace(inputs.Get("re-fundpassword"))

	// Validate user inputs
	if models.ValidString(password) && models.ValidString(re_password) && models.ValidString(fundpass) && models.ValidString(re_fundpass) && password == re_password && fundpass == re_fundpass && authen == code {
		// Check if code matches input
		if code != authen {
			models.Log(models.Log_Struct{"info", "Reset:", errors.New("Code not matches.")})
			this.SetSession("Error", ERROR_CODENOTMATCH)
			return
		}

		// Update DB
		if !models.UpdateUserPass(username, models.EncodePass(password), models.EncodePass(fundpass)) {
			models.Log(models.Log_Struct{"info", "Reset:", errors.New("Cant update password of user.")})
			this.SetSession("Error", ERROR_CANTUPDATE)
			return
		}

		this.succ()
		return
	}

	models.Log(models.Log_Struct{"info", "Reset:", errors.New("Failed, invalid data.")})
	this.SetSession("Error", ERROR_INVALIDINPUT)
	this.fail()
}
Beispiel #6
0
func (this *LoginRouter) Post() {
	//fmt.Println(this.CheckXsrfCookie())

	// Get inputs and validate
	inputs := this.Input()

	// Check token in case twice submit
	var token string
	token_sess := this.GetSession("Token")
	if token_sess != nil {
		this.DelSession("Token")
		token = fmt.Sprintf("%d", token_sess.(int64))
	}
	if token != inputs.Get("token") {
		this.SetSession("Error", ERROR_TWICESUBMIT)
		models.Log(models.Log_Struct{"error", "Login:"******"Submit twice")})
		this.fail()
		return
	}

	// Check cookie in case bots
	cookie_sess := this.GetSession("Cookie")
	if cookie_sess != nil {
		this.DelSession("Cookie")
		cookie := cookie_sess.(string)
		if cookie != this.Ctx.GetCookie("nobot") {
			this.SetSession("Error", ERROR_CAPTCHA)
			models.Log(models.Log_Struct{"error", "Login:"******"No bot is allowed")})
			this.fail()
			return
		}
	}

	// Validate user inputs
	username := strings.TrimSpace(inputs.Get("username"))
	password := strings.TrimSpace(inputs.Get("password"))

	if models.ValidString(username) && models.ValidString(password) {
		// Check if user exist
		if !models.UserExist(username) {
			models.Log(models.Log_Struct{"info", "Login:"******"User not exist")})
			this.SetSession("Error", ERROR_USERNOTEXIST)
			this.fail()
			return
		}
		// Check if password correct
		if !models.PassMatch(username, password) {
			models.Log(models.Log_Struct{"info", "Login:"******"Password incorrect")})
			this.SetSession("Error", ERROR_PASSINCORRECT)
			this.fail()
			return
		}

		// Update IP
		ip := strings.Split(this.Ctx.Request.RemoteAddr, ":")[0]
		models.UpdateIP(username, ip)

		// Get Uid
		id := models.UidByUsername(username)

		this.SetSession("_User", Session_User{Uid: id, Username: username, Ip: ip})
		this.succ()
		return
	}

	models.Log(models.Log_Struct{"info", "Login:"******"Failed, invalid data")})
	this.SetSession("Error", ERROR_INVALIDINPUT)
	this.fail()
}