1
1
package tunnel
2
2
3
- import (
4
- "context"
5
- "fmt"
6
- "log"
7
- "net"
8
-
9
- "github.com/jackc/pgx/v5"
10
- )
11
-
12
3
type SSHTunnelConfig struct {
13
4
SSHAddress string
14
5
SSHUser string
@@ -17,36 +8,36 @@ type SSHTunnelConfig struct {
17
8
RemoteEndpoint string
18
9
}
19
10
20
- func main () {
21
- config := SSHTunnelConfig {
22
- SSHAddress : "postgresHost:22" ,
23
- SSHUser : "sshUser" ,
24
- SSHPrivateKeyPath : "key.pem" ,
25
- LocalEndpoint : "localhost:0" ,
26
- RemoteEndpoint : "remoteHost:5432" ,
27
- }
28
-
29
- listener , err := SetupSSHTunnel (config )
30
- if err != nil {
31
- log .Fatalf ("Failed to start SSH tunnel: %v" , err )
32
- }
33
- defer listener .Close ()
34
-
35
- // Construct DSN for PostgreSQL connection
36
- localAddr := listener .Addr ().(* net.TCPAddr )
37
- dsn := fmt .Sprintf ("host=%s port=%d user=postgres password=123 dbname=db_name sslmode=disable" , localAddr .IP , localAddr .Port )
38
-
39
- ctx := context .TODO ()
40
- // Connect to PostgreSQL via SSH tunnel
41
- db , err := pgx .Connect (ctx , dsn )
42
- if err != nil {
43
- log .Fatalf ("Failed to connect to PostgreSQL: %v" , err )
44
- }
45
-
46
- // Use db to perform SQL operations
47
- if err := db .Ping (ctx ); err != nil {
48
- log .Fatalf ("Failed to ping database: %v" , err )
49
- }
50
-
51
- fmt .Println ("Successfully connected to PostgreSQL through SSH tunnel!" )
52
- }
11
+ // func main() {
12
+ // config := SSHTunnelConfig{
13
+ // SSHAddress: "postgresHost:22",
14
+ // SSHUser: "sshUser",
15
+ // SSHPrivateKeyPath: "key.pem",
16
+ // LocalEndpoint: "localhost:0",
17
+ // RemoteEndpoint: "remoteHost:5432",
18
+ // }
19
+
20
+ // listener, err := SetupSSHTunnel(config)
21
+ // if err != nil {
22
+ // log.Fatalf("Failed to start SSH tunnel: %v", err)
23
+ // }
24
+ // defer listener.Close()
25
+
26
+ // // Construct DSN for PostgreSQL connection
27
+ // localAddr := listener.Addr().(*net.TCPAddr)
28
+ // dsn := fmt.Sprintf("host=%s port=%d user=postgres password=123 dbname=db_name sslmode=disable", localAddr.IP, localAddr.Port)
29
+
30
+ // ctx := context.TODO()
31
+ // // Connect to PostgreSQL via SSH tunnel
32
+ // db, err := pgx.Connect(ctx, dsn)
33
+ // if err != nil {
34
+ // log.Fatalf("Failed to connect to PostgreSQL: %v", err)
35
+ // }
36
+
37
+ // // Use db to perform SQL operations
38
+ // if err := db.Ping(ctx); err != nil {
39
+ // log.Fatalf("Failed to ping database: %v", err)
40
+ // }
41
+
42
+ // fmt.Println("Successfully connected to PostgreSQL through SSH tunnel!")
43
+ // }
0 commit comments