diff --git a/src/api.go b/src/api.go index f367de2..aa47411 100644 --- a/src/api.go +++ b/src/api.go @@ -222,6 +222,8 @@ func initAPIs() { authRouter.HandleFunc("/api/info/pprof", pprof.Index) //If you got APIs to add, append them here + // get available docker containers + authRouter.HandleFunc("/api/docker/containers", handleDockerContainersList) } // Function to renders Auth related APIs diff --git a/src/docker.go b/src/docker.go new file mode 100644 index 0000000..71f67c9 --- /dev/null +++ b/src/docker.go @@ -0,0 +1,46 @@ +package main + +import ( + "context" + "encoding/json" + "net/http" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/client" + "imuslab.com/zoraxy/mod/utils" +) + +func handleDockerContainersList(w http.ResponseWriter, r *http.Request) { + apiClient, err := client.NewClientWithOpts(client.WithVersion("1.43")) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + defer apiClient.Close() + + containers, err := apiClient.ContainerList(context.Background(), container.ListOptions{All: true}) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + networks, err := apiClient.NetworkList(context.Background(), types.NetworkListOptions{}) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + result := map[string]interface{}{ + "network": networks, + "containers": containers, + } + + js, err := json.Marshal(result) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + utils.SendJSONResponse(w, string(js)) +} diff --git a/src/web/components/rules.html b/src/web/components/rules.html index e5b7913..5948fa8 100644 --- a/src/web/components/rules.html +++ b/src/web/components/rules.html @@ -26,7 +26,10 @@