[pion]写一个简单的turn服务器
生活随笔
收集整理的這篇文章主要介紹了
[pion]写一个简单的turn服务器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
100行不到,運行一個最簡單的TURN服務器。
源碼
package mainimport ("fmt""github.com/pion/turn/v2""log""net""net/http""strconv" )var turnServer *turn.Serverfunc startTurn(publicIP string, listenPort int, realm string, username string, password string) {udpListener, err := net.ListenPacket("udp4", "0.0.0.0:"+strconv.Itoa(listenPort))if err != nil {log.Panicf("Failed to create TURN server listener: %s", err)}usersMap := map[string][]byte{}usersMap[username] = turn.GenerateAuthKey(username, realm, password)turnServer, err = turn.NewServer(turn.ServerConfig{Realm: realm,// Set AuthHandler callback// This is called everytime a user tries to authenticate with the TURN server// Return the key for that user, or false when no user is foundAuthHandler: func(username string, realm string, srcAddr net.Addr) ([]byte, bool) {fmt.Printf("Received connect auth, username=%s, realm=%s\n", username, realm)// framework will check auth keyif key, ok := usersMap[username]; ok {return key, true}return nil, false},// PacketConnConfigs is a list of UDP Listeners and the configuration around themPacketConnConfigs: []turn.PacketConnConfig{{PacketConn: udpListener,RelayAddressGenerator: &turn.RelayAddressGeneratorStatic{RelayAddress: net.ParseIP(publicIP), // Claim that we are listening on IP passed by user (This should be your Public IP)Address: "0.0.0.0", // But actually be listening on every interface},},},})fmt.Printf("turn server public ip=%s, listen port=%d, realm=%s, username=%s, password=%s\n",publicIP, listenPort, realm, username, password)if err != nil {log.Panic(err)} }func main() {var listenAddress stringlistenAddress = "0.0.0.0:8080"http.Handle("/", http.FileServer(http.Dir(".")))fmt.Printf("HTTP server listen on http://%s\n", listenAddress)startTurn("127.0.0.1", 13902, "localhost", "foo", "bar")panic(http.ListenAndServe(listenAddress, nil)) }測試客戶端
參考:
https://blog.csdn.net/pengjian444/article/details/122401335
本文借助了pion/turn實現
總結
以上是生活随笔為你收集整理的[pion]写一个简单的turn服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: log4j的配置文件的位置
- 下一篇: 日志配置文件详解-logback,log