Pat*_*Pat 7 sha1 hmac webrtc coturn
我正在玩这个,能够使用 Node/Javascript 让它立即工作,使用 Golang 花了一些时间(这只是生成要发送到 coturn 的用户/密码。)注意秘密应该与 coturn 配置和 API 中的匹配JS/Go 方面。
coturn上的配置:/etc/turnserver.conf
listening-port=443
tls-listening-port=443
listening-ip=10.100.0.2
relay-ip=10.100.0.2
external-ip=123.456.78.9
min-port=10000
max-port=20000
verbose
fingerprint
lt-cred-mech
server-name=myserver
realm=myserver
cert=/etc/SSL/fullchain.pem
pkey=/etc/SSL/privkey.pem
log-file=/var/log/turnserver.log
use-auth-secret
static-auth-secret=MySecret
Run Code Online (Sandbox Code Playgroud)
以下是 Node/Js 实现 API(从其他地方复制 - 有效):
var crypto = require('crypto');
var unixTimeStamp = parseInt(Date.now()/1000) + 24*3600, // this credential valid for 24 hours
TempUser = [unixTimeStamp, "SomeUser"].join(':'),
TempPassword,
hmac = crypto.createHmac('sha1', "MySecret");
hmac.setEncoding('base64');
hmac.write(TempUser);
hmac.end();
TempPassword = hmac.read();
Run Code Online (Sandbox Code Playgroud)
以下是GOLANG实现API(花了点时间):
UserId := "SomeUser"
// This worked, returned the exact seconds
timestamp := strconv.FormatInt(time.Now().UTC().Unix()+24*3600, 10)
// Example: The above is 1602692130
secret := "MySecret"
TempUser := timestamp + ":" + UserId // For API Auth, coturn expects this format, the timestamp is the expiry date of the final temp user/password.
// Create a new HMAC by defining the hash type and the key (as byte array)
//h := hmac.New(sha256.New, []byte(secret)) // sha256 does not work, use sha1
h := hmac.New(sha1.New, []byte(secret))
h.Write([]byte(TempUser))
//sha := b64.URLEncoding.EncodeToString(h.Sum(nil)) // URLEncoding did not work
TempPassword := b64.StdEncoding.EncodeToString(h.Sum(nil)) // StdEncoding worked
Run Code Online (Sandbox Code Playgroud)
Webrtc客户端上的JS。请注意,我们在这里使用 TempUser 和 TempPassword 发送到 coturn。
...
const stunUrl = 'stun:mystun_server',
turnUsername = TempUser,
turnPassword = TempPassword,
...
'iceServers': [
{ 'url': stunUrl },
{
'urls': turnUrl1,
'username': turnUsername,
'credential': turnPassword
},
Run Code Online (Sandbox Code Playgroud)
现在 coturn 将使用上面的 TempUser 和 TempPassword 进行身份验证。希望有人会发现这很有用。谢谢!
归档时间: |
|
查看次数: |
2230 次 |
最近记录: |