From 55e4bef78efc425e5d28c3ac62b0212966098e0c Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Tue, 11 Apr 2023 22:33:11 -0700 Subject: [PATCH] switching strategies for existing turn server --- app/mobile/src/context/useRingContext.hook.js | 7 +----- net/server/internal/sturn/message.go | 24 ++++++++++++++----- net/server/internal/sturn/sturn.go | 1 + net/server/main.go | 6 ----- net/web/src/context/useRingContext.hook.js | 9 +------ 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/app/mobile/src/context/useRingContext.hook.js b/app/mobile/src/context/useRingContext.hook.js index 4f2426d0..c84e58a2 100644 --- a/app/mobile/src/context/useRingContext.hook.js +++ b/app/mobile/src/context/useRingContext.hook.js @@ -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 = { diff --git a/net/server/internal/sturn/message.go b/net/server/internal/sturn/message.go index ba4405c1..25901a71 100644 --- a/net/server/internal/sturn/message.go +++ b/net/server/internal/sturn/message.go @@ -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(); } } diff --git a/net/server/internal/sturn/sturn.go b/net/server/internal/sturn/sturn.go index 6bc03afc..f3366170 100644 --- a/net/server/internal/sturn/sturn.go +++ b/net/server/internal/sturn/sturn.go @@ -25,6 +25,7 @@ type SturnAllocation struct { port int permissions []string conn net.PacketConn + addr net.Addr } type SturnSession struct { diff --git a/net/server/main.go b/net/server/main.go index bc751cf4..e914f69b 100644 --- a/net/server/main.go +++ b/net/server/main.go @@ -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(); } diff --git a/net/web/src/context/useRingContext.hook.js b/net/web/src/context/useRingContext.hook.js index 487c1537..ae3f4f6c 100644 --- a/net/web/src/context/useRingContext.hook.js +++ b/net/web/src/context/useRingContext.hook.js @@ -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'