-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
145 lines (130 loc) · 2.78 KB
/
config.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// entry of ssh.config
type SSHEntry struct {
displayName string
serverName string
port int
user string
password string
keyFile string
}
func (entry *SSHEntry) String() string {
out := fmt.Sprintf("server name: %s\nport: %d\nuser: %s\nprivate key file: %s\n",
entry.serverName, entry.port, entry.user, entry.keyFile)
return out
}
func parseSSHEntry(lines []string) (*SSHEntry, error) {
var defaultKeyFile string // default keyFile
homeDir, err := os.UserHomeDir()
if err == nil {
defaultKeyFile = homeDir + "/.ssh/id_rsa"
}
entry := SSHEntry{
port: 22,
keyFile: defaultKeyFile,
}
for _, line := range lines {
line = strings.Split(line, "#")[0] // remove comment part
line = strings.TrimSpace(line) // trim spaces
// Host
if strings.HasPrefix(line, "Host ") {
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
entry.displayName = parts[1]
}
// HostName
if strings.HasPrefix(line, "HostName ") {
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
entry.serverName = parts[1]
}
// Port
if strings.HasPrefix(line, "Port ") {
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
i, err := strconv.Atoi(parts[1])
if err != nil {
continue
}
entry.port = i
}
// IdentityFile
if strings.HasPrefix(line, "IdentityFile ") {
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
entry.keyFile = parts[1]
}
// User
if strings.HasPrefix(line, "User ") {
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
entry.user = parts[1]
}
// Password [ ext ]
if strings.HasPrefix(line, "Password ") {
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
entry.password = parts[1]
}
}
return &entry, nil
}
func loadSSHConfig(r io.Reader) ([]*SSHEntry, error) {
var entries []*SSHEntry
scanner := bufio.NewScanner(r)
var entryLines []string
inHost := false
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(strings.TrimSpace(line), "Host ") {
// until the first Host
if inHost {
entry, err := parseSSHEntry(entryLines)
if err != nil {
return entries, err
}
entries = append(entries, entry)
}
entryLines = entryLines[:0]
inHost = true
}
entryLines = append(entryLines, line)
}
if err := scanner.Err(); err != nil {
return entries, err
}
entry, err := parseSSHEntry(entryLines)
if err != nil {
return entries, err
}
entries = append(entries, entry)
return entries, nil
}
func loadSSHConfigFile(filePath string) ([]*SSHEntry, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
return loadSSHConfig(file)
}