package cmd import ( "bytes" "crypto/rand" "crypto/rsa" "crypto/sha1" "crypto/x509" "encoding/base64" "encoding/json" "fmt" "io" "net/url" "os" "os/exec" "runtime" "github.com/cozy/cozy-stack/client/request" "github.com/cozy/cozy-stack/cmd/browser" build "github.com/cozy/cozy-stack/pkg/config" "github.com/spf13/cobra" ) var toolsCmdGroup = &cobra.Command{ Use: "tools ", Short: "Regroup some tools for debugging and tests", RunE: func(cmd *cobra.Command, args []string) error { return cmd.Usage() }, } var heapCmd = &cobra.Command{ Use: "heap", Short: "Dump a sampling of memory allocations of live objects", Long: ` This command can be used for memory profiling. It dumps a sampling of memory allocations of live objects on stdout. See https://go.dev/doc/diagnostics#profiling. `, Example: "$ cozy-stack tools heap > heap.pprof && go tool pprof heap.pprof", RunE: func(cmd *cobra.Command, args []string) error { ac := newAdminClient() return dumpProfile(ac.ProfileHeap) }, } var flagGoroutineDebug int var goroutineCmd = &cobra.Command{ Use: "goroutine", Short: "Dump the stack traces of all current goroutines", Long: ` This command can be used to look for goroutine leaks. It dumps the stack traces of all current goroutines on stdout. See https://go.dev/doc/diagnostics#profiling. `, Example: "$ cozy-stack tools goroutine > goroutine.pprof && go tool pprof goroutine.pprof", RunE: func(cmd *cobra.Command, args []string) error { ac := newAdminClient() return dumpProfile(func() (io.ReadCloser, error) { return ac.ProfileGoroutine(flagGoroutineDebug) }) }, } func dumpProfile(fetch func() (io.ReadCloser, error)) error { profile, err := fetch() if err != nil { return err } _, err = io.Copy(os.Stdout, profile) if errc := profile.Close(); errc != nil && err == nil { err = errc } return err } var unxorDocumentID = &cobra.Command{ Use: "unxor-document-id ", Short: "transform the id of a shared document", Long: ` This command can be used when you have the identifier of a shared document on a recipient instance, and you want the identifier of the same document on the owner's instance. `, Example: `$ cozy-stack tools unxor-document-id bob.localhost:8080 7f47c470c7b1013a8a8818c04daba326 8cced87acb34b151cc8d7e864e0690ed`, RunE: func(cmd *cobra.Command, args []string) error { if len(args) != 3 { return cmd.Usage() } ac := newAdminClient() path := fmt.Sprintf("/instances/%s/sharings/%s/unxor/%s", args[0], args[1], args[2]) res, err := ac.Req(&request.Options{ Method: "GET", Path: path, }) if err != nil { return err } defer res.Body.Close() var data map[string]interface{} if err := json.NewDecoder(res.Body).Decode(&data); err != nil { return err } fmt.Fprintf(os.Stdout, "ID: %q\n", data["id"]) return nil }, } var encryptRSACmd = &cobra.Command{ Use: "encrypt-with-rsa