mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
To make room for the WebSocket server we're actually going to use as a primary server. Move server-webrtc docs to server-webrtc/README.md.
67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
// An HTTP-based signaling channel for the WebRTC server. It imitates the
|
|
// broker 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)
|
|
}
|