|
| 1 | +package peerbroker |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + |
| 6 | + "github.com/pion/webrtc/v3" |
| 7 | + "golang.org/x/xerrors" |
| 8 | + |
| 9 | + "github.com/coder/coder/peer" |
| 10 | + "github.com/coder/coder/peerbroker/proto" |
| 11 | +) |
| 12 | + |
| 13 | +// Dial consumes the PeerBroker gRPC connection negotiation stream to produce a WebRTC peered connection. |
| 14 | +func Dial(stream proto.DRPCPeerBroker_NegotiateConnectionClient, iceServers []webrtc.ICEServer, opts *peer.ConnOpts) (*peer.Conn, error) { |
| 15 | + // Convert WebRTC ICE servers to the protobuf type. |
| 16 | + protoIceServers := make([]*proto.WebRTCICEServer, 0, len(iceServers)) |
| 17 | + for _, iceServer := range iceServers { |
| 18 | + var credentialString string |
| 19 | + if value, ok := iceServer.Credential.(string); ok { |
| 20 | + credentialString = value |
| 21 | + } |
| 22 | + protoIceServers = append(protoIceServers, &proto.WebRTCICEServer{ |
| 23 | + Urls: iceServer.URLs, |
| 24 | + Username: iceServer.Username, |
| 25 | + Credential: credentialString, |
| 26 | + CredentialType: int32(iceServer.CredentialType), |
| 27 | + }) |
| 28 | + } |
| 29 | + if len(protoIceServers) > 0 { |
| 30 | + // Send ICE servers to connect with. |
| 31 | + // Client sends ICE servers so clients can determine the node |
| 32 | + // servers will meet at. eg. us-west1.coder.com could be a TURN server. |
| 33 | + err := stream.Send(&proto.NegotiateConnection_ClientToServer{ |
| 34 | + Message: &proto.NegotiateConnection_ClientToServer_Servers{ |
| 35 | + Servers: &proto.WebRTCICEServers{ |
| 36 | + Servers: protoIceServers, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + return nil, xerrors.Errorf("write ice servers: %w", err) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + peerConn, err := peer.Client(iceServers, opts) |
| 46 | + if err != nil { |
| 47 | + return nil, xerrors.Errorf("create peer connection: %w", err) |
| 48 | + } |
| 49 | + go func() { |
| 50 | + defer stream.Close() |
| 51 | + // Exchanging messages from the peer connection to negotiate a connection. |
| 52 | + for { |
| 53 | + select { |
| 54 | + case <-peerConn.Closed(): |
| 55 | + return |
| 56 | + case sessionDescription := <-peerConn.LocalSessionDescription(): |
| 57 | + err = stream.Send(&proto.NegotiateConnection_ClientToServer{ |
| 58 | + Message: &proto.NegotiateConnection_ClientToServer_Offer{ |
| 59 | + Offer: &proto.WebRTCSessionDescription{ |
| 60 | + SdpType: int32(sessionDescription.Type), |
| 61 | + Sdp: sessionDescription.SDP, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }) |
| 65 | + if err != nil { |
| 66 | + _ = peerConn.CloseWithError(xerrors.Errorf("send local session description: %w", err)) |
| 67 | + return |
| 68 | + } |
| 69 | + case iceCandidate := <-peerConn.LocalCandidate(): |
| 70 | + err = stream.Send(&proto.NegotiateConnection_ClientToServer{ |
| 71 | + Message: &proto.NegotiateConnection_ClientToServer_IceCandidate{ |
| 72 | + IceCandidate: iceCandidate.Candidate, |
| 73 | + }, |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + _ = peerConn.CloseWithError(xerrors.Errorf("send local candidate: %w", err)) |
| 77 | + return |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + }() |
| 82 | + go func() { |
| 83 | + // Exchanging messages from the server to negotiate a connection. |
| 84 | + for { |
| 85 | + serverToClientMessage, err := stream.Recv() |
| 86 | + if err != nil { |
| 87 | + _ = peerConn.CloseWithError(err) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + switch { |
| 92 | + case serverToClientMessage.GetAnswer() != nil: |
| 93 | + peerConn.SetRemoteSessionDescription(webrtc.SessionDescription{ |
| 94 | + Type: webrtc.SDPType(serverToClientMessage.GetAnswer().SdpType), |
| 95 | + SDP: serverToClientMessage.GetAnswer().Sdp, |
| 96 | + }) |
| 97 | + case serverToClientMessage.GetIceCandidate() != "": |
| 98 | + err = peerConn.AddRemoteCandidate(webrtc.ICECandidateInit{ |
| 99 | + Candidate: serverToClientMessage.GetIceCandidate(), |
| 100 | + }) |
| 101 | + if err != nil { |
| 102 | + _ = peerConn.CloseWithError(xerrors.Errorf("add remote candidate: %w", err)) |
| 103 | + return |
| 104 | + } |
| 105 | + default: |
| 106 | + _ = peerConn.CloseWithError(xerrors.Errorf("unhandled message: %s", reflect.TypeOf(serverToClientMessage).String())) |
| 107 | + return |
| 108 | + } |
| 109 | + } |
| 110 | + }() |
| 111 | + |
| 112 | + return peerConn, nil |
| 113 | +} |
0 commit comments