main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package main
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "crypto/rand"
  6. "encoding/base64"
  7. "fmt"
  8. "github.com/google/uuid"
  9. "io"
  10. "log"
  11. "log/syslog"
  12. "net/http"
  13. "os"
  14. "github.com/labstack/echo"
  15. )
  16. var _appversion string = "0.1"
  17. var _appname string = "ZiTel-Sysbo-WS"
  18. func audit(txt string) {
  19. syslogger, err := syslog.New(syslog.LOG_INFO, _appname)
  20. if err != nil {
  21. log.Fatalln(err)
  22. }
  23. log.SetOutput(syslogger)
  24. log.Println(txt)
  25. }
  26. var RealIP string
  27. var URL string = "https://ipa.sf.faraborddi.dc"
  28. func extractIP(next echo.HandlerFunc) echo.HandlerFunc {
  29. return func(c echo.Context) error {
  30. RealIP = c.RealIP()
  31. audit("Recieved request from: " + RealIP)
  32. return next(c)
  33. }
  34. }
  35. func basicAuth(username, password string) string {
  36. auth := username + "@IPA:" + password
  37. return base64.StdEncoding.EncodeToString([]byte(auth))
  38. }
  39. type _response struct {
  40. Message string `json:"message"`
  41. Origin string `json:"origin"`
  42. Code int `json:"code"`
  43. Uuid string `json:"uuid"`
  44. }
  45. func main() {
  46. if len(os.Args) != 3 {
  47. fmt.Println("Wrong Usage:\n\t ./CMD IP Port")
  48. audit("Application in the wrong way")
  49. os.Exit(1)
  50. }
  51. echoHandler := echo.New()
  52. echoHandler.Use(extractIP)
  53. audit("Application " + _appname + " (" + _appversion + ") Started by " + os.Getenv("USER"))
  54. echoHandler.GET("/", func(c echo.Context) error {
  55. return c.String(http.StatusOK, "Hello, World!")
  56. })
  57. h := &handler{}
  58. echoHandler.POST("/login", h.login)
  59. //echoHandler.GET("/private", h.private, isLoggedIn)
  60. echoHandler.POST("/findMAC", h.findMAC, isLoggedIn, isAdmin)
  61. //echoHandler.GET("/admin", h.private, isLoggedIn, isAdmin)
  62. //echoHandler.POST("/token", h.token)
  63. echoHandler.Logger.Fatal(echoHandler.Start(os.Args[1] + ":" + os.Args[2]))
  64. }
  65. func uuidgen(resource string) (string, int) {
  66. id := uuid.New()
  67. if len(resource) < 3 {
  68. return "resource name should be at least 3 characters!", 1001
  69. }
  70. //fmt.Println("uuidGen for ", id, " at ", resource)
  71. return fmt.Sprintf("%s", id), 1000
  72. }
  73. func encrypt(key []byte, text string) string {
  74. // key := []byte(keyText)
  75. //fmt.Println("Encrypt by: ", key)
  76. plaintext := []byte(text)
  77. block, err := aes.NewCipher(key)
  78. if err != nil {
  79. //panic(err)
  80. fmt.Println("encrypt got error")
  81. return ""
  82. }
  83. // The IV needs to be unique, but not secure. Therefore it's common to
  84. // include it at the beginning of the ciphertext.
  85. ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  86. iv := ciphertext[:aes.BlockSize]
  87. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  88. fmt.Println("encrypt got error")
  89. return ""
  90. }
  91. stream := cipher.NewCFBEncrypter(block, iv)
  92. stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
  93. // convert to base64
  94. return base64.URLEncoding.EncodeToString(ciphertext)
  95. }
  96. func decrypt(key []byte, cryptoText string) string {
  97. ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)
  98. //fmt.Println("Decrypt by: ", key)
  99. block, err := aes.NewCipher(key)
  100. if err != nil {
  101. fmt.Println("encrypt got error")
  102. return ""
  103. //panic(err)
  104. }
  105. // The IV needs to be unique, but not secure. Therefore it's common to
  106. // include it at the beginning of the ciphertext.
  107. if len(ciphertext) < aes.BlockSize {
  108. fmt.Println("encrypt got error")
  109. return ""
  110. //panic("ciphertext too short")
  111. }
  112. iv := ciphertext[:aes.BlockSize]
  113. ciphertext = ciphertext[aes.BlockSize:]
  114. stream := cipher.NewCFBDecrypter(block, iv)
  115. // XORKeyStream can work in-place if the two arguments are the same.
  116. stream.XORKeyStream(ciphertext, ciphertext)
  117. return fmt.Sprintf("%s", ciphertext)
  118. }