mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 20:49:16 +00:00
switching strategies for existing turn server
This commit is contained in:
parent
80ce1025dd
commit
55e4bef78e
@ -49,15 +49,10 @@ export function useRingContext() {
|
|||||||
|
|
||||||
const iceServers = [
|
const iceServers = [
|
||||||
{
|
{
|
||||||
urls: 'turn:192.168.13.233:5001?transport=udp',
|
urls: 'turn:44.238.207.157:3478?transport=udp',
|
||||||
username: 'user',
|
username: 'user',
|
||||||
credential: 'pass'
|
credential: 'pass'
|
||||||
},
|
},
|
||||||
{
|
|
||||||
urls: 'turn:98.234.232.221:5001?transport=udp',
|
|
||||||
username: 'user',
|
|
||||||
credential: 'pass'
|
|
||||||
}
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const constraints = {
|
const constraints = {
|
||||||
|
@ -223,6 +223,7 @@ func (s *Sturn) handleSendIndication(msg *SturnMessage, addr net.Addr, buf []byt
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set := false
|
||||||
for _, permission := range allocation.permissions {
|
for _, permission := range allocation.permissions {
|
||||||
if permission == peer.strValue {
|
if permission == peer.strValue {
|
||||||
address := fmt.Sprintf("%s:%d", peer.strValue, peer.intValue)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set = true
|
||||||
|
fmt.Println("stun/turn data", dst.String());
|
||||||
_, err = allocation.conn.WriteTo(data.binValue, dst)
|
_, err = allocation.conn.WriteTo(data.binValue, dst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("write error");
|
fmt.Println("write error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !set {
|
||||||
|
fmt.Println("dropped indication");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Sturn) handleBindingRequest(msg *SturnMessage, addr net.Addr) {
|
func (s *Sturn) handleBindingRequest(msg *SturnMessage, addr net.Addr) {
|
||||||
@ -284,11 +290,13 @@ func (s *Sturn) handleRefreshRequest(msg *SturnMessage, addr net.Addr) {
|
|||||||
return
|
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 := &SturnAllocation{}
|
||||||
allocation.port = port
|
allocation.port = port
|
||||||
allocation.conn = conn
|
allocation.conn = conn
|
||||||
allocation.source = source
|
allocation.source = source
|
||||||
|
allocation.addr = addr
|
||||||
allocation.transaction = make([]byte, len(transaction))
|
allocation.transaction = make([]byte, len(transaction))
|
||||||
copy(allocation.transaction, transaction)
|
copy(allocation.transaction, transaction)
|
||||||
allocation.response = make([]byte, len(response))
|
allocation.response = make([]byte, len(response))
|
||||||
@ -396,8 +404,9 @@ func (s *Sturn) handleAllocateRequest(msg *SturnMessage, addr net.Addr) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to write stun response")
|
fmt.Printf("failed to write stun response")
|
||||||
} else {
|
} 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)
|
(*s.conn).WriteTo(s.buf[:n], addr)
|
||||||
|
fmt.Println("allocated: ", addr.String())
|
||||||
go s.relay(allocation);
|
go s.relay(allocation);
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -423,12 +432,11 @@ func (s *Sturn) relay(allocation *SturnAllocation) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("stun/turn relay");
|
|
||||||
|
|
||||||
s.sync.Lock();
|
s.sync.Lock();
|
||||||
split := strings.Split(addr.String(), ":")
|
split := strings.Split(addr.String(), ":")
|
||||||
ip := split[0]
|
ip := split[0]
|
||||||
port, _ := strconv.Atoi(split[1]);
|
port, _ := strconv.Atoi(split[1]);
|
||||||
|
set := false
|
||||||
for _, permission := range allocation.permissions {
|
for _, permission := range allocation.permissions {
|
||||||
if permission == ip {
|
if permission == ip {
|
||||||
|
|
||||||
@ -451,17 +459,21 @@ func (s *Sturn) relay(allocation *SturnAllocation) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
err, l := writeMessage(relay, buf)
|
err, l := writeMessage(relay, buf)
|
||||||
dst, err := net.ResolveUDPAddr("udp", allocation.source)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("no resolve");
|
fmt.Println("no resolve");
|
||||||
} else {
|
} 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 {
|
if err != nil {
|
||||||
fmt.Println("writeto failed");
|
fmt.Println("writeto failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !set {
|
||||||
|
fmt.Println("dropped relay");
|
||||||
|
}
|
||||||
s.sync.Unlock();
|
s.sync.Unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ type SturnAllocation struct {
|
|||||||
port int
|
port int
|
||||||
permissions []string
|
permissions []string
|
||||||
conn net.PacketConn
|
conn net.PacketConn
|
||||||
|
addr net.Addr
|
||||||
}
|
}
|
||||||
|
|
||||||
type SturnSession struct {
|
type SturnSession struct {
|
||||||
|
@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
app "databag/internal"
|
app "databag/internal"
|
||||||
"databag/internal/store"
|
"databag/internal/store"
|
||||||
"databag/internal/sturn"
|
|
||||||
"github.com/gorilla/handlers"
|
"github.com/gorilla/handlers"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -19,9 +18,6 @@ func main() {
|
|||||||
origins := handlers.AllowedOrigins([]string{"*"})
|
origins := handlers.AllowedOrigins([]string{"*"})
|
||||||
methods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"})
|
methods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"})
|
||||||
|
|
||||||
sturn.Listen(5001, 5002, 99)
|
|
||||||
sturn.TestSession()
|
|
||||||
|
|
||||||
args := os.Args
|
args := os.Args
|
||||||
if len(args) == 3 {
|
if len(args) == 3 {
|
||||||
port := ":" + args[2]
|
port := ":" + args[2]
|
||||||
@ -36,6 +32,4 @@ func main() {
|
|||||||
log.Printf("starting server");
|
log.Printf("starting server");
|
||||||
log.Fatal(http.ListenAndServe(":7000", handlers.CORS(origins, methods)(router)))
|
log.Fatal(http.ListenAndServe(":7000", handlers.CORS(origins, methods)(router)))
|
||||||
}
|
}
|
||||||
|
|
||||||
sturn.Close();
|
|
||||||
}
|
}
|
||||||
|
@ -39,14 +39,7 @@ export function useRingContext() {
|
|||||||
const iceServers = [
|
const iceServers = [
|
||||||
{
|
{
|
||||||
//urls: 'turn:98.234.232.221:5001?transport=udp',
|
//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'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
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',
|
//urls: 'turn:35.165.123.117:5001?transport=udp',
|
||||||
username: 'user',
|
username: 'user',
|
||||||
credential: 'pass'
|
credential: 'pass'
|
||||||
|
Loading…
Reference in New Issue
Block a user