consolidate RequestInfo into MeekChannel with a simplified Negotiate method

This commit is contained in:
Serene Han 2016-01-19 18:06:40 -08:00
parent b4a07c4192
commit 5e9b23de9e
2 changed files with 37 additions and 52 deletions

View file

@ -1,5 +1,5 @@
// Exchange WebRTC SessionDescriptions over a domain-fronted HTTP // Exchange WebRTC SessionDescriptions over a domain-fronted HTTP
// signalling channel. // signaling channel.
package main package main
import ( import (
@ -12,71 +12,60 @@ import (
"github.com/keroserene/go-webrtc" "github.com/keroserene/go-webrtc"
) )
// RequestInfo encapsulates all the configuration used for a requestresponse // Meek Signalling Channel.
// roundtrip, including variables that may come from SOCKS args or from the type MeekChannel struct {
// command line.
type RequestInfo struct {
// What to put in the X-Session-ID header - SessionID string
// The desired potentially filtered URL to request.
URL *url.URL
// The Host header to put in the HTTP request (optional and may be // The Host header to put in the HTTP request (optional and may be
// different from the host name in URL). // different from the host name in URL).
Host string Host string
Method string
trueURL *url.URL
externalUrl string
transport http.Transport // Used to make all requests.
} }
func NewRequestInfo(targetUrl string, front string) *RequestInfo { // Construct a new MeekChannel, where
info := new(RequestInfo) // |broker| is the URL of the facilitating program which assigns proxies
requestUrl, err := url.Parse(targetUrl) // to clients, and
// |front| is URL of the front domain.
func NewMeekChannel(broker string, front string) *MeekChannel {
targetUrl, err := url.Parse(broker)
if nil != err { if nil != err {
return nil return nil
} }
info.URL = requestUrl mc := new(MeekChannel)
info.Host = front mc.Host = front
return info mc.Method = "POST"
}
// Meek Signalling Channel. mc.trueURL = targetUrl
type MeekChannel struct { mc.externalUrl = front + "/reg/test" // TODO: Have a better suffix.
info *RequestInfo
// Used to make all requests.
transport http.Transport
}
func NewMeekChannel(info *RequestInfo) *MeekChannel {
m := new(MeekChannel)
// We make a copy of DefaultTransport because we want the default Dial // We make a copy of DefaultTransport because we want the default Dial
// and TLSHandshakeTimeout settings. But we want to disable the default // and TLSHandshakeTimeout settings. But we want to disable the default
// ProxyFromEnvironment setting. Proxy is overridden below if // ProxyFromEnvironment setting.
// options.ProxyURL is set. mc.transport = *http.DefaultTransport.(*http.Transport)
m.transport = *http.DefaultTransport.(*http.Transport) mc.transport.Proxy = nil
m.transport.Proxy = nil return mc
m.info = info
return m
} }
// Do an HTTP roundtrip using the payload data in buf. // Roundtrip HTTP POST using WebRTC SessionDescriptions.
func (mc *MeekChannel) roundTripHTTP(buf []byte) (*http.Response, error) { //
// Compose an innocent looking request. // Sends an SDP offer to the meek broker, which assigns a proxy and responds
req, err := http.NewRequest("POST", mc.info.Host+"/reg/123", bytes.NewReader(buf)) // with an SDP answer from a designated remote WebRTC peer.
func (mc *MeekChannel) Negotiate(offer *webrtc.SessionDescription) (
*webrtc.SessionDescription, error) {
data := bytes.NewReader([]byte(offer.Serialize()))
request, err := http.NewRequest(mc.Method, mc.externalUrl, data)
if nil != err { if nil != err {
return nil, err return nil, err
} }
// Set actually desired host in the request. request.Host = mc.trueURL.String()
req.Host = mc.info.URL.String() resp, err := mc.transport.RoundTrip(request)
// req.Header.Set("X-Session-Id", m.info.SessionID)
return mc.transport.RoundTrip(req)
}
// Send an SDP offer to the meek facilitator, and wait for an SDP answer from
// the assigned proxy in the response.
func (mc *MeekChannel) Negotiate(offer *webrtc.SessionDescription) (
*webrtc.SessionDescription, error) {
resp, err := mc.roundTripHTTP([]byte(offer.Serialize()))
if nil != err { if nil != err {
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
log.Println("MeekChannel Response: ", resp) log.Println("MeekChannel Response: ", resp)
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if nil != err { if nil != err {
return nil, err return nil, err
@ -85,7 +74,6 @@ func (mc *MeekChannel) Negotiate(offer *webrtc.SessionDescription) (
return answer, nil return answer, nil
} }
// Simple interim non-fronting HTTP POST negotiation, to be removed when more // Simple interim non-fronting HTTP POST negotiation, to be removed when more
// general fronting is present. // general fronting is present.
func sendOfferHTTP(url string, offer *webrtc.SessionDescription) (*webrtc.SessionDescription, error) { func sendOfferHTTP(url string, offer *webrtc.SessionDescription) (*webrtc.SessionDescription, error) {

View file

@ -255,12 +255,9 @@ func handler(conn *pt.SocksConn) error {
}() }()
defer conn.Close() defer conn.Close()
// Prepare meek signalling channel.
info := NewRequestInfo(BROKER_URL, FRONT_URL)
meek := NewMeekChannel(info)
config := webrtc.NewConfiguration( config := webrtc.NewConfiguration(
webrtc.OptionIceServer("stun:stun.l.google.com:19302")) webrtc.OptionIceServer("stun:stun.l.google.com:19302"))
meek := NewMeekChannel(BROKER_URL, FRONT_URL)
remote, err := dialWebRTC(config, meek) remote, err := dialWebRTC(config, meek)
if err != nil { if err != nil {
conn.Reject() conn.Reject()