-
Notifications
You must be signed in to change notification settings - Fork 138
/
request.go
260 lines (222 loc) · 6.92 KB
/
request.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
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 xgress
import (
"encoding/json"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/channel/v2/protobufs"
"github.com/openziti/identity"
"github.com/openziti/transport/v2"
"github.com/openziti/ziti/common/ctrl_msg"
"github.com/openziti/ziti/common/pb/ctrl_pb"
"github.com/pkg/errors"
"io"
"time"
)
type Request struct {
Id string `json:"id"`
ServiceId string `json:"svcId"`
}
func RequestFromJSON(payload []byte) (*Request, error) {
request := &Request{}
err := json.Unmarshal(payload, request)
if err != nil {
return nil, err
}
return request, nil
}
func (r *Request) ToJSON() ([]byte, error) {
bytes, err := json.Marshal(r)
if err != nil {
return nil, err
}
return bytes, nil
}
type Response struct {
Success bool `json:"scc"`
Message string `json:"msg"`
CircuitId string `json:"circuitId"`
}
func ResponseFromJSON(payload []byte) (*Response, error) {
response := &Response{}
err := json.Unmarshal(payload, response)
if err != nil {
return nil, err
}
return response, nil
}
func (r *Response) ToJSON() ([]byte, error) {
bytes, err := json.Marshal(r)
if err != nil {
return nil, err
}
return bytes, nil
}
func SendRequest(request *Request, peer io.Writer) error {
requestJSON, err := request.ToJSON()
if err != nil {
return err
}
_, err = peer.Write(append(requestJSON, []byte{'\n'}...))
if err != nil {
return err
}
return nil
}
func ReceiveRequest(peer transport.Conn) (*Request, error) {
line, err := ReadUntilNewline(peer)
if err != nil {
return nil, err
}
request, err := RequestFromJSON(line)
if err != nil {
return nil, err
}
return request, nil
}
func SendResponse(response *Response, peer io.Writer) error {
responseJSON, err := response.ToJSON()
if err != nil {
return err
}
_, err = peer.Write(append(responseJSON, []byte{'\n'}...))
if err != nil {
return err
}
return nil
}
func ReceiveResponse(peer transport.Conn) (*Response, error) {
line, err := ReadUntilNewline(peer)
if err != nil {
return nil, err
}
response, err := ResponseFromJSON(line)
if err != nil {
return nil, err
}
return response, nil
}
type CircuitInfo struct {
CtrlId string
CircuitId *identity.TokenId
Address Address
ResponseMsg *channel.Message
}
var circuitError = errors.New("error connecting circuit")
type networkControllers interface {
AnyCtrlChannel() channel.Channel
AnyValidCtrlChannel() channel.Channel
GetCtrlChannel(ctrlId string) channel.Channel
DefaultRequestTimeout() time.Duration
ForEach(f func(ctrlId string, ch channel.Channel))
}
func GetCircuit(ctrl networkControllers, ingressId string, service string, timeout time.Duration, peerData map[uint32][]byte) (*CircuitInfo, error) {
ch := ctrl.AnyCtrlChannel()
if ch == nil {
return nil, errors.New("ctrl not ready")
}
log := pfxlog.Logger()
circuitRequest := &ctrl_pb.CircuitRequest{
IngressId: ingressId,
Service: service,
PeerData: peerData,
}
reply, err := protobufs.MarshalTyped(circuitRequest).WithTimeout(timeout).SendForReply(ch)
if err != nil {
log.Errorf("failed to send CircuitRequest message (%v)", err)
return nil, circuitError
}
if reply.ContentType == ctrl_msg.CircuitSuccessType {
var address string
circuitId := &identity.TokenId{Token: string(reply.Body)}
circuitId.Data = make(map[uint32][]byte)
for k, v := range reply.Headers {
if k == ctrl_msg.CircuitSuccessAddressHeader {
address = string(v)
} else {
circuitId.Data[uint32(k)] = v
}
}
log.WithField("circuitId", circuitId.Token).Debug("created new circuit")
return &CircuitInfo{
CtrlId: ch.Id(),
CircuitId: circuitId,
Address: Address(address),
ResponseMsg: reply,
}, nil
} else if reply.ContentType == ctrl_msg.CircuitFailedType {
errMsg := string(reply.Body)
log.WithError(err).Error("failure creating circuit")
return nil, errors.New(errMsg)
} else {
log.Errorf("unexpected controller response to circuit request, response content type [%v]", reply.ContentType)
return nil, circuitError
}
}
func CreateCircuit(ctrl networkControllers, peer Connection, request *Request, bindHandler BindHandler, options *Options) *Response {
circuitInfo, err := GetCircuit(ctrl, request.Id, request.ServiceId, options.GetCircuitTimeout, nil)
if err != nil {
return &Response{Success: false, Message: err.Error()}
}
x := NewXgress(circuitInfo.CircuitId.Token, circuitInfo.CtrlId, circuitInfo.Address, peer, Initiator, options, map[string]string{
"serviceId": request.ServiceId,
})
bindHandler.HandleXgressBind(x)
x.Start()
return &Response{Success: true, CircuitId: circuitInfo.CircuitId.Token}
}
func RemoveTerminator(ctrls networkControllers, terminatorId string) error {
log := pfxlog.Logger()
request := &ctrl_pb.RemoveTerminatorRequest{
TerminatorId: terminatorId,
}
responseMsg, err := protobufs.MarshalTyped(request).WithTimeout(ctrls.DefaultRequestTimeout()).SendForReply(ctrls.AnyCtrlChannel())
if err != nil {
log.WithError(err).Errorf("failed to send RemoveTerminatorRequest message")
return err
}
if responseMsg != nil && responseMsg.ContentType == channel.ContentTypeResultType {
result := channel.UnmarshalResult(responseMsg)
if result.Success {
log.Debugf("successfully removed service terminator [s/%s]", terminatorId)
return nil
}
log.Errorf("failure removing service terminator (%v)", result.Message)
return errors.New(result.Message)
} else {
log.Errorf("unexpected controller response, ContentType [%v]", responseMsg.ContentType)
return errors.Errorf("unexpected controller response, ContentType [%v]", responseMsg.ContentType)
}
}
func RemoveTerminators(ctrls networkControllers, terminatorIds []string) {
log := pfxlog.Logger()
request := &ctrl_pb.RemoveTerminatorsRequest{
TerminatorIds: terminatorIds,
}
responseMsg, err := protobufs.MarshalTyped(request).WithTimeout(ctrls.DefaultRequestTimeout()).SendForReply(ctrls.AnyValidCtrlChannel())
if err != nil {
log.WithError(err).Errorf("failed to send RemoveTerminatorsRequest message")
return
}
if responseMsg != nil && responseMsg.ContentType == channel.ContentTypeResultType {
result := channel.UnmarshalResult(responseMsg)
if result.Success {
log.Debug("successfully removed terminators")
} else {
log.Errorf("failure removing terminators (%v)", result.Message)
}
} else {
log.Errorf("unexpected controller response, ContentType [%v]", responseMsg.ContentType)
}
}