Skip to content

Commit f0d493d

Browse files
Tenzercyrilgdn
andauthored
Add SOCKS proxy support (cyrilgdn#195)
* Add SOCKS proxy support This copies the implentation from the Redshift provider: brainly/terraform-provider-redshift#19. It allows users to proxy the requests to the database server through a SOCKS proxy, in case the database server cannot be reached directly. * Mention SOCKS5 proxy support to usage documentation As part of this I found out the format of the values in the environment variables isn't standardized, so there only place we potentially could link to about the format of the values would be the source code for `golang.org/x/net/proxy`. I thought that was a bit much, so I've instead included a simple example instead. Co-authored-by: Cyril Gaudin <cyril.gaudin@gmail.com>
1 parent 7425fc7 commit f0d493d

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/lib/pq v1.10.4
1212
github.com/sean-/postgresql-acl v0.0.0-20161225120419-d10489e5d217
1313
gocloud.dev v0.25.0
14+
golang.org/x/net v0.0.0-20220401154927-543a649e0bdd
1415
)
1516

1617
require (
@@ -70,7 +71,6 @@ require (
7071
go.uber.org/multierr v1.8.0 // indirect
7172
go.uber.org/zap v1.21.0 // indirect
7273
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect
73-
golang.org/x/net v0.0.0-20220401154927-543a649e0bdd // indirect
7474
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a // indirect
7575
golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f // indirect
7676
golang.org/x/text v0.3.7 // indirect

postgresql/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (c *Client) Connect() (*DBConnection, error) {
250250
var db *sql.DB
251251
var err error
252252
if c.config.Scheme == "postgres" {
253-
db, err = sql.Open("postgres", dsn)
253+
db, err = sql.Open(proxyDriverName, dsn)
254254
} else {
255255
db, err = postgres.Open(context.Background(), dsn)
256256
}

postgresql/proxy_driver.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package postgresql
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"database/sql/driver"
7+
"net"
8+
"time"
9+
10+
"github.com/lib/pq"
11+
"golang.org/x/net/proxy"
12+
)
13+
14+
const proxyDriverName = "postgresql-proxy"
15+
16+
type proxyDriver struct{}
17+
18+
func (d proxyDriver) Open(name string) (driver.Conn, error) {
19+
return pq.DialOpen(d, name)
20+
}
21+
22+
func (d proxyDriver) Dial(network, address string) (net.Conn, error) {
23+
dialer := proxy.FromEnvironment()
24+
return dialer.Dial(network, address)
25+
}
26+
27+
func (d proxyDriver) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
28+
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
29+
defer cancel()
30+
return proxy.Dial(ctx, network, address)
31+
}
32+
33+
func init() {
34+
sql.Register(proxyDriverName, proxyDriver{})
35+
}

website/docs/index.html.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,10 @@ resource postgresql_database "test_db" {
194194
}
195195
```
196196

197+
### SOCKS5 Proxy Support
198+
199+
The provider supports connecting via a SOCKS5 proxy, but when the `postgres` scheme is used. It can be configured by setting the `ALL_PROXY` or `all_proxy` environment variable to a value like `socks5://127.0.0.1:1080`.
200+
201+
The `NO_PROXY` or `no_proxy` environment can also be set to opt out of proxying for specific hostnames or ports.
202+
197203
[libpq]: https://pkg.go.dev/github.com/lib/pq

0 commit comments

Comments
 (0)