|
| 1 | +package ssh |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/creativeprojects/clog" |
| 12 | + "golang.org/x/crypto/ssh" |
| 13 | + "golang.org/x/crypto/ssh/knownhosts" |
| 14 | +) |
| 15 | + |
| 16 | +const startPort = 10001 |
| 17 | + |
| 18 | +type SSH struct { |
| 19 | + config Config |
| 20 | + port int |
| 21 | + client *ssh.Client |
| 22 | + tunnel net.Listener |
| 23 | +} |
| 24 | + |
| 25 | +func NewSSH(config Config) *SSH { |
| 26 | + return &SSH{ |
| 27 | + config: config, |
| 28 | + port: startPort, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func (s *SSH) Connect() error { |
| 33 | + err := s.config.Validate() |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + hostKeyCallback, err := knownhosts.New(s.config.KnownHostsPath) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("cannot load host keys from known_hosts: %w", err) |
| 40 | + } |
| 41 | + key, err := os.ReadFile(s.config.PrivateKeyPath) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("unable to read private key: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Create the Signer for this private key. |
| 47 | + signer, err := ssh.ParsePrivateKey(key) |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("unable to parse private key: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + config := &ssh.ClientConfig{ |
| 53 | + User: s.config.Username, |
| 54 | + Auth: []ssh.AuthMethod{ |
| 55 | + // Use the PublicKeys method for remote authentication. |
| 56 | + ssh.PublicKeys(signer), |
| 57 | + }, |
| 58 | + HostKeyCallback: hostKeyCallback, |
| 59 | + } |
| 60 | + |
| 61 | + // Connect to the remote server and perform the SSH handshake. |
| 62 | + s.client, err = ssh.Dial("tcp", s.config.Host, config) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("unable to connect: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Request the remote side to open a local port |
| 68 | + s.tunnel, err = s.client.Listen("tcp", fmt.Sprintf("localhost:%d", s.port)) |
| 69 | + if err != nil { |
| 70 | + log.Fatal("unable to register tcp forward: ", err) |
| 71 | + } |
| 72 | + |
| 73 | + go func() { |
| 74 | + // Serve HTTP with your SSH server acting as a reverse proxy. |
| 75 | + http.Serve(s.tunnel, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { |
| 76 | + fmt.Fprintf(resp, "Hello world!\n") |
| 77 | + })) |
| 78 | + }() |
| 79 | + |
| 80 | + // Each ClientConn can support multiple interactive sessions, |
| 81 | + // represented by a Session. |
| 82 | + session, err := s.client.NewSession() |
| 83 | + if err != nil { |
| 84 | + log.Fatal("Failed to create session: ", err) |
| 85 | + } |
| 86 | + defer session.Close() |
| 87 | + |
| 88 | + // Once a Session is created, we can execute a single command on |
| 89 | + // the remote side using the Run method. |
| 90 | + var b bytes.Buffer |
| 91 | + session.Stdout = &b |
| 92 | + if err := session.Run(fmt.Sprintf("curl http://localhost:%d", s.port)); err != nil { |
| 93 | + log.Fatal("Failed to run: " + err.Error()) |
| 94 | + } |
| 95 | + fmt.Println(b.String()) |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (s *SSH) Close() { |
| 100 | + if s.tunnel != nil { |
| 101 | + err := s.tunnel.Close() |
| 102 | + if err != nil { |
| 103 | + clog.Warningf("unable to close tunnel: %s", err) |
| 104 | + } |
| 105 | + } |
| 106 | + if s.client != nil { |
| 107 | + err := s.client.Close() |
| 108 | + if err != nil { |
| 109 | + clog.Warningf("unable to close ssh connection: %s", err) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments