Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
galene
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
galene
Commits
57e08201
Commit
57e08201
authored
May 31, 2020
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Split out web serving into a separate file.
parent
8ef01d4e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
196 additions
and
185 deletions
+196
-185
sfu.go
sfu.go
+1
-185
webserver.go
webserver.go
+195
-0
No files found.
sfu.go
View file @
57e08201
...
@@ -6,25 +6,14 @@
...
@@ -6,25 +6,14 @@
package
main
package
main
import
(
import
(
"bufio"
"encoding/json"
"errors"
"flag"
"flag"
"fmt"
"html"
"io"
"log"
"log"
"net/http"
"os"
"os"
"os/signal"
"os/signal"
"path/filepath"
"path/filepath"
"runtime"
"runtime"
"runtime/pprof"
"runtime/pprof"
"strings"
"syscall"
"syscall"
"time"
"github.com/gorilla/websocket"
)
)
var
httpAddr
string
var
httpAddr
string
...
@@ -94,183 +83,10 @@ func main() {
...
@@ -94,183 +83,10 @@ func main() {
iceFilename
=
filepath
.
Join
(
staticRoot
,
"ice-servers.json"
)
iceFilename
=
filepath
.
Join
(
staticRoot
,
"ice-servers.json"
)
http
.
Handle
(
"/"
,
mungeHandler
{
http
.
FileServer
(
http
.
Dir
(
staticRoot
))})
http
.
HandleFunc
(
"/group/"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
mungeHeader
(
w
)
http
.
ServeFile
(
w
,
r
,
staticRoot
+
"/sfu.html"
)
})
http
.
HandleFunc
(
"/ws"
,
wsHandler
)
http
.
HandleFunc
(
"/public-groups.json"
,
publicHandler
)
http
.
HandleFunc
(
"/stats"
,
statsHandler
)
go
readPublicGroups
()
go
readPublicGroups
()
webserver
()
go
func
()
{
server
:=
&
http
.
Server
{
Addr
:
httpAddr
,
ReadTimeout
:
60
*
time
.
Second
,
WriteTimeout
:
30
*
time
.
Second
,
IdleTimeout
:
120
*
time
.
Second
,
}
var
err
error
log
.
Printf
(
"Listening on %v"
,
httpAddr
)
err
=
server
.
ListenAndServeTLS
(
filepath
.
Join
(
dataDir
,
"cert.pem"
),
filepath
.
Join
(
dataDir
,
"key.pem"
),
)
log
.
Fatalf
(
"ListenAndServeTLS: %v"
,
err
)
}()
terminate
:=
make
(
chan
os
.
Signal
,
1
)
terminate
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
terminate
,
syscall
.
SIGINT
)
signal
.
Notify
(
terminate
,
syscall
.
SIGINT
)
<-
terminate
<-
terminate
}
}
func
mungeHeader
(
w
http
.
ResponseWriter
)
{
w
.
Header
()
.
Add
(
"Content-Security-Policy"
,
"connect-src ws: wss: 'self'; img-src data: 'self'; default-src 'self'"
)
}
type
mungeHandler
struct
{
h
http
.
Handler
}
func
(
h
mungeHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
mungeHeader
(
w
)
h
.
h
.
ServeHTTP
(
w
,
r
)
}
func
publicHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Header
()
.
Set
(
"content-type"
,
"application/json"
)
w
.
Header
()
.
Set
(
"cache-control"
,
"no-cache"
)
if
r
.
Method
==
"HEAD"
{
return
}
g
:=
getPublicGroups
()
e
:=
json
.
NewEncoder
(
w
)
e
.
Encode
(
g
)
return
}
func
getPassword
()
(
string
,
string
,
error
)
{
f
,
err
:=
os
.
Open
(
filepath
.
Join
(
dataDir
,
"passwd"
))
if
err
!=
nil
{
return
""
,
""
,
err
}
defer
f
.
Close
()
r
:=
bufio
.
NewReader
(
f
)
s
,
err
:=
r
.
ReadString
(
'\n'
)
if
err
!=
nil
{
return
""
,
""
,
err
}
l
:=
strings
.
SplitN
(
strings
.
TrimSpace
(
s
),
":"
,
2
)
if
len
(
l
)
!=
2
{
return
""
,
""
,
errors
.
New
(
"couldn't parse passwords"
)
}
return
l
[
0
],
l
[
1
],
nil
}
func
statsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
bail
:=
func
()
{
w
.
Header
()
.
Set
(
"www-authenticate"
,
"basic realm=
\"
stats
\"
"
)
http
.
Error
(
w
,
"Haha!"
,
http
.
StatusUnauthorized
)
}
u
,
p
,
err
:=
getPassword
()
if
err
!=
nil
{
log
.
Printf
(
"Passwd: %v"
,
err
)
bail
()
return
}
username
,
password
,
ok
:=
r
.
BasicAuth
()
if
!
ok
||
username
!=
u
||
password
!=
p
{
bail
()
return
}
w
.
Header
()
.
Set
(
"content-type"
,
"text/html; charset=utf-8"
)
w
.
Header
()
.
Set
(
"cache-control"
,
"no-cache"
)
if
r
.
Method
==
"HEAD"
{
return
}
stats
:=
getGroupStats
()
fmt
.
Fprintf
(
w
,
"<!DOCTYPE html>
\n
<html><head>
\n
"
)
fmt
.
Fprintf
(
w
,
"<title>Stats</title>
\n
"
)
fmt
.
Fprintf
(
w
,
"<head><body>
\n
"
)
printBitrate
:=
func
(
w
io
.
Writer
,
rate
,
maxRate
uint64
)
error
{
var
err
error
if
maxRate
!=
0
&&
maxRate
!=
^
uint64
(
0
)
{
_
,
err
=
fmt
.
Fprintf
(
w
,
"%v/%v"
,
rate
,
maxRate
)
}
else
{
_
,
err
=
fmt
.
Fprintf
(
w
,
"%v"
,
rate
)
}
return
err
}
printTrack
:=
func
(
w
io
.
Writer
,
t
trackStats
)
{
fmt
.
Fprintf
(
w
,
"<tr><td></td><td></td><td></td>"
)
fmt
.
Fprintf
(
w
,
"<td>"
)
printBitrate
(
w
,
t
.
bitrate
,
t
.
maxBitrate
)
fmt
.
Fprintf
(
w
,
"</td>"
)
fmt
.
Fprintf
(
w
,
"<td>%d%%</td>"
,
t
.
loss
,
)
if
t
.
jitter
>
0
{
fmt
.
Fprintf
(
w
,
"<td>%v</td>"
,
t
.
jitter
)
}
else
{
fmt
.
Fprintf
(
w
,
"<td></td>"
)
}
fmt
.
Fprintf
(
w
,
"</tr>"
)
}
for
_
,
gs
:=
range
stats
{
fmt
.
Fprintf
(
w
,
"<p>%v</p>
\n
"
,
html
.
EscapeString
(
gs
.
name
))
fmt
.
Fprintf
(
w
,
"<table>"
)
for
_
,
cs
:=
range
gs
.
clients
{
fmt
.
Fprintf
(
w
,
"<tr><td>%v</td></tr>
\n
"
,
cs
.
id
)
for
_
,
up
:=
range
cs
.
up
{
fmt
.
Fprintf
(
w
,
"<tr><td></td><td>Up</td><td>%v</td></tr>
\n
"
,
up
.
id
)
for
_
,
t
:=
range
up
.
tracks
{
printTrack
(
w
,
t
)
}
}
for
_
,
up
:=
range
cs
.
down
{
fmt
.
Fprintf
(
w
,
"<tr><td></td><td>Down</td><td> %v</td></tr>
\n
"
,
up
.
id
)
for
_
,
t
:=
range
up
.
tracks
{
printTrack
(
w
,
t
)
}
}
}
fmt
.
Fprintf
(
w
,
"</table>
\n
"
)
}
fmt
.
Fprintf
(
w
,
"</body></html>
\n
"
)
}
var
upgrader
websocket
.
Upgrader
func
wsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
conn
,
err
:=
upgrader
.
Upgrade
(
w
,
r
,
nil
)
if
err
!=
nil
{
log
.
Printf
(
"Websocket upgrade: %v"
,
err
)
return
}
go
func
()
{
err
:=
startClient
(
conn
)
if
err
!=
nil
{
log
.
Printf
(
"client: %v"
,
err
)
}
}()
}
webserver.go
0 → 100644
View file @
57e08201
package
main
import
(
"bufio"
"encoding/json"
"errors"
"fmt"
"html"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/gorilla/websocket"
)
func
webserver
()
{
http
.
Handle
(
"/"
,
mungeHandler
{
http
.
FileServer
(
http
.
Dir
(
staticRoot
))})
http
.
HandleFunc
(
"/group/"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
mungeHeader
(
w
)
http
.
ServeFile
(
w
,
r
,
staticRoot
+
"/sfu.html"
)
})
http
.
HandleFunc
(
"/ws"
,
wsHandler
)
http
.
HandleFunc
(
"/public-groups.json"
,
publicHandler
)
http
.
HandleFunc
(
"/stats"
,
statsHandler
)
go
func
()
{
server
:=
&
http
.
Server
{
Addr
:
httpAddr
,
ReadTimeout
:
60
*
time
.
Second
,
WriteTimeout
:
30
*
time
.
Second
,
IdleTimeout
:
120
*
time
.
Second
,
}
var
err
error
err
=
server
.
ListenAndServeTLS
(
filepath
.
Join
(
dataDir
,
"cert.pem"
),
filepath
.
Join
(
dataDir
,
"key.pem"
),
)
if
err
!=
nil
{
log
.
Printf
(
"ListenAndServeTLS: %v"
,
err
)
}
}()
}
func
mungeHeader
(
w
http
.
ResponseWriter
)
{
w
.
Header
()
.
Add
(
"Content-Security-Policy"
,
"connect-src ws: wss: 'self'; img-src data: 'self'; default-src 'self'"
)
}
type
mungeHandler
struct
{
h
http
.
Handler
}
func
(
h
mungeHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
mungeHeader
(
w
)
h
.
h
.
ServeHTTP
(
w
,
r
)
}
func
publicHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Header
()
.
Set
(
"content-type"
,
"application/json"
)
w
.
Header
()
.
Set
(
"cache-control"
,
"no-cache"
)
if
r
.
Method
==
"HEAD"
{
return
}
g
:=
getPublicGroups
()
e
:=
json
.
NewEncoder
(
w
)
e
.
Encode
(
g
)
return
}
func
getPassword
()
(
string
,
string
,
error
)
{
f
,
err
:=
os
.
Open
(
filepath
.
Join
(
dataDir
,
"passwd"
))
if
err
!=
nil
{
return
""
,
""
,
err
}
defer
f
.
Close
()
r
:=
bufio
.
NewReader
(
f
)
s
,
err
:=
r
.
ReadString
(
'\n'
)
if
err
!=
nil
{
return
""
,
""
,
err
}
l
:=
strings
.
SplitN
(
strings
.
TrimSpace
(
s
),
":"
,
2
)
if
len
(
l
)
!=
2
{
return
""
,
""
,
errors
.
New
(
"couldn't parse passwords"
)
}
return
l
[
0
],
l
[
1
],
nil
}
func
statsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
bail
:=
func
()
{
w
.
Header
()
.
Set
(
"www-authenticate"
,
"basic realm=
\"
stats
\"
"
)
http
.
Error
(
w
,
"Haha!"
,
http
.
StatusUnauthorized
)
}
u
,
p
,
err
:=
getPassword
()
if
err
!=
nil
{
log
.
Printf
(
"Passwd: %v"
,
err
)
bail
()
return
}
username
,
password
,
ok
:=
r
.
BasicAuth
()
if
!
ok
||
username
!=
u
||
password
!=
p
{
bail
()
return
}
w
.
Header
()
.
Set
(
"content-type"
,
"text/html; charset=utf-8"
)
w
.
Header
()
.
Set
(
"cache-control"
,
"no-cache"
)
if
r
.
Method
==
"HEAD"
{
return
}
stats
:=
getGroupStats
()
fmt
.
Fprintf
(
w
,
"<!DOCTYPE html>
\n
<html><head>
\n
"
)
fmt
.
Fprintf
(
w
,
"<title>Stats</title>
\n
"
)
fmt
.
Fprintf
(
w
,
"<head><body>
\n
"
)
printBitrate
:=
func
(
w
io
.
Writer
,
rate
,
maxRate
uint64
)
error
{
var
err
error
if
maxRate
!=
0
&&
maxRate
!=
^
uint64
(
0
)
{
_
,
err
=
fmt
.
Fprintf
(
w
,
"%v/%v"
,
rate
,
maxRate
)
}
else
{
_
,
err
=
fmt
.
Fprintf
(
w
,
"%v"
,
rate
)
}
return
err
}
printTrack
:=
func
(
w
io
.
Writer
,
t
trackStats
)
{
fmt
.
Fprintf
(
w
,
"<tr><td></td><td></td><td></td>"
)
fmt
.
Fprintf
(
w
,
"<td>"
)
printBitrate
(
w
,
t
.
bitrate
,
t
.
maxBitrate
)
fmt
.
Fprintf
(
w
,
"</td>"
)
fmt
.
Fprintf
(
w
,
"<td>%d%%</td>"
,
t
.
loss
,
)
if
t
.
jitter
>
0
{
fmt
.
Fprintf
(
w
,
"<td>%v</td>"
,
t
.
jitter
)
}
else
{
fmt
.
Fprintf
(
w
,
"<td></td>"
)
}
fmt
.
Fprintf
(
w
,
"</tr>"
)
}
for
_
,
gs
:=
range
stats
{
fmt
.
Fprintf
(
w
,
"<p>%v</p>
\n
"
,
html
.
EscapeString
(
gs
.
name
))
fmt
.
Fprintf
(
w
,
"<table>"
)
for
_
,
cs
:=
range
gs
.
clients
{
fmt
.
Fprintf
(
w
,
"<tr><td>%v</td></tr>
\n
"
,
cs
.
id
)
for
_
,
up
:=
range
cs
.
up
{
fmt
.
Fprintf
(
w
,
"<tr><td></td><td>Up</td><td>%v</td></tr>
\n
"
,
up
.
id
)
for
_
,
t
:=
range
up
.
tracks
{
printTrack
(
w
,
t
)
}
}
for
_
,
up
:=
range
cs
.
down
{
fmt
.
Fprintf
(
w
,
"<tr><td></td><td>Down</td><td> %v</td></tr>
\n
"
,
up
.
id
)
for
_
,
t
:=
range
up
.
tracks
{
printTrack
(
w
,
t
)
}
}
}
fmt
.
Fprintf
(
w
,
"</table>
\n
"
)
}
fmt
.
Fprintf
(
w
,
"</body></html>
\n
"
)
}
var
upgrader
websocket
.
Upgrader
func
wsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
conn
,
err
:=
upgrader
.
Upgrade
(
w
,
r
,
nil
)
if
err
!=
nil
{
log
.
Printf
(
"Websocket upgrade: %v"
,
err
)
return
}
go
func
()
{
err
:=
startClient
(
conn
)
if
err
!=
nil
{
log
.
Printf
(
"client: %v"
,
err
)
}
}()
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment