123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package main
- import (
- "crypto/aes"
- "crypto/cipher"
- "crypto/rand"
- "encoding/base64"
- "fmt"
- "github.com/google/uuid"
- "io"
- "log"
- "log/syslog"
- "net/http"
- "os"
- "github.com/labstack/echo"
- )
- var _appversion string = "0.1"
- var _appname string = "ZiTel-Sysbo-WS"
- func audit(txt string) {
- syslogger, err := syslog.New(syslog.LOG_INFO, _appname)
- if err != nil {
- log.Fatalln(err)
- }
- log.SetOutput(syslogger)
- log.Println(txt)
- }
- var RealIP string
- var URL string = "https://ipa.sf.faraborddi.dc"
- func extractIP(next echo.HandlerFunc) echo.HandlerFunc {
- return func(c echo.Context) error {
- RealIP = c.RealIP()
- audit("Recieved request from: " + RealIP)
- return next(c)
- }
- }
- func basicAuth(username, password string) string {
- auth := username + "@IPA:" + password
- return base64.StdEncoding.EncodeToString([]byte(auth))
- }
- type _response struct {
- Message string `json:"message"`
- Origin string `json:"origin"`
- Code int `json:"code"`
- Uuid string `json:"uuid"`
- }
- func main() {
- if len(os.Args) != 3 {
- fmt.Println("Wrong Usage:\n\t ./CMD IP Port")
- audit("Application in the wrong way")
- os.Exit(1)
- }
- echoHandler := echo.New()
- echoHandler.Use(extractIP)
- audit("Application " + _appname + " (" + _appversion + ") Started by " + os.Getenv("USER"))
- echoHandler.GET("/", func(c echo.Context) error {
- return c.String(http.StatusOK, "Hello, World!")
- })
- h := &handler{}
- echoHandler.POST("/login", h.login)
- //echoHandler.GET("/private", h.private, isLoggedIn)
- echoHandler.POST("/findMAC", h.findMAC, isLoggedIn, isAdmin)
- //echoHandler.GET("/admin", h.private, isLoggedIn, isAdmin)
- //echoHandler.POST("/token", h.token)
- echoHandler.Logger.Fatal(echoHandler.Start(os.Args[1] + ":" + os.Args[2]))
- }
- func uuidgen(resource string) (string, int) {
- id := uuid.New()
- if len(resource) < 3 {
- return "resource name should be at least 3 characters!", 1001
- }
- //fmt.Println("uuidGen for ", id, " at ", resource)
- return fmt.Sprintf("%s", id), 1000
- }
- func encrypt(key []byte, text string) string {
- // key := []byte(keyText)
- //fmt.Println("Encrypt by: ", key)
- plaintext := []byte(text)
- block, err := aes.NewCipher(key)
- if err != nil {
- //panic(err)
- fmt.Println("encrypt got error")
- return ""
- }
- // The IV needs to be unique, but not secure. Therefore it's common to
- // include it at the beginning of the ciphertext.
- ciphertext := make([]byte, aes.BlockSize+len(plaintext))
- iv := ciphertext[:aes.BlockSize]
- if _, err := io.ReadFull(rand.Reader, iv); err != nil {
- fmt.Println("encrypt got error")
- return ""
- }
- stream := cipher.NewCFBEncrypter(block, iv)
- stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
- // convert to base64
- return base64.URLEncoding.EncodeToString(ciphertext)
- }
- func decrypt(key []byte, cryptoText string) string {
- ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)
- //fmt.Println("Decrypt by: ", key)
- block, err := aes.NewCipher(key)
- if err != nil {
- fmt.Println("encrypt got error")
- return ""
- //panic(err)
- }
- // The IV needs to be unique, but not secure. Therefore it's common to
- // include it at the beginning of the ciphertext.
- if len(ciphertext) < aes.BlockSize {
- fmt.Println("encrypt got error")
- return ""
- //panic("ciphertext too short")
- }
- iv := ciphertext[:aes.BlockSize]
- ciphertext = ciphertext[aes.BlockSize:]
- stream := cipher.NewCFBDecrypter(block, iv)
- // XORKeyStream can work in-place if the two arguments are the same.
- stream.XORKeyStream(ciphertext, ciphertext)
- return fmt.Sprintf("%s", ciphertext)
- }
|