-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialer.go
136 lines (115 loc) · 3.66 KB
/
dialer.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
package permuteproxy
import (
"context"
"fmt"
"net"
"github.com/wzshiming/permuteproxy/protocols"
)
// NewDialer returns a Dialer from the uri
// If the last kind is proxy, it will return Dialer
func (p *Proxy) NewDialer(uri string) (Dialer, error) {
protocol, err := protocols.NewProtocol(uri)
if err != nil {
return nil, err
}
ep := protocol.Endpoint
handler := handle[ep.Network]
if handler.Dialer == nil {
return nil, fmt.Errorf("first scheme %q not supported: %w", ep.Network, protocols.ErrInvalidScheme)
}
dialer := p.Dialer
if dialer == nil {
d := handler.Dialer
dialer = DialerFunc(func(ctx context.Context, network, address string) (Conn, error) {
ctx = withContext(ctx, p)
return d.DialContext(ctx, ep.Network, ep.Address)
})
} else {
d := dialer
dialer = DialerFunc(func(ctx context.Context, network, address string) (Conn, error) {
ctx = withContext(ctx, p)
return d.DialContext(ctx, ep.Network, ep.Address)
})
}
var wrapper protocols.Wrapper
for i := len(protocol.Wrappers) - 1; i >= 0; i-- {
wrapper = protocol.Wrappers[i]
handler = handle[wrapper.Scheme]
if i == 0 && handler.DialerWrapper == nil {
break
} else {
if handler.DialerWrapper == nil {
return nil, fmt.Errorf("%d scheme %q not supported: %w", i, wrapper.Scheme, protocols.ErrInvalidScheme)
}
metadata := wrapper.Metadata
if metadata == nil {
metadata = Metadata{}
}
ctx := withContext(context.Background(), p.withDialer(dialer))
dialer, err = handler.DialerWrapper.DialerWrap(ctx, metadata)
if err != nil {
return nil, fmt.Errorf("failed to wrap scheme %q: %w", wrapper.Scheme, err)
}
}
}
if handler.DialerWrapper == nil {
return nil, fmt.Errorf("last scheme %q not supported: %w", ep.Network, protocols.ErrInvalidScheme)
}
return dialer, nil
}
// NewDialConn returns a DialConn for the given uri.
// If the last kind is stream-like. will return DialConn
func (p *Proxy) NewDialConn(uri string) (DialConn, error) {
protocol, err := protocols.NewProtocol(uri)
if err != nil {
return nil, err
}
ep := protocol.Endpoint
handler := handle[ep.Network]
if handler.Dialer == nil {
return nil, fmt.Errorf("first scheme %q not supported: %w", ep.Network, protocols.ErrInvalidScheme)
}
dialer := p.Dialer
if dialer == nil {
d := handler.Dialer
dialer = DialerFunc(func(ctx context.Context, network, address string) (Conn, error) {
ctx = withContext(ctx, p)
return d.DialContext(ctx, ep.Network, ep.Address)
})
} else {
d := dialer
dialer = DialerFunc(func(ctx context.Context, network, address string) (Conn, error) {
ctx = withContext(ctx, p)
return d.DialContext(ctx, ep.Network, ep.Address)
})
}
var wrapper protocols.Wrapper
for i := len(protocol.Wrappers) - 1; i >= 0; i-- {
wrapper = protocol.Wrappers[i]
handler = handle[wrapper.Scheme]
if i == 0 && handler.DialerWrapper == nil {
break
} else {
if handler.DialerWrapper == nil {
return nil, fmt.Errorf("%d scheme %q not supported: %w", i, wrapper.Scheme, protocols.ErrInvalidScheme)
}
metadata := wrapper.Metadata
if metadata == nil {
metadata = Metadata{}
}
ctx := withContext(context.Background(), p.withDialer(dialer))
dialer, err = handler.DialerWrapper.DialerWrap(ctx, metadata)
if err != nil {
return nil, fmt.Errorf("failed to wrap scheme %q: %w", wrapper.Scheme, err)
}
}
}
if handler.DialerWrapper != nil {
return nil, fmt.Errorf("last scheme %q not supported: %w", ep.Network, protocols.ErrInvalidScheme)
}
dialConn := DialConnFunc(func(ctx context.Context) (net.Conn, error) {
ctx = withContext(ctx, p)
return dialer.DialContext(ctx, ep.Network, ep.Address)
})
return dialConn, nil
}