|
| 1 | +package diagnostic |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/cloudflare/cloudflared/logger" |
| 12 | +) |
| 13 | + |
| 14 | +const configurationEndpoint = "diag/configuration" |
| 15 | + |
| 16 | +type httpClient struct { |
| 17 | + http.Client |
| 18 | + baseURL url.URL |
| 19 | +} |
| 20 | + |
| 21 | +func NewHTTPClient(baseURL url.URL) *httpClient { |
| 22 | + httpTransport := http.Transport{ |
| 23 | + TLSHandshakeTimeout: defaultTimeout, |
| 24 | + ResponseHeaderTimeout: defaultTimeout, |
| 25 | + } |
| 26 | + |
| 27 | + return &httpClient{ |
| 28 | + http.Client{ |
| 29 | + Transport: &httpTransport, |
| 30 | + Timeout: defaultTimeout, |
| 31 | + }, |
| 32 | + baseURL, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (client *httpClient) GET(ctx context.Context, url string) (*http.Response, error) { |
| 37 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("error creating GET request: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + req.Header.Add("Accept", "application/json;version=1") |
| 43 | + |
| 44 | + response, err := client.Do(req) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("error GET request: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + return response, nil |
| 50 | +} |
| 51 | + |
| 52 | +type LogConfiguration struct { |
| 53 | + logFile string |
| 54 | + logDirectory string |
| 55 | + uid int // the uid of the user that started cloudflared |
| 56 | +} |
| 57 | + |
| 58 | +func (client *httpClient) GetLogConfiguration(ctx context.Context) (*LogConfiguration, error) { |
| 59 | + endpoint, err := url.JoinPath(client.baseURL.String(), configurationEndpoint) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("error parsing URL: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + response, err := client.GET(ctx, endpoint) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + defer response.Body.Close() |
| 70 | + |
| 71 | + var data map[string]string |
| 72 | + if err := json.NewDecoder(response.Body).Decode(&data); err != nil { |
| 73 | + return nil, fmt.Errorf("failed to decode body: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + uidStr, exists := data[configurationKeyUID] |
| 77 | + if !exists { |
| 78 | + return nil, ErrKeyNotFound |
| 79 | + } |
| 80 | + |
| 81 | + uid, err := strconv.Atoi(uidStr) |
| 82 | + if err != nil { |
| 83 | + return nil, fmt.Errorf("error convertin pid to int: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + logFile, exists := data[logger.LogFileFlag] |
| 87 | + if exists { |
| 88 | + return &LogConfiguration{logFile, "", uid}, nil |
| 89 | + } |
| 90 | + |
| 91 | + logDirectory, exists := data[logger.LogDirectoryFlag] |
| 92 | + if exists { |
| 93 | + return &LogConfiguration{"", logDirectory, uid}, nil |
| 94 | + } |
| 95 | + |
| 96 | + return nil, ErrKeyNotFound |
| 97 | +} |
| 98 | + |
| 99 | +type HTTPClient interface { |
| 100 | + GetLogConfiguration(ctx context.Context) (LogConfiguration, error) |
| 101 | +} |
0 commit comments