2024-11-17 18:10:35 +00:00
|
|
|
package sshproxy
|
|
|
|
|
|
|
|
type SSHProxy struct {
|
|
|
|
// internalPorts maps internal docker ssh ports to the corresponding external ssh ports
|
|
|
|
// that users use to ssh into workspaces
|
|
|
|
internalPorts map[int]int
|
|
|
|
|
|
|
|
connections map[int]*proxyConnection
|
2024-11-17 18:30:07 +00:00
|
|
|
|
|
|
|
closedConnections chan *proxyConnection
|
2024-11-17 18:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New() *SSHProxy {
|
2024-11-17 18:30:07 +00:00
|
|
|
p := &SSHProxy{
|
|
|
|
internalPorts: map[int]int{},
|
|
|
|
connections: map[int]*proxyConnection{},
|
|
|
|
closedConnections: make(chan *proxyConnection),
|
2024-11-17 18:10:35 +00:00
|
|
|
}
|
2024-11-17 18:30:07 +00:00
|
|
|
|
|
|
|
go p.handleClosedConnections()
|
|
|
|
|
|
|
|
return p
|
2024-11-17 18:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SSHProxy) NewProxyEntryTo(toPort int) error {
|
2024-11-17 18:30:07 +00:00
|
|
|
c, err := newProxyConnection(toPort, p.closedConnections)
|
2024-11-17 18:10:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go c.start()
|
|
|
|
|
|
|
|
p.connections[toPort] = c
|
|
|
|
p.internalPorts[toPort] = c.externalPort
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SSHProxy) FindExternalPort(internalPort int) int {
|
|
|
|
if port, ok := p.internalPorts[internalPort]; ok {
|
|
|
|
return port
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
2024-11-17 18:30:07 +00:00
|
|
|
|
|
|
|
func (p *SSHProxy) handleClosedConnections() {
|
|
|
|
for c := range p.closedConnections {
|
|
|
|
delete(p.internalPorts, c.internalPort)
|
|
|
|
delete(p.connections, c.internalPort)
|
|
|
|
}
|
|
|
|
}
|