-
Notifications
You must be signed in to change notification settings - Fork 318
/
transport.go
504 lines (423 loc) · 15.1 KB
/
transport.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package git
/*
#include <string.h>
#include <git2.h>
#include <git2/sys/transport.h>
typedef struct {
git_smart_subtransport parent;
void *handle;
} _go_managed_smart_subtransport;
typedef struct {
git_smart_subtransport_stream parent;
void *handle;
} _go_managed_smart_subtransport_stream;
int _go_git_transport_register(const char *prefix, void *handle);
int _go_git_transport_smart(git_transport **out, git_remote *owner, int stateless, _go_managed_smart_subtransport *subtransport_payload);
void _go_git_setup_smart_subtransport_stream(_go_managed_smart_subtransport_stream *stream);
*/
import "C"
import (
"fmt"
"io"
"reflect"
"runtime"
"sync"
"unsafe"
)
var (
// globalRegisteredSmartTransports is a mapping of global, git2go-managed
// transports.
globalRegisteredSmartTransports = struct {
sync.Mutex
transports map[string]*RegisteredSmartTransport
}{
transports: make(map[string]*RegisteredSmartTransport),
}
)
// unregisterManagedTransports unregisters all git2go-managed transports.
func unregisterManagedTransports() error {
globalRegisteredSmartTransports.Lock()
originalTransports := globalRegisteredSmartTransports.transports
globalRegisteredSmartTransports.transports = make(map[string]*RegisteredSmartTransport)
globalRegisteredSmartTransports.Unlock()
var err error
for protocol, managed := range originalTransports {
unregisterErr := managed.Free()
if err == nil && unregisterErr != nil {
err = fmt.Errorf("failed to unregister transport for %q: %v", protocol, unregisterErr)
}
}
return err
}
// SmartServiceAction is an action that the smart transport can ask a
// subtransport to perform.
type SmartServiceAction int
const (
// SmartServiceActionUploadpackLs is used upon connecting to a remote, and is
// used to perform reference discovery prior to performing a pull operation.
SmartServiceActionUploadpackLs SmartServiceAction = C.GIT_SERVICE_UPLOADPACK_LS
// SmartServiceActionUploadpack is used when performing a pull operation.
SmartServiceActionUploadpack SmartServiceAction = C.GIT_SERVICE_UPLOADPACK
// SmartServiceActionReceivepackLs is used upon connecting to a remote, and is
// used to perform reference discovery prior to performing a push operation.
SmartServiceActionReceivepackLs SmartServiceAction = C.GIT_SERVICE_RECEIVEPACK_LS
// SmartServiceActionReceivepack is used when performing a push operation.
SmartServiceActionReceivepack SmartServiceAction = C.GIT_SERVICE_RECEIVEPACK
)
// Transport encapsulates a way to communicate with a Remote.
type Transport struct {
doNotCompare
ptr *C.git_transport
}
// SmartRemoteConnectOptions gets a copy of the proxy options for this transport.
func (t *Transport) SmartRemoteConnectOptions() (*RemoteConnectOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var copts C.git_remote_connect_options
if ret := C.git_transport_remote_connect_options(&copts, t.ptr); ret < 0 {
return nil, MakeGitError(ret)
}
return remoteConnectOptionsFromC(&copts), nil
}
// SmartCredentials calls the credentials callback for this transport.
func (t *Transport) SmartCredentials(user string, methods CredentialType) (*Credential, error) {
cred := newCredential()
var cstr *C.char
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if user != "" {
cstr = C.CString(user)
defer C.free(unsafe.Pointer(cstr))
}
ret := C.git_transport_smart_credentials(&cred.ptr, t.ptr, cstr, C.int(methods))
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
// SmartCertificateCheck calls the certificate check for this transport.
func (t *Transport) SmartCertificateCheck(cert *Certificate, valid bool, hostname string) error {
var ccert *C.git_cert
switch cert.Kind {
case CertificateHostkey:
chostkeyCert := C.git_cert_hostkey{
parent: C.git_cert{
cert_type: C.GIT_CERT_HOSTKEY_LIBSSH2,
},
_type: C.git_cert_ssh_t(cert.Kind),
hostkey: (*C.char)(C.CBytes(cert.Hostkey.Hostkey)),
hostkey_len: C.size_t(len(cert.Hostkey.Hostkey)),
}
defer C.free(unsafe.Pointer(chostkeyCert.hostkey))
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_md5[0]), unsafe.Pointer(&cert.Hostkey.HashMD5[0]), C.size_t(len(cert.Hostkey.HashMD5)))
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_sha1[0]), unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), C.size_t(len(cert.Hostkey.HashSHA1)))
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_sha256[0]), unsafe.Pointer(&cert.Hostkey.HashSHA256[0]), C.size_t(len(cert.Hostkey.HashSHA256)))
if cert.Hostkey.SSHPublicKey.Type() == "ssh-rsa" {
chostkeyCert.raw_type = C.GIT_CERT_SSH_RAW_TYPE_RSA
} else if cert.Hostkey.SSHPublicKey.Type() == "ssh-dss" {
chostkeyCert.raw_type = C.GIT_CERT_SSH_RAW_TYPE_DSS
} else {
chostkeyCert.raw_type = C.GIT_CERT_SSH_RAW_TYPE_UNKNOWN
}
ccert = (*C.git_cert)(unsafe.Pointer(&chostkeyCert))
case CertificateX509:
cx509Cert := C.git_cert_x509{
parent: C.git_cert{
cert_type: C.GIT_CERT_X509,
},
len: C.size_t(len(cert.X509.Raw)),
data: C.CBytes(cert.X509.Raw),
}
defer C.free(cx509Cert.data)
ccert = (*C.git_cert)(unsafe.Pointer(&cx509Cert))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
chostname := C.CString(hostname)
defer C.free(unsafe.Pointer(chostname))
cvalid := C.int(0)
if valid {
cvalid = C.int(1)
}
ret := C.git_transport_smart_certificate_check(t.ptr, ccert, cvalid, chostname)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
// SmartSubtransport is the interface for custom subtransports which carry data
// for the smart transport.
type SmartSubtransport interface {
// Action creates a SmartSubtransportStream for the provided url and
// requested action.
Action(url string, action SmartServiceAction) (SmartSubtransportStream, error)
// Close closes the SmartSubtransport.
//
// Subtransports are guaranteed a call to Close between
// calls to Action, except for the following two "natural" progressions
// of actions against a constant URL.
//
// 1. UPLOADPACK_LS -> UPLOADPACK
// 2. RECEIVEPACK_LS -> RECEIVEPACK
Close() error
// Free releases the resources of the SmartSubtransport.
Free()
}
// SmartSubtransportStream is the interface for streams used by the smart
// transport to read and write data from a subtransport.
type SmartSubtransportStream interface {
io.Reader
io.Writer
// Free releases the resources of the SmartSubtransportStream.
Free()
}
// SmartSubtransportCallback is a function which creates a new subtransport for
// the smart transport.
type SmartSubtransportCallback func(remote *Remote, transport *Transport) (SmartSubtransport, error)
// RegisteredSmartTransport represents a transport that has been registered.
type RegisteredSmartTransport struct {
doNotCompare
name string
stateless bool
callback SmartSubtransportCallback
handle unsafe.Pointer
}
// NewRegisteredSmartTransport adds a custom transport definition, to be used
// in addition to the built-in set of transports that come with libgit2.
func NewRegisteredSmartTransport(
name string,
stateless bool,
callback SmartSubtransportCallback,
) (*RegisteredSmartTransport, error) {
return newRegisteredSmartTransport(name, stateless, callback, false)
}
func newRegisteredSmartTransport(
name string,
stateless bool,
callback SmartSubtransportCallback,
global bool,
) (*RegisteredSmartTransport, error) {
if !global {
// Check if we had already registered a smart transport for this protocol. If
// we had, free it. The user is now responsible for this transport for the
// lifetime of the library.
globalRegisteredSmartTransports.Lock()
if managed, ok := globalRegisteredSmartTransports.transports[name]; ok {
delete(globalRegisteredSmartTransports.transports, name)
globalRegisteredSmartTransports.Unlock()
err := managed.Free()
if err != nil {
return nil, err
}
} else {
globalRegisteredSmartTransports.Unlock()
}
}
cstr := C.CString(name)
defer C.free(unsafe.Pointer(cstr))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
registeredSmartTransport := &RegisteredSmartTransport{
name: name,
stateless: stateless,
callback: callback,
}
registeredSmartTransport.handle = pointerHandles.Track(registeredSmartTransport)
ret := C._go_git_transport_register(cstr, registeredSmartTransport.handle)
if ret != 0 {
pointerHandles.Untrack(registeredSmartTransport.handle)
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(registeredSmartTransport, (*RegisteredSmartTransport).Free)
return registeredSmartTransport, nil
}
// Free releases all resources used by the RegisteredSmartTransport and
// unregisters the custom transport definition referenced by it.
func (t *RegisteredSmartTransport) Free() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cstr := C.CString(t.name)
defer C.free(unsafe.Pointer(cstr))
if ret := C.git_transport_unregister(cstr); ret < 0 {
return MakeGitError(ret)
}
pointerHandles.Untrack(t.handle)
runtime.SetFinalizer(t, nil)
t.handle = nil
return nil
}
//export smartTransportCallback
func smartTransportCallback(
errorMessage **C.char,
out **C.git_transport,
owner *C.git_remote,
handle unsafe.Pointer,
) C.int {
registeredSmartTransport := pointerHandles.Get(handle).(*RegisteredSmartTransport)
remote, ok := remotePointers.get(owner)
if !ok {
// create a new empty remote and set it
// as a weak pointer, so that control stays in golang
remote = createNewEmptyRemote()
remote.weak = true
}
managed := &managedSmartSubtransport{
remote: remote,
callback: registeredSmartTransport.callback,
subtransport: (*C._go_managed_smart_subtransport)(C.calloc(1, C.size_t(unsafe.Sizeof(C._go_managed_smart_subtransport{})))),
}
managedHandle := pointerHandles.Track(managed)
managed.handle = managedHandle
managed.subtransport.handle = managedHandle
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C._go_git_transport_smart(out, owner, cbool(registeredSmartTransport.stateless), managed.subtransport)
if ret != 0 {
pointerHandles.Untrack(managedHandle)
}
return ret
}
//export smartTransportSubtransportCallback
func smartTransportSubtransportCallback(
errorMessage **C.char,
wrapperPtr *C._go_managed_smart_subtransport,
owner *C.git_transport,
) C.int {
subtransport := pointerHandles.Get(wrapperPtr.handle).(*managedSmartSubtransport)
underlyingSmartSubtransport, err := subtransport.callback(subtransport.remote, &Transport{ptr: owner})
if err != nil {
return setCallbackError(errorMessage, err)
}
subtransport.underlying = underlyingSmartSubtransport
return C.int(ErrorCodeOK)
}
type managedSmartSubtransport struct {
owner *C.git_transport
callback SmartSubtransportCallback
remote *Remote
subtransport *C._go_managed_smart_subtransport
underlying SmartSubtransport
handle unsafe.Pointer
currentManagedStream *managedSmartSubtransportStream
}
func getSmartSubtransportInterface(subtransport *C.git_smart_subtransport) *managedSmartSubtransport {
wrapperPtr := (*C._go_managed_smart_subtransport)(unsafe.Pointer(subtransport))
return pointerHandles.Get(wrapperPtr.handle).(*managedSmartSubtransport)
}
//export smartSubtransportActionCallback
func smartSubtransportActionCallback(
errorMessage **C.char,
out **C.git_smart_subtransport_stream,
t *C.git_smart_subtransport,
url *C.char,
action C.git_smart_service_t,
) C.int {
subtransport := getSmartSubtransportInterface(t)
underlyingStream, err := subtransport.underlying.Action(C.GoString(url), SmartServiceAction(action))
if err != nil {
return setCallbackError(errorMessage, err)
}
// It's okay to do strict equality here: we expect both to be identical.
if subtransport.currentManagedStream == nil || subtransport.currentManagedStream.underlying != underlyingStream {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
stream := (*C._go_managed_smart_subtransport_stream)(C.calloc(1, C.size_t(unsafe.Sizeof(C._go_managed_smart_subtransport_stream{}))))
managed := &managedSmartSubtransportStream{
underlying: underlyingStream,
streamPtr: stream,
}
managedHandle := pointerHandles.Track(managed)
managed.handle = managedHandle
stream.handle = managedHandle
C._go_git_setup_smart_subtransport_stream(stream)
subtransport.currentManagedStream = managed
}
*out = &subtransport.currentManagedStream.streamPtr.parent
return C.int(ErrorCodeOK)
}
//export smartSubtransportCloseCallback
func smartSubtransportCloseCallback(errorMessage **C.char, t *C.git_smart_subtransport) C.int {
subtransport := getSmartSubtransportInterface(t)
subtransport.currentManagedStream = nil
if subtransport.underlying != nil {
err := subtransport.underlying.Close()
if err != nil {
return setCallbackError(errorMessage, err)
}
}
return C.int(ErrorCodeOK)
}
//export smartSubtransportFreeCallback
func smartSubtransportFreeCallback(t *C.git_smart_subtransport) {
subtransport := getSmartSubtransportInterface(t)
if subtransport.underlying != nil {
subtransport.underlying.Free()
subtransport.underlying = nil
}
pointerHandles.Untrack(subtransport.handle)
C.free(unsafe.Pointer(subtransport.subtransport))
subtransport.handle = nil
subtransport.subtransport = nil
}
type managedSmartSubtransportStream struct {
owner *C.git_smart_subtransport_stream
streamPtr *C._go_managed_smart_subtransport_stream
underlying SmartSubtransportStream
handle unsafe.Pointer
}
func getSmartSubtransportStreamInterface(subtransportStream *C.git_smart_subtransport_stream) *managedSmartSubtransportStream {
managedSubtransportStream := (*C._go_managed_smart_subtransport_stream)(unsafe.Pointer(subtransportStream))
return pointerHandles.Get(managedSubtransportStream.handle).(*managedSmartSubtransportStream)
}
//export smartSubtransportStreamReadCallback
func smartSubtransportStreamReadCallback(
errorMessage **C.char,
s *C.git_smart_subtransport_stream,
buffer *C.char,
bufSize C.size_t,
bytesRead *C.size_t,
) C.int {
stream := getSmartSubtransportStreamInterface(s)
var p []byte
header := (*reflect.SliceHeader)(unsafe.Pointer(&p))
header.Cap = int(bufSize)
header.Len = int(bufSize)
header.Data = uintptr(unsafe.Pointer(buffer))
n, err := stream.underlying.Read(p)
*bytesRead = C.size_t(n)
if n == 0 && err != nil {
if err == io.EOF {
return C.int(ErrorCodeOK)
}
return setCallbackError(errorMessage, err)
}
return C.int(ErrorCodeOK)
}
//export smartSubtransportStreamWriteCallback
func smartSubtransportStreamWriteCallback(
errorMessage **C.char,
s *C.git_smart_subtransport_stream,
buffer *C.char,
bufLen C.size_t,
) C.int {
stream := getSmartSubtransportStreamInterface(s)
var p []byte
header := (*reflect.SliceHeader)(unsafe.Pointer(&p))
header.Cap = int(bufLen)
header.Len = int(bufLen)
header.Data = uintptr(unsafe.Pointer(buffer))
if _, err := stream.underlying.Write(p); err != nil {
return setCallbackError(errorMessage, err)
}
return C.int(ErrorCodeOK)
}
//export smartSubtransportStreamFreeCallback
func smartSubtransportStreamFreeCallback(s *C.git_smart_subtransport_stream) {
stream := getSmartSubtransportStreamInterface(s)
stream.underlying.Free()
pointerHandles.Untrack(stream.handle)
C.free(unsafe.Pointer(stream.streamPtr))
stream.handle = nil
stream.streamPtr = nil
}