Skip to content

Commit d7b9785

Browse files
authored
feat: turn rest api rfc support; (#98)
1 parent 0c98a15 commit d7b9785

22 files changed

+539
-385
lines changed

.github/workflows/release.yml

+6-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
-
2222
uses: actions/checkout@v4
2323
-
24-
uses: actions/cache@v3
24+
uses: actions/cache@v4
2525
with:
2626
path: |
2727
~/.cargo/bin/
@@ -70,23 +70,23 @@ jobs:
7070
-
7171
name: Upload artifact (Linux)
7272
if: runner.os == 'Linux' && matrix.arch == 'x86_64'
73-
uses: actions/upload-artifact@v3
73+
uses: actions/upload-artifact@v4
7474
with:
7575
name: linux-x86_64
7676
path: |
7777
./target/release/turn-server-linux-x86_64
7878
-
7979
name: Upload artifact (Linux)
8080
if: runner.os == 'Linux' && matrix.arch == 'aarch64'
81-
uses: actions/upload-artifact@v3
81+
uses: actions/upload-artifact@v4
8282
with:
83-
name: linux-x86_64
83+
name: linux-aarch64
8484
path: |
8585
./target/aarch64-unknown-linux-gnu/release/turn-server-linux-aarch64
8686
-
8787
name: Upload artifact (Windows)
8888
if: runner.os == 'Windows' && matrix.arch == 'x86_64'
89-
uses: actions/upload-artifact@v3
89+
uses: actions/upload-artifact@v4
9090
with:
9191
name: windows-x86_64
9292
path: |
@@ -95,12 +95,9 @@ jobs:
9595
needs: build
9696
runs-on: ubuntu-latest
9797
steps:
98-
-
99-
name: Checkout code
100-
uses: actions/checkout@v3
10198
-
10299
name: Download All Artifacts
103-
uses: actions/download-artifact@v4.1.7
100+
uses: actions/download-artifact@v4
104101
with:
105102
path: artifacts
106103
-

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ If you have extensive standard support requirements for turn servers and need mo
4444
* [start the server](./docs/start-the-server.md)
4545
* [configure](./docs/configure.md)
4646
* [rest api](./docs/rest-api.md)
47-
* [web hooks](./docs/web-hooks.md)
47+
* [http hooks](./docs/http-hooks.md)
4848
* [driver](./drivers)
4949

5050
## Features

docs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* [Start the server](start-the-server.md)
66
* [Configure](configure.md)
77
* [REST API](rest-api.md)
8-
* [Web Hooks](web-hooks.md)
8+
* [HTTP Hooks](http-hooks.md)

docs/configure.md

+32-12
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ external = "127.0.0.1:3478"
4545
# environment.
4646
bind = "127.0.0.1:3000"
4747

48-
# web hooks url
48+
# hooks url
4949
#
5050
# This option is used to specify the http address of the hooks service.
5151
#
@@ -62,16 +62,25 @@ bind = "127.0.0.1:3000"
6262
# An enum representing the available verbosity levels of the logger.
6363
level = "info"
6464

65+
[auth]
66+
# Static authentication key value (string) that applies only to the TURN
67+
# REST API.
68+
#
69+
# If set, the turn server will not request external services via the HTTP
70+
# Hooks API to obtain the key.
71+
#
72+
# static_auth_secret = ""
73+
6574
# static user password
6675
#
6776
# This option can be used to specify the
6877
# static identity authentication information used by the turn server for
6978
# verification. Note: this is a high-priority authentication method, turn
7079
# The server will try to use static authentication first, and then use
7180
# external control service authentication.
72-
[auth]
73-
user1 = "test"
74-
user2 = "test"
81+
[auth.static_credentials]
82+
# user1 = "test"
83+
# user2 = "test"
7584
```
7685

7786
## Configuration keys
@@ -80,7 +89,7 @@ user2 = "test"
8089

8190
### `turn.realm`
8291

83-
* Type: strings
92+
* Type: string
8493
* Default: "localhost"
8594

8695
This option describes the realm of the turn service. For the definition of realm, please refer to [RFC](https://datatracker.ietf.org/doc/html/rfc5766#section-3).
@@ -98,23 +107,23 @@ This option describes the interface to which the turn service is bound. A turn s
98107

99108
### `[turn.interfaces.transport]`
100109

101-
* Type: enum of strings
110+
* Type: enum of string
102111

103112
Describes the transport protocol used by the interface. The value can be `udp` or `tcp`, which correspond to udp turn and tcp turn respectively, and choose whether to bind the turn service to a udp socket or a tcp socket.
104113

105114
***
106115

107116
### `[turn.interfaces.bind]`
108117

109-
* Type: strings
118+
* Type: string
110119

111120
The IP address and port number bound to the interface. This is the address to which the internal socket is bound.
112121

113122
***
114123

115124
### `[turn.interfaces.external]`
116125

117-
* Type: strings
126+
* Type: string
118127

119128
bind is used to bind to the address of your local NIC, for example, you have two NICs A and B on your server, the IP address of NIC A is 192.168.1.2, and the address of NIC B is 192.168.1.3, if you bind to NIC A, you should bind to the address of 192.168.1.2, and bind to 0.0.0.0 means that it listens to all of them at the same time.
120129

@@ -126,7 +135,7 @@ As for why bind and external are needed, this is because for the stun protocol,
126135

127136
### `api.bind`
128137

129-
* Type: strings
138+
* Type: string
130139
* Default: "127.0.0.1:3000"
131140

132141
Describes the address to which the turn api server is bound.
@@ -139,7 +148,7 @@ The turn service provides an external REST API. External parties can control the
139148

140149
### `api.hooks`
141150

142-
* Type: strings
151+
* Type: string
143152
* Default: None
144153

145154
Describes the address of external Web Hooks. The default value is empty. The purpose of Web Hooks is to allow the turn service to push to external services when authentication is required and event updates occur.
@@ -152,15 +161,26 @@ The turn service provides an external REST API. External parties can control the
152161

153162
### `log.level`
154163

155-
* Type: enum of strings
164+
* Type: enum of string
156165
* Default: "info"
157166

158167
Describes the log level of the turn service. Possible values ​​are `"error"`, `"warn"`, `"info"`, `"debug"`, `"trace"`.
159168

160169
***
161170

162-
### `auth`
171+
### `auth.static_credentials`
163172

164173
* Type: key values
165174

166175
Describes static authentication information, with username and password as key pair. Static identity authentication is authentication information provided to the turn service in advance. The turn service will first look for this table when it needs to authenticate the turn session. If it cannot find it, it will use Web Hooks for external authentication.
176+
177+
***
178+
179+
### `auth.static_auth_secret`
180+
181+
* Type: string
182+
* Default: None
183+
184+
Static authentication key value (string) that applies only to the TURN REST API.
185+
186+
If set, the turn server will not request external services via the HTTP Hooks API to obtain the key.

docs/http-hooks.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Web Hooks
2+
3+
#### Global Request Headers
4+
5+
* `realm` - <sup>string</sup> - turn server realm
6+
* `rid` - <sup>string</sup> - The runtime ID of the turn server
7+
8+
rid: A new ID is generated each time the server is started. This is a random string. Its main function is to determine whether the turn server has been restarted.
9+
10+
***
11+
12+
### GET - `/password?addr=&name=`
13+
14+
Get the current user's password, which is mainly used to provide authentication for the turn server.
15+
16+
***
17+
18+
### POST - `/events` - Events
19+
20+
binding request:
21+
22+
* `kind` - <sup>string</sup> - "binding"
23+
* `addr` - <sup>string</sup> - The IP address and port number of the UDP or TCP connection used by the client.
24+
25+
allocate request:
26+
27+
* `kind` - <sup>string</sup> - "allocated"
28+
* `name` - <sup>string</sup> - The username used for the turn session.
29+
* `addr` - <sup>string</sup> - The IP address and port number of the UDP or TCP connection used by the client.
30+
* `port` - <sup>uint16</sup> - The port to which the request is assigned.
31+
32+
channel binding request:
33+
34+
* `kind` - <sup>string</sup> - "channel_bind"
35+
* `name` - <sup>string</sup> - The username used for the turn session.
36+
* `addr` - <sup>string</sup> - The IP address and port number of the UDP or TCP connection used by the client.
37+
* `channel` - <sup>uint16</sup> - The channel to which the request is binding.
38+
39+
create permission request:
40+
41+
* `kind` - <sup>string</sup> - "create_permission"
42+
* `name` - <sup>string</sup> - The username used for the turn session.
43+
* `addr` - <sup>string</sup> - The IP address and port number of the UDP or TCP connection used by the client.
44+
* `relay` - <sup>uint16</sup> - The port number of the other side specified when the privilege was created.
45+
46+
refresh request:
47+
48+
* `kind` - <sup>string</sup> - "refresh"
49+
* `name` - <sup>string</sup> - The username used for the turn session.
50+
* `addr` - <sup>string</sup> - The IP address and port number of the UDP or TCP connection used by the client.
51+
* `expiration` - <sup>uint32</sup> - Time to expiration in seconds.
52+
53+
session closed:
54+
55+
* `kind` - <sup>string</sup> - "abort"
56+
* `name` - <sup>string</sup> - The username used for the turn session.
57+
* `addr` - <sup>string</sup> - The IP address and port number of the UDP or TCP connection used by the client.

docs/web-hooks.md

-57
This file was deleted.

stun/src/message.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl<'a, 'b> MessageWriter<'a> {
213213
// long key,
214214
// digest the message buffer,
215215
// create the new MessageIntegrity attribute.
216-
let hmac_output = util::hmac_sha1(auth, vec![self.raw])?.into_bytes();
216+
let hmac_output = util::hmac_sha1(auth, &[self.raw])?.into_bytes();
217217
let property_buf = hmac_output.as_slice();
218218

219219
// write MessageIntegrity attribute.
@@ -325,14 +325,14 @@ impl<'a, 'b> MessageReader<'a, 'b> {
325325

326326
// create multiple submit.
327327
let size_buf = (self.valid_offset + 4).to_be_bytes();
328-
let body = vec![
328+
let body = [
329329
&self.buf[0..2],
330330
&size_buf,
331331
&self.buf[4..self.valid_offset as usize],
332332
];
333333

334334
// digest the message buffer.
335-
let hmac_output = util::hmac_sha1(auth, body)?.into_bytes();
335+
let hmac_output = util::hmac_sha1(auth, &body)?.into_bytes();
336336
let property_buf = hmac_output.as_slice();
337337

338338
// Compare local and original attribute.

stun/src/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ pub fn long_key(username: &str, key: &str, realm: &str) -> [u8; 16] {
7070
/// 0x74, 0xe2, 0x3c, 0x26, 0xc5, 0xb1, 0x03, 0xb2, 0x6d,
7171
/// ];
7272
///
73-
/// let hmac_output = stun::util::hmac_sha1(&key, vec![&buffer])
73+
/// let hmac_output = stun::util::hmac_sha1(&key, &[&buffer])
7474
/// .unwrap()
7575
/// .into_bytes();
7676
/// assert_eq!(hmac_output.as_slice(), &sign);
7777
/// ```
78-
pub fn hmac_sha1(key: &[u8], source: Vec<&[u8]>) -> Result<CtOutput<Hmac<sha1::Sha1>>, StunError> {
78+
pub fn hmac_sha1(key: &[u8], source: &[&[u8]]) -> Result<CtOutput<Hmac<sha1::Sha1>>, StunError> {
7979
match Hmac::<sha1::Sha1>::new_from_slice(key) {
8080
Err(_) => Err(StunError::ShaFailed),
8181
Ok(mut mac) => {

tests/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55
publish = false
66

77
[dependencies]
8+
base64 = "0.22.1"
89
once_cell = "1.18.0"
910
tokio = { version = "1", features = ["full"] }
1011
stun = { path = "../stun", version = "1" }

0 commit comments

Comments
 (0)