The new Ripple frontend.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tracking.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "crypto/sha256"
  4. "database/sql"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "time"
  10. "zxq.co/x/rs"
  11. "github.com/gin-gonic/gin"
  12. )
  13. func setYCookie(userID int, c *gin.Context) {
  14. var token string
  15. err := db.QueryRow("SELECT token FROM identity_tokens WHERE userid = ? LIMIT 1", userID).Scan(&token)
  16. if err != nil && err != sql.ErrNoRows {
  17. c.Error(err)
  18. return
  19. }
  20. if token != "" {
  21. addY(c, token)
  22. return
  23. }
  24. for {
  25. token = fmt.Sprintf("%x", sha256.Sum256([]byte(rs.String(32))))
  26. if db.QueryRow("SELECT 1 FROM identity_tokens WHERE token = ? LIMIT 1", token).Scan(new(int)) == sql.ErrNoRows {
  27. break
  28. }
  29. }
  30. db.Exec("INSERT INTO identity_tokens(userid, token) VALUES (?, ?)", userID, token)
  31. addY(c, token)
  32. }
  33. func addY(c *gin.Context, y string) {
  34. http.SetCookie(c.Writer, &http.Cookie{
  35. Name: "y",
  36. Value: y,
  37. Expires: time.Now().Add(time.Hour * 24 * 30 * 6),
  38. })
  39. }
  40. func logIP(c *gin.Context, user int) {
  41. db.Exec(`INSERT INTO ip_user (userid, ip, occurencies) VALUES (?, ?, '1')
  42. ON DUPLICATE KEY UPDATE occurencies = occurencies + 1`, user, clientIP(c))
  43. }
  44. func setCountry(c *gin.Context, user int) error {
  45. raw, err := http.Get(config.IP_API + "/" + clientIP(c) + "/country")
  46. if err != nil {
  47. return err
  48. }
  49. data, err := ioutil.ReadAll(raw.Body)
  50. if err != nil {
  51. return err
  52. }
  53. country := strings.TrimSpace(string(data))
  54. if country == "" || len(country) != 2 {
  55. return nil
  56. }
  57. db.Exec("UPDATE users_stats SET country = ? WHERE id = ?", country, user)
  58. return nil
  59. }