Connect to remote server using MakeConfig struct and returns *ssh.Session
()
| 214 | |
| 215 | // Connect to remote server using MakeConfig struct and returns *ssh.Session |
| 216 | func (ssh_conf *MakeConfig) Connect() (*ssh.Session, *ssh.Client, error) { |
| 217 | var client *ssh.Client |
| 218 | var err error |
| 219 | |
| 220 | // Default protocol is: tcp. |
| 221 | if ssh_conf.Protocol == "" { |
| 222 | ssh_conf.Protocol = PROTOCOL_TCP |
| 223 | } |
| 224 | if ssh_conf.Proxy.Protocol == "" { |
| 225 | ssh_conf.Proxy.Protocol = PROTOCOL_TCP |
| 226 | } |
| 227 | |
| 228 | targetConfig, closer := getSSHConfig(DefaultConfig{ |
| 229 | User: ssh_conf.User, |
| 230 | Key: ssh_conf.Key, |
| 231 | KeyPath: ssh_conf.KeyPath, |
| 232 | Passphrase: ssh_conf.Passphrase, |
| 233 | Password: ssh_conf.Password, |
| 234 | Timeout: ssh_conf.Timeout, |
| 235 | Ciphers: ssh_conf.Ciphers, |
| 236 | KeyExchanges: ssh_conf.KeyExchanges, |
| 237 | Fingerprint: ssh_conf.Fingerprint, |
| 238 | UseInsecureCipher: ssh_conf.UseInsecureCipher, |
| 239 | }) |
| 240 | if closer != nil { |
| 241 | defer func() { _ = closer.Close() }() |
| 242 | } |
| 243 | |
| 244 | // Enable proxy command |
| 245 | if ssh_conf.Proxy.Server != "" { |
| 246 | proxyConfig, closer := getSSHConfig(DefaultConfig{ |
| 247 | User: ssh_conf.Proxy.User, |
| 248 | Key: ssh_conf.Proxy.Key, |
| 249 | KeyPath: ssh_conf.Proxy.KeyPath, |
| 250 | Passphrase: ssh_conf.Proxy.Passphrase, |
| 251 | Password: ssh_conf.Proxy.Password, |
| 252 | Timeout: ssh_conf.Proxy.Timeout, |
| 253 | Ciphers: ssh_conf.Proxy.Ciphers, |
| 254 | KeyExchanges: ssh_conf.Proxy.KeyExchanges, |
| 255 | Fingerprint: ssh_conf.Proxy.Fingerprint, |
| 256 | UseInsecureCipher: ssh_conf.Proxy.UseInsecureCipher, |
| 257 | }) |
| 258 | if closer != nil { |
| 259 | defer func() { _ = closer.Close() }() |
| 260 | } |
| 261 | |
| 262 | proxyClient, err := ssh.Dial(string(ssh_conf.Proxy.Protocol), net.JoinHostPort(ssh_conf.Proxy.Server, ssh_conf.Proxy.Port), proxyConfig) |
| 263 | if err != nil { |
| 264 | return nil, nil, err |
| 265 | } |
| 266 | |
| 267 | // Apply timeout to the connection from proxy to target server |
| 268 | timeout := ssh_conf.Timeout |
| 269 | if timeout == 0 { |
| 270 | timeout = defaultTimeout |
| 271 | } |
| 272 | |
| 273 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |