package bridge
import (
"errors"
"strings"
"sync"
"time"
)
type Pool struct {
Connections map [string ]*Session
sync .Mutex
}
type TunnelPool struct {
Connections map [string ]*Tunnel
sync .Mutex
}
var (
Sessions Pool = Pool {Connections : make (map [string ]*Session )}
Tunnels TunnelPool = TunnelPool {Connections : make (map [string ]*Tunnel )}
)
func (p *Pool ) Get (userID , serverID string ) (*Session , error ) {
if conn , ok := p .Connections [userID +serverID ]; ok {
return conn , nil
} else {
return nil , errors .New ("connection does not exist" )
}
}
func (p *Pool ) Set (userID , serverID string , session *Session ) {
p .Lock ()
defer p .Unlock ()
p .Connections [userID +serverID ] = session
}
func (p *Pool ) GetRaw (userID , remoteHost , username string ) (*Session , error ) {
if conn , ok := p .Connections [userID +remoteHost +username ]; ok {
conn .LastConnection = time .Now ()
return conn , nil
} else {
return nil , errors .New ("connection does not exist" )
}
}
func (p *Pool ) SetRaw (userID , remoteHost , username string , session *Session ) {
p .Lock ()
defer p .Unlock ()
p .Connections [userID +remoteHost +username ] = session
}
func (p *Pool ) Delete (key string ) {
p .Lock ()
defer p .Unlock ()
delete (p .Connections , key )
}
func (t *TunnelPool ) Get (remoteHost , remotePort , username string ) (*Tunnel , error ) {
if tunnel , ok := Tunnels .Connections [remoteHost +":" +remotePort +":" +username ]; ok {
tunnel .LastConnection = time .Now ()
return tunnel , nil
} else {
return nil , errors .New ("tunnel does not exist" )
}
}
func (t *TunnelPool ) Set (remoteHost , remotePort , username string , tunnel *Tunnel ) {
t .Lock ()
defer t .Unlock ()
Tunnels .Connections [remoteHost +":" +remotePort +":" +username ] = tunnel
}
func (t *TunnelPool ) Delete (key string ) {
t .Lock ()
defer t .Unlock ()
t .Connections [key ] = nil
delete (t .Connections , key )
}
func VerifyAuth (username , password , ipAddress , port , keyType string ) bool {
if keyType == "ssh" {
return VerifySSH (username , password , ipAddress , port )
} else if keyType == "ssh_certificate" {
return VerifySSHCertificate (username , password , ipAddress , port )
} else if strings .Contains (keyType , "winrm" ) {
secure := true
if strings .Contains (keyType , "insecure" ) {
secure = false
}
return VerifyWinRm (username , password , ipAddress , port , secure )
}
return true
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .