-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunnel.go
108 lines (86 loc) · 2.58 KB
/
tunnel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"errors"
"fmt"
"io"
"net"
"os"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/crypto/ssh"
)
func tunnel(ctx context.Context) (*ssh.Client, net.Listener, error) {
authMethod, err := sshAgentAuth()
if err != nil {
return nil, nil, err
}
config := &ssh.ClientConfig{
User: viper.GetString("SSH_USERNAME"),
Auth: []ssh.AuthMethod{
authMethod,
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
bastionAddr := fmt.Sprintf("%s:%s", viper.GetString("BASTION_HOST"), viper.GetString("BASTION_PORT"))
bastionConn, err := ssh.Dial("tcp", bastionAddr, config)
if err != nil {
return nil, nil, fmt.Errorf("failed to dial bastion host: %v", err)
}
localAddr := fmt.Sprintf("localhost:%s", viper.GetString("SSH_LOCAL_PORT"))
targetAddr := fmt.Sprintf("%s:%s", viper.GetString("TARGET_HOST"), viper.GetString("SSH_REMOTE_PORT"))
listener, err := net.Listen("tcp", localAddr)
if err != nil {
return nil, nil, fmt.Errorf("failed to create listener on bastion host: %v", err)
}
log.Printf("SSH tunnel established: %s -> %s", localAddr, targetAddr)
runtime.EventsEmit(ctx, "tunnel", true)
go func() {
for {
localConn, err := listener.Accept()
if err != nil {
fmt.Println("Failed to accept connection:", err)
listener.Close()
bastionConn.Close()
runtime.EventsEmit(ctx, "tunnel", false)
break
}
go handleLocalConnection(bastionConn, localConn, targetAddr)
}
}()
return bastionConn, listener, nil
}
func publicKeyFile(file string) (ssh.AuthMethod, error) {
key, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read private key: %w", err)
}
passPhrase := viper.GetString("SSH_PASSPHRASE")
var signer ssh.Signer
if len(passPhrase) > 0 {
signer, err = ssh.ParsePrivateKeyWithPassphrase(key, []byte(viper.GetString("SSH_PASSPHRASE")))
} else {
signer, err = ssh.ParsePrivateKey(key)
}
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %w", err)
}
return ssh.PublicKeys(signer), nil
}
func handleLocalConnection(client *ssh.Client, localConn net.Conn, targetAddr string) {
targetConn, err := client.Dial("tcp", targetAddr)
if err != nil {
log.Printf("Failed to connect to target host: %v", err)
return
}
go io.Copy(localConn, targetConn)
go io.Copy(targetConn, localConn)
}
func sshAgentAuth() (ssh.AuthMethod, error) {
sshKey := viper.GetString("SSH_KEY")
if len(sshKey) > 0 {
return publicKeyFile(sshKey)
}
return nil, errors.New("missing SSH_KEY, please configure it in the settings")
}