net.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package util
  17. import (
  18. "net"
  19. "net/http"
  20. "net/url"
  21. "strings"
  22. "time"
  23. "github.com/88250/gulu"
  24. "github.com/88250/lute/ast"
  25. "github.com/gin-gonic/gin"
  26. "github.com/imroc/req/v3"
  27. "github.com/siyuan-note/httpclient"
  28. "github.com/siyuan-note/logging"
  29. )
  30. func ValidOptionalPort(port string) bool {
  31. if port == "" {
  32. return true
  33. }
  34. if port[0] != ':' {
  35. return false
  36. }
  37. for _, b := range port[1:] {
  38. if b < '0' || b > '9' {
  39. return false
  40. }
  41. }
  42. return true
  43. }
  44. func IsLocalHostname(hostname string) bool {
  45. if "localhost" == hostname || strings.HasSuffix(hostname, ".localhost") {
  46. return true
  47. }
  48. if ip := net.ParseIP(hostname); nil != ip {
  49. return ip.IsLoopback()
  50. }
  51. return false
  52. }
  53. func IsLocalHost(host string) bool {
  54. if hostname, _, err := net.SplitHostPort(strings.TrimSpace(host)); nil != err {
  55. return false
  56. } else {
  57. return IsLocalHostname(hostname)
  58. }
  59. }
  60. func IsLocalOrigin(origin string) bool {
  61. if url, err := url.Parse(origin); nil == err {
  62. return IsLocalHostname(url.Hostname())
  63. }
  64. return false
  65. }
  66. func IsOnline(checkURL string, skipTlsVerify bool) bool {
  67. _, err := url.Parse(checkURL)
  68. if nil != err {
  69. logging.LogWarnf("invalid check URL [%s]", checkURL)
  70. return false
  71. }
  72. if "" == checkURL {
  73. return false
  74. }
  75. if isOnline(checkURL, skipTlsVerify) {
  76. return true
  77. }
  78. logging.LogWarnf("network is offline [checkURL=%s]", checkURL)
  79. return false
  80. }
  81. func isOnline(checkURL string, skipTlsVerify bool) (ret bool) {
  82. c := req.C().SetTimeout(3 * time.Second)
  83. if skipTlsVerify {
  84. c.EnableInsecureSkipVerify()
  85. }
  86. c.SetUserAgent(UserAgent)
  87. for i := 0; i < 3; i++ {
  88. resp, err := c.R().Get(checkURL)
  89. if resp.GetHeader("Location") != "" {
  90. return true
  91. }
  92. switch err.(type) {
  93. case *url.Error:
  94. if err.(*url.Error).URL != checkURL {
  95. // DNS 重定向
  96. logging.LogWarnf("network is online [DNS redirect, checkURL=%s, retURL=%s]", checkURL, err.(*url.Error).URL)
  97. return true
  98. }
  99. }
  100. ret = nil == err
  101. if ret {
  102. break
  103. }
  104. time.Sleep(1 * time.Second)
  105. logging.LogWarnf("check url [%s] is online failed: %s", checkURL, err)
  106. }
  107. return
  108. }
  109. func GetRemoteAddr(req *http.Request) string {
  110. ret := req.Header.Get("X-forwarded-for")
  111. ret = strings.TrimSpace(ret)
  112. if "" == ret {
  113. ret = req.Header.Get("X-Real-IP")
  114. }
  115. ret = strings.TrimSpace(ret)
  116. if "" == ret {
  117. return req.RemoteAddr
  118. }
  119. return strings.Split(ret, ",")[0]
  120. }
  121. func JsonArg(c *gin.Context, result *gulu.Result) (arg map[string]interface{}, ok bool) {
  122. arg = map[string]interface{}{}
  123. if err := c.BindJSON(&arg); nil != err {
  124. result.Code = -1
  125. result.Msg = "parses request failed"
  126. return
  127. }
  128. ok = true
  129. return
  130. }
  131. func InvalidIDPattern(idArg string, result *gulu.Result) bool {
  132. if ast.IsNodeIDPattern(idArg) {
  133. return false
  134. }
  135. result.Code = -1
  136. result.Msg = "invalid ID argument"
  137. return true
  138. }
  139. func IsValidURL(str string) bool {
  140. _, err := url.Parse(str)
  141. return nil == err
  142. }
  143. func initHttpClient() {
  144. http.DefaultClient = httpclient.GetCloudFileClient2Min()
  145. http.DefaultTransport = httpclient.NewTransport(false)
  146. }