utils.go 8.7 KB

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