-
Notifications
You must be signed in to change notification settings - Fork 0
/
classic_dialer.go
156 lines (129 loc) · 4.16 KB
/
classic_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package channel
import (
"errors"
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/identity"
"github.com/openziti/transport/v2"
"time"
)
type classicDialer struct {
identity *identity.TokenId
endpoint transport.Address
localBinding string
headers map[int32][]byte
underlayFactory func(messageStrategy MessageStrategy, peer transport.Conn, version uint32) classicUnderlay
messageStrategy MessageStrategy
transportConfig transport.Configuration
}
type DialerConfig struct {
Identity *identity.TokenId
Endpoint transport.Address
LocalBinding string
Headers map[int32][]byte
MessageStrategy MessageStrategy
TransportConfig transport.Configuration
}
func NewClassicDialer(cfg DialerConfig) UnderlayFactory {
result := &classicDialer{
identity: cfg.Identity,
endpoint: cfg.Endpoint,
localBinding: cfg.LocalBinding,
headers: cfg.Headers,
messageStrategy: cfg.MessageStrategy,
transportConfig: cfg.TransportConfig,
}
if cfg.Endpoint.Type() == "dtls" {
result.underlayFactory = newDatagramUnderlay
} else {
result.underlayFactory = newClassicImpl
}
return result
}
func (self *classicDialer) Create(timeout time.Duration) (Underlay, error) {
log := pfxlog.ContextLogger(self.endpoint.String())
log.Debug("started")
defer log.Debug("exited")
if timeout == 0 {
timeout = 15 * time.Second
}
deadline := time.Now().Add(timeout)
version := uint32(2)
tryCount := 0
log.Debugf("Attempting to dial with bind: %s", self.localBinding)
for time.Now().Before(deadline) {
peer, err := self.endpoint.DialWithLocalBinding("classic", self.localBinding, self.identity, timeout, self.transportConfig)
if err != nil {
return nil, err
}
underlay := self.underlayFactory(self.messageStrategy, peer, version)
if err = self.sendHello(underlay, deadline); err != nil {
if tryCount > 0 {
return nil, err
} else {
log.WithError(err).Warnf("error initiating channel with hello")
}
tryCount++
version, _ = GetRetryVersion(err)
log.Warnf("Retrying dial with protocol version %v", version)
continue
}
return underlay, nil
}
return nil, errors.New("timeout waiting for dial")
}
func (self *classicDialer) sendHello(underlay classicUnderlay, deadline time.Time) error {
log := pfxlog.ContextLogger(underlay.Label())
defer log.Debug("exited")
log.Debug("started")
peer := underlay.getPeer()
if err := peer.SetDeadline(deadline); err != nil {
return err
}
defer func() {
if err := peer.SetDeadline(time.Time{}); err != nil { // clear write deadline
log.WithError(err).Error("unable to clear deadline")
}
}()
request := NewHello(self.identity.Token, self.headers)
request.SetSequence(HelloSequence)
if err := underlay.Tx(request); err != nil {
_ = underlay.Close()
return err
}
response, err := underlay.Rx()
if err != nil {
if errors.Is(err, BadMagicNumberError) {
return fmt.Errorf("could not negotiate connection with %v, invalid header", peer.RemoteAddr().String())
}
return err
}
if !response.IsReplyingTo(HelloSequence) || response.ContentType != ContentTypeResultType {
return fmt.Errorf("channel synchronization error, expected %v, got %v", HelloSequence, response.ReplyFor())
}
result := UnmarshalResult(response)
if !result.Success {
return errors.New(result.Message)
}
connectionId := string(response.Headers[ConnectionIdHeader])
id := ""
if val, ok := response.GetStringHeader(IdHeader); ok {
id = val
} else if certs := underlay.Certificates(); len(certs) > 0 {
id = certs[0].Subject.CommonName
}
underlay.init(id, connectionId, response.Headers)
return nil
}