-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathconfignet.go
More file actions
230 lines (207 loc) · 8.45 KB
/
Copy pathconfignet.go
File metadata and controls
230 lines (207 loc) · 8.45 KB
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package confignet // import "go.opentelemetry.io/collector/config/confignet"
import (
"context"
"fmt"
"net"
"strings"
"time"
)
// TransportType represents a type of network transport protocol
type TransportType string
const (
TransportTypeTCP TransportType = "tcp"
TransportTypeTCP4 TransportType = "tcp4"
TransportTypeTCP6 TransportType = "tcp6"
TransportTypeUDP TransportType = "udp"
TransportTypeUDP4 TransportType = "udp4"
TransportTypeUDP6 TransportType = "udp6"
TransportTypeIP TransportType = "ip"
TransportTypeIP4 TransportType = "ip4"
TransportTypeIP6 TransportType = "ip6"
TransportTypeUnix TransportType = "unix"
TransportTypeUnixgram TransportType = "unixgram"
TransportTypeUnixPacket TransportType = "unixpacket"
TransportTypeNpipe TransportType = "npipe"
transportTypeEmpty TransportType = ""
)
// UnmarshalText unmarshalls text to a TransportType.
// Valid values are "tcp", "tcp4", "tcp6", "udp", "udp4",
// "udp6", "ip", "ip4", "ip6", "unix", "unixgram", "unixpacket" and "npipe"
func (tt *TransportType) UnmarshalText(in []byte) error {
typ := TransportType(in)
switch typ {
case TransportTypeTCP,
TransportTypeTCP4,
TransportTypeTCP6,
TransportTypeUDP,
TransportTypeUDP4,
TransportTypeUDP6,
TransportTypeIP,
TransportTypeIP4,
TransportTypeIP6,
TransportTypeUnix,
TransportTypeUnixgram,
TransportTypeUnixPacket,
TransportTypeNpipe,
transportTypeEmpty:
*tt = typ
return nil
default:
return fmt.Errorf("unsupported transport type %q", typ)
}
}
// DialerConfig contains options for connecting to an address.
type DialerConfig struct {
// Timeout is the maximum amount of time a dial will wait for
// a connect to complete. The default is no timeout.
Timeout time.Duration `mapstructure:"timeout,omitempty"`
// prevent unkeyed literal initialization
_ struct{}
}
// NewDefaultDialerConfig creates a new DialerConfig with any default values set
func NewDefaultDialerConfig() DialerConfig {
return DialerConfig{}
}
// NpipeConfig contains options specific to Windows named pipe transport.
type NpipeConfig struct {
// SecurityDescriptor is a Security Descriptor Definition Language (SDDL)
// string applied to the named pipe when a listener is created. When empty,
// Windows applies its default named pipe DACL, which is roughly equivalent
// to "D:P(A;;GA;;;SY)(A;;GA;;;BA)(A;;0x12019b;;;WD)(A;;0x12019b;;;AN)"
// — full control for LocalSystem (SY) and Administrators (BA), and read
// plus limited write for Everyone (WD) and Anonymous Logon (AN).
// See:
// - https://learn.microsoft.com/en-us/windows/win32/ipc/named-pipe-security-and-access-rights
// - https://learn.microsoft.com/en-us/windows/win32/secauthz/security-descriptor-definition-language
SecurityDescriptor string `mapstructure:"security_descriptor,omitempty"`
// prevent unkeyed literal initialization
_ struct{}
}
// NewDefaultNpipeConfig creates a new NpipeConfig with any default values set
func NewDefaultNpipeConfig() NpipeConfig {
return NpipeConfig{}
}
// AddrConfig represents a network endpoint address.
type AddrConfig struct {
// Endpoint configures the address for this network connection.
// For TCP and UDP networks, the address has the form "host:port". The host must be a literal IP address,
// or a host name that can be resolved to IP addresses. The port must be a literal port number or a service name.
// If the host is a literal IPv6 address it must be enclosed in square brackets, as in "[2001:db8::1]:80" or
// "[fe80::1%zone]:80". The zone specifies the scope of the literal IPv6 address as defined in RFC 4007.
Endpoint string `mapstructure:"endpoint,omitempty"`
// Transport to use. Allowed protocols are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only),
// "udp6" (IPv6-only), "ip", "ip4" (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram", "unixpacket" and
// "npipe" (Windows named pipes, Windows-only).
Transport TransportType `mapstructure:"transport,omitempty"`
// DialerConfig contains options for connecting to an address.
DialerConfig DialerConfig `mapstructure:"dialer,omitempty"`
// NpipeConfig contains options specific to the "npipe" transport (Windows named pipes).
// Settings in this section are ignored for all other transport types.
NpipeConfig NpipeConfig `mapstructure:"npipe,omitempty"`
// prevent unkeyed literal initialization
_ struct{}
}
// NewDefaultAddrConfig creates a new AddrConfig with any default values set
func NewDefaultAddrConfig() AddrConfig {
return AddrConfig{
DialerConfig: NewDefaultDialerConfig(),
NpipeConfig: NewDefaultNpipeConfig(),
}
}
// Dial equivalent with net.Dialer's DialContext for this address.
func (na *AddrConfig) Dial(ctx context.Context) (net.Conn, error) {
if na.Transport == TransportTypeNpipe {
return dialNpipe(ctx, na.Endpoint, na.DialerConfig.Timeout)
}
d := net.Dialer{Timeout: na.DialerConfig.Timeout}
return d.DialContext(ctx, string(na.Transport), na.Endpoint)
}
// Listen equivalent with net.ListenConfig's Listen for this address.
func (na *AddrConfig) Listen(ctx context.Context) (net.Listener, error) {
if na.Transport == TransportTypeNpipe {
return listenNpipe(na.Endpoint, na.NpipeConfig.SecurityDescriptor)
}
lc := net.ListenConfig{}
return lc.Listen(ctx, string(na.Transport), na.Endpoint)
}
func (na *AddrConfig) Validate() error {
switch na.Transport {
case TransportTypeTCP,
TransportTypeTCP4,
TransportTypeTCP6,
TransportTypeUDP,
TransportTypeUDP4,
TransportTypeUDP6,
TransportTypeIP,
TransportTypeIP4,
TransportTypeIP6,
TransportTypeUnix,
TransportTypeUnixgram,
TransportTypeUnixPacket:
return nil
case TransportTypeNpipe:
return validateNpipePath(na.Endpoint)
default:
return fmt.Errorf("invalid transport type %q", na.Transport)
}
}
// validateNpipePath validates a Windows named pipe path.
// Named pipe paths must follow the format: \\<server>\pipe\<name>
// See: https://learn.microsoft.com/en-us/windows/win32/ipc/pipe-names
func validateNpipePath(endpoint string) error {
const maxLen = 256
if len(endpoint) > maxLen {
return fmt.Errorf("named pipe path %q exceeds maximum length of %d characters", endpoint, maxLen)
}
if !strings.HasPrefix(endpoint, `\\`) {
return fmt.Errorf(`named pipe path must start with "\\": %q`, endpoint)
}
// After \\, find the \pipe\ component (case-insensitive per Windows rules)
rest := strings.ToLower(endpoint[2:])
pipeIdx := strings.Index(rest, `\pipe\`)
if pipeIdx < 0 {
return fmt.Errorf(`named pipe path must contain "\pipe\": %q`, endpoint)
}
if pipeIdx == 0 {
return fmt.Errorf("named pipe path must have a non-empty server name: %q", endpoint)
}
pipeName := endpoint[2+pipeIdx+len(`\pipe\`):]
if pipeName == "" {
return fmt.Errorf(`named pipe path must have a non-empty pipe name after "\pipe\": %q`, endpoint)
}
if strings.ContainsRune(pipeName, '\\') {
return fmt.Errorf("named pipe name must not contain backslashes: %q", endpoint)
}
return nil
}
// TCPAddrConfig represents a TCP endpoint address.
type TCPAddrConfig struct {
// Endpoint configures the address for this network connection.
// The address has the form "host:port". The host must be a literal IP address, or a host name that can be
// resolved to IP addresses. The port must be a literal port number or a service name.
// If the host is a literal IPv6 address it must be enclosed in square brackets, as in "[2001:db8::1]:80" or
// "[fe80::1%zone]:80". The zone specifies the scope of the literal IPv6 address as defined in RFC 4007.
Endpoint string `mapstructure:"endpoint,omitempty"`
// DialerConfig contains options for connecting to an address.
DialerConfig DialerConfig `mapstructure:"dialer,omitempty"`
// prevent unkeyed literal initialization
_ struct{}
}
// NewDefaultTCPAddrConfig creates a new TCPAddrConfig with any default values set
func NewDefaultTCPAddrConfig() TCPAddrConfig {
return TCPAddrConfig{
DialerConfig: NewDefaultDialerConfig(),
}
}
// Dial equivalent with net.Dialer's DialContext for this address.
func (na *TCPAddrConfig) Dial(ctx context.Context) (net.Conn, error) {
d := net.Dialer{Timeout: na.DialerConfig.Timeout}
return d.DialContext(ctx, string(TransportTypeTCP), na.Endpoint)
}
// Listen equivalent with net.ListenConfig's Listen for this address.
func (na *TCPAddrConfig) Listen(ctx context.Context) (net.Listener, error) {
lc := net.ListenConfig{}
return lc.Listen(ctx, string(TransportTypeTCP), na.Endpoint)
}