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