Skip to content
Snippets Groups Projects
Commit be728984 authored by Jonah Husson's avatar Jonah Husson
Browse files

trace implementation

parent 397aad15
No related branches found
No related tags found
2 merge requests!61Hotfix/fix is online,!39Merge release into master
......@@ -317,7 +317,7 @@ func (c *ProtoComms) ServeWithWeb() {
jww.WARN.Printf("Starting HTTP server!")
c.httpServer = &http.Server{
Handler: httpServer,
Handler: &httpTraceWrapper{ws: httpServer},
}
if err := c.httpServer.Serve(l); err != nil {
......@@ -515,7 +515,7 @@ func (c *ProtoComms) ServeHttps(keyPair tls.Certificate) error {
jww.WARN.Printf("Starting HTTPS server!")
c.httpsServer = &http.Server{
Handler: httpsServer,
Handler: &httpTraceWrapper{ws: httpsServer},
}
if err := c.httpsServer.Serve(tlsLis); err != nil {
......
......@@ -5,6 +5,5 @@ package connect
import "net/http"
func (wc *webConn) addHeaders(header http.Header) http.Header {
header.Add("content-type", "application/grpc-web+proto")
return header
}
......@@ -5,7 +5,6 @@ package connect
import "net/http"
func (wc *webConn) addHeaders(header http.Header) http.Header {
header.Set("Content-Type", "application/grpc-web+proto")
header.Add("js.fetch:mode", "no-cors")
return header
}
package connect
import (
"github.com/improbable-eng/grpc-web/go/grpcweb"
"io"
"net/http"
)
type httpTraceWrapper struct {
ws *grpcweb.WrappedGrpcServer
}
func (htw *httpTraceWrapper) ServeHTTP(respWriter http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodTrace {
// TODO or do we just return a 200? writing the body back is proper http trace protocol but how much do we actually care?
received, err := io.ReadAll(req.Body)
if err != nil {
respWriter.WriteHeader(500)
return
}
_, err = respWriter.Write(received)
if err != nil {
respWriter.WriteHeader(500)
return
}
return
} else {
htw.ws.ServeHTTP(respWriter, req)
}
}
......@@ -3,6 +3,9 @@ package connect
import (
"crypto/tls"
"crypto/x509"
"fmt"
"golang.org/x/net/http2"
"io"
"net/http"
"net/http/httptrace"
"regexp"
......@@ -202,7 +205,7 @@ func (wc *webConn) IsOnline() (time.Duration, bool) {
func (wc *webConn) isOnlineHelper(addr string, pingTimeout time.Duration) (time.Duration, bool) {
start := time.Now()
tr := &http.Transport{
tr := &http2.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
......@@ -211,8 +214,8 @@ func (wc *webConn) isOnlineHelper(addr string, pingTimeout time.Duration) (time.
Transport: tr,
Timeout: pingTimeout,
}
target := "https://" + addr + "/mixmessages.Gateway/RequestTlsCert"
req, err := http.NewRequest("POST", target, nil)
target := "https://" + addr + "/*"
req, err := http.NewRequest(http.MethodTrace, target, nil)
if err != nil {
jww.WARN.Printf("Failed to initiate request: %+v", err)
return time.Since(start), false
......@@ -233,19 +236,31 @@ func (wc *webConn) isOnlineHelper(addr string, pingTimeout time.Duration) (time.
// IMPORTANT - enables better HTTP(S) discovery, because many browsers block CORS by default.
req.Header = wc.addHeaders(req.Header)
jww.TRACE.Printf("(GO request): %+v", req)
//ctx, cancel := context.WithTimeout(req.Context(), 5*time.Second)
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
if _, err = client.Do(req); err != nil {
var resp *http.Response
if resp, err = client.Do(req); err != nil {
jww.TRACE.Printf("(GO error): %s", err.Error())
fmt.Println(err)
if checkErrorExceptions(err) {
fmt.Println(1)
jww.DEBUG.Printf(
"Web connectivity verified for address %s with error %+v",
addr, err)
} else {
fmt.Println(2)
jww.WARN.Printf(
"Failed to verify connectivity for address %s: %+v", addr, err)
//cancel()
return time.Since(start), false
}
}
//cancel()
if resp != nil {
fmt.Println(resp)
respBody, _ := io.ReadAll(resp.Body)
fmt.Println(string(respBody))
}
client.CloseIdleConnections()
return time.Since(start), true
}
......
......@@ -146,6 +146,6 @@ func Test_checkErrorExceptions(t *testing.T) {
//func Test_isOnline_actual(t *testing.T) {
// targetAddr := ".xxnode.io:22840"
// wc := webConn{}
// _, ok := wc.isOnlineHelper(targetAddr, time.Second*10)
// _, ok := wc.isOnlineHelper(targetAddr, 100*time.Millisecond)
// t.Fatal(ok)
//}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment