/
sshTunnel.go
120 lines (99 loc) · 2.83 KB
/
sshTunnel.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
109
110
111
112
113
114
115
116
117
118
119
120
package sshtunnel
import (
"errors"
"fmt"
"io"
"log"
"net"
"os"
"github.com/r4ulcl/nTask/manager/utils"
"golang.org/x/crypto/ssh"
)
func forwardData(src, dest net.Conn) {
_, err := io.Copy(src, dest)
if err != nil {
log.Printf("Error forwarding data: %v", err)
}
src.Close()
dest.Close()
}
func publicKeyFile(file string) (ssh.AuthMethod, error) {
buffer, err := os.ReadFile(file)
if err != nil {
return nil, err
}
key, err := ssh.ParsePrivateKey(buffer)
if err != nil {
return nil, err
}
return ssh.PublicKeys(key), nil
}
func StartSSH(config *utils.ManagerSSHConfig, portAPI string, verbose, debug bool) {
log.Println("SSH StartSSH")
for ip, port := range config.IPPort {
go func(ip, port string) {
log.Println("SSH connecction", ip, port)
if !checkFileExists(config.PrivateKeyPath) {
log.Fatal("File ", config.PrivateKeyPath, " not found")
}
auth, err := publicKeyFile(config.PrivateKeyPath)
if err != nil {
log.Fatal("Error loading file ", config.PrivateKeyPath, err)
}
// SSH connection configuration
sshConfig := &ssh.ClientConfig{
User: config.SSHUsername,
Auth: []ssh.AuthMethod{
auth,
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// If a password is provided, add it as an additional authentication method
if config.PrivateKeyPassword != "" {
sshConfig.Auth = append(sshConfig.Auth, ssh.Password(config.PrivateKeyPassword))
}
// Connect to the SSH server
sshClient, err := ssh.Dial("tcp", ip+":"+port, sshConfig)
if err != nil {
log.Fatalf("Failed to dial: %s", err)
}
// Remote port to forward
remoteAddr := "127.0.0.1:" + portAPI
// Local address to forward to
localAddr := "127.0.0.1:" + portAPI
if debug {
log.Println("SSH remoteAddr", remoteAddr)
}
// Request remote port forwarding
remoteListener, err := sshClient.Listen("tcp", remoteAddr)
if err != nil {
log.Fatalf("Failed to request remote port forwarding: %v", err)
}
defer remoteListener.Close()
fmt.Printf("Remote port forwarding %s to %s via SSH...\n", remoteAddr, localAddr)
for {
// Wait for a connection on the remote port
remoteConn, err := remoteListener.Accept()
if err != nil {
log.Fatalf("Failed to accept connection on remote port: %v", err)
}
// Connect to the local server
localConn, err := net.Dial("tcp", localAddr)
if err != nil {
log.Printf("Failed to connect to local server: %v", err)
remoteConn.Close()
continue
}
// Start forwarding data between local and remote connections
go forwardData(remoteConn, localConn)
go forwardData(localConn, remoteConn)
}
}(ip, port)
}
}
// ssh-keygen -t rsa -b 2048
func checkFileExists(filePath string) bool {
_, error := os.Stat(filePath)
//return !os.IsNotExist(err)
return !errors.Is(error, os.ErrNotExist)
}