2020-07-04 20:57:54 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
2020-10-01 23:04:58 +00:00
|
|
|
"strconv"
|
2020-07-04 20:57:54 +00:00
|
|
|
"strings"
|
2020-10-11 22:23:55 +00:00
|
|
|
"time"
|
2020-07-04 20:57:54 +00:00
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/AlecAivazis/survey/v2/terminal"
|
2021-01-15 04:32:38 +00:00
|
|
|
"github.com/apex/log"
|
2020-07-04 20:57:54 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2020-11-28 23:57:10 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers/operatingsystem"
|
2021-08-02 21:07:00 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-07-04 20:57:54 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2021-01-15 04:32:38 +00:00
|
|
|
"github.com/pterodactyl/wings/environment"
|
|
|
|
"github.com/pterodactyl/wings/loggers/cli"
|
2020-07-04 20:57:54 +00:00
|
|
|
"github.com/pterodactyl/wings/system"
|
|
|
|
)
|
|
|
|
|
2021-11-15 17:24:52 +00:00
|
|
|
const (
|
|
|
|
DefaultHastebinUrl = "https://ptero.co"
|
|
|
|
DefaultLogLines = 200
|
2020-07-04 20:57:54 +00:00
|
|
|
)
|
|
|
|
|
2021-11-15 17:24:52 +00:00
|
|
|
var diagnosticsArgs struct {
|
|
|
|
IncludeEndpoints bool
|
|
|
|
IncludeLogs bool
|
|
|
|
ReviewBeforeUpload bool
|
|
|
|
HastebinURL string
|
|
|
|
LogLines int
|
|
|
|
}
|
|
|
|
|
2021-01-15 04:32:38 +00:00
|
|
|
func newDiagnosticsCommand() *cobra.Command {
|
|
|
|
command := &cobra.Command{
|
|
|
|
Use: "diagnostics",
|
|
|
|
Short: "Collect and report information about this Wings instance to assist in debugging.",
|
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
initConfig()
|
|
|
|
log.SetHandler(cli.Default)
|
|
|
|
},
|
|
|
|
Run: diagnosticsCmdRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
command.Flags().StringVar(&diagnosticsArgs.HastebinURL, "hastebin-url", DefaultHastebinUrl, "the url of the hastebin instance to use")
|
|
|
|
command.Flags().IntVar(&diagnosticsArgs.LogLines, "log-lines", DefaultLogLines, "the number of log lines to include in the report")
|
2020-07-04 20:57:54 +00:00
|
|
|
|
2021-01-15 04:32:38 +00:00
|
|
|
return command
|
2020-07-04 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// diagnosticsCmdRun collects diagnostics about wings, it's configuration and the node.
|
|
|
|
// We collect:
|
|
|
|
// - wings and docker versions
|
|
|
|
// - relevant parts of daemon configuration
|
|
|
|
// - the docker debug output
|
|
|
|
// - running docker containers
|
|
|
|
// - logs
|
|
|
|
func diagnosticsCmdRun(cmd *cobra.Command, args []string) {
|
|
|
|
questions := []*survey.Question{
|
|
|
|
{
|
|
|
|
Name: "IncludeEndpoints",
|
|
|
|
Prompt: &survey.Confirm{Message: "Do you want to include endpoints (i.e. the FQDN/IP of your panel)?", Default: false},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "IncludeLogs",
|
|
|
|
Prompt: &survey.Confirm{Message: "Do you want to include the latest logs?", Default: true},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ReviewBeforeUpload",
|
|
|
|
Prompt: &survey.Confirm{
|
2021-11-15 17:56:43 +00:00
|
|
|
Message: "Do you want to review the collected data before uploading to " + diagnosticsArgs.HastebinURL + "?",
|
2020-09-05 19:08:40 +00:00
|
|
|
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.",
|
2020-07-04 20:57:54 +00:00
|
|
|
Default: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := survey.Ask(questions, &diagnosticsArgs); err != nil {
|
|
|
|
if err == terminal.InterruptErr {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dockerVersion, dockerInfo, dockerErr := getDockerInfo()
|
|
|
|
|
|
|
|
output := &strings.Builder{}
|
2020-09-05 19:08:40 +00:00
|
|
|
fmt.Fprintln(output, "Pterodactyl Wings - Diagnostics Report")
|
2020-07-04 20:57:54 +00:00
|
|
|
printHeader(output, "Versions")
|
2021-11-15 17:56:43 +00:00
|
|
|
fmt.Fprintln(output, " Wings:", system.Version)
|
2020-07-04 20:57:54 +00:00
|
|
|
if dockerErr == nil {
|
2021-11-15 17:56:43 +00:00
|
|
|
fmt.Fprintln(output, " Docker:", dockerVersion.Version)
|
2020-07-04 20:57:54 +00:00
|
|
|
}
|
|
|
|
if v, err := kernel.GetKernelVersion(); err == nil {
|
2021-11-15 17:56:43 +00:00
|
|
|
fmt.Fprintln(output, " Kernel:", v)
|
2020-07-04 20:57:54 +00:00
|
|
|
}
|
|
|
|
if os, err := operatingsystem.GetOperatingSystem(); err == nil {
|
2021-11-15 17:56:43 +00:00
|
|
|
fmt.Fprintln(output, " OS:", os)
|
2020-07-04 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printHeader(output, "Wings Configuration")
|
2021-01-15 04:32:38 +00:00
|
|
|
if err := config.FromFile(config.DefaultLocation); err != nil {
|
|
|
|
}
|
|
|
|
cfg := config.Get()
|
2021-11-15 17:56:43 +00:00
|
|
|
fmt.Fprintln(output, " Panel Location:", redact(cfg.PanelLocation))
|
2021-01-15 04:32:38 +00:00
|
|
|
fmt.Fprintln(output, "")
|
2021-11-15 17:56:43 +00:00
|
|
|
fmt.Fprintln(output, " Internal Webserver:", redact(cfg.Api.Host), ":", cfg.Api.Port)
|
|
|
|
fmt.Fprintln(output, " SSL Enabled:", cfg.Api.Ssl.Enabled)
|
|
|
|
fmt.Fprintln(output, " SSL Certificate:", redact(cfg.Api.Ssl.CertificateFile))
|
|
|
|
fmt.Fprintln(output, " SSL Key:", redact(cfg.Api.Ssl.KeyFile))
|
2021-01-15 04:32:38 +00:00
|
|
|
fmt.Fprintln(output, "")
|
2021-11-15 17:56:43 +00:00
|
|
|
fmt.Fprintln(output, " SFTP Server:", redact(cfg.System.Sftp.Address), ":", cfg.System.Sftp.Port)
|
|
|
|
fmt.Fprintln(output, " SFTP Read-Only:", cfg.System.Sftp.ReadOnly)
|
2021-01-15 04:32:38 +00:00
|
|
|
fmt.Fprintln(output, "")
|
2021-11-15 17:56:43 +00:00
|
|
|
fmt.Fprintln(output, " Root Directory:", cfg.System.RootDirectory)
|
|
|
|
fmt.Fprintln(output, " Logs Directory:", cfg.System.LogDirectory)
|
|
|
|
fmt.Fprintln(output, " Data Directory:", cfg.System.Data)
|
|
|
|
fmt.Fprintln(output, " Archive Directory:", cfg.System.ArchiveDirectory)
|
|
|
|
fmt.Fprintln(output, " Backup Directory:", cfg.System.BackupDirectory)
|
2021-01-15 04:32:38 +00:00
|
|
|
fmt.Fprintln(output, "")
|
2021-11-15 17:56:43 +00:00
|
|
|
fmt.Fprintln(output, " Username:", cfg.System.Username)
|
|
|
|
fmt.Fprintln(output, " Server Time:", time.Now().Format(time.RFC1123Z))
|
|
|
|
fmt.Fprintln(output, " Debug Mode:", cfg.Debug)
|
2020-07-04 20:57:54 +00:00
|
|
|
|
|
|
|
printHeader(output, "Docker: Info")
|
2021-01-15 04:32:38 +00:00
|
|
|
if dockerErr == nil {
|
|
|
|
fmt.Fprintln(output, "Server Version:", dockerInfo.ServerVersion)
|
|
|
|
fmt.Fprintln(output, "Storage Driver:", dockerInfo.Driver)
|
|
|
|
if dockerInfo.DriverStatus != nil {
|
|
|
|
for _, pair := range dockerInfo.DriverStatus {
|
|
|
|
fmt.Fprintf(output, " %s: %s\n", pair[0], pair[1])
|
|
|
|
}
|
2020-07-04 20:57:54 +00:00
|
|
|
}
|
2021-01-15 04:32:38 +00:00
|
|
|
if dockerInfo.SystemStatus != nil {
|
|
|
|
for _, pair := range dockerInfo.SystemStatus {
|
|
|
|
fmt.Fprintf(output, " %s: %s\n", pair[0], pair[1])
|
|
|
|
}
|
2020-07-04 20:57:54 +00:00
|
|
|
}
|
2021-01-15 04:32:38 +00:00
|
|
|
fmt.Fprintln(output, "LoggingDriver:", dockerInfo.LoggingDriver)
|
|
|
|
fmt.Fprintln(output, " CgroupDriver:", dockerInfo.CgroupDriver)
|
|
|
|
if len(dockerInfo.Warnings) > 0 {
|
|
|
|
for _, w := range dockerInfo.Warnings {
|
|
|
|
fmt.Fprintln(output, w)
|
|
|
|
}
|
2020-07-04 20:57:54 +00:00
|
|
|
}
|
2021-01-15 04:32:38 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(output, dockerErr.Error())
|
2020-07-04 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printHeader(output, "Docker: Running Containers")
|
|
|
|
c := exec.Command("docker", "ps")
|
|
|
|
if co, err := c.Output(); err == nil {
|
|
|
|
output.Write(co)
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(output, "Couldn't list containers: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
printHeader(output, "Latest Wings Logs")
|
|
|
|
if diagnosticsArgs.IncludeLogs {
|
2020-10-01 23:04:58 +00:00
|
|
|
p := "/var/log/pterodactyl/wings.log"
|
|
|
|
if cfg != nil {
|
|
|
|
p = path.Join(cfg.System.LogDirectory, "wings.log")
|
|
|
|
}
|
|
|
|
if c, err := exec.Command("tail", "-n", strconv.Itoa(diagnosticsArgs.LogLines), p).Output(); err != nil {
|
|
|
|
fmt.Fprintln(output, "No logs found or an error occurred.")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(output, "%s\n", string(c))
|
|
|
|
}
|
2020-07-04 20:57:54 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(output, "Logs redacted.")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("\n--------------- generated report ---------------")
|
|
|
|
fmt.Println(output.String())
|
|
|
|
fmt.Print("--------------- end of report ---------------\n\n")
|
|
|
|
|
|
|
|
upload := !diagnosticsArgs.ReviewBeforeUpload
|
|
|
|
if !upload {
|
|
|
|
survey.AskOne(&survey.Confirm{Message: "Upload to " + diagnosticsArgs.HastebinURL + "?", Default: false}, &upload)
|
|
|
|
}
|
|
|
|
if upload {
|
2021-01-15 04:32:38 +00:00
|
|
|
u, err := uploadToHastebin(diagnosticsArgs.HastebinURL, output.String())
|
2020-07-04 20:57:54 +00:00
|
|
|
if err == nil {
|
2021-01-15 04:32:38 +00:00
|
|
|
fmt.Println("Your report is available here: ", u)
|
2020-07-04 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDockerInfo() (types.Version, types.Info, error) {
|
2021-01-15 04:32:38 +00:00
|
|
|
client, err := environment.Docker()
|
2020-07-04 20:57:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.Version{}, types.Info{}, err
|
|
|
|
}
|
2021-01-15 04:32:38 +00:00
|
|
|
dockerVersion, err := client.ServerVersion(context.Background())
|
2020-07-04 20:57:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.Version{}, types.Info{}, err
|
|
|
|
}
|
2021-01-15 04:32:38 +00:00
|
|
|
dockerInfo, err := client.Info(context.Background())
|
2020-07-04 20:57:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.Version{}, types.Info{}, err
|
|
|
|
}
|
|
|
|
return dockerVersion, dockerInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadToHastebin(hbUrl, content string) (string, error) {
|
|
|
|
r := strings.NewReader(content)
|
|
|
|
u, err := url.Parse(hbUrl)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
u.Path = path.Join(u.Path, "documents")
|
|
|
|
res, err := http.Post(u.String(), "plain/text", r)
|
|
|
|
if err != nil || res.StatusCode != 200 {
|
|
|
|
fmt.Println("Failed to upload report to ", u.String(), err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
pres := make(map[string]interface{})
|
2021-11-15 17:24:52 +00:00
|
|
|
body, err := io.ReadAll(res.Body)
|
2020-07-04 20:57:54 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to parse response.", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
json.Unmarshal(body, &pres)
|
|
|
|
if key, ok := pres["key"].(string); ok {
|
|
|
|
u, _ := url.Parse(hbUrl)
|
|
|
|
u.Path = path.Join(u.Path, key)
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
2020-09-05 19:08:40 +00:00
|
|
|
return "", errors.New("failed to find key in response")
|
2020-07-04 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func redact(s string) string {
|
|
|
|
if !diagnosticsArgs.IncludeEndpoints {
|
|
|
|
return "{redacted}"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func printHeader(w io.Writer, title string) {
|
|
|
|
fmt.Fprintln(w, "\n|\n|", title)
|
|
|
|
fmt.Fprintln(w, "| ------------------------------")
|
|
|
|
}
|