-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct.go
108 lines (92 loc) · 2.49 KB
/
direct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package reversetunnel
import (
"fmt"
"net"
"sync"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/services"
log "github.com/Sirupsen/logrus"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
)
func newDirectSite(domainName string, client auth.ClientI) *directSite {
return &directSite{
client: client,
domainName: domainName,
log: log.WithFields(log.Fields{
teleport.Component: teleport.ComponentReverseTunnel,
teleport.ComponentFields: map[string]string{
"domainName": domainName,
"side": "server",
"type": "localSite",
},
}),
}
}
// directSite allows to directly access the remote servers
// not using any tunnel, and using standard SSH
type directSite struct {
sync.Mutex
client auth.ClientI
authServer string
log *log.Entry
domainName string
connections []*remoteConn
lastUsed int
lastActive time.Time
srv *server
}
func (s *directSite) GetClient() (auth.ClientI, error) {
return s.client, nil
}
func (s *directSite) String() string {
return fmt.Sprintf("localSite(%v)", s.domainName)
}
func (s *directSite) GetStatus() string {
return RemoteSiteStatusOnline
}
func (s *directSite) GetName() string {
return s.domainName
}
func (s *directSite) GetLastConnected() time.Time {
return time.Now()
}
func (s *directSite) ConnectToServer(server, user string, auth []ssh.AuthMethod) (*ssh.Client, error) {
s.log.Infof("ConnectToServer(server=%v, user=%v)", server, user)
client, err := ssh.Dial(
"tcp",
server,
&ssh.ClientConfig{
User: user,
Auth: auth,
})
if err != nil {
return nil, trace.Wrap(err)
}
return client, nil
}
func (s *directSite) Dial(network string, addr string) (net.Conn, error) {
s.log.Infof("Dial(net=%v, addr=%v)", network, addr)
return net.Dial(network, addr)
}
func (s *directSite) DialServer(addr string) (net.Conn, error) {
s.log.Infof("DialServer(addr=%v)", addr)
return s.Dial("tcp", addr)
}
func findServer(addr string, servers []services.Server) (*services.Server, error) {
for i := range servers {
srv := &servers[i]
_, port, err := net.SplitHostPort(srv.Addr)
if err != nil {
log.Warningf("server %v(%v) has incorrect address format (%v)",
srv.Addr, srv.Hostname, err.Error())
} else {
if (len(srv.Hostname) != 0) && (len(port) != 0) && (addr == srv.Hostname+":"+port || addr == srv.Addr) {
return srv, nil
}
}
}
return nil, trace.NotFound("server %v is unknown", addr)
}