/
match_criteria.go
110 lines (95 loc) · 2.18 KB
/
match_criteria.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
// Copyright 2020, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package config
import (
"bytes"
"io"
"strings"
)
const (
criteriaAll = "all"
criteriaCanonical = "canonical"
criteriaExec = "exec"
criteriaFinal = "final"
criteriaHost = "host"
criteriaLocalUser = "localuser"
criteriaOriginalHost = "originalhost"
criteriaUser = "user"
)
type matchCriteria struct {
name string
arg string
patterns []*pattern
isNegate bool
}
func newMatchCriteria(name, arg string) (criteria *matchCriteria, err error) {
criteria = &matchCriteria{
name: name,
arg: arg,
}
if len(arg) == 0 {
return criteria, nil
}
if name == criteriaExec {
return criteria, nil
}
listPattern := strings.Split(arg, ",")
criteria.patterns = make([]*pattern, 0, len(listPattern))
for _, raw := range listPattern {
pat := newPattern(raw)
criteria.patterns = append(criteria.patterns, pat)
}
return criteria, nil
}
// MarshalText encode the criteria back to ssh_config format.
func (mcriteria *matchCriteria) MarshalText() (text []byte, err error) {
var buf bytes.Buffer
if mcriteria.isNegate {
buf.WriteByte('!')
}
buf.WriteString(mcriteria.name)
var (
pat *pattern
x int
)
for x, pat = range mcriteria.patterns {
if x == 0 {
buf.WriteByte(' ')
} else {
buf.WriteByte(',')
}
pat.WriteTo(&buf)
}
return buf.Bytes(), nil
}
// WriteTo marshal the matchCriteria into text and write it to w.
func (mcriteria *matchCriteria) WriteTo(w io.Writer) (n int64, err error) {
var text []byte
text, _ = mcriteria.MarshalText()
var c int
c, err = w.Write(text)
return int64(c), err
}
func (mcriteria *matchCriteria) isMatch(s string) bool {
switch mcriteria.name {
case criteriaAll:
if mcriteria.isNegate {
return false
}
return true
case criteriaCanonical:
//TODO
case criteriaExec:
//TODO
case criteriaFinal:
//TODO
case criteriaHost, criteriaLocalUser, criteriaOriginalHost, criteriaUser:
for _, pat := range mcriteria.patterns {
if pat.isMatch(s) {
return !mcriteria.isNegate
}
}
}
return false
}