-
Notifications
You must be signed in to change notification settings - Fork 104
feat: tunnel raw TCP database connections through HTTP CONNECT proxy #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
bb6c448
0bf8d14
5b58c8e
96f9325
ae5cd16
08a59bb
f4909fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package mssql | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
|
|
||
| mssqldb "github.com/microsoft/go-mssqldb" | ||
| ) | ||
|
|
||
| type httpProxyDialer struct { | ||
| proxy *url.URL | ||
| } | ||
|
|
||
| func (d *httpProxyDialer) DialContext(ctx context.Context, network, addr string) (_ net.Conn, err error) { | ||
| conn, err := (&net.Dialer{}).DialContext(ctx, "tcp", d.proxy.Host) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer func() { | ||
| if err != nil { | ||
| closeErr := conn.Close() | ||
| if closeErr != nil { | ||
| err = fmt.Errorf("error closing connection (%w) after: %w", closeErr, err) | ||
| } | ||
| } | ||
| }() | ||
| req := &http.Request{ | ||
| Method: http.MethodConnect, | ||
| URL: &url.URL{Host: addr}, | ||
| Host: addr, | ||
| Header: http.Header{}, | ||
| } | ||
| if err = req.Write(conn); err != nil { | ||
| return nil, err | ||
| } | ||
| var resp *http.Response | ||
| resp, err = http.ReadResponse(bufio.NewReader(conn), req) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will possibly hijack bytes from the database server - as NewReader does a 4 KB read and that could be some of the bytes not just from the proxy but the sql server banner data? |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| resp.Body.Close() //nolint:errcheck | ||
| if resp.StatusCode != http.StatusOK { | ||
| err = fmt.Errorf("proxy CONNECT to %s: %s", addr, resp.Status) | ||
| return nil, err | ||
| } | ||
| return conn, nil | ||
| } | ||
|
|
||
| // openDB opens a *sql.DB for the given DSN. When HTTP_PROXY or HTTPS_PROXY is | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it respect HTTP_PROXY? you construct a https URL below? |
||
| // set and applicable to the target endpoint, connections are tunnelled through | ||
| // the proxy via HTTP CONNECT. | ||
| func openDB(endpoint, port, dsn string) (*sql.DB, error) { | ||
| req, _ := http.NewRequest(http.MethodConnect, "https://"+endpoint+":"+port, nil) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this panic on invalid URL?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You construct a &http.Request{ manually in the code the other place - should probably use the same here? this would avoid discarding the error too
chlunde marked this conversation as resolved.
|
||
| proxyURL, err := http.ProxyFromEnvironment(req) | ||
| if err != nil || proxyURL == nil { | ||
| return sql.Open(driverName, dsn) | ||
| } | ||
| connector, err := mssqldb.NewConnector(dsn) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| connector.Dialer = &httpProxyDialer{proxy: proxyURL} | ||
| return sql.OpenDB(connector), nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package mssql | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net" | ||
| "net/url" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestHttpProxyDialerSuccess(t *testing.T) { | ||
| proxy, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer proxy.Close() //nolint:errcheck | ||
|
|
||
| go func() { | ||
| conn, err := proxy.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| defer conn.Close() //nolint:errcheck | ||
| buf := make([]byte, 4096) | ||
| _, _ = conn.Read(buf) | ||
| _, _ = fmt.Fprint(conn, "HTTP/1.1 200 Connection established\r\n\r\n") | ||
| }() | ||
|
|
||
| d := &httpProxyDialer{proxy: &url.URL{Host: proxy.Addr().String()}} | ||
| conn, err := d.DialContext(context.Background(), "tcp", "db.example.com:1433") | ||
| if err != nil { | ||
| t.Fatalf("DialContext() unexpected error: %v", err) | ||
| } | ||
| conn.Close() //nolint:errcheck | ||
| } | ||
|
|
||
| func TestHttpProxyDialerRejected(t *testing.T) { | ||
| proxy, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer proxy.Close() //nolint:errcheck | ||
|
|
||
| go func() { | ||
| conn, err := proxy.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| defer conn.Close() //nolint:errcheck | ||
| buf := make([]byte, 4096) | ||
| _, _ = conn.Read(buf) | ||
| _, _ = fmt.Fprint(conn, "HTTP/1.1 403 Forbidden\r\n\r\n") | ||
| }() | ||
|
|
||
| d := &httpProxyDialer{proxy: &url.URL{Host: proxy.Addr().String()}} | ||
| _, err = d.DialContext(context.Background(), "tcp", "db.example.com:1433") | ||
| if err == nil { | ||
| t.Fatal("DialContext() expected error on non-200 response, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestOpenDBNoProxy(t *testing.T) { | ||
| t.Setenv("HTTP_PROXY", "") | ||
| t.Setenv("HTTPS_PROXY", "") | ||
| t.Setenv("NO_PROXY", "*") | ||
|
|
||
| db, err := openDB("localhost", "1433", "sqlserver://u:p@localhost:1433?database=db") | ||
| if err != nil { | ||
| t.Fatalf("openDB() unexpected error: %v", err) | ||
| } | ||
| db.Close() //nolint:errcheck | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package mysql | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
|
|
||
| gomysql "github.com/go-sql-driver/mysql" | ||
| ) | ||
|
|
||
| func tunnel(ctx context.Context, proxy *url.URL, addr string) (_ net.Conn, err error) { | ||
| conn, err := (&net.Dialer{}).DialContext(ctx, "tcp", proxy.Host) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer func() { | ||
| if err != nil { | ||
| closeErr := conn.Close() | ||
| if closeErr != nil { | ||
| err = fmt.Errorf("error closing connection (%w) after: %w", closeErr, err) | ||
| } | ||
| } | ||
| }() | ||
| req := &http.Request{ | ||
| Method: http.MethodConnect, | ||
| URL: &url.URL{Host: addr}, | ||
| Host: addr, | ||
| Header: http.Header{}, | ||
| } | ||
| if err = req.Write(conn); err != nil { | ||
| return nil, err | ||
| } | ||
| var resp *http.Response | ||
| resp, err = http.ReadResponse(bufio.NewReader(conn), req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| resp.Body.Close() //nolint:errcheck | ||
| if resp.StatusCode != http.StatusOK { | ||
| err = fmt.Errorf("proxy CONNECT to %s: %s", addr, resp.Status) | ||
| return nil, err | ||
| } | ||
| return conn, nil | ||
| } | ||
|
|
||
| // openDB opens a *sql.DB for the given DSN. When HTTP_PROXY or HTTPS_PROXY is | ||
| // set and applicable to the target endpoint, connections are tunnelled through | ||
| // the proxy via HTTP CONNECT. | ||
| func openDB(endpoint, port, dsn string) (*sql.DB, error) { | ||
| req, _ := http.NewRequest(http.MethodConnect, "https://"+endpoint+":"+port, nil) | ||
| proxyURL, err := http.ProxyFromEnvironment(req) | ||
| if err != nil || proxyURL == nil { | ||
| return sql.Open("mysql", dsn) | ||
| } | ||
| cfg, err := gomysql.ParseDSN(dsn) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| cfg.DialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
| return tunnel(ctx, proxyURL, addr) | ||
| } | ||
| connector, err := gomysql.NewConnector(cfg) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return sql.OpenDB(connector), nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package mysql | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net" | ||
| "net/url" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestTunnelSuccess(t *testing.T) { | ||
| proxy, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer proxy.Close() //nolint:errcheck | ||
|
|
||
| go func() { | ||
| conn, err := proxy.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| defer conn.Close() //nolint:errcheck | ||
| buf := make([]byte, 4096) | ||
| _, _ = conn.Read(buf) | ||
| _, _ = fmt.Fprint(conn, "HTTP/1.1 200 Connection established\r\n\r\n") | ||
| }() | ||
|
|
||
| proxyURL := &url.URL{Host: proxy.Addr().String()} | ||
| conn, err := tunnel(context.Background(), proxyURL, "db.example.com:3306") | ||
| if err != nil { | ||
| t.Fatalf("tunnel() unexpected error: %v", err) | ||
| } | ||
| conn.Close() //nolint:errcheck | ||
| } | ||
|
|
||
| func TestTunnelProxyRejected(t *testing.T) { | ||
| proxy, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer proxy.Close() //nolint:errcheck | ||
|
|
||
| go func() { | ||
| conn, err := proxy.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| defer conn.Close() //nolint:errcheck | ||
| buf := make([]byte, 4096) | ||
| _, _ = conn.Read(buf) | ||
| _, _ = fmt.Fprint(conn, "HTTP/1.1 407 Proxy Authentication Required\r\n\r\n") | ||
| }() | ||
|
|
||
| proxyURL := &url.URL{Host: proxy.Addr().String()} | ||
| _, err = tunnel(context.Background(), proxyURL, "db.example.com:3306") | ||
| if err == nil { | ||
| t.Fatal("tunnel() expected error on non-200 response, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestOpenDBNoProxy(t *testing.T) { | ||
| t.Setenv("HTTP_PROXY", "") | ||
| t.Setenv("HTTPS_PROXY", "") | ||
| t.Setenv("NO_PROXY", "*") | ||
|
|
||
| db, err := openDB("localhost", "3306", "u:p@tcp(localhost:3306)/db") | ||
| if err != nil { | ||
| t.Fatalf("openDB() unexpected error: %v", err) | ||
| } | ||
| db.Close() //nolint:errcheck | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.