Skip to content

Instantly share code, notes, and snippets.

@icchy
Created Aug 7, 2019
Embed
What would you like to do?
Antivirus Oracle demo server
package main
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/gob"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
type Data struct {
Controllable1 string
Secret string
Controllable2 string
}
func genRandStr(size int) string {
data := make([]byte, size)
if _, err := rand.Read(data); err != nil {
log.Fatal(err)
}
return string(data)
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
secret := "this_secret_can_not_be_leaked"
if _, err := os.Stat("tmp"); os.IsNotExist(err) {
err := os.Mkdir("tmp", 0755)
if err != nil {
panic(err)
}
}
e.GET("/", func(c echo.Context) error {
c1 := c.FormValue("c1")
c2 := c.FormValue("c2")
name := fmt.Sprintf("%x", sha256.Sum256([]byte(c1+c2)))
data := &Data{
Controllable1: c1,
Secret: secret,
Controllable2: c2,
}
buf := bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(data)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join("tmp", name), buf.Bytes(), 0644)
if err != nil {
return err
}
return c.JSON(http.StatusOK, name)
})
e.GET("/:name", func(c echo.Context) error {
name := c.Param("name")
_, err := os.Stat(path.Join("tmp", name))
if os.IsNotExist(err) {
return err
}
_, err = ioutil.ReadFile(path.Join("tmp", name))
if err != nil {
return err
}
return c.String(http.StatusOK, "ok")
})
e.Logger.Fatal(e.Start(":8080"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment