Turn call an OK

master
Bel LaPointe 2020-05-13 14:44:04 -06:00
parent e1ee647767
commit 5418ef2b37
2 changed files with 35 additions and 39 deletions

View File

@ -1,20 +1,4 @@
class Config { 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) { static getCookie(cname) {
var name = cname + "="; var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie); var decodedCookie = decodeURIComponent(document.cookie);
@ -30,14 +14,14 @@ class Config {
} }
return ""; return "";
} }
static setCookie(cname, cvalue) { static setCookie(cname, cvalue) {
var d = new Date(); var d = new Date();
d.setTime(d.getTime() + (1*24*60*60*1000)); d.setTime(d.getTime() + (1*24*60*60*1000));
var expires = "expires="+ d.toUTCString(); var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
} }
static getUUID() { static getUUID() {
var uuid = Config.getCookie('uuid'); var uuid = Config.getCookie('uuid');
if (!uuid) { if (!uuid) {
@ -46,22 +30,33 @@ class Config {
} }
return uuid; return uuid;
} }
static createUUID() { static createUUID() {
return "" + new Date(); return "" + new Date();
function s4() { function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
} }
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); 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 { class View {
static write(foo, color) { static write(foo, color) {
var msg = [].slice.call(arguments[2]).join(" "); var msg = [].slice.call(arguments[2]).join(" ");
foo(msg); foo(msg);
document.getElementById("log").innerHTML += document.getElementById("log").innerHTML +=
'<br><span style="color: ' + color + ';">' + msg + '</span>'; '<br><span style="color: ' + color + ';">' + msg + '</span>';
} }
@ -101,7 +96,7 @@ class Preview {
} }
class Server { class Server {
static server; server = null;
constructor(address, cb) { constructor(address, cb) {
Server.server = new WebSocket(address); Server.server = new WebSocket(address);
@ -110,8 +105,6 @@ class Server {
} }
class Peer { class Peer {
peer;
constructor(stream) { constructor(stream) {
this.peer = new RTCPeerConnection(Config.iceConfig); this.peer = new RTCPeerConnection(Config.iceConfig);
this.peer.onicecandidate = this.gotIceCandidate; this.peer.onicecandidate = this.gotIceCandidate;
@ -135,30 +128,28 @@ class Peer {
.catch(View.error); .catch(View.error);
} }
} }
Peer.peer = null;
class Entropy { class Entropy {
static local = null;
static peer = null;
static pageReady() { static pageReady() {
Entropy.local = Local.build("video"); Entropy.local = Local.build("video");
} }
static start(isCaller) { static start(isCaller) {
Entropy.peer = new Peer(Entropy.local.stream); Entropy.peer = new Peer(Entropy.local.stream);
if(isCaller) { if(isCaller) {
Entropy.peer.offer(Entropy.createdDescription); Entropy.peer.offer(Entropy.createdDescription);
} }
} }
static gotMessageFromServer(message) { 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); var signal = JSON.parse(message.data);
// Ignore messages from ourself // Ignore messages from ourself
if(signal.uuid == Config.getUUID()) return; if(signal.uuid == Config.getUUID()) return;
if(signal.sdp) { if(signal.sdp) {
Entropy.peer.peer.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() { Entropy.peer.peer.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
// Only create answers in response to offers // Only create answers in response to offers
@ -170,21 +161,23 @@ class Entropy {
Entropy.peer.peer.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(View.error); Entropy.peer.peer.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(View.error);
} }
} }
static createdDescription(description) { static createdDescription(description) {
View.log('got description'); View.log('got description');
Entropy.peer.peer Entropy.peer.peer
.setLocalDescription(description) .setLocalDescription(description)
.then(function() { .then(function() {
Server.server.send(JSON.stringify({ Server.server.send(JSON.stringify({
'sdp': Entropy.peer.peer.localDescription, 'sdp': Entropy.peer.peer.localDescription,
'uuid': Config.getUUID(), 'uuid': Config.getUUID(),
})); }));
}) })
.catch(View.error); .catch(View.error);
} }
} }
Entropy.local = null;
Entropy.peer = null;
class Local { class Local {
stream = null; stream = null;
@ -250,7 +243,11 @@ window.console = console;
function pageReady() { function pageReady() {
Config.getUUID(); 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(); Entropy.pageReady();
} }

View File

@ -30,11 +30,10 @@ func New() *Server {
} }
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("base", path.Base(r.URL.Path))
if !s.Authorize(w, r) { if !s.Authorize(w, r) {
return return
} }
log.Println("ext", path.Ext(r.URL.Path))
if path.Ext(r.URL.Path) != "" { if path.Ext(r.URL.Path) != "" {
s.fs.ServeHTTP(w, r) s.fs.ServeHTTP(w, r)
} else if _, err := os.Stat(path.Join(config().GetString("d"), r.URL.Path[1:])); os.IsNotExist(err) { } else if _, err := os.Stat(path.Join(config().GetString("d"), r.URL.Path[1:])); os.IsNotExist(err) {