|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "path" |
| 11 | + "regexp" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/AlecAivazis/survey/v2" |
| 15 | + "github.com/AlecAivazis/survey/v2/terminal" |
| 16 | + "github.com/goccy/go-json" |
| 17 | + "github.com/spf13/cobra" |
| 18 | + |
| 19 | + "github.com/pterodactyl/wings/config" |
| 20 | +) |
| 21 | + |
| 22 | +var configureArgs struct { |
| 23 | + PanelURL string |
| 24 | + Token string |
| 25 | + ConfigPath string |
| 26 | + Node string |
| 27 | + Override bool |
| 28 | + AllowInsecure bool |
| 29 | +} |
| 30 | + |
| 31 | +var nodeIdRegex = regexp.MustCompile(`^(\d+)$`) |
| 32 | + |
| 33 | +var configureCmd = &cobra.Command{ |
| 34 | + Use: "configure", |
| 35 | + Short: "Use a token to configure wings automatically", |
| 36 | + Run: configureCmdRun, |
| 37 | +} |
| 38 | + |
| 39 | +func init() { |
| 40 | + configureCmd.PersistentFlags().StringVarP(&configureArgs.PanelURL, "panel-url", "p", "", "The base URL for this daemon's panel") |
| 41 | + configureCmd.PersistentFlags().StringVarP(&configureArgs.Token, "token", "t", "", "The API key to use for fetching node information") |
| 42 | + configureCmd.PersistentFlags().StringVarP(&configureArgs.Node, "node", "n", "", "The ID of the node which will be connected to this daemon") |
| 43 | + configureCmd.PersistentFlags().StringVarP(&configureArgs.ConfigPath, "config-path", "c", config.DefaultLocation, "The path where the configuration file should be made") |
| 44 | + configureCmd.PersistentFlags().BoolVar(&configureArgs.Override, "override", false, "Set to true to override an existing configuration for this node") |
| 45 | + configureCmd.PersistentFlags().BoolVar(&configureArgs.AllowInsecure, "allow-insecure", false, "Set to true to disable certificate checking") |
| 46 | +} |
| 47 | + |
| 48 | +func configureCmdRun(cmd *cobra.Command, args []string) { |
| 49 | + if configureArgs.AllowInsecure { |
| 50 | + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{ |
| 51 | + InsecureSkipVerify: true, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if _, err := os.Stat(configureArgs.ConfigPath); err == nil && !configureArgs.Override { |
| 56 | + survey.AskOne(&survey.Confirm{Message: "Override existing configuration file"}, &configureArgs.Override) |
| 57 | + if !configureArgs.Override { |
| 58 | + fmt.Println("Aborting process; a configuration file already exists for this node.") |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | + } else if err != nil && !os.IsNotExist(err) { |
| 62 | + panic(err) |
| 63 | + } |
| 64 | + |
| 65 | + var questions []*survey.Question |
| 66 | + if configureArgs.PanelURL == "" { |
| 67 | + questions = append(questions, &survey.Question{ |
| 68 | + Name: "PanelURL", |
| 69 | + Prompt: &survey.Input{Message: "Panel URL: "}, |
| 70 | + Validate: func(ans interface{}) error { |
| 71 | + if str, ok := ans.(string); ok { |
| 72 | + _, err := url.ParseRequestURI(str) |
| 73 | + return err |
| 74 | + } |
| 75 | + return nil |
| 76 | + }, |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + if configureArgs.Token == "" { |
| 81 | + questions = append(questions, &survey.Question{ |
| 82 | + Name: "Token", |
| 83 | + Prompt: &survey.Input{Message: "API Token: "}, |
| 84 | + Validate: func(ans interface{}) error { |
| 85 | + if str, ok := ans.(string); ok { |
| 86 | + if len(str) == 0 { |
| 87 | + return fmt.Errorf("please provide a valid authentication token") |
| 88 | + } |
| 89 | + } |
| 90 | + return nil |
| 91 | + }, |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + if configureArgs.Node == "" { |
| 96 | + questions = append(questions, &survey.Question{ |
| 97 | + Name: "Node", |
| 98 | + Prompt: &survey.Input{Message: "Node ID: "}, |
| 99 | + Validate: func(ans interface{}) error { |
| 100 | + if str, ok := ans.(string); ok { |
| 101 | + if !nodeIdRegex.Match([]byte(str)) { |
| 102 | + return fmt.Errorf("please provide a valid authentication token") |
| 103 | + } |
| 104 | + } |
| 105 | + return nil |
| 106 | + }, |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + if err := survey.Ask(questions, &configureArgs); err != nil { |
| 111 | + if err == terminal.InterruptErr { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + panic(err) |
| 116 | + } |
| 117 | + |
| 118 | + c := &http.Client{ |
| 119 | + Timeout: time.Second * 30, |
| 120 | + } |
| 121 | + |
| 122 | + req, err := getRequest() |
| 123 | + if err != nil { |
| 124 | + panic(err) |
| 125 | + } |
| 126 | + |
| 127 | + fmt.Printf("%+v", req.Header) |
| 128 | + fmt.Printf(req.URL.String()) |
| 129 | + |
| 130 | + res, err := c.Do(req) |
| 131 | + if err != nil { |
| 132 | + fmt.Println("Failed to fetch configuration from the panel.\n", err.Error()) |
| 133 | + os.Exit(1) |
| 134 | + } |
| 135 | + defer res.Body.Close() |
| 136 | + |
| 137 | + if res.StatusCode == http.StatusForbidden || res.StatusCode == http.StatusUnauthorized { |
| 138 | + fmt.Println("The authentication credentials provided were not valid.") |
| 139 | + os.Exit(1) |
| 140 | + } else if res.StatusCode != http.StatusOK { |
| 141 | + b, _ := io.ReadAll(res.Body) |
| 142 | + |
| 143 | + fmt.Println("An error occurred while processing this request.\n", string(b)) |
| 144 | + os.Exit(1) |
| 145 | + } |
| 146 | + |
| 147 | + b, err := io.ReadAll(res.Body) |
| 148 | + |
| 149 | + cfg, err := config.NewAtPath(configPath) |
| 150 | + if err != nil { |
| 151 | + panic(err) |
| 152 | + } |
| 153 | + |
| 154 | + if err := json.Unmarshal(b, cfg); err != nil { |
| 155 | + panic(err) |
| 156 | + } |
| 157 | + |
| 158 | + if err = config.WriteToDisk(cfg); err != nil { |
| 159 | + panic(err) |
| 160 | + } |
| 161 | + |
| 162 | + fmt.Println("Successfully configured wings.") |
| 163 | +} |
| 164 | + |
| 165 | +func getRequest() (*http.Request, error) { |
| 166 | + u, err := url.Parse(configureArgs.PanelURL) |
| 167 | + if err != nil { |
| 168 | + panic(err) |
| 169 | + } |
| 170 | + |
| 171 | + u.Path = path.Join(u.Path, fmt.Sprintf("api/application/nodes/%s/configuration", configureArgs.Node)) |
| 172 | + |
| 173 | + r, err := http.NewRequest(http.MethodGet, u.String(), nil) |
| 174 | + if err != nil { |
| 175 | + return nil, err |
| 176 | + } |
| 177 | + |
| 178 | + r.Header.Set("Accept", "application/vnd.pterodactyl.v1+json") |
| 179 | + r.Header.Set("Content-Type", "application/json") |
| 180 | + r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", configureArgs.Token)) |
| 181 | + |
| 182 | + return r, nil |
| 183 | +} |
0 commit comments