-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpatterns.go
186 lines (150 loc) · 3.87 KB
/
patterns.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
package mafmt
import (
"strings"
ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
)
// Define a dns4 format multiaddr
var DNS4 = Base(madns.P_DNS4)
// Define a dns6 format multiaddr
var DNS6 = Base(madns.P_DNS6)
// Define a dnsaddr, dns4 or dns6 format multiaddr
var DNS = Or(
Base(madns.P_DNSADDR),
DNS4,
DNS6,
)
// Define IP as either ipv4 or ipv6
var IP = Or(Base(ma.P_IP4), Base(ma.P_IP6))
// Define TCP as 'tcp' on top of either ipv4 or ipv6, or dns equivalents.
var TCP = Or(
And(DNS, Base(ma.P_TCP)),
And(IP, Base(ma.P_TCP)),
)
// Define UDP as 'udp' on top of either ipv4 or ipv6, or dns equivalents.
var UDP = Or(
And(DNS, Base(ma.P_UDP)),
And(IP, Base(ma.P_UDP)),
)
// Define UTP as 'utp' on top of udp (on top of ipv4 or ipv6).
var UTP = And(UDP, Base(ma.P_UTP))
// Define QUIC as 'quic' on top of udp (on top of ipv4 or ipv6)
var QUIC = And(UDP, Base(ma.P_QUIC))
// Define garlic32 (i2p hashed) as it self
var GARLIC32 = Base(ma.P_GARLIC32)
// Define garlic64 (i2p destination) as it self
var GARLIC64 = Base(ma.P_GARLIC64)
// Define garlic (i2p destination or hashed) as any of garlic32 or garlic64
var GARLIC = Or(GARLIC64, GARLIC32)
// Define sam3 as tcp or udp and sam3
// Sam3 have a special case, he can't be used to connect to other peers but his
// instance allow to listen and connect garlic
var SAM3 = And(Or(TCP, UDP), Base(ma.P_SAM3))
// Define unreliable transport as udp or sam3
var Unreliable = Or(UDP)
// Now define a Reliable transport as either tcp or utp or quic or garlic
var Reliable = Or(TCP, UTP, QUIC, GARLIC)
// IPFS can run over any reliable underlying transport protocol
var IPFS = And(Reliable, Base(ma.P_IPFS))
// Define http over TCP or DNS or http over DNS format multiaddr
var HTTP = Or(
And(TCP, Base(ma.P_HTTP)),
And(IP, Base(ma.P_HTTP)),
And(DNS, Base(ma.P_HTTP)),
)
// Define https over TCP or DNS or https over DNS format multiaddr
var HTTPS = Or(
And(TCP, Base(ma.P_HTTPS)),
And(IP, Base(ma.P_HTTPS)),
And(DNS, Base(ma.P_HTTPS)),
)
// Define p2p-webrtc-direct over HTTP or p2p-webrtc-direct over HTTPS format multiaddr
var WebRTCDirect = Or(
And(HTTP, Base(ma.P_P2P_WEBRTC_DIRECT)),
And(HTTPS, Base(ma.P_P2P_WEBRTC_DIRECT)))
const (
or = iota
and = iota
)
func And(ps ...Pattern) Pattern {
return &pattern{
Op: and,
Args: ps,
}
}
func Or(ps ...Pattern) Pattern {
return &pattern{
Op: or,
Args: ps,
}
}
type Pattern interface {
Matches(ma.Multiaddr) bool
partialMatch([]ma.Protocol) (bool, []ma.Protocol)
String() string
}
type pattern struct {
Args []Pattern
Op int
}
func (ptrn *pattern) Matches(a ma.Multiaddr) bool {
ok, rem := ptrn.partialMatch(a.Protocols())
return ok && len(rem) == 0
}
func (ptrn *pattern) partialMatch(pcs []ma.Protocol) (bool, []ma.Protocol) {
switch ptrn.Op {
case or:
for _, a := range ptrn.Args {
ok, rem := a.partialMatch(pcs)
if ok {
return true, rem
}
}
return false, nil
case and:
if len(pcs) < len(ptrn.Args) {
return false, nil
}
for i := 0; i < len(ptrn.Args); i++ {
ok, rem := ptrn.Args[i].partialMatch(pcs)
if !ok {
return false, nil
}
pcs = rem
}
return true, pcs
default:
panic("unrecognized pattern operand")
}
}
func (ptrn *pattern) String() string {
var sub []string
for _, a := range ptrn.Args {
sub = append(sub, a.String())
}
switch ptrn.Op {
case and:
return strings.Join(sub, "/")
case or:
return "{" + strings.Join(sub, "|") + "}"
default:
panic("unrecognized pattern op!")
}
}
type Base int
func (p Base) Matches(a ma.Multiaddr) bool {
pcs := a.Protocols()
return pcs[0].Code == int(p) && len(pcs) == 1
}
func (p Base) partialMatch(pcs []ma.Protocol) (bool, []ma.Protocol) {
if len(pcs) == 0 {
return false, nil
}
if pcs[0].Code == int(p) {
return true, pcs[1:]
}
return false, nil
}
func (p Base) String() string {
return ma.ProtocolWithCode(int(p)).Name
}