-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Package main implements a TURN server using | ||
// ephemeral credentials. | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
"syscall" | ||
|
||
"github.com/pion/logging" | ||
"github.com/pion/turn/v3" | ||
) | ||
|
||
func main() { | ||
publicIP := flag.String("public-ip", "", "IP Address that TURN can be contacted by.") | ||
port := flag.Int("port", 3478, "Listening port.") | ||
authSecret := flag.String("authSecret", "", "Shared secret for the Long Term Credential Mechanism") | ||
realm := flag.String("realm", "pion.ly", "Realm (defaults to \"pion.ly\")") | ||
flag.Parse() | ||
|
||
if len(*publicIP) == 0 { | ||
log.Fatalf("'public-ip' is required") | ||
} else if len(*authSecret) == 0 { | ||
log.Fatalf("'authSecret' 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 TURN server listener: %s", err) | ||
} | ||
|
||
// NewLongTermAuthHandler takes a pion.LeveledLogger. This allows you to intercept messages | ||
// and process them yourself. | ||
logger := logging.NewDefaultLeveledLoggerForScope("lt-creds", logging.LogLevelTrace, os.Stdout) | ||
|
||
s, 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 found | ||
AuthHandler: turn.LongTermTURNRESTAuthHandler(*authSecret, logger), | ||
// PacketConnConfigs is a list of UDP Listeners and the configuration around them | ||
PacketConnConfigs: []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 | ||
}, | ||
}, | ||
}, | ||
}) | ||
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 = s.Close(); err != nil { | ||
log.Panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters