switching strategies for existing turn server

This commit is contained in:
Roland Osborne 2023-04-11 22:33:11 -07:00
parent 80ce1025dd
commit 55e4bef78e
5 changed files with 21 additions and 26 deletions

View File

@ -49,15 +49,10 @@ export function useRingContext() {
const iceServers = [
{
urls: 'turn:192.168.13.233:5001?transport=udp',
urls: 'turn:44.238.207.157:3478?transport=udp',
username: 'user',
credential: 'pass'
},
{
urls: 'turn:98.234.232.221:5001?transport=udp',
username: 'user',
credential: 'pass'
}
];
const constraints = {

View File

@ -223,6 +223,7 @@ func (s *Sturn) handleSendIndication(msg *SturnMessage, addr net.Addr, buf []byt
return
}
set := false
for _, permission := range allocation.permissions {
if permission == peer.strValue {
address := fmt.Sprintf("%s:%d", peer.strValue, peer.intValue)
@ -232,12 +233,17 @@ func (s *Sturn) handleSendIndication(msg *SturnMessage, addr net.Addr, buf []byt
return
}
set = true
fmt.Println("stun/turn data", dst.String());
_, err = allocation.conn.WriteTo(data.binValue, dst)
if err != nil {
fmt.Println("write error");
}
}
}
if !set {
fmt.Println("dropped indication");
}
}
func (s *Sturn) handleBindingRequest(msg *SturnMessage, addr net.Addr) {
@ -284,11 +290,13 @@ func (s *Sturn) handleRefreshRequest(msg *SturnMessage, addr net.Addr) {
return
}
func (s *Sturn) setAllocation(source string, transaction []byte, response []byte, port int, conn net.PacketConn, session *SturnSession) (*SturnAllocation) {
func (s *Sturn) setAllocation(addr net.Addr, transaction []byte, response []byte, port int, conn net.PacketConn, session *SturnSession) (*SturnAllocation) {
source := addr.String()
allocation := &SturnAllocation{}
allocation.port = port
allocation.conn = conn
allocation.source = source
allocation.addr = addr
allocation.transaction = make([]byte, len(transaction))
copy(allocation.transaction, transaction)
allocation.response = make([]byte, len(response))
@ -396,8 +404,9 @@ func (s *Sturn) handleAllocateRequest(msg *SturnMessage, addr net.Addr) {
if err != nil {
fmt.Printf("failed to write stun response")
} else {
allocation := s.setAllocation(addr.String(), msg.transaction, s.buf[:n], relayPort, conn, session)
allocation := s.setAllocation(addr, msg.transaction, s.buf[:n], relayPort, conn, session)
(*s.conn).WriteTo(s.buf[:n], addr)
fmt.Println("allocated: ", addr.String())
go s.relay(allocation);
}
return
@ -423,12 +432,11 @@ func (s *Sturn) relay(allocation *SturnAllocation) {
return
}
fmt.Println("stun/turn relay");
s.sync.Lock();
split := strings.Split(addr.String(), ":")
ip := split[0]
port, _ := strconv.Atoi(split[1]);
set := false
for _, permission := range allocation.permissions {
if permission == ip {
@ -451,17 +459,21 @@ func (s *Sturn) relay(allocation *SturnAllocation) {
};
err, l := writeMessage(relay, buf)
dst, err := net.ResolveUDPAddr("udp", allocation.source)
if err != nil {
fmt.Println("no resolve");
} else {
_, err := allocation.conn.WriteTo(buf[:l], dst);
fmt.Println("---- stun/turn relay", allocation.addr.String());
_, err := allocation.conn.WriteTo(buf[:l], allocation.addr);
set = true
if err != nil {
fmt.Println("writeto failed");
}
}
}
}
if !set {
fmt.Println("dropped relay");
}
s.sync.Unlock();
}
}

View File

@ -25,6 +25,7 @@ type SturnAllocation struct {
port int
permissions []string
conn net.PacketConn
addr net.Addr
}
type SturnSession struct {

View File

@ -3,7 +3,6 @@ package main
import (
app "databag/internal"
"databag/internal/store"
"databag/internal/sturn"
"github.com/gorilla/handlers"
"log"
"net/http"
@ -19,9 +18,6 @@ func main() {
origins := handlers.AllowedOrigins([]string{"*"})
methods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"})
sturn.Listen(5001, 5002, 99)
sturn.TestSession()
args := os.Args
if len(args) == 3 {
port := ":" + args[2]
@ -36,6 +32,4 @@ func main() {
log.Printf("starting server");
log.Fatal(http.ListenAndServe(":7000", handlers.CORS(origins, methods)(router)))
}
sturn.Close();
}

View File

@ -39,14 +39,7 @@ export function useRingContext() {
const iceServers = [
{
//urls: 'turn:98.234.232.221:5001?transport=udp',
urls: 'turn:192.168.13.233:5001?transport=udp',
//urls: 'turn:35.165.123.117:5001?transport=udp',
username: 'user',
credential: 'pass'
},
{
urls: 'turn:98.234.232.221:5001?transport=udp',
//urls: 'turn:192.168.13.233:5001?transport=udp',
urls: 'turn:44.238.207.157:3478?transport=udp',
//urls: 'turn:35.165.123.117:5001?transport=udp',
username: 'user',
credential: 'pass'