Files

57 lines
1.3 KiB
Go

// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
// Package main implements a simple TURN server
package main
import (
"flag"
"log"
"net"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/hanzokms/turn/v4"
)
func main() {
publicIP := flag.String("public-ip", "", "IP Address that STUN can be contacted by.")
port := flag.Int("port", 3478, "Listening port.")
flag.Parse()
if len(*publicIP) == 0 {
log.Fatalf("'public-ip' is required")
}
// Create a UDP listener to pass into pion/turn
// pion/turn itself doesn't allocate any UDP sockets, but lets the user pass them in
// this allows us to add logging, storage or modify inbound/outbound traffic
udpListener, err := net.ListenPacket("udp4", "0.0.0.0:"+strconv.Itoa(*port))
if err != nil {
log.Panicf("Failed to create STUN server listener: %s", err)
}
server, err := turn.NewServer(turn.ServerConfig{
// PacketConnConfigs is a list of UDP Listeners and the configuration around them
PacketConnConfigs: []turn.PacketConnConfig{
{
PacketConn: udpListener,
},
},
})
if err != nil {
log.Panic(err)
}
// Block until user sends SIGINT or SIGTERM
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
if err = server.Close(); err != nil {
log.Panic(err)
}
}