pico
created pr with
pico/pgs-ssh-proxy.1
added pico/pgs-ssh-proxy.2
1: b200a43 = 1: b200a43 feat(pgs): ability to proxy ssh conns to a separate service
-: ------- > 2: 667f0fc refactor(pssh): remove the handled bool
cmds
checkout latest patchset:
ssh pr.pico.sh pull pico:pgs-ssh-proxy | git am -3checkout specific patchset revision:
ssh pr.pico.sh pull pico:pgs-ssh-proxy [rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pico:pgs-ssh-proxyadd comment to patch request:
ssh pr.pico.sh comment pico:pgs-ssh-proxy "lgtm!"
Patchset
pico/pgs-ssh-proxy.1
feat(pgs): ability to proxy ssh conns to a separate service
Eric Bower
2026-11-27T14:14:04ZWith pssh.Config.RawConnHandler we can proxy raw tcp data from one ssh service to another. This allows us to proxy ssh connections from pgs.sh to a separate service where we might store all static files. Essentially we have a hook to perform an io.Copy(client, proxy) between the user and the backend ssh service
Semantic diff summary
1 added,
2 modified,
0 signature changed,
0 removed
across 2 analyzed files
+42
-0
pkg/apps/pgs/ssh.go
#
| ... | ... | @@ -22,6 +26,7 @@ func createSshServer(cfg *Config, ctx context.Context, cacheClearingQueue chan s | |
| 22 | 26 | host := shared.GetEnv("PGS_HOST", "0.0.0.0") | |
| 23 | 27 | port := shared.GetEnv("PGS_SSH_PORT", "2222") | |
| 24 | 28 | promPort := shared.GetEnv("PGS_PROM_PORT", "9222") | |
| 29 | + | proxyAddr := shared.GetEnv("PGS_SSH_PROXY_ADDR", "") | |
| 25 | 30 | logger := cfg.Logger | |
| 26 | 31 | ||
| 27 | 32 | handler := NewUploadAssetHandler( |
| ... | ... | @@ -65,6 +70,13 @@ func createSshServer(cfg *Config, ctx context.Context, cacheClearingQueue chan s | |
| 65 | 70 | "direct-tcpip": tunkit.LocalForwardHandler(webTunnel), | |
| 66 | 71 | }, | |
| 67 | 72 | ) | |
| 73 | + | if err != nil { | |
| 74 | + | return nil, err | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | if proxyAddr != "" { | |
| 78 | + | server.Config.RawConnHandler = createRawProxyHandler(proxyAddr) | |
| 79 | + | } | |
| 68 | 80 | ||
| 69 | 81 | return server, err | |
| 70 | 82 | } |
| ... | ... | @@ -108,3 +120,33 @@ func StartSshServer(cfg *Config, killCh chan error) { | |
| 108 | 120 | exit() | |
| 109 | 121 | } | |
| 110 | 122 | } | |
| 123 | + | ||
| 124 | + | func createRawProxyHandler(targetAddr string) func(context.Context, net.Conn) (bool, error) { | |
| 125 | + | return func(ctx context.Context, client net.Conn) (bool, error) { | |
| 126 | + | dialer := &net.Dialer{Timeout: 10 * time.Second} | |
| 127 | + | upstream, err := dialer.DialContext(ctx, "tcp", targetAddr) | |
| 128 | + | if err != nil { | |
| 129 | + | return true, err | |
| 130 | + | } | |
| 131 | + | ||
| 132 | + | defer func() { | |
| 133 | + | _ = upstream.Close() | |
| 134 | + | }() | |
| 135 | + | ||
| 136 | + | errCh := make(chan error, 2) | |
| 137 | + | go func() { | |
| 138 | + | _, err := io.Copy(upstream, client) | |
| 139 | + | errCh <- err | |
| 140 | + | }() | |
| 141 | + | go func() { | |
| 142 | + | _, err := io.Copy(client, upstream) | |
| 143 | + | errCh <- err | |
| 144 | + | }() | |
| 145 | + | ||
| 146 | + | err = <-errCh | |
| 147 | + | if errors.Is(err, io.EOF) { | |
| 148 | + | return true, nil | |
| 149 | + | } | |
| 150 | + | return true, err | |
| 151 | + | } | |
| 152 | + | } |
+8
-0
pkg/pssh/server.go
#
| ... | ... | @@ -578,6 +578,7 @@ type SSHServerConfig struct { | |
| 578 | 578 | // HandshakeTimeout bounds the handshake and auth. Zero means | |
| 579 | 579 | // DefaultHandshakeTimeout and a negative value means no limit. | |
| 580 | 580 | HandshakeTimeout time.Duration | |
| 581 | + | RawConnHandler func(ctx context.Context, conn net.Conn) (handled bool, err error) | |
| 581 | 582 | } | |
| 582 | 583 | ||
| 583 | 584 | // DefaultHandshakeTimeout keeps a client that never finishes its handshake |
| ... | ... | @@ -721,6 +722,13 @@ func (s *SSHServer) HandleConn(conn net.Conn) error { | |
| 721 | 722 | _ = conn.Close() | |
| 722 | 723 | }() | |
| 723 | 724 | ||
| 725 | + | if s.Config.RawConnHandler != nil { | |
| 726 | + | handled, err := s.Config.RawConnHandler(s.Ctx, conn) | |
| 727 | + | if handled { | |
| 728 | + | return err | |
| 729 | + | } | |
| 730 | + | } | |
| 731 | + | ||
| 724 | 732 | timeout := s.Config.HandshakeTimeout | |
| 725 | 733 | if timeout == 0 { | |
| 726 | 734 | timeout = DefaultHandshakeTimeout |