utils.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package common
  2. import (
  3. crand "crypto/rand"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "html/template"
  8. "io"
  9. "log"
  10. "math/big"
  11. "math/rand"
  12. "net"
  13. "net/url"
  14. "os"
  15. "os/exec"
  16. "runtime"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/google/uuid"
  21. "github.com/pkg/errors"
  22. )
  23. func OpenBrowser(url string) {
  24. var err error
  25. switch runtime.GOOS {
  26. case "linux":
  27. err = exec.Command("xdg-open", url).Start()
  28. case "windows":
  29. err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
  30. case "darwin":
  31. err = exec.Command("open", url).Start()
  32. }
  33. if err != nil {
  34. log.Println(err)
  35. }
  36. }
  37. func GetIp() (ip string) {
  38. ips, err := net.InterfaceAddrs()
  39. if err != nil {
  40. log.Println(err)
  41. return ip
  42. }
  43. for _, a := range ips {
  44. if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
  45. if ipNet.IP.To4() != nil {
  46. ip = ipNet.IP.String()
  47. if strings.HasPrefix(ip, "10") {
  48. return
  49. }
  50. if strings.HasPrefix(ip, "172") {
  51. return
  52. }
  53. if strings.HasPrefix(ip, "192.168") {
  54. return
  55. }
  56. ip = ""
  57. }
  58. }
  59. }
  60. return
  61. }
  62. func GetNetworkIps() []string {
  63. var networkIps []string
  64. ips, err := net.InterfaceAddrs()
  65. if err != nil {
  66. log.Println(err)
  67. return networkIps
  68. }
  69. for _, a := range ips {
  70. if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
  71. if ipNet.IP.To4() != nil {
  72. ip := ipNet.IP.String()
  73. // Include common private network ranges
  74. if strings.HasPrefix(ip, "10.") ||
  75. strings.HasPrefix(ip, "172.") ||
  76. strings.HasPrefix(ip, "192.168.") {
  77. networkIps = append(networkIps, ip)
  78. }
  79. }
  80. }
  81. }
  82. return networkIps
  83. }
  84. // IsRunningInContainer detects if the application is running inside a container
  85. func IsRunningInContainer() bool {
  86. // Method 1: Check for .dockerenv file (Docker containers)
  87. if _, err := os.Stat("/.dockerenv"); err == nil {
  88. return true
  89. }
  90. // Method 2: Check cgroup for container indicators
  91. if data, err := os.ReadFile("/proc/1/cgroup"); err == nil {
  92. content := string(data)
  93. if strings.Contains(content, "docker") ||
  94. strings.Contains(content, "containerd") ||
  95. strings.Contains(content, "kubepods") ||
  96. strings.Contains(content, "/lxc/") {
  97. return true
  98. }
  99. }
  100. // Method 3: Check environment variables commonly set by container runtimes
  101. containerEnvVars := []string{
  102. "KUBERNETES_SERVICE_HOST",
  103. "DOCKER_CONTAINER",
  104. "container",
  105. }
  106. for _, envVar := range containerEnvVars {
  107. if os.Getenv(envVar) != "" {
  108. return true
  109. }
  110. }
  111. // Method 4: Check if init process is not the traditional init
  112. if data, err := os.ReadFile("/proc/1/comm"); err == nil {
  113. comm := strings.TrimSpace(string(data))
  114. // In containers, process 1 is often not "init" or "systemd"
  115. if comm != "init" && comm != "systemd" {
  116. // Additional check: if it's a common container entrypoint
  117. if strings.Contains(comm, "docker") ||
  118. strings.Contains(comm, "containerd") ||
  119. strings.Contains(comm, "runc") {
  120. return true
  121. }
  122. }
  123. }
  124. return false
  125. }
  126. var sizeKB = 1024
  127. var sizeMB = sizeKB * 1024
  128. var sizeGB = sizeMB * 1024
  129. func Bytes2Size(num int64) string {
  130. numStr := ""
  131. unit := "B"
  132. if num/int64(sizeGB) > 1 {
  133. numStr = fmt.Sprintf("%.2f", float64(num)/float64(sizeGB))
  134. unit = "GB"
  135. } else if num/int64(sizeMB) > 1 {
  136. numStr = fmt.Sprintf("%d", int(float64(num)/float64(sizeMB)))
  137. unit = "MB"
  138. } else if num/int64(sizeKB) > 1 {
  139. numStr = fmt.Sprintf("%d", int(float64(num)/float64(sizeKB)))
  140. unit = "KB"
  141. } else {
  142. numStr = fmt.Sprintf("%d", num)
  143. }
  144. return numStr + " " + unit
  145. }
  146. func Seconds2Time(num int) (time string) {
  147. if num/31104000 > 0 {
  148. time += strconv.Itoa(num/31104000) + " 年 "
  149. num %= 31104000
  150. }
  151. if num/2592000 > 0 {
  152. time += strconv.Itoa(num/2592000) + " 个月 "
  153. num %= 2592000
  154. }
  155. if num/86400 > 0 {
  156. time += strconv.Itoa(num/86400) + " 天 "
  157. num %= 86400
  158. }
  159. if num/3600 > 0 {
  160. time += strconv.Itoa(num/3600) + " 小时 "
  161. num %= 3600
  162. }
  163. if num/60 > 0 {
  164. time += strconv.Itoa(num/60) + " 分钟 "
  165. num %= 60
  166. }
  167. time += strconv.Itoa(num) + " 秒"
  168. return
  169. }
  170. func Interface2String(inter interface{}) string {
  171. switch inter.(type) {
  172. case string:
  173. return inter.(string)
  174. case int:
  175. return fmt.Sprintf("%d", inter.(int))
  176. case float64:
  177. return fmt.Sprintf("%f", inter.(float64))
  178. case bool:
  179. if inter.(bool) {
  180. return "true"
  181. } else {
  182. return "false"
  183. }
  184. case nil:
  185. return ""
  186. }
  187. return fmt.Sprintf("%v", inter)
  188. }
  189. func UnescapeHTML(x string) interface{} {
  190. return template.HTML(x)
  191. }
  192. func IntMax(a int, b int) int {
  193. if a >= b {
  194. return a
  195. } else {
  196. return b
  197. }
  198. }
  199. func GetUUID() string {
  200. code := uuid.New().String()
  201. code = strings.Replace(code, "-", "", -1)
  202. return code
  203. }
  204. const keyChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  205. func GenerateRandomCharsKey(length int) (string, error) {
  206. b := make([]byte, length)
  207. maxI := big.NewInt(int64(len(keyChars)))
  208. for i := range b {
  209. n, err := crand.Int(crand.Reader, maxI)
  210. if err != nil {
  211. return "", err
  212. }
  213. b[i] = keyChars[n.Int64()]
  214. }
  215. return string(b), nil
  216. }
  217. func GenerateRandomKey(length int) (string, error) {
  218. bytes := make([]byte, length*3/4) // 对于48位的输出,这里应该是36
  219. if _, err := crand.Read(bytes); err != nil {
  220. return "", err
  221. }
  222. return base64.StdEncoding.EncodeToString(bytes), nil
  223. }
  224. func GenerateKey() (string, error) {
  225. //rand.Seed(time.Now().UnixNano())
  226. return GenerateRandomCharsKey(48)
  227. }
  228. func GetRandomInt(max int) int {
  229. //rand.Seed(time.Now().UnixNano())
  230. return rand.Intn(max)
  231. }
  232. func GetTimestamp() int64 {
  233. return time.Now().Unix()
  234. }
  235. func GetTimeString() string {
  236. now := time.Now()
  237. return fmt.Sprintf("%s%d", now.Format("20060102150405"), now.UnixNano()%1e9)
  238. }
  239. func Max(a int, b int) int {
  240. if a >= b {
  241. return a
  242. } else {
  243. return b
  244. }
  245. }
  246. func MessageWithRequestId(message string, id string) string {
  247. return fmt.Sprintf("%s (request id: %s)", message, id)
  248. }
  249. func RandomSleep() {
  250. // Sleep for 0-3000 ms
  251. time.Sleep(time.Duration(rand.Intn(3000)) * time.Millisecond)
  252. }
  253. func GetPointer[T any](v T) *T {
  254. return &v
  255. }
  256. func Any2Type[T any](data any) (T, error) {
  257. var zero T
  258. bytes, err := json.Marshal(data)
  259. if err != nil {
  260. return zero, err
  261. }
  262. var res T
  263. err = json.Unmarshal(bytes, &res)
  264. if err != nil {
  265. return zero, err
  266. }
  267. return res, nil
  268. }
  269. // SaveTmpFile saves data to a temporary file. The filename would be apppended with a random string.
  270. func SaveTmpFile(filename string, data io.Reader) (string, error) {
  271. f, err := os.CreateTemp(os.TempDir(), filename)
  272. if err != nil {
  273. return "", errors.Wrapf(err, "failed to create temporary file %s", filename)
  274. }
  275. defer f.Close()
  276. _, err = io.Copy(f, data)
  277. if err != nil {
  278. return "", errors.Wrapf(err, "failed to copy data to temporary file %s", filename)
  279. }
  280. return f.Name(), nil
  281. }
  282. // BuildURL concatenates base and endpoint, returns the complete url string
  283. func BuildURL(base string, endpoint string) string {
  284. u, err := url.Parse(base)
  285. if err != nil {
  286. return base + endpoint
  287. }
  288. end := endpoint
  289. if end == "" {
  290. end = "/"
  291. }
  292. ref, err := url.Parse(end)
  293. if err != nil {
  294. return base + endpoint
  295. }
  296. return u.ResolveReference(ref).String()
  297. }