MCPcopy Create free account
hub / github.com/libgit2/git2go / startSSHServer

Function startSSHServer

remote_test.go:279–444  ·  view source on GitHub ↗
(t *testing.T, hostKey ssh.Signer, authorizedKeys []ssh.PublicKey)

Source from the content-addressed store, hash-verified

277}
278
279func startSSHServer(t *testing.T, hostKey ssh.Signer, authorizedKeys []ssh.PublicKey) net.Listener {
280 t.Helper()
281
282 marshaledAuthorizedKeys := make([][]byte, len(authorizedKeys))
283 for i, authorizedKey := range authorizedKeys {
284 marshaledAuthorizedKeys[i] = authorizedKey.Marshal()
285 }
286
287 config := &ssh.ServerConfig{
288 PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
289 marshaledPubKey := pubKey.Marshal()
290 for _, marshaledAuthorizedKey := range marshaledAuthorizedKeys {
291 if bytes.Equal(marshaledPubKey, marshaledAuthorizedKey) {
292 return &ssh.Permissions{
293 // Record the public key used for authentication.
294 Extensions: map[string]string{
295 "pubkey-fp": ssh.FingerprintSHA256(pubKey),
296 },
297 }, nil
298 }
299 }
300 t.Logf("unknown public key for %q:\n\t%+v\n\t%+v\n", c.User(), pubKey.Marshal(), authorizedKeys)
301 return nil, fmt.Errorf("unknown public key for %q", c.User())
302 },
303 }
304 config.AddHostKey(hostKey)
305
306 listener, err := net.Listen("tcp", "localhost:0")
307 if err != nil {
308 t.Fatalf("Failed to listen for connection: %v", err)
309 }
310
311 go func() {
312 nConn, err := listener.Accept()
313 if err != nil {
314 if strings.Contains(err.Error(), "use of closed network connection") {
315 return
316 }
317 t.Logf("Failed to accept incoming connection: %v", err)
318 return
319 }
320 defer nConn.Close()
321
322 conn, chans, reqs, err := ssh.NewServerConn(nConn, config)
323 if err != nil {
324 t.Logf("failed to handshake: %+v, %+v", conn, err)
325 return
326 }
327
328 // The incoming Request channel must be serviced.
329 go func() {
330 for newRequest := range reqs {
331 t.Logf("new request %v", newRequest)
332 }
333 }()
334
335 // Service only the first channel request
336 newChannel := <-chans

Callers 1

TestRemoteSSHFunction · 0.85

Calls 4

newChannelPipeFunction · 0.85
EqualMethod · 0.80
ErrorMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…