package docker import ( "context" "fmt" "strconv" "strings" "github.com/miaopanel/miaopanel/internal/site" "github.com/miaopanel/miaopanel/internal/store" ) // Bind creates an nginx reverse proxy site for one published container port. func (s *Service) Bind(ctx context.Context, ref string, hostPort int, domains []string) (*store.Site, error) { if !s.nginxReady() { return nil, fmt.Errorf("%w: 请先在应用商店安装 nginx,再绑定域名", ErrInvalid) } c, err := s.resolveContainer(ctx, ref) if err != nil { return nil, err } target, err := selectPort(c.Ports, hostPort) if err != nil { return nil, err } panel := panelPort(s.cfg.Addr) if target.HostPort == 80 || target.HostPort == 443 || target.HostPort == panel { return nil, fmt.Errorf("%w: 不能绑定 80/443 或面板端口 %d", ErrInvalid, panel) } cleanDomains := make([]string, 0, len(domains)) for _, d := range domains { d = strings.TrimSpace(d) if d != "" { cleanDomains = append(cleanDomains, d) } } if len(cleanDomains) == 0 { return nil, fmt.Errorf("%w: 至少填写一个域名", ErrInvalid) } slug := s.dockerSlug(ctx, c.Name, target.HostPort) st, err := s.sites.Create(ctx, site.Input{ Name: slug, Kind: "proxy", Domains: cleanDomains, Port: target.HostPort, Upstream: fmt.Sprintf("http://127.0.0.1:%d", target.HostPort), Remark: fmt.Sprintf("Docker %s:%d", c.Name, target.ContainerPort), Options: store.SiteOptions{ SecurityHead: true, WAF: true, WAFRate: 20, WAFBurst: 40, DockerContainerID: c.ID, DockerContainerName: c.Name, DockerContainerPort: target.ContainerPort, DockerHostPort: target.HostPort, }, }) if err != nil { return nil, err } return st, nil } func selectPort(ports []Port, hostPort int) (Port, error) { if hostPort > 0 { for _, p := range ports { if p.Published && p.HostPort == hostPort { return p, nil } } return Port{}, fmt.Errorf("%w: 容器没有发布宿主端口 %d", ErrInvalid, hostPort) } for _, p := range ports { if p.Published && p.HostPort > 0 { return p, nil } } return Port{}, fmt.Errorf("%w: 容器没有已发布的端口,请先用 -p 映射端口", ErrInvalid) } // Unbind removes the proxy site created for a container. func (s *Service) Unbind(ctx context.Context, siteID int64) error { st, err := s.db.SiteByID(ctx, siteID) if err != nil { return ErrInvalid } if st.Kind != "proxy" || st.Options.DockerContainerID == "" { return fmt.Errorf("%w: 该站点不是 Docker 域名绑定", ErrInvalid) } return s.sites.Delete(ctx, siteID, true) } func (s *Service) dockerSlug(ctx context.Context, name string, hostPort int) string { base := strings.ToLower(name) var b strings.Builder lastDash := false for _, r := range base { ok := (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '.' || r == '_' || r == '-' if ok { b.WriteRune(r) lastDash = r == '-' continue } if !lastDash { b.WriteByte('-') lastDash = true } } base = strings.Trim(b.String(), "-._") if base == "" { base = "container" } base = "docker-" + base + "-" + strconv.Itoa(hostPort) if len(base) > 48 { base = strings.TrimRight(base[:48], "-") } candidate := base for i := 2; i <= 99; i++ { if _, err := s.db.SiteBySlug(ctx, candidate); err != nil { return candidate } candidate = fmt.Sprintf("%s-%d", base, i) } return base + "-" + randomHex(3) }