miaopanel
1package docker
2
3import (
4 "context"
5 "fmt"
6 "strconv"
7 "strings"
8
9 "github.com/miaopanel/miaopanel/internal/site"
10 "github.com/miaopanel/miaopanel/internal/store"
11)
12
13// Bind creates an nginx reverse proxy site for one published container port.
14func (s *Service) Bind(ctx context.Context, ref string, hostPort int, domains []string) (*store.Site, error) {
15 if !s.nginxReady() {
16 return nil, fmt.Errorf("%w: 请先在应用商店安装 nginx,再绑定域名", ErrInvalid)
17 }
18 c, err := s.resolveContainer(ctx, ref)
19 if err != nil {
20 return nil, err
21 }
22 target, err := selectPort(c.Ports, hostPort)
23 if err != nil {
24 return nil, err
25 }
26 panel := panelPort(s.cfg.Addr)
27 if target.HostPort == 80 || target.HostPort == 443 || target.HostPort == panel {
28 return nil, fmt.Errorf("%w: 不能绑定 80/443 或面板端口 %d", ErrInvalid, panel)
29 }
30
31 cleanDomains := make([]string, 0, len(domains))
32 for _, d := range domains {
33 d = strings.TrimSpace(d)
34 if d != "" {
35 cleanDomains = append(cleanDomains, d)
36 }
37 }
38 if len(cleanDomains) == 0 {
39 return nil, fmt.Errorf("%w: 至少填写一个域名", ErrInvalid)
40 }
41
42 slug := s.dockerSlug(ctx, c.Name, target.HostPort)
43 st, err := s.sites.Create(ctx, site.Input{
44 Name: slug,
45 Kind: "proxy",
46 Domains: cleanDomains,
47 Port: target.HostPort,
48 Upstream: fmt.Sprintf("http://127.0.0.1:%d", target.HostPort),
49 Remark: fmt.Sprintf("Docker %s:%d", c.Name, target.ContainerPort),
50 Options: store.SiteOptions{
51 SecurityHead: true,
52 WAF: true,
53 WAFRate: 20,
54 WAFBurst: 40,
55 DockerContainerID: c.ID,
56 DockerContainerName: c.Name,
57 DockerContainerPort: target.ContainerPort,
58 DockerHostPort: target.HostPort,
59 },
60 })
61 if err != nil {
62 return nil, err
63 }
64 return st, nil
65}
66
67func selectPort(ports []Port, hostPort int) (Port, error) {
68 if hostPort > 0 {
69 for _, p := range ports {
70 if p.Published && p.HostPort == hostPort {
71 return p, nil
72 }
73 }
74 return Port{}, fmt.Errorf("%w: 容器没有发布宿主端口 %d", ErrInvalid, hostPort)
75 }
76 for _, p := range ports {
77 if p.Published && p.HostPort > 0 {
78 return p, nil
79 }
80 }
81 return Port{}, fmt.Errorf("%w: 容器没有已发布的端口,请先用 -p 映射端口", ErrInvalid)
82}
83
84// Unbind removes the proxy site created for a container.
85func (s *Service) Unbind(ctx context.Context, siteID int64) error {
86 st, err := s.db.SiteByID(ctx, siteID)
87 if err != nil {
88 return ErrInvalid
89 }
90 if st.Kind != "proxy" || st.Options.DockerContainerID == "" {
91 return fmt.Errorf("%w: 该站点不是 Docker 域名绑定", ErrInvalid)
92 }
93 return s.sites.Delete(ctx, siteID, true)
94}
95
96func (s *Service) dockerSlug(ctx context.Context, name string, hostPort int) string {
97 base := strings.ToLower(name)
98 var b strings.Builder
99 lastDash := false
100 for _, r := range base {
101 ok := (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '.' || r == '_' || r == '-'
102 if ok {
103 b.WriteRune(r)
104 lastDash = r == '-'
105 continue
106 }
107 if !lastDash {
108 b.WriteByte('-')
109 lastDash = true
110 }
111 }
112 base = strings.Trim(b.String(), "-._")
113 if base == "" {
114 base = "container"
115 }
116 base = "docker-" + base + "-" + strconv.Itoa(hostPort)
117 if len(base) > 48 {
118 base = strings.TrimRight(base[:48], "-")
119 }
120 candidate := base
121 for i := 2; i <= 99; i++ {
122 if _, err := s.db.SiteBySlug(ctx, candidate); err != nil {
123 return candidate
124 }
125 candidate = fmt.Sprintf("%s-%d", base, i)
126 }
127 return base + "-" + randomHex(3)
128}