| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package docker
- /*
- containers.go
- Container listing and lifecycle (start/stop/restart/remove/inspect). Every
- container reference coming from a request is validated against a strict
- charset before it is handed to the docker CLI: because args are passed as
- discrete exec.Command elements there is no shell to inject into, but the
- validation additionally prevents a value like "-x" from being interpreted as
- a docker flag.
- */
- import (
- "encoding/json"
- "errors"
- "net/http"
- "regexp"
- "strings"
- "imuslab.com/arozos/mod/utils"
- )
- // containerRefRegex matches valid docker container IDs and names. Names cannot
- // start with a dash, which also blocks flag-injection through a reference.
- var containerRefRegex = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]*$`)
- // ContainerInfo mirrors the subset of `docker ps -a --format "{{json .}}"`
- // per-line output that the UI consumes. The docker CLI emits every value as a
- // string.
- type ContainerInfo struct {
- ID string `json:"ID"`
- Names string `json:"Names"`
- Image string `json:"Image"`
- Command string `json:"Command"`
- State string `json:"State"`
- Status string `json:"Status"`
- Ports string `json:"Ports"`
- CreatedAt string `json:"CreatedAt"`
- RunningFor string `json:"RunningFor"`
- Size string `json:"Size"`
- }
- // validateContainerRef rejects empty or malformed container references.
- func validateContainerRef(ref string) error {
- if ref == "" {
- return errors.New("empty container reference")
- }
- if !containerRefRegex.MatchString(ref) {
- return errors.New("invalid container reference")
- }
- return nil
- }
- // ListContainers returns all containers, running and stopped.
- func (d *DockerManager) ListContainers() ([]ContainerInfo, error) {
- out, err := runDocker("ps", "-a", "--no-trunc", "--format", "{{json .}}")
- if err != nil {
- return nil, err
- }
- return parseContainerList(out)
- }
- // parseContainerList parses the line-delimited JSON of `docker ps`. Malformed
- // lines are skipped defensively rather than failing the whole listing.
- func parseContainerList(out []byte) ([]ContainerInfo, error) {
- results := []ContainerInfo{}
- for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
- line = strings.TrimSpace(line)
- if line == "" {
- continue
- }
- var c ContainerInfo
- if err := json.Unmarshal([]byte(line), &c); err != nil {
- continue
- }
- results = append(results, c)
- }
- return results, nil
- }
- // simpleContainerAction runs a one-argument docker subcommand (start/stop/
- // restart) against a validated container reference.
- func (d *DockerManager) simpleContainerAction(action, ref string) error {
- if err := validateContainerRef(ref); err != nil {
- return err
- }
- _, err := runDocker(action, ref)
- return err
- }
- // RemoveContainer force-removes a container (stopping it first if running).
- func (d *DockerManager) RemoveContainer(ref string) error {
- if err := validateContainerRef(ref); err != nil {
- return err
- }
- _, err := runDocker("rm", "-f", ref)
- return err
- }
- // InspectContainer returns the raw `docker inspect` JSON for one container.
- func (d *DockerManager) InspectContainer(ref string) ([]byte, error) {
- if err := validateContainerRef(ref); err != nil {
- return nil, err
- }
- return runDocker("inspect", ref)
- }
- /* ---------- HTTP handlers (registered admin-only in src/docker.go) ---------- */
- // HandleContainerList serves the full container list as JSON.
- func (d *DockerManager) HandleContainerList(w http.ResponseWriter, r *http.Request) {
- containers, err := d.ListContainers()
- if err != nil {
- utils.SendErrorResponse(w, err.Error())
- return
- }
- js, _ := json.Marshal(containers)
- utils.SendJSONResponse(w, string(js))
- }
- // HandleContainerInspect serves raw inspect JSON for one container.
- func (d *DockerManager) HandleContainerInspect(w http.ResponseWriter, r *http.Request) {
- ref, err := utils.GetPara(r, "id")
- if err != nil {
- utils.SendErrorResponse(w, "missing container id")
- return
- }
- out, err := d.InspectContainer(ref)
- if err != nil {
- utils.SendErrorResponse(w, err.Error())
- return
- }
- utils.SendJSONResponse(w, string(out))
- }
- // handleContainerAction is the shared POST handler body for start/stop/restart/
- // remove. actionFn performs the work for the validated reference.
- func (d *DockerManager) handleContainerAction(w http.ResponseWriter, r *http.Request, actionFn func(ref string) error) {
- if r.Method != http.MethodPost {
- utils.SendErrorResponse(w, "method not allowed")
- return
- }
- ref, err := utils.PostPara(r, "id")
- if err != nil {
- utils.SendErrorResponse(w, "missing container id")
- return
- }
- if err := actionFn(ref); err != nil {
- utils.SendErrorResponse(w, err.Error())
- return
- }
- utils.SendOK(w)
- }
- func (d *DockerManager) HandleContainerStart(w http.ResponseWriter, r *http.Request) {
- d.handleContainerAction(w, r, func(ref string) error { return d.simpleContainerAction("start", ref) })
- }
- func (d *DockerManager) HandleContainerStop(w http.ResponseWriter, r *http.Request) {
- d.handleContainerAction(w, r, func(ref string) error { return d.simpleContainerAction("stop", ref) })
- }
- func (d *DockerManager) HandleContainerRestart(w http.ResponseWriter, r *http.Request) {
- d.handleContainerAction(w, r, func(ref string) error { return d.simpleContainerAction("restart", ref) })
- }
- func (d *DockerManager) HandleContainerRemove(w http.ResponseWriter, r *http.Request) {
- d.handleContainerAction(w, r, d.RemoveContainer)
- }
|