仰望星辰工作室

miaopanel

miaopanel/ internal/docker/parse.go 3.1 KB · 119 行 原始文件
1package docker
2
3import (
4 "encoding/json"
5 "fmt"
6 "strconv"
7 "strings"
8)
9
10// jsonLines parses one JSON object per line, skipping malformed rows.
11func jsonLines(text string) []map[string]any {
12 out := make([]map[string]any, 0, 32)
13 for _, line := range strings.Split(text, "\n") {
14 line = strings.TrimSpace(line)
15 if line == "" || !strings.HasPrefix(line, "{") {
16 continue
17 }
18 var row map[string]any
19 if err := json.Unmarshal([]byte(line), &row); err == nil {
20 out = append(out, row)
21 }
22 }
23 return out
24}
25
26func textField(row map[string]any, key string) string {
27 if v, ok := row[key].(string); ok {
28 return v
29 }
30 return ""
31}
32
33// parsePorts understands the `docker ps` Ports column, for example
34// "0.0.0.0:8080->80/tcp, :::8080->80/tcp" or "80/tcp".
35func parsePorts(raw string) []Port {
36 out := make([]Port, 0, 8)
37 seen := map[string]bool{}
38 for _, part := range strings.Split(raw, ",") {
39 part = strings.TrimSpace(part)
40 if part == "" {
41 continue
42 }
43 ports := parsePortEntry(part)
44 for _, p := range ports {
45 key := fmt.Sprintf("%d|%d|%s", p.HostPort, p.ContainerPort, p.Protocol)
46 if seen[key] {
47 continue
48 }
49 seen[key] = true
50 out = append(out, p)
51 }
52 }
53 return out
54}
55
56func parsePortEntry(part string) []Port {
57 if idx := strings.Index(part, "->"); idx >= 0 {
58 left := strings.TrimSpace(part[:idx])
59 right := strings.TrimSpace(part[idx+2:])
60 protocol := "tcp"
61 if j := strings.LastIndex(right, "/"); j >= 0 {
62 protocol = right[j+1:]
63 right = right[:j]
64 }
65 hostIP, hostStart, hostEnd := splitPortRange(left)
66 containerStart, containerEnd := splitRange(right)
67 if hostStart == 0 {
68 return []Port{{HostIP: hostIP, ContainerPort: containerStart, Protocol: protocol}}
69 }
70 if hostEnd > hostStart && containerEnd > containerStart &&
71 hostEnd-hostStart == containerEnd-containerStart && hostEnd-hostStart < 100 {
72 out := make([]Port, 0, hostEnd-hostStart+1)
73 for i := 0; i <= hostEnd-hostStart; i++ {
74 out = append(out, Port{
75 HostIP: hostIP, HostPort: hostStart + i,
76 ContainerPort: containerStart + i, Protocol: protocol, Published: true,
77 })
78 }
79 return out
80 }
81 return []Port{{
82 HostIP: hostIP, HostPort: hostStart, ContainerPort: containerStart,
83 Protocol: protocol, Published: true,
84 }}
85 }
86
87 // "8080/tcp" or "80/udp": exposed but not published to the host.
88 containerPart := part
89 protocol := "tcp"
90 if j := strings.LastIndex(containerPart, "/"); j >= 0 {
91 protocol = containerPart[j+1:]
92 containerPart = containerPart[:j]
93 }
94 container, _ := splitRange(containerPart)
95 return []Port{{ContainerPort: container, Protocol: protocol}}
96}
97
98func splitPortRange(raw string) (string, int, int) {
99 raw = strings.TrimSpace(raw)
100 if i := strings.LastIndex(raw, ":"); i >= 0 {
101 host := raw[:i]
102 raw = raw[i+1:]
103 start, end := splitRange(raw)
104 return host, start, end
105 }
106 start, end := splitRange(raw)
107 return "", start, end
108}
109
110func splitRange(raw string) (int, int) {
111 raw = strings.TrimSpace(strings.TrimPrefix(raw, ":"))
112 if i := strings.Index(raw, "-"); i >= 0 {
113 a, _ := strconv.Atoi(raw[:i])
114 b, _ := strconv.Atoi(raw[i+1:])
115 return a, b
116 }
117 p, _ := strconv.Atoi(raw)
118 return p, p
119}