main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package main
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "crypto/rand"
  6. "encoding/base64"
  7. "fmt"
  8. "github.com/labstack/echo"
  9. "io"
  10. "log"
  11. "log/syslog"
  12. "net/http"
  13. "net/smtp"
  14. "os"
  15. "time"
  16. )
  17. var _appversion string = "0.1"
  18. var _appname string = "ZiCloud-API"
  19. var URL string = "https://ipa-cl.zi-tel.com"
  20. func audit(txt string) {
  21. syslogger, err := syslog.New(syslog.LOG_INFO, _appname)
  22. if err != nil {
  23. log.Fatalln(err)
  24. }
  25. log.SetOutput(syslogger)
  26. log.Println(txt)
  27. }
  28. var RealIP string
  29. var secretKey = []byte("P*%!5+u!$y+cgM+P8bybzgnXpsd2Lv2z") // 32 bytes
  30. func sendMail(str string, subject string, recipient string) {
  31. auth := smtp.PlainAuth("", "zicloud@zi-tel.com", "5Sd?^AQx@r2OGRvS?i|DO0", "mail.zi-tel.com")
  32. to := []string{recipient}
  33. buff := make([]byte, 8)
  34. rand.Read(buff)
  35. random_str := base64.StdEncoding.EncodeToString(buff)
  36. msg := []byte("To:" + recipient + "\r\n" +
  37. "Date: " + time.Now().String() + "\r\n" +
  38. "Message-Id: <" + random_str + "@ZiCloud.com>" + "\r\n" +
  39. "subject: " + subject + "\r\n" +
  40. "From: ZiCloud <" + "zicloud@zi-tel.com" + ">\r\n" +
  41. str)
  42. err := smtp.SendMail("mail.zi-tel.com:25", auth, "zicloud@zi-tel.com", to, msg)
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. }
  47. func extractIP(next echo.HandlerFunc) echo.HandlerFunc {
  48. return func(c echo.Context) error {
  49. RealIP = c.RealIP()
  50. audit("Recieved request from: " + RealIP)
  51. return next(c)
  52. }
  53. }
  54. func main() {
  55. if len(os.Args) != 3 {
  56. fmt.Println("Wrong Usage:\n\t ./CMD IP Port")
  57. audit("Application in the wrong way")
  58. os.Exit(1)
  59. }
  60. echoHandler := echo.New()
  61. echoHandler.Use(extractIP)
  62. audit("Application " + _appname + " (" + _appversion + ") Started by " + os.Getenv("USER"))
  63. echoHandler.GET("/", func(c echo.Context) error {
  64. return c.String(http.StatusOK, "Hello, World!")
  65. })
  66. h := &handler{}
  67. echoHandler.POST("/login", h.login)
  68. echoHandler.GET("/private", h.private, isLoggedIn)
  69. echoHandler.GET("/admin", h.private, isLoggedIn, isAdmin)
  70. echoHandler.POST("/addUser", h.addUser, isLoggedIn, isAdmin)
  71. echoHandler.POST("/disableUser", h.disableUser, isLoggedIn, isAdmin)
  72. echoHandler.POST("/resetUser", h.resetUser)
  73. echoHandler.POST("/dnsrecordadd", h.dnsrecordadd, isLoggedIn, isAdmin)
  74. echoHandler.Logger.Fatal(echoHandler.Start(os.Args[1] + ":" + os.Args[2]))
  75. }
  76. func encrypt(key []byte, text string) string {
  77. // key := []byte(keyText)
  78. plaintext := []byte(text)
  79. block, err := aes.NewCipher(key)
  80. if err != nil {
  81. panic(err)
  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. panic(err)
  89. }
  90. stream := cipher.NewCFBEncrypter(block, iv)
  91. stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
  92. // convert to base64
  93. return base64.URLEncoding.EncodeToString(ciphertext)
  94. }
  95. func decrypt(key []byte, cryptoText string) string {
  96. ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)
  97. block, err := aes.NewCipher(key)
  98. if err != nil {
  99. panic(err)
  100. }
  101. // The IV needs to be unique, but not secure. Therefore it's common to
  102. // include it at the beginning of the ciphertext.
  103. if len(ciphertext) < aes.BlockSize {
  104. panic("ciphertext too short")
  105. }
  106. iv := ciphertext[:aes.BlockSize]
  107. ciphertext = ciphertext[aes.BlockSize:]
  108. stream := cipher.NewCFBDecrypter(block, iv)
  109. // XORKeyStream can work in-place if the two arguments are the same.
  110. stream.XORKeyStream(ciphertext, ciphertext)
  111. return fmt.Sprintf("%s", ciphertext)
  112. }