| 1 | package gotify |
| 2 | |
| 3 | import ( |
| 4 | "crypto/tls" |
| 5 | "net/http" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient" |
| 9 | ) |
| 10 | |
| 11 | // HTTPClientManager handles HTTP client creation and configuration. |
| 12 | type HTTPClientManager interface { |
| 13 | CreateTransport(config *Config) *http.Transport |
| 14 | CreateClient(transport *http.Transport) *http.Client |
| 15 | } |
| 16 | |
| 17 | // DefaultHTTPClientManager provides the default implementation of HTTPClientManager. |
| 18 | type DefaultHTTPClientManager struct{} |
| 19 | |
| 20 | const ( |
| 21 | // HTTPTimeout defines the HTTP client timeout in seconds. |
| 22 | HTTPTimeout = 10 |
| 23 | ) |
| 24 | |
| 25 | // CreateClient creates an HTTP client with timeout and transport. |
| 26 | // This method assembles an HTTP client with the configured transport and timeout settings |
| 27 | // to ensure reliable API communication with appropriate performance characteristics. |
| 28 | // Parameters: |
| 29 | // - transport: The HTTP transport with TLS and proxy configuration |
| 30 | // |
| 31 | // Returns: *http.Client ready for making API requests with proper timeout and transport settings. |
| 32 | func (m *DefaultHTTPClientManager) CreateClient(transport *http.Transport) *http.Client { |
| 33 | return &http.Client{ |
| 34 | Transport: transport, // Use the configured transport for TLS and proxy handling |
| 35 | Timeout: HTTPTimeout * time.Second, // Set timeout to prevent hanging requests |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // CreateTransport sets up the HTTP transport with TLS configuration and proxy settings. |
| 40 | // This method configures the underlying HTTP transport layer with appropriate security settings |
| 41 | // based on the service configuration, particularly handling TLS verification preferences. |
| 42 | // Returns: *http.Transport configured with TLS settings for secure or insecure connections. |
| 43 | // |
| 44 | |
| 45 | func (m *DefaultHTTPClientManager) CreateTransport(config *Config) *http.Transport { |
| 46 | return &http.Transport{ |
| 47 | TLSClientConfig: &tls.Config{ |
| 48 | MinVersion: tls.VersionTLS12, |
| 49 | InsecureSkipVerify: config.DisableTLS || |
| 50 | config.InsecureSkipVerify, |
| 51 | }, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // initClient initializes the HTTP client and related components. |
| 56 | // This method ensures that the transport, HTTP client, JSON client, |
| 57 | // and TLS warning logging are performed when needed, allowing re-initialization if the client becomes nil. |
| 58 | // This function is called by the Service and modifies its fields. |
| 59 | func initClient(service *Service, manager HTTPClientManager) { |
| 60 | service.mu.Lock() |
| 61 | defer service.mu.Unlock() |
| 62 | |
| 63 | if service.httpClient == nil || service.client == nil { |
| 64 | transport := manager.CreateTransport(service.Config) |
| 65 | service.httpClient = manager.CreateClient(transport) |
| 66 | |
| 67 | service.client = jsonclient.NewWithHTTPClient(service.httpClient) |
| 68 | if service.Config.DisableTLS { |
| 69 | service.Log("Warning: TLS is disabled, using insecure HTTP connections") |
| 70 | } |
| 71 | |
| 72 | if service.Config.InsecureSkipVerify { |
| 73 | service.Log("Warning: TLS certificate verification is disabled") |
| 74 | } |
| 75 | } |
| 76 | } |