-
Notifications
You must be signed in to change notification settings - Fork 0
/
tailer.go
148 lines (113 loc) · 3.18 KB
/
tailer.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"log"
"os"
"strconv"
"tailer/internal/config"
"tailer/internal/remote"
"golang.org/x/crypto/ssh"
)
var servers = make(map[string]config.Server)
func main() {
if len(os.Args) < 2 || len(os.Args) > 3 {
log.Fatal("Provide project name as an argument")
}
projectName := os.Args[1]
c := config.NewConfig("./config.yaml")
var lines int
var err error
if len(os.Args) == 3 {
lines, err = strconv.Atoi(os.Args[2])
if err != nil {
log.Fatal("Invalid lines number value")
}
} else {
lines = c.DefaultLines
}
projects := make(map[string]config.Project)
for _, project := range c.Projects {
projects[project.Name] = project
}
for _, server := range c.Servers {
servers[server.Name] = server
}
project, exists := projects[projectName]
if !exists {
log.Fatal("Project config not found")
}
var bastionClient, client *ssh.Client
// resolve ssh client
if project.BastionServer != "" {
bastionClient, client = newBastionHostSshClient(project)
defer bastionClient.Close()
} else {
client = newSshClient(project)
}
defer client.Close()
targetConn := remote.Connection{Client: client}
command := fmt.Sprintf("tail -f -n%d %s", lines, project.FilePath)
fmt.Println("Executing: ", command)
targetConn.Run(command)
}
func newBastionHostSshClient(project config.Project) (*ssh.Client, *ssh.Client) {
bastionServer, exists := servers[project.BastionServer]
if !exists {
log.Fatal("BastionServer config not found")
}
fmt.Println("Bastion Server: ", bastionServer.Name)
targetServer, exists := servers[project.Server]
if !exists {
log.Fatal("Target server config not found")
}
fmt.Println("Target Server: ", targetServer.Name)
sshConfig := &ssh.ClientConfig{
User: bastionServer.Username,
Auth: []ssh.AuthMethod{
ssh.Password(bastionServer.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
fmt.Println("Connecting to bastion server")
bastionClient, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", bastionServer.Host, bastionServer.Port), sshConfig)
if err != nil {
log.Fatal(err)
}
targetServerAddr := fmt.Sprintf("%s:%s", targetServer.Host, targetServer.Port)
fmt.Println("Connecting to target server")
conn, err := bastionClient.Dial("tcp", targetServerAddr)
if err != nil {
log.Fatal(err)
}
ncc, chans, reqs, err := ssh.NewClientConn(conn, targetServerAddr, &ssh.ClientConfig{
User: targetServer.Username,
Auth: []ssh.AuthMethod{
ssh.Password(targetServer.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
log.Fatal(err)
}
targetClient := ssh.NewClient(ncc, chans, reqs)
return bastionClient, targetClient
}
func newSshClient(project config.Project) *ssh.Client {
targetServer, exists := servers[project.Server]
if !exists {
log.Fatal("Target server config not found")
}
fmt.Println("Target Server: ", targetServer.Name)
sshConfig := &ssh.ClientConfig{
User: targetServer.Username,
Auth: []ssh.AuthMethod{
ssh.Password(targetServer.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", targetServer.Host, targetServer.Port), sshConfig)
if err != nil {
log.Fatal(err)
}
return client
}