From 5418ef2b371be282aa91cc0a2923163e6d064d1a Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Wed, 13 May 2020 14:44:04 -0600 Subject: [PATCH] Turn call an OK --- public/webrtc.js | 71 +++++++++++++++++++++++------------------------- server.go | 3 +- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/public/webrtc.js b/public/webrtc.js index 79f81f4..cdbc315 100755 --- a/public/webrtc.js +++ b/public/webrtc.js @@ -1,20 +1,4 @@ class Config { - static iceConfig = { - 'iceServers': [ - {'urls': 'stun:stun.stunprotocol.org:3478'}, - {'urls': 'stun:stun.l.google.com:19302'}, - /* - { - 'urls': 'turn:turn.home.blapointe.com:5349', - 'credentialType': 'password', - 'username': 'user', - 'credential': 'pass', - }, - */ - ], - 'iceTransportPolicy': 'all', // all for p2p, relay for turn - }; - static getCookie(cname) { var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); @@ -30,14 +14,14 @@ class Config { } return ""; } - + static setCookie(cname, cvalue) { var d = new Date(); d.setTime(d.getTime() + (1*24*60*60*1000)); var expires = "expires="+ d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } - + static getUUID() { var uuid = Config.getCookie('uuid'); if (!uuid) { @@ -46,22 +30,33 @@ class Config { } return uuid; } - + static createUUID() { return "" + new Date(); function s4() { return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); } - + return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); } } +Config.iceConfig = { + 'iceServers': [ + { + 'urls': 'turn:turn.home.blapointe.com:5349', + 'credentialType': 'password', + 'username': 'user', + 'credential': 'pass', + }, + ], + 'iceTransportPolicy': 'relay', +}; class View { static write(foo, color) { var msg = [].slice.call(arguments[2]).join(" "); foo(msg); - document.getElementById("log").innerHTML += + document.getElementById("log").innerHTML += '
' + msg + ''; } @@ -101,7 +96,7 @@ class Preview { } class Server { - static server; + server = null; constructor(address, cb) { Server.server = new WebSocket(address); @@ -110,8 +105,6 @@ class Server { } class Peer { - peer; - constructor(stream) { this.peer = new RTCPeerConnection(Config.iceConfig); this.peer.onicecandidate = this.gotIceCandidate; @@ -135,30 +128,28 @@ class Peer { .catch(View.error); } } +Peer.peer = null; class Entropy { - static local = null; - static peer = null; - static pageReady() { Entropy.local = Local.build("video"); } - + static start(isCaller) { Entropy.peer = new Peer(Entropy.local.stream); if(isCaller) { Entropy.peer.offer(Entropy.createdDescription); } } - + static gotMessageFromServer(message) { - if(!Entropy.peer.peer) Entropy.start(false); - + if(!Entropy.peer || !Entropy.peer.peer) Entropy.start(false); + var signal = JSON.parse(message.data); - + // Ignore messages from ourself if(signal.uuid == Config.getUUID()) return; - + if(signal.sdp) { Entropy.peer.peer.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() { // Only create answers in response to offers @@ -170,21 +161,23 @@ class Entropy { Entropy.peer.peer.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(View.error); } } - + static createdDescription(description) { View.log('got description'); - + Entropy.peer.peer .setLocalDescription(description) .then(function() { Server.server.send(JSON.stringify({ - 'sdp': Entropy.peer.peer.localDescription, + 'sdp': Entropy.peer.peer.localDescription, 'uuid': Config.getUUID(), })); }) .catch(View.error); } } +Entropy.local = null; +Entropy.peer = null; class Local { stream = null; @@ -250,7 +243,11 @@ window.console = console; function pageReady() { Config.getUUID(); - new Server('wss://' + window.location.hostname + '/abc', Entropy.gotMessageFromServer); + var host = window.location.hostname; + if (window.location.port) { + host += ":" + window.location.port; + } + new Server('wss://' + host + '/abc', Entropy.gotMessageFromServer); Entropy.pageReady(); } diff --git a/server.go b/server.go index af89d20..f77a252 100644 --- a/server.go +++ b/server.go @@ -30,11 +30,10 @@ func New() *Server { } func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { + log.Println("base", path.Base(r.URL.Path)) if !s.Authorize(w, r) { return } - - log.Println("ext", path.Ext(r.URL.Path)) if path.Ext(r.URL.Path) != "" { s.fs.ServeHTTP(w, r) } else if _, err := os.Stat(path.Join(config().GetString("d"), r.URL.Path[1:])); os.IsNotExist(err) {