Debian Codegraph @

docs/guides/proxy/index.md

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