网络相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
3.7 KiB

3 years ago
3 years ago
3 years ago
  1. /*
  2. * url functions
  3. */
  4. package network
  5. import (
  6. "bytes"
  7. "crypto/md5"
  8. "encoding/hex"
  9. "errors"
  10. "io/ioutil"
  11. "net"
  12. "net/http"
  13. "net/url"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "git.tetele.net/tgo/crypter"
  18. )
  19. /**
  20. * post 请求
  21. */
  22. func FnPost(url, param string) ([]byte, error) {
  23. httpClient := &http.Client{}
  24. req, err := http.NewRequest("POST", url, strings.NewReader(param))
  25. if err != nil {
  26. return []byte(""), err
  27. }
  28. req.Header.Set("Content-Type", "application/x-www-form-urlencoded") //"application/x-www-form-urlencoded"
  29. resp, err := httpClient.Do(req)
  30. if err != nil {
  31. return []byte(""), err
  32. }
  33. defer resp.Body.Close()
  34. body, err := ioutil.ReadAll(resp.Body)
  35. if err != nil {
  36. return []byte(""), err
  37. }
  38. return body, nil
  39. }
  40. /**
  41. * post 请求
  42. */
  43. func PostJson(url string, param []byte) ([]byte, error) {
  44. httpClient := &http.Client{}
  45. req, err := http.NewRequest("POST", url, bytes.NewBuffer(param))
  46. if err != nil {
  47. return []byte(""), err
  48. }
  49. req.Header.Set("Content-Type", "application/json")
  50. resp, err := httpClient.Do(req)
  51. if err != nil {
  52. return []byte(""), err
  53. }
  54. defer resp.Body.Close()
  55. body, err := ioutil.ReadAll(resp.Body)
  56. if err != nil {
  57. return []byte(""), err
  58. }
  59. return body, nil
  60. }
  61. /**
  62. * 取本地IP
  63. */
  64. func GetLocalIp() string {
  65. addrs, err := net.InterfaceAddrs()
  66. if err != nil {
  67. return ""
  68. }
  69. for _, address := range addrs {
  70. // 检查ip地址判断是否回环地址
  71. if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  72. if ipnet.IP.To4() != nil && !ipnet.IP.IsLinkLocalUnicast() {
  73. return ipnet.IP.String()
  74. }
  75. }
  76. }
  77. return ""
  78. }
  79. /**
  80. * 生成一个token
  81. */
  82. func CreateTokenWithTime(sToken string) string {
  83. now := time.Now()
  84. iNow := now.Format("20060102150405")
  85. sToken += iNow
  86. h := md5.New()
  87. h.Write([]byte(sToken)) // 需要加密的字符串
  88. cipher2Str := h.Sum(nil)
  89. return hex.EncodeToString(cipher2Str) // 输出加密结果
  90. }
  91. /**
  92. * 生成一个token
  93. */
  94. // func CreateToken(sToken string) string {
  95. // now := time.Now()
  96. // iNow := now.Format("20060102150405")
  97. // sToken += iNow
  98. // h := md5.New()
  99. // h.Write([]byte(sToken)) // 需要加密的字符串
  100. // cipher2Str := h.Sum(nil)
  101. // sMd5 := hex.EncodeToString(cipher2Str) // 输出加密结果
  102. // tokenEn := crypter.DesEn(sToken, "gzyc8642")
  103. // return sMd5 + "|" + ToString(now.Unix()) + "|" + tokenEn
  104. // }
  105. /**
  106. * 验证token
  107. */
  108. func CheckToken(token string) (bool, string) {
  109. arr := strings.Split(token, "|")
  110. if len(arr) != 3 {
  111. return false, ""
  112. }
  113. tokenDes := crypter.DesDe(arr[2], "gzyc8642")
  114. tokenTime := tokenDes[len(tokenDes)-14:]
  115. timeInt, err := strconv.ParseInt(arr[1], 10, 64)
  116. if err != nil {
  117. return false, ""
  118. }
  119. currentTime := time.Unix(timeInt, 0).Format("20060102150405")
  120. if currentTime != tokenTime {
  121. return false, ""
  122. }
  123. h := md5.New()
  124. h.Write([]byte(tokenDes)) // 需要加密的字符串
  125. cipher2Str := h.Sum(nil)
  126. sMd5 := hex.EncodeToString(cipher2Str) // 输出加密结果
  127. if sMd5 == arr[0] {
  128. return true, tokenDes[0 : len(tokenDes)-14]
  129. }
  130. return false, ""
  131. }
  132. /**
  133. * 给参数签名
  134. */
  135. func QuerySign(mQuery map[string]string) string {
  136. sParam := ""
  137. for k, v := range mQuery {
  138. sParam += k + "=" + v + "&"
  139. }
  140. query, Err := url.ParseQuery(sParam)
  141. if Err != nil {
  142. return ""
  143. } else {
  144. return crypter.Md5Password(query.Encode(), "guzeng1")
  145. }
  146. }
  147. /**
  148. * 从url中取域名端口
  149. * 2019/05/20
  150. */
  151. func GetIpPortFromUrl(sUrl string) (string, string, error) {
  152. if sUrl == "" {
  153. return "", "", errors.New("param error")
  154. }
  155. if !strings.Contains(sUrl, "http://") && !strings.Contains(sUrl, "https://") {
  156. sUrl = "http://" + sUrl
  157. }
  158. u, err := url.Parse(sUrl)
  159. if err != nil {
  160. return "", "", err
  161. }
  162. return u.Hostname(), u.Port(), nil
  163. }