Debian Codegraph @

docs/guides/proxy/index.md

raw
1 # Proxy Setup
2
3 ## Overview
4
5 Shoutrrr supports proxying HTTP requests for notification services, allowing you to route traffic through a proxy server. This can be configured using an environment variable or by customizing the HTTP client in code.
6
7 **For per-sender egress control and SSRF protection (recommended for untrusted notification URLs), use a custom `http.Client` and `DialContext` via `SenderOptions`.**
8
9 `shoutrrr.Send` cannot take these options. Use `shoutrrr.NewSenderWithOptions`, `CreateSenderWithOptions`, or `router.NewWithOptions`.
10
11 ## Usage
12
13 ### Environment Variable
14
15 Set the `HTTP_PROXY` environment variable to the proxy URL.
16 This applies to all HTTP-based services used by Shoutrrr that rely on the default transport.
17
18 ```bash title="Set HTTP_PROXY Environment Variable"
19 export HTTP_PROXY="socks5://localhost:1337"
20 ```
21
22 !!! Note
23 This is a process-global setting and affects `http.DefaultClient` and clients that inherit `http.DefaultTransport`.
24 It is not suitable for per-sender SSRF controls.
25
26 ### Custom HTTP Client (Per-Sender, Recommended for SSRF/Egress Control)
27
28 Supply a custom `*http.Client` (with custom `Transport`, `DialContext`, TLS config, etc.) when creating a sender or router. The client is injected into services that support it and used for all their outbound HTTP.
29
30 Use `shoutrrr.NewSenderWithOptions` (or `CreateSenderWithOptions`, `router.NewWithOptions`).
31
32 ```go title="Configure Custom HTTP Client for SSRF Protection"
33 package main
34
35 import (
36 "context"
37 "fmt"
38 "log"
39 "net"
40 "net/http"
41 "time"
42
43 "github.com/nicholas-fedor/shoutrrr"
44 "github.com/nicholas-fedor/shoutrrr/pkg/types"
45 )
46
47 // isBlockedIP is an example SSRF guard. Implement your own policy.
48 func isBlockedIP(ip net.IP) bool {
49 return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast()
50 }
51
52 func dialAllowed(ctx context.Context, network, addr string) (net.Conn, error) {
53 host, port, err := net.SplitHostPort(addr)
54 if err != nil {
55 return nil, err
56 }
57 ips, err := net.DefaultResolver.LookupIPAddr(ctx, host)
58 if err != nil {
59 return nil, err
60 }
61 d := &net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}
62 for _, ip := range ips {
63 if isBlockedIP(ip.IP) {
64 continue
65 }
66 conn, err := d.DialContext(ctx, network, net.JoinHostPort(ip.IP.String(), port))
67 if err == nil {
68 return conn, nil
69 }
70 }
71 return nil, &net.OpError{Op: "dial", Net: network, Err: fmt.Errorf("destination blocked by egress policy")}
72 }
73
74 func main() {
75 // Custom Transport with DialContext that performs egress/SSRF checks.
76 transport := &http.Transport{
77 DialContext: dialAllowed,
78 // Proxy: http.ProxyFromEnvironment, // opt-in if you also want env proxies for this client
79 ForceAttemptHTTP2: true,
80 MaxIdleConns: 100,
81 IdleConnTimeout: 90 * time.Second,
82 TLSHandshakeTimeout: 10 * time.Second,
83 ExpectContinueTimeout: 1 * time.Second,
84 }
85
86 customClient := &http.Client{
87 Transport: transport,
88 Timeout: 60 * time.Second,
89 }
90
91 opts := types.SenderOptions{
92 HTTPClient: customClient,
93 DialContext: transport.DialContext,
94 // Timeout: 30 * time.Second, // optional per-router override
95 }
96
97 url := "discord://abc123@123456789"
98 sender, err := shoutrrr.NewSenderWithOptions(nil, opts, url)
99 if err != nil {
100 log.Fatal(err)
101 }
102
103 if errs := sender.Send("Hello via custom client!", nil); len(errs) > 0 {
104 for _, e := range errs {
105 log.Println("Error:", e)
106 }
107 }
108 }
109 ```
110
111 **Notes on custom clients and dialers:**
112
113 - A non-nil `SenderOptions.HTTPClient` is propagated by the router to services implementing `types.HTTPClientSetter`.
114 - A non-nil `SenderOptions.DialContext` is propagated to services implementing `types.DialContextSetter` (SMTP and MQTT). TLS wrapping still happens after the TCP dial.
115 - `DialContext` must be safe for concurrent use. A custom dialer bypasses MQTT `all_proxy`; implement proxying in the function if needed.
116 - Custom clients usually bypass `HTTP_PROXY`/`HTTPS_PROXY` unless their `Transport.Proxy` is configured to consult the environment.
117 - All default timeouts/TLS behavior is preserved when no custom client or dialer is supplied.
118 - The same client instance is reused for the lifetime of the sender/router.
119
120 ## Examples
121
122 <!-- markdownlint-disable -->
123 ### Using Environment Variable for Proxy (Global)
124
125 !!! Example
126 ```bash title="Set Proxy and Send Notification"
127 export HTTP_PROXY="socks5://localhost:1337"
128 shoutrrr send --url "discord://abc123@123456789" --message "Hello via proxy!"
129 ```
130
131 ```text title="Expected Output"
132 Notification sent
133 ```
134
135 ### Using Custom HTTP Client in Go (Per-Sender / SSRF Control)
136
137 !!! Example
138 ```go title="Send Notification with Custom Client (SSRF-safe)"
139 package main
140
141 import (
142 "context"
143 "fmt"
144 "log"
145 "net"
146 "net/http"
147 "time"
148
149 "github.com/nicholas-fedor/shoutrrr"
150 "github.com/nicholas-fedor/shoutrrr/pkg/types"
151 )
152
153 func isBlockedIP(ip net.IP) bool {
154 return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast()
155 }
156
157 func main() {
158 transport := &http.Transport{
159 DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
160 host, port, err := net.SplitHostPort(addr)
161 if err != nil {
162 return nil, err
163 }
164 ips, err := net.DefaultResolver.LookupIPAddr(ctx, host)
165 if err != nil {
166 return nil, err
167 }
168 d := &net.Dialer{Timeout: 30 * time.Second}
169 for _, ip := range ips {
170 if isBlockedIP(ip.IP) {
171 continue
172 }
173 conn, err := d.DialContext(ctx, network, net.JoinHostPort(ip.IP.String(), port))
174 if err == nil {
175 return conn, nil
176 }
177 }
178 return nil, &net.OpError{Op: "dial", Net: network, Err: fmt.Errorf("destination blocked by egress policy")}
179 },
180 }
181 custom := &http.Client{Transport: transport}
182
183 sender, err := shoutrrr.NewSenderWithOptions(nil, types.SenderOptions{HTTPClient: custom, DialContext: transport.DialContext}, "discord://abc123@123456789")
184 if err != nil {
185 log.Fatal(err)
186 }
187 if errs := sender.Send("Hello via custom egress-controlled client!", nil); len(errs) > 0 {
188 for _, e := range errs {
189 log.Println(e)
190 }
191 }
192 }
193 ```
194
195 ```text title="Expected Output (Success)"
196 (No output on success)
197 ```
198
199 ```text title="Expected Output (Error)"
200 Error: failed to send message: unexpected response status code
201 ```
202 <!-- markdownlint-restore -->
203
204 ## Notes
205
206 - **Environment Variable**: `HTTP_PROXY` supports protocols like `http`, `https`, or `socks5`. It affects all HTTP-based services globally.
207 - **Custom HTTP Client**: Provides fine-grained control over proxy settings, suitable for Go applications requiring specific transport configurations.
208 - **Custom DialContext**: Applies the same destination policy to SMTP and MQTT TCP connections. HTTP services continue to use `HTTPClient`.
209 - **Service Compatibility**: Ensure the proxy supports the protocol used by the service (e.g., HTTPS for Discord, SMTP).
210 - **Timeouts**: The custom client example includes a 30-second dial timeout and 10-second TLS handshake timeout, adjustable as needed.