<commit_before>package main

import (
	"crypto/md5"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/server-may-cry/bubble-go/controllers"
	"github.com/server-may-cry/bubble-go/storage"
	"gopkg.in/gin-gonic/gin.v1"
	"gopkg.in/mgo.v2"
)

func init() {
	log.SetFlags(log.LstdFlags | log.Llongfile)

	rawMongoURL := os.Getenv("MONGODB_URI")
	if rawMongoURL == "" {
		log.Fatal("$MONGODB_URI must be set")
	}
	mongoConnection, err := mgo.Dial(rawMongoURL)
	if err != nil {
		log.Fatal(err)
	}
	storage.MongoDB = mongoConnection
}

func main() {
	router := gin.Default()

	router.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"foo": "bar",
		})
	})

	securedGroup := router.Group("")
	securedGroup.Use(signatureValidatorMiddleware)
	{
		securedGroup.POST("/ReqEnter", controllers.ReqEnter)
		securedGroup.POST("/ReqBuyProduct", controllers.ReqBuyProduct)
		securedGroup.POST("/ReqReduceTries", controllers.ReqReduceTries)
		securedGroup.POST("/ReqReduceCredits", controllers.ReqReduceCredits)
		securedGroup.POST("/ReqSavePlayerProgress", controllers.ReqSavePlayerProgress)
		securedGroup.POST("/ReqUsersProgress", controllers.ReqUsersProgress)
	}
	router.POST("/pay/:platform", controllers.PayPlatform) // vk|ok

	router.GET("/crossdomain.xml", func(c *gin.Context) {
		c.String(http.StatusOK, "<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" /></cross-domain-policy>")
	})
	// http://119226.selcdn.ru/bubble/ShootTheBubbleDevVK.html
	// http://bubble-srv-dev.herokuapp.com/bubble/ShootTheBubbleDevVK.html
	router.GET("/bubble/*filePath", controllers.ServeStatick)
	router.GET("/cache-clear", controllers.ClearStatickCache)

	router.GET("/exception", func(c *gin.Context) {
		log.Fatal("test log.Fatal")
	})

	loaderio := os.Getenv("LOADERIO")
	loaderioRoute := fmt.Sprintf("/loaderio-%s", loaderio)
	router.GET(loaderioRoute, func(c *gin.Context) {
		c.String(http.StatusOK, loaderio)
	})

	router.Run()
}

func signatureValidatorMiddleware(c *gin.Context) {
	request := controllers.AuthRequestPart{}
	if err := c.BindJSON(&request); err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	var stringToHash string
	switch request.SysID {
	case "VK":
		appID := os.Getenv("VK_APP_ID")
		secret := os.Getenv("VK_SECRET")
		stringToHash = fmt.Sprintf("%s_%s_%s", appID, request.ExtID, secret)
	case "OK":
		secret := os.Getenv("OK_SECRET")
		stringToHash = fmt.Sprintf("%s%s%s", request.ExtID, request.SessionKey, secret)
	default:
		c.AbortWithError(http.StatusBadRequest, fmt.Errorf("Unknown platform %s", request.SysID))
	}
	data := []byte(stringToHash)
	expectedMD5 := md5.Sum(data)
	log.Print("middleware", " ", stringToHash, " ", data, " ", expectedMD5, " ", expectedAuthKey, " ", request.AuthKey)
	expectedAuthKey := fmt.Sprintf("%x", expectedMD5)
	if expectedAuthKey != request.AuthKey {
		log.Print(expectedAuthKey)
		c.AbortWithError(http.StatusUnauthorized, fmt.Errorf("Bad auth key %s", request.AuthKey))
	}
}<commit_msg>Update main.go<commit_after>package main

import (
	"crypto/md5"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/server-may-cry/bubble-go/controllers"
	"github.com/server-may-cry/bubble-go/storage"
	"gopkg.in/gin-gonic/gin.v1"
	"gopkg.in/mgo.v2"
)

func init() {
	log.SetFlags(log.LstdFlags | log.Llongfile)

	rawMongoURL := os.Getenv("MONGODB_URI")
	if rawMongoURL == "" {
		log.Fatal("$MONGODB_URI must be set")
	}
	mongoConnection, err := mgo.Dial(rawMongoURL)
	if err != nil {
		log.Fatal(err)
	}
	storage.MongoDB = mongoConnection
}

func main() {
	router := gin.Default()

	router.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"foo": "bar",
		})
	})

	securedGroup := router.Group("")
	securedGroup.Use(signatureValidatorMiddleware)
	{
		securedGroup.POST("/ReqEnter", controllers.ReqEnter)
		securedGroup.POST("/ReqBuyProduct", controllers.ReqBuyProduct)
		securedGroup.POST("/ReqReduceTries", controllers.ReqReduceTries)
		securedGroup.POST("/ReqReduceCredits", controllers.ReqReduceCredits)
		securedGroup.POST("/ReqSavePlayerProgress", controllers.ReqSavePlayerProgress)
		securedGroup.POST("/ReqUsersProgress", controllers.ReqUsersProgress)
	}
	router.POST("/pay/:platform", controllers.PayPlatform) // vk|ok

	router.GET("/crossdomain.xml", func(c *gin.Context) {
		c.String(http.StatusOK, "<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" /></cross-domain-policy>")
	})
	// http://119226.selcdn.ru/bubble/ShootTheBubbleDevVK.html
	// http://bubble-srv-dev.herokuapp.com/bubble/ShootTheBubbleDevVK.html
	router.GET("/bubble/*filePath", controllers.ServeStatick)
	router.GET("/cache-clear", controllers.ClearStatickCache)

	router.GET("/exception", func(c *gin.Context) {
		log.Fatal("test log.Fatal")
	})

	loaderio := os.Getenv("LOADERIO")
	loaderioRoute := fmt.Sprintf("/loaderio-%s", loaderio)
	router.GET(loaderioRoute, func(c *gin.Context) {
		c.String(http.StatusOK, loaderio)
	})

	router.Run()
}

func signatureValidatorMiddleware(c *gin.Context) {
	request := controllers.AuthRequestPart{}
	if err := c.BindJSON(&request); err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	var stringToHash string
	switch request.SysID {
	case "VK":
		appID := os.Getenv("VK_APP_ID")
		secret := os.Getenv("VK_SECRET")
		stringToHash = fmt.Sprintf("%s_%s_%s", appID, request.ExtID, secret)
	case "OK":
		secret := os.Getenv("OK_SECRET")
		stringToHash = fmt.Sprintf("%s%s%s", request.ExtID, request.SessionKey, secret)
	default:
		c.AbortWithError(http.StatusBadRequest, fmt.Errorf("Unknown platform %s", request.SysID))
	}
	data := []byte(stringToHash)
	expectedMD5 := md5.Sum(data)
	expectedAuthKey := fmt.Sprintf("%x", expectedMD5)
	log.Print("middleware", " ", stringToHash, " ", data, " ", expectedMD5, " ", expectedAuthKey, " ", request.AuthKey)
	if expectedAuthKey != request.AuthKey {
		log.Print(expectedAuthKey)
		c.AbortWithError(http.StatusUnauthorized, fmt.Errorf("Bad auth key %s", request.AuthKey))
	}
}
{'fraction_non_alphanumeric': 0.17137602407624142, 'fraction_numerical': 0.005015883631499749, 'mean_word_length': 5.060790273556231, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 2, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3903083', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2200, 'n_words': 319}