forked from mikkeloscar/sshconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
203 lines (182 loc) · 4.45 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
package sshconfig
import (
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
)
// 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
}
// MustParseSSHConfig must parse the SSH config given by path or it will panic
func MustParseSSHConfig(path string) []*SSHHost {
config, err := ParseSSHConfig(path)
if err != nil {
panic(err)
}
return config
}
// ParseSSHConfig parses a SSH config given by path.
func ParseSSHConfig(path string) ([]*SSHHost, error) {
// read config file
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return parse(string(content))
}
// parses an openssh config file
func parse(input string) ([]*SSHHost, error) {
sshConfigs := []*SSHHost{}
var next item
var sshHost *SSHHost
lexer := lex(input)
Loop:
for {
token := lexer.nextItem()
if sshHost == nil && token.typ != itemHost {
return nil, fmt.Errorf("config variable before Host variable")
}
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 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
}