11package cmd
22
33import (
4+ "bytes"
45 "errors"
56 "fmt"
67 "io"
8+ "math/rand"
9+ "mime/multipart"
710 "net/http"
811 "net/url"
9- "os"
1012 "os/exec"
1113 "path"
1214 "strconv"
@@ -28,15 +30,15 @@ import (
2830)
2931
3032const (
31- DefaultHastebinUrl = "https://ptero.co "
33+ DefaultPastebinUrl = "https://pb.kubectyl.org "
3234 DefaultLogLines = 200
3335)
3436
3537var diagnosticsArgs struct {
3638 IncludeEndpoints bool
3739 IncludeLogs bool
3840 ReviewBeforeUpload bool
39- HastebinURL string
41+ PastebinURL string
4042 LogLines int
4143}
4244
@@ -51,7 +53,7 @@ func newDiagnosticsCommand() *cobra.Command {
5153 Run : diagnosticsCmdRun ,
5254 }
5355
54- command .Flags ().StringVar (& diagnosticsArgs .HastebinURL , "hastebin-url" , DefaultHastebinUrl , "the url of the hastebin instance to use" )
56+ command .Flags ().StringVar (& diagnosticsArgs .PastebinURL , "hastebin-url" , DefaultPastebinUrl , "the url of the hastebin instance to use" )
5557 command .Flags ().IntVar (& diagnosticsArgs .LogLines , "log-lines" , DefaultLogLines , "the number of log lines to include in the report" )
5658
5759 return command
@@ -77,7 +79,7 @@ func diagnosticsCmdRun(*cobra.Command, []string) {
7779 {
7880 Name : "ReviewBeforeUpload" ,
7981 Prompt : & survey.Confirm {
80- Message : "Do you want to review the collected data before uploading to " + diagnosticsArgs .HastebinURL + "?" ,
82+ Message : "Do you want to review the collected data before uploading to " + diagnosticsArgs .PastebinURL + "?" ,
8183 Help : "The data, especially the logs, might contain sensitive information, so you should review it. You will be asked again if you want to upload." ,
8284 Default : true ,
8385 },
@@ -112,27 +114,17 @@ func diagnosticsCmdRun(*cobra.Command, []string) {
112114 {"Logs Directory" , cfg .System .LogDirectory },
113115 {"Data Directory" , cfg .System .Data },
114116 {"Archive Directory" , cfg .System .ArchiveDirectory },
115- {"Backup Directory" , cfg .System .BackupDirectory },
116117
117- {"Username" , cfg .System .Username },
118118 {"Server Time" , time .Now ().Format (time .RFC1123Z )},
119119 {"Debug Mode" , fmt .Sprintf ("%t" , cfg .Debug )},
120120 }
121121
122- table := tablewriter .NewWriter (os . Stdout )
122+ table := tablewriter .NewWriter (output )
123123 table .SetHeader ([]string {"Variable" , "Value" })
124124 table .SetRowLine (true )
125125 table .AppendBulk (data )
126126 table .Render ()
127127
128- printHeader (output , "Docker: Running Containers" )
129- c := exec .Command ("docker" , "ps" )
130- if co , err := c .Output (); err == nil {
131- output .Write (co )
132- } else {
133- fmt .Fprint (output , "Couldn't list containers: " , err )
134- }
135-
136128 printHeader (output , "Latest Kuber Logs" )
137129 if diagnosticsArgs .IncludeLogs {
138130 p := "/var/log/kubectyl/kuber.log"
@@ -162,16 +154,38 @@ func diagnosticsCmdRun(*cobra.Command, []string) {
162154 fmt .Println (output .String ())
163155 fmt .Print ("--------------- end of report ---------------\n \n " )
164156
165- // upload := !diagnosticsArgs.ReviewBeforeUpload
166- // if !upload {
167- // survey.AskOne(&survey.Confirm{Message: "Upload to " + diagnosticsArgs.HastebinURL + "?", Default: false}, &upload)
168- // }
169- // if upload {
170- // u, err := uploadToHastebin(diagnosticsArgs.HastebinURL, output.String())
171- // if err == nil {
172- // fmt.Println("Your report is available here: ", u)
173- // }
174- // }
157+ upload := ! diagnosticsArgs .ReviewBeforeUpload
158+ if ! upload {
159+ survey .AskOne (& survey.Confirm {Message : "Upload to " + diagnosticsArgs .PastebinURL + "?" , Default : false }, & upload )
160+ }
161+ if upload {
162+ passwordFunc := func (length int ) string {
163+ charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
164+ password := make ([]byte , length )
165+
166+ for i := 0 ; i < length ; i ++ {
167+ password [i ] = charset [rand .Intn (len (charset ))]
168+ }
169+
170+ return string (password )
171+ }
172+ rand .Seed (time .Now ().UnixNano ())
173+
174+ password := passwordFunc (8 )
175+ result , err := uploadToPastebin (diagnosticsArgs .PastebinURL , output .String (), password )
176+ if err == nil {
177+ seconds , err := strconv .Atoi (fmt .Sprintf ("%v" , result ["expire" ]))
178+ if err != nil {
179+ return
180+ }
181+
182+ expireTime := fmt .Sprintf ("%d hours, %d minutes, %d seconds" , seconds / 3600 , (seconds % 3600 )/ 60 , seconds % 60 )
183+
184+ fmt .Println ("Your report is available here:" , result ["url" ])
185+ fmt .Println ("Will expire in" , expireTime )
186+ fmt .Printf ("You can edit your pastebin here: %s\n " , result ["admin" ])
187+ }
188+ }
175189}
176190
177191// func getDockerInfo() (types.Version, types.Info, error) {
@@ -190,31 +204,45 @@ func diagnosticsCmdRun(*cobra.Command, []string) {
190204// return dockerVersion, dockerInfo, nil
191205// }
192206
193- func uploadToHastebin (hbUrl , content string ) (string , error ) {
194- r := strings .NewReader (content )
195- u , err := url .Parse (hbUrl )
207+ func uploadToPastebin (pbURL , content , password string ) (map [string ]interface {}, error ) {
208+ payload := & bytes.Buffer {}
209+ writer := multipart .NewWriter (payload )
210+ writer .WriteField ("c" , content )
211+ writer .WriteField ("e" , "300" )
212+ writer .WriteField ("s" , password )
213+ writer .Close ()
214+
215+ u , err := url .Parse (pbURL )
196216 if err != nil {
197- return "" , err
217+ return nil , err
198218 }
199- u .Path = path .Join (u .Path , "documents" )
200- res , err := http .Post (u .String (), "plain/text" , r )
219+
220+ req , err := http .NewRequest ("POST" , u .String (), payload )
221+ if err != nil {
222+ return nil , err
223+ }
224+ req .Header .Set ("Content-Type" , writer .FormDataContentType ())
225+
226+ client := & http.Client {}
227+ res , err := client .Do (req )
201228 if err != nil || res .StatusCode != 200 {
202- fmt .Println ("Failed to upload report to " , u .String (), err )
203- return "" , err
229+ fmt .Println ("Failed to upload report to" , u .String (), err )
230+ return nil , err
204231 }
232+
205233 pres := make (map [string ]interface {})
206234 body , err := io .ReadAll (res .Body )
207235 if err != nil {
208236 fmt .Println ("Failed to parse response." , err )
209- return "" , err
237+ return nil , err
210238 }
211239 json .Unmarshal (body , & pres )
212- if key , ok := pres ["key" ].(string ); ok {
213- u , _ := url .Parse (hbUrl )
214- u .Path = path .Join (u .Path , key )
215- return u .String (), nil
240+ if key , ok := pres ["url" ].(string ); ok {
241+ u .Path = key
242+ return pres , nil
216243 }
217- return "" , errors .New ("failed to find key in response" )
244+
245+ return nil , errors .New ("failed to find key in response" )
218246}
219247
220248func redact (s string ) string {
0 commit comments