mycrl / turn-rs

A pure rust implemented turn server.
GNU Lesser General Public License v2.1
302 stars 22 forks source link

How to use it with webrtc-rs/webrtc #79

Closed timzaak closed 11 months ago

timzaak commented 1 year ago

There is an simple example of webrtc-rs using turn-rs, but it does not work.

两者无法链接,能否帮忙看下问题在哪里? (已解决)

webrtc code: https://github.com/ForNetCode/fornet/blob/relay/relay/src/main.rs turn_server.yaml: https://github.com/ForNetCode/fornet/blob/relay/relay/tun_server_config.toml

webrtc log:

2023-10-22T13:13:38.605501Z INFO webrtc_ice::mdns: mDNS is using 0.0.0.0:5353 as dest_addr
2023-10-22T13:13:38.616239Z INFO webrtc_mdns::conn: Looping and listening Ok(0.0.0.0:5353)
2023-10-22T13:13:38.619656Z INFO relay: Offer: RTCSessionDescription { sdp_type: Offer, sdp: "v=0\r\no=- 7242921887174724487 605323500 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=fingerprint:sha-256 44:0F:BD:67:9D:1F:77:73:22:1F:ED:84:A6:4E:5E:AD:41:8A:20:3C:C0:E3:D3:6C:64:2B:48:19:55:DA:C1:3D\r\na=group:BUNDLE 0\r\nm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\nc=IN IP4 0.0.0.0\r\na=setup:actpass\r\na=mid:0\r\na=sendrecv\r\na=sctp-port:5000\r\na=ice-ufrag:zVpjTvrnIxOHBtQp\r\na=ice-pwd:VOTokqhcXeCQBlTgxjyrQEbwgdgCtaOn\r\n", parsed: Some(SessionDescription { version: 0, origin: Origin { username: "-", session_id: 7242921887174724487, session_version: 605323500, network_type: "IN", address_type: "IP4", unicast_address: "0.0.0.0" }, session_name: "-", session_information: None, uri: None, email_address: None, phone_number: None, connection_information: None, bandwidth: [], time_descriptions: [TimeDescription { timing: Timing { start_time: 0, stop_time: 0 }, repeat_times: [] }], time_zones: [], encryption_key: None, attributes: [Attribute { key: "fingerprint", value: Some("sha-256 44:0F:BD:67:9D:1F:77:73:22:1F:ED:84:A6:4E:5E:AD:41:8A:20:3C:C0:E3:D3:6C:64:2B:48:19:55:DA:C1:3D") }, Attribute { key: "group", value: Some("BUNDLE 0") }], media_descriptions: [MediaDescription { media_name: MediaName { media: "application", port: RangedPort { value: 9, range: None }, protos: ["UDP", "DTLS", "SCTP"], formats: ["webrtc-datachannel"] }, media_title: None, connection_information: Some(ConnectionInformation { network_type: "IN", address_type: "IP4", address: Some(Address { address: "0.0.0.0", ttl: None, range: None }) }), bandwidth: [], encryption_key: None, attributes: [Attribute { key: "setup", value: Some("actpass") }, Attribute { key: "mid", value: Some("0") }, Attribute { key: "sendrecv", value: None }, Attribute { key: "sctp-port:5000", value: None }, Attribute { key: "ice-ufrag", value: Some("zVpjTvrnIxOHBtQp") }, Attribute { key: "ice-pwd", value: Some("VOTokqhcXeCQBlTgxjyrQEbwgdgCtaOn") }] }] }) } 2023-10-22T13:13:38.619927Z INFO webrtc::peer_connection: signaling state changed to have-local-offer
2023-10-22T13:13:38.620054Z INFO webrtc::peer_connection: signaling state changed to have-remote-offer
2023-10-22T13:13:38.620301Z INFO webrtc_ice::mdns: mDNS is using 0.0.0.0:5353 as dest_addr 2023-10-22T13:13:38.622968Z INFO webrtc_mdns::conn: Looping and listening Ok(0.0.0.0:5353) 2023-10-22T13:13:38.625754Z INFO webrtc_ice::agent::agent_internal: [controlled]: Setting new connection state: Checking 2023-10-22T13:13:38.625831Z INFO webrtc::peer_connection: signaling state changed to stable 2023-10-22T13:13:38.625963Z INFO webrtc::peer_connection: ICE connection state changed: checking 2023-10-22T13:13:38.625982Z WARN webrtc_ice::agent::agent_internal: [controlled]: pingAllCandidates called with no candidate pairs. Connection is not possible yet. 2023-10-22T13:13:38.703621Z INFO relay: on ice connection: None 2023-10-22T13:13:38.706905Z WARN webrtc_ice::agent::agent_internal: [controlled]: pingAllCandidates called with no candidate pairs. Connection is not possible yet.
2023-10-22T13:13:38.706954Z INFO relay: answer on ice connection: Some(RTCSessionDescription { sdp_type: Offer, sdp: "v=0\r\no=- 7242921887174724487 605323500 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=fingerprint:sha-256 44:0F:BD:67:9D:1F:77:73 :22:1F:ED:84:A6:4E:5E:AD:41:8A:20:3C:C0:E3:D3:6C:64:2B:48:19:55:DA:C1:3D\r\na=group:BUNDLE 0\r\nm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\nc=IN IP4 0.0.0.0\r\na=setup:actpass\r\na=mid:0\r\na=sendrecv\r\na=sctp-port:5000\r\na=ice-u frag:zVpjTvrnIxOHBtQp\r\na=ice-pwd:VOTokqhcXeCQBlTgxjyrQEbwgdgCtaOn\r\n", parsed: Some(SessionDescription { version: 0, origin: Origin { username: "-", session_id: 7242921887174724487, session_version: 605323500, network_type: "IN", addres s_type: "IP4", unicast_address: "0.0.0.0" }, session_name: "-", session_information: None, uri: None, email_address: None, phone_number: None, connection_information: None, bandwidth: [], time_descriptions: [TimeDescription { timing: Timin g { start_time: 0, stop_time: 0 }, repeat_times: [] }], time_zones: [], encryption_key: None, attributes: [Attribute { key: "fingerprint", value: Some("sha-256 44:0F:BD:67:9D:1F:77:73:22:1F:ED:84:A6:4E:5E:AD:41:8A:20:3C:C0:E3:D3:6C:64:2B:4 8:19:55:DA:C1:3D") }, Attribute { key: "group", value: Some("BUNDLE 0") }], media_descriptions: [MediaDescription { media_name: MediaName { media: "application", port: RangedPort { value: 9, range: None }, protos: ["UDP", "DTLS", "SCTP"], formats: ["webrtc-datachannel"] }, media_title: None, connection_information: Some(ConnectionInformation { network_type: "IN", address_type: "IP4", address: Some(Address { address: "0.0.0.0", ttl: None, range: None }) }), bandwidth: [], en cryption_key: None, attributes: [Attribute { key: "setup", value: Some("actpass") }, Attribute { key: "mid", value: Some("0") }, Attribute { key: "sendrecv", value: None }, Attribute { key: "sctp-port", value: Some("5000") }, Attribute { key: "ice-ufrag", value: Some("zVpjTvrnIxOHBtQp") }, Attribute { key: "ice-pwd", value: Some("VOTokqhcXeCQBlTgxjyrQEbwgdgCtaOn") }] }] }) }) 2023-10-22T13:13:38.707266Z INFO relay: answer collect Session Description finish 2023-10-22T13:13:38.707455Z INFO webrtc::peer_connection: signaling state changed to stable 2023-10-22T13:13:38.707660Z INFO webrtc_ice::agent::agent_internal: [controlling]: Setting new connection state: Checking 2023-10-22T13:13:38.707750Z INFO webrtc::peer_connection: ICE connection state changed: checking 2023-10-22T13:13:38.920682Z WARN webrtc_ice::agent::agent_internal: [controlled]: pingAllCandidates called with no candidate pairs. Connection is not possible yet.
2023-10-22T13:13:39.125836Z WARN webrtc_ice::agent::agent_internal: [controlled]: pingAllCandidates called with no candidate pairs. Connection is not possible yet.
2023-10-22T13:13:39.343719Z WARN webrtc_ice::agent::agent_internal: [controlled]: pingAllCandidates called with no candidate pairs. Connection is not possible yet.
2023-10-22T13:13:39.558588Z WARN webrtc_ice::agent::agent_internal: [controlled]: pingAllCandidates called with no candidate pairs. Connection is not possible yet.
2023-10-22T13:13:39.760740Z WARN webrtc_ice::agent::agent_internal: [controlled]: pingAllCandidates called with no candidate pairs. Connection is not possible yet.

turn-rs log:

2023-10-22T13:13:38.717Z INFO [turn_server::observer] auth: addr=120.235.172.61:4415, name="offer", pwd=Some("test") 2023-10-22T13:13:38.717Z INFO [turn_server::observer] allocate: addr=120.235.172.61:4415, name="offer", port=55168 2023-10-22T13:13:38.719Z INFO [turn_server::observer] auth: addr=120.235.172.61:4416, name="answer", pwd=Some("test") 2023-10-22T13:13:38.719Z INFO [turn_server::observer] allocate: addr=120.235.172.61:4416, name="answer", port=63488 2023-10-22T13:13:38.760Z INFO [turn_server::observer] create permission: addr=120.235.172.61:4415, name="offer", realy=113.31.103.71:63488 2023-10-22T13:13:38.798Z INFO [turn_server::observer] channel bind: addr=120.235.172.61:4415, name="offer", number=16384

timzaak commented 1 year ago

是否两者 都应该创建 offer 请求, 但这样, 他们怎么进行 offer/answer 应答? 解决了,如果是用 gathering_complete_promise 这种方式进行 ICE candidate 获取和配对, Offer 的 peerConnection 要先调用 gathering_complete_promise, 再轮到 Answer 端 调用 gathering_complete_promise, 之后两者才能通信。

但过一段时间后, webrtc 会报错(连接不会受影响):

ERROR turn::client::relay_conn: fail to refresh permissions: CreatePermission error response (error 403: Forbidden)
2023-10-22T14:00:00.299052Z WARN turn::client::relay_conn: refresh permissions failed

timzaak commented 1 year ago

https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/ 测试: 配置: turn:113.31.103.71:13478 user pass

会报错: The server stun:113.31.103.71:13478 returned an error with code=701: STUN server address is incompatible.

我这边本地测试没出现问题

timzaak commented 1 year ago

api hook turn-rs最好能将密码传递给 auth 服务, 原因: 密码可能是动态的,我们用的 Auth 服务会根据传递过来的密码进行二次签名校验。 看了一圈代码,密码只能是静态

mycrl commented 11 months ago

~api hook turn-rs最好能将密码传递给 auth 服务, 原因:~ ~密码可能是动态的,我们用的 Auth 服务会根据传递过来的密码进行二次签名校验。~ 看了一圈代码,密码只能是静态

turn是没法知道client的密码的,因为client发过来的是has过的数据,turn如何确定密码对不对,就是拿auth api中第三方给的的密码根据规则has之后对比has之后的值,所以你这个需求不行,因为client不会把明文密码发送给server,并且是不可逆的has