forked from mikkeloscar/sshconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
266 lines (234 loc) · 5.98 KB
/
parser.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package sshconfig
import (
"fmt"
"io/fs"
"io/ioutil"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/mitchellh/go-homedir"
)
// SSHHost defines a single host entry in a ssh config
type SSHHost struct {
Host []string
HostName string
User string
Port int
ProxyCommand string
HostKeyAlgorithms string
IdentityFile string
LocalForwards []Forward
RemoteForwards []Forward
DynamicForwards []DynamicForward
}
// Forward defines a single port forward entry
type Forward struct {
InHost string
InPort int
OutHost string
OutPort int
}
// NewForward returns Forward object parsed from LocalForward or RemoteForward string
func NewForward(f string) (Forward, error) {
r := regexp.MustCompile(`((\S+):)?(\d+)\s+(\S+):(\d+)`)
m := r.FindStringSubmatch(f)
if len(m) < 6 {
return Forward{}, fmt.Errorf("Invalid forward: %#v", f)
}
InPort, err := strconv.Atoi(m[3])
if err != nil {
return Forward{}, err
}
OutPort, err := strconv.Atoi(m[5])
if err != nil {
return Forward{}, err
}
return Forward{
InHost: m[2],
InPort: InPort,
OutHost: m[4],
OutPort: OutPort,
}, nil
}
// DynamicForward defines a single dynamic port forward entry
type DynamicForward struct {
Host string
Port int
}
// NewDynamicForward returns DForward object parsed from DynamicForward string
func NewDynamicForward(f string) (DynamicForward, error) {
r := regexp.MustCompile(`((\S+):)?(\d+)`)
m := r.FindStringSubmatch(f)
if len(m) < 4 {
return DynamicForward{}, fmt.Errorf("Invalid dynamic forward: %#v", f)
}
InPort, err := strconv.Atoi(m[3])
if err != nil {
return DynamicForward{}, err
}
return DynamicForward{
Host: m[2],
Port: InPort,
}, nil
}
// MustParse must parse the SSH config given by path or it will panic
func MustParse(path string) []*SSHHost {
config, err := Parse(path)
if err != nil {
panic(err)
}
return config
}
// MustParseSSHConfig must parse the SSH config given by path or it will panic
// Deprecated: Use MustParse instead.
func MustParseSSHConfig(path string) []*SSHHost {
return MustParse(path)
}
// ParseSSHConfig parses a SSH config given by path.
// Deprecated: Use Parse instead.
func ParseSSHConfig(path string) ([]*SSHHost, error) {
return Parse(path)
}
// Parse parses a SSH config given by path.
func Parse(path string) ([]*SSHHost, error) {
// read config file
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return parse(string(content), path)
}
// ParseFS parses a SSH config given by path contained in fsys.
func ParseFS(fsys fs.FS, path string) ([]*SSHHost, error) {
// read config file
content, err := fs.ReadFile(fsys, path)
if err != nil {
return nil, err
}
return parse(string(content), path)
}
// parses an openssh config file
func parse(input string, path string) ([]*SSHHost, error) {
sshConfigs := []*SSHHost{}
var next item
var sshHost *SSHHost
lexer := lex(input)
Loop:
for {
token := lexer.nextItem()
if sshHost == nil {
if token.typ != itemEOF && token.typ != itemHost && token.typ != itemInclude {
return nil, fmt.Errorf("%s:%d: config variable before Host variable", path, token.pos)
}
} else if token.typ == itemInclude {
return nil, fmt.Errorf("include not allowed in Host block")
}
switch token.typ {
case itemHost:
if sshHost != nil {
sshConfigs = append(sshConfigs, sshHost)
}
sshHost = &SSHHost{Host: []string{}, Port: 22}
case itemHostValue:
sshHost.Host = strings.Split(token.val, " ")
case itemHostName:
next = lexer.nextItem()
if next.typ != itemValue {
return nil, fmt.Errorf(next.val)
}
sshHost.HostName = next.val
case itemUser:
next = lexer.nextItem()
if next.typ != itemValue {
return nil, fmt.Errorf(next.val)
}
sshHost.User = next.val
case itemPort:
next = lexer.nextItem()
if next.typ != itemValue {
return nil, fmt.Errorf(next.val)
}
port, err := strconv.Atoi(next.val)
if err != nil {
return nil, err
}
sshHost.Port = port
case itemProxyCommand:
next = lexer.nextItem()
if next.typ != itemValue {
return nil, fmt.Errorf(next.val)
}
sshHost.ProxyCommand = next.val
case itemHostKeyAlgorithms:
next = lexer.nextItem()
if next.typ != itemValue {
return nil, fmt.Errorf(next.val)
}
sshHost.HostKeyAlgorithms = next.val
case itemIdentityFile:
next = lexer.nextItem()
if next.typ != itemValue {
return nil, fmt.Errorf(next.val)
}
sshHost.IdentityFile = next.val
case itemLocalForward:
next = lexer.nextItem()
f, err := NewForward(next.val)
if err != nil {
return nil, err
}
sshHost.LocalForwards = append(sshHost.LocalForwards, f)
case itemRemoteForward:
next = lexer.nextItem()
f, err := NewForward(next.val)
if err != nil {
return nil, err
}
sshHost.RemoteForwards = append(sshHost.RemoteForwards, f)
case itemDynamicForward:
next = lexer.nextItem()
f, err := NewDynamicForward(next.val)
if err != nil {
return nil, err
}
sshHost.DynamicForwards = append(sshHost.DynamicForwards, f)
case itemInclude:
next = lexer.nextItem()
if next.typ != itemValue {
return nil, fmt.Errorf(next.val)
}
includePath := next.val
if strings.HasPrefix(includePath, "~") {
expandedPath, err := homedir.Expand(includePath)
if err != nil {
return nil, err
}
includePath = expandedPath
} else if !strings.HasPrefix(includePath, "/") {
includePath = filepath.Join(filepath.Dir(path), next.val)
}
files, err := filepath.Glob(includePath)
if err != nil {
return nil, err
}
for _, f := range files {
includeSshConfigs, err := Parse(f)
if err != nil {
return nil, err
}
sshConfigs = append(sshConfigs, includeSshConfigs...)
}
case itemError:
return nil, fmt.Errorf("%s at pos %d", token.val, token.pos)
case itemEOF:
if sshHost != nil {
sshConfigs = append(sshConfigs, sshHost)
}
break Loop
default:
// continue onwards
}
}
return sshConfigs, nil
}