utils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package fllcc
  2. import (
  3. "crypto/md5"
  4. "encoding/base64"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "log"
  9. "math/rand"
  10. "net"
  11. "net/url"
  12. "strings"
  13. "time"
  14. )
  15. var random *rand.Rand
  16. func init() {
  17. random = rand.New(rand.NewSource(time.Now().Unix()))
  18. }
  19. func getFreePort() (int, error) {
  20. for i := 4000; i <= 5000; i++ {
  21. if portIsFree(i) {
  22. return i, nil
  23. }
  24. }
  25. return 0, errors.New("没有可用端口")
  26. }
  27. func portIsFree(checkPort int) bool {
  28. addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%d", checkPort))
  29. if err != nil {
  30. return false
  31. }
  32. l, err := net.ListenTCP("tcp", addr)
  33. if err != nil {
  34. return false
  35. }
  36. defer l.Close()
  37. return true
  38. }
  39. func urlToABS(base, reference string) (string, error) {
  40. u, err := url.Parse(reference)
  41. if err != nil {
  42. return "", err
  43. }
  44. baseUrl, err := url.Parse(base)
  45. if err != nil {
  46. return "", err
  47. }
  48. return baseUrl.ResolveReference(u).String(), nil
  49. }
  50. func DescData(content string, secretKey string) (string, error) {
  51. return descData(content, secretKey)
  52. }
  53. func descData(content string, secretKey string) (string, error) {
  54. lockStream := "GHI9+JKLxyz012MNOPYbcRSTUVW8/ABCw3DEZaFXefghijklm7=nopqrsdQtuv456"
  55. lockLen := len(lockStream)
  56. enLend := len(content)
  57. randomLock := string([]rune(content)[enLend-1])
  58. lockCount := strings.Index(lockStream, randomLock)
  59. password := getMD5Hash(secretKey + randomLock)
  60. txtStream := content[0 : enLend-1]
  61. tmpStream := ""
  62. k := 0
  63. for i := 0; i < len(txtStream); i++ {
  64. if k == len(password) {
  65. k = 0
  66. }
  67. pwdInt := password[k]
  68. findStr := string([]rune(txtStream)[i])
  69. j := strings.Index(lockStream, findStr) - lockCount - int(pwdInt)
  70. for {
  71. if j >= 0 {
  72. break
  73. }
  74. j += lockLen
  75. }
  76. tmpStream += string([]rune(lockStream)[j])
  77. k++
  78. }
  79. byteList, err := base64.StdEncoding.DecodeString(tmpStream)
  80. if err != nil {
  81. return "", err
  82. }
  83. return string(byteList), nil
  84. }
  85. func EncData(txtStream string, secretKey string) (string, error) {
  86. password := secretKey
  87. lockstream := "GHI9+JKLxyz012MNOPYbcRSTUVW8/ABCw3DEZaFXefghijklm7=nopqrsdQtuv456"
  88. //随机找一个数字,并从密锁串中找到一个密锁值
  89. lockLen := len(lockstream)
  90. lockCount := random.Int31n(int32(lockLen))
  91. randomLock := string([]rune(lockstream)[lockCount])
  92. password = getMD5Hash(password + randomLock)
  93. txtStream = base64.StdEncoding.EncodeToString([]byte(txtStream))
  94. tmpStream := ""
  95. j := 0
  96. k := 0
  97. for i := 0; i < len(txtStream); i++ {
  98. if k == len(password) {
  99. k = 0
  100. }
  101. findChar := string([]rune(txtStream)[i])
  102. j = (strings.Index(lockstream, findChar) + int(lockCount) + int(password[k])) % lockLen
  103. tmpStream += string([]rune(lockstream)[j])
  104. k++
  105. }
  106. return tmpStream + randomLock, nil
  107. }
  108. func getMD5Hash(text string) string {
  109. hash := md5.Sum([]byte(text))
  110. return hex.EncodeToString(hash[:])
  111. }
  112. func outputLog(v ...interface{}) {
  113. logStr := fmt.Sprintln(v...)
  114. log.Println(logStr)
  115. //_, _ = logBuffer.Write([]byte(logStr))
  116. }