commit ff7cc926f76913cdf1e061b78282499b0553bd25 Author: David Fifield david@bamsoftware.com Date: Mon Jan 18 21:24:18 2016 -0800
Add an HTTP signaling receiver in the server.
This is a stand-in for some kind of faciliator that is separate from the server transport plugin. --- server/http.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ server/snowflake.go | 15 ++++++++++++ server/torrc | 2 +- 3 files changed, 83 insertions(+), 1 deletion(-)
diff --git a/server/http.go b/server/http.go new file mode 100644 index 0000000..99e03d0 --- /dev/null +++ b/server/http.go @@ -0,0 +1,67 @@ +// An HTTP-based signaling channel for the WebRTC server. It imitates the +// facilitator as seen by clients, but it doesn't connect them to an +// intermediate WebRTC proxy, rather connects them directly to this WebRTC +// server. This code should be deleted when we have proxies in place. + +package main + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + + "github.com/keroserene/go-webrtc" +) + +type httpHandler struct { + config *webrtc.Configuration +} + +func (h *httpHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { + switch req.Method { + case "GET": + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.WriteHeader(http.StatusOK) + w.Write([]byte(`HTTP signaling channel + +Send a POST request containing an SDP offer. The response will +contain an SDP answer. +`)) + return + case "POST": + break + default: + http.Error(w, "Bad request.", http.StatusBadRequest) + return + } + + // POST handling begins here. + body, err := ioutil.ReadAll(http.MaxBytesReader(w, req.Body, 100000)) + if err != nil { + http.Error(w, "Bad request.", http.StatusBadRequest) + return + } + offer := webrtc.DeserializeSessionDescription(string(body)) + if offer == nil { + http.Error(w, "Bad request.", http.StatusBadRequest) + return + } + + pc, err := makePeerConnectionFromOffer(offer, h.config) + if err != nil { + http.Error(w, fmt.Sprintf("Cannot create offer: %s", err), http.StatusInternalServerError) + return + } + + log.Println("answering HTTP POST") + + w.WriteHeader(http.StatusOK) + w.Write([]byte(pc.LocalDescription().Serialize())) +} + +func receiveSignalsHTTP(addr string, config *webrtc.Configuration) error { + http.Handle("/", &httpHandler{config}) + log.Printf("listening HTTP on %s", addr) + return http.ListenAndServe(addr, nil) +} diff --git a/server/snowflake.go b/server/snowflake.go index 88a07d8..88eecca 100644 --- a/server/snowflake.go +++ b/server/snowflake.go @@ -2,6 +2,7 @@ package main
import ( "bufio" + "flag" "fmt" "io" "log" @@ -214,6 +215,10 @@ func receiveSignalsFIFO(filename string, config *webrtc.Configuration) error {
func main() { var err error + var httpAddr string + + flag.StringVar(&httpAddr, "http", "", "listen for HTTP signaling") + flag.Parse()
logFile, err = os.OpenFile("snowflake.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) if err != nil { @@ -240,6 +245,16 @@ func main() { } }()
+ // Start HTTP-based signaling receiver. + if httpAddr != "" { + go func() { + err := receiveSignalsHTTP(httpAddr, webRTCConfig) + if err != nil { + log.Printf("receiveSignalsHTTP: %s", err) + } + }() + } + for _, bindaddr := range ptInfo.Bindaddrs { switch bindaddr.MethodName { case ptMethodName: diff --git a/server/torrc b/server/torrc index 44b5964..db54242 100644 --- a/server/torrc +++ b/server/torrc @@ -5,4 +5,4 @@ SocksPort 0 ExitPolicy reject *:* DataDirectory datadir
-ServerTransportPlugin snowflake exec ./server +ServerTransportPlugin snowflake exec ./server -http 127.0.0.1:8080