2019-09-23 03:47:38 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-12-17 04:34:58 +00:00
|
|
|
"encoding/json"
|
2019-09-23 03:47:38 +00:00
|
|
|
"fmt"
|
2020-05-29 05:07:53 +00:00
|
|
|
"github.com/apex/log"
|
2019-11-17 01:01:38 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-09-23 03:47:38 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-04-10 21:39:07 +00:00
|
|
|
// Initializes the requester instance.
|
2019-09-23 03:47:38 +00:00
|
|
|
func NewRequester() *PanelRequest {
|
|
|
|
return &PanelRequest{
|
|
|
|
Response: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PanelRequest struct {
|
|
|
|
Response *http.Response
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builds the base request instance that can be used with the HTTP client.
|
|
|
|
func (r *PanelRequest) GetClient() *http.Client {
|
|
|
|
return &http.Client{Timeout: time.Second * 30}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PanelRequest) SetHeaders(req *http.Request) *http.Request {
|
|
|
|
req.Header.Set("Accept", "application/vnd.pterodactyl.v1+json")
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2020-04-10 22:22:15 +00:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s.%s", config.Get().AuthenticationTokenId, config.Get().AuthenticationToken))
|
2019-09-23 03:47:38 +00:00
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PanelRequest) GetEndpoint(endpoint string) string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%s/api/remote/%s",
|
|
|
|
strings.TrimSuffix(config.Get().PanelLocation, "/"),
|
|
|
|
strings.TrimPrefix(strings.TrimPrefix(endpoint, "/"), "api/remote/"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-17 22:28:04 +00:00
|
|
|
// Logs the request into the debug log with all of the important request bits.
|
|
|
|
// The authorization key will be cleaned up before being output.
|
|
|
|
func (r *PanelRequest) logDebug(req *http.Request) {
|
|
|
|
headers := make(map[string][]string)
|
|
|
|
for k, v := range req.Header {
|
|
|
|
if k != "Authorization" || len(v) == 0 {
|
|
|
|
headers[k] = v
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
headers[k] = []string{v[0][0:15] + "(redacted)"}
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:07:53 +00:00
|
|
|
log.WithFields(log.Fields{
|
2020-09-05 19:08:40 +00:00
|
|
|
"method": req.Method,
|
2020-05-29 05:07:53 +00:00
|
|
|
"endpoint": req.URL.String(),
|
2020-09-05 19:08:40 +00:00
|
|
|
"headers": headers,
|
2020-05-29 05:07:53 +00:00
|
|
|
}).Debug("making request to external HTTP endpoint")
|
2020-05-17 22:28:04 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 03:47:38 +00:00
|
|
|
func (r *PanelRequest) Get(url string) (*http.Response, error) {
|
|
|
|
c := r.GetClient()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, r.GetEndpoint(url), nil)
|
|
|
|
req = r.SetHeaders(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-17 22:28:04 +00:00
|
|
|
r.logDebug(req)
|
2019-09-23 03:47:38 +00:00
|
|
|
|
|
|
|
return c.Do(req)
|
|
|
|
}
|
|
|
|
|
2019-12-07 23:53:07 +00:00
|
|
|
func (r *PanelRequest) Post(url string, data []byte) (*http.Response, error) {
|
|
|
|
c := r.GetClient()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, r.GetEndpoint(url), bytes.NewBuffer(data))
|
|
|
|
req = r.SetHeaders(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-17 22:28:04 +00:00
|
|
|
r.logDebug(req)
|
2019-12-07 23:53:07 +00:00
|
|
|
|
|
|
|
return c.Do(req)
|
|
|
|
}
|
|
|
|
|
2019-09-23 03:47:38 +00:00
|
|
|
// Determines if the API call encountered an error. If no request has been made
|
|
|
|
// the response will be false.
|
|
|
|
func (r *PanelRequest) HasError() bool {
|
|
|
|
if r.Response == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-12-07 23:53:07 +00:00
|
|
|
return r.Response.StatusCode >= 300 || r.Response.StatusCode < 200
|
2019-09-23 03:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reads the body from the response and returns it, then replaces it on the response
|
|
|
|
// so that it can be read again later.
|
|
|
|
func (r *PanelRequest) ReadBody() ([]byte, error) {
|
|
|
|
var b []byte
|
|
|
|
if r.Response == nil {
|
|
|
|
return nil, errors.New("no response exists on interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Response.Body != nil {
|
|
|
|
b, _ = ioutil.ReadAll(r.Response.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Response.Body = ioutil.NopCloser(bytes.NewBuffer(b))
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
2019-12-07 23:53:07 +00:00
|
|
|
|
|
|
|
func (r *PanelRequest) HttpResponseCode() int {
|
|
|
|
if r.Response == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Response.StatusCode
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:41:45 +00:00
|
|
|
func IsRequestError(err error) bool {
|
|
|
|
_, ok := err.(*RequestError)
|
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-12-17 04:34:58 +00:00
|
|
|
type RequestError struct {
|
2020-09-13 20:55:40 +00:00
|
|
|
response *http.Response
|
|
|
|
Code string `json:"code"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Detail string `json:"detail"`
|
2019-12-17 04:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the error response in a string form that can be more easily consumed.
|
2020-06-23 03:41:45 +00:00
|
|
|
func (re *RequestError) Error() string {
|
2020-09-13 20:55:40 +00:00
|
|
|
return fmt.Sprintf("Error response from Panel: %s: %s (HTTP/%d)", re.Code, re.Detail, re.response.StatusCode)
|
2019-12-17 04:34:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 03:41:45 +00:00
|
|
|
func (re *RequestError) String() string {
|
|
|
|
return re.Error()
|
|
|
|
}
|
|
|
|
|
2019-12-17 04:34:58 +00:00
|
|
|
type RequestErrorBag struct {
|
|
|
|
Errors []RequestError `json:"errors"`
|
|
|
|
}
|
|
|
|
|
2019-09-23 03:47:38 +00:00
|
|
|
// Returns the error message from the API call as a string. The error message will be formatted
|
|
|
|
// similar to the below example:
|
|
|
|
//
|
|
|
|
// HttpNotFoundException: The requested resource does not exist. (HTTP/404)
|
2019-12-17 04:34:58 +00:00
|
|
|
func (r *PanelRequest) Error() *RequestError {
|
|
|
|
body, _ := r.ReadBody()
|
2019-09-23 03:47:38 +00:00
|
|
|
|
2019-12-17 04:34:58 +00:00
|
|
|
bag := RequestErrorBag{}
|
|
|
|
json.Unmarshal(body, &bag)
|
2019-09-23 03:47:38 +00:00
|
|
|
|
2020-09-13 20:55:40 +00:00
|
|
|
e := new(RequestError)
|
|
|
|
if len(bag.Errors) > 0 {
|
|
|
|
e = &bag.Errors[0]
|
2019-09-23 03:47:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 20:55:40 +00:00
|
|
|
e.response = r.Response
|
|
|
|
|
|
|
|
return e
|
2019-12-17 04:34:58 +00:00
|
|
|
}
|