Ejemplo n.º 1
0
func (whv *WebHttpView) register() {
	http.HandleFunc("/http/in/replay", func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if r := recover(); r != nil {
				err := util.MakePanicTrace(r)
				whv.Error("Replay failed: %v", err)
				http.Error(w, err, 500)
			}
		}()

		r.ParseForm()
		txnid := r.Form.Get("txnid")
		if txn, ok := whv.idToTxn[txnid]; ok {
			reqBytes, err := base64.StdEncoding.DecodeString(txn.Req.Raw)
			if err != nil {
				panic(err)
			}
			whv.ctl.PlayRequest(txn.ConnCtx.Tunnel, reqBytes)
			w.Write([]byte(http.StatusText(200)))
		} else {
			http.Error(w, http.StatusText(400), 400)
		}
	})

	http.HandleFunc("/http/in", func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if r := recover(); r != nil {
				err := util.MakePanicTrace(r)
				whv.Error("HTTP web view failed: %v", err)
				http.Error(w, err, 500)
			}
		}()

		//		pageTmpl, err := assets.Asset("assets/client/page.html")
		pageTmpl, err := ioutil.ReadFile("assets/client/page.html")
		if err != nil {
			panic(err)
		}

		tmpl := template.Must(template.New("page.html").Delims("{%", "%}").Parse(string(pageTmpl)))

		payloadData := SerializedPayload{
			Txns:    whv.HttpRequests.Slice(),
			UiState: SerializedUiState{Tunnels: whv.ctl.State().GetTunnels()},
		}

		payload, err := json.Marshal(payloadData)
		if err != nil {
			panic(err)
		}

		// write the response
		if err := tmpl.Execute(w, string(payload)); err != nil {
			panic(err)
		}
	})
}
Ejemplo n.º 2
0
func (ctl *Controller) Go(fn func()) {
	go func() {
		defer func() {
			if r := recover(); r != nil {
				err := util.MakePanicTrace(r)
				ctl.Error(err)
				ctl.Shutdown(err)
			}
		}()

		fn()
	}()
}