to run this test, add the following to /etc/hosts: 127.0.0.1 example.com 127.0.0.1 example2.com and then set the environment FABIO_IHAVEHOSTENTRIES=true This also runs in Github Actions by default, since the workflow adds these aliases.
(t *testing.T)
| 25 | // and then set the environment FABIO_IHAVEHOSTENTRIES=true |
| 26 | // This also runs in Github Actions by default, since the workflow adds these aliases. |
| 27 | func TestProxyTCPAndHTTPS(t *testing.T) { |
| 28 | if os.Getenv("TRAVIS") != "true" && |
| 29 | os.Getenv("CI") != "true" && |
| 30 | os.Getenv("FABIO_IHAVEHOSTENTRIES") != "true" { |
| 31 | t.Skip("skipping because env FABIO_IHAVEHOSTENTRIES is not set to true") |
| 32 | } |
| 33 | |
| 34 | tlsCfg1 := tlsServerConfig() |
| 35 | tlsCfg2 := tlsServerConfig2() |
| 36 | tcpServer := httptest.NewUnstartedServer(okHandler) |
| 37 | tcpServer.TLS = tlsCfg2 |
| 38 | tcpServer.StartTLS() |
| 39 | defer tcpServer.Close() |
| 40 | |
| 41 | httpPayload := []byte(`OK HTTP`) |
| 42 | |
| 43 | httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 44 | w.Write(httpPayload) |
| 45 | })) |
| 46 | defer httpServer.Close() |
| 47 | |
| 48 | tpl := `route add srv / %s opts "proto=https" |
| 49 | route add tcproute example2.com/ tcp://%s opts "proto=tcp"` |
| 50 | |
| 51 | table, _ := route.NewTable(bytes.NewBufferString(fmt.Sprintf(tpl, httpServer.URL, tcpServer.Listener.Addr()))) |
| 52 | hp := &HTTPProxy{ |
| 53 | Lookup: func(r *http.Request) *route.Target { |
| 54 | return table.Lookup(r, route.Picker["rr"], route.Matcher["prefix"], globCache, globEnabled) |
| 55 | }, |
| 56 | } |
| 57 | |
| 58 | tp := &tcp.SNIProxy{ |
| 59 | Lookup: func(h string) *route.Target { |
| 60 | return table.LookupHost(h, route.Picker["rr"]) |
| 61 | }, |
| 62 | } |
| 63 | m := func(_ context.Context, h string) bool { |
| 64 | // TODO - matcher needs to move out of main |
| 65 | // so we can test it more easily. Probably |
| 66 | // the other functions too. |
| 67 | t := table.LookupHost(h, route.Picker["rr"]) |
| 68 | if t == nil { |
| 69 | return false |
| 70 | } |
| 71 | // Make sure this is supposed to be a tcp proxy. |
| 72 | // opts proto= overrides scheme if present. |
| 73 | var ( |
| 74 | ok bool |
| 75 | proto string |
| 76 | ) |
| 77 | if proto, ok = t.Opts["proto"]; !ok && t.URL != nil { |
| 78 | proto = t.URL.Scheme |
| 79 | } |
| 80 | return proto == "tcp" |
| 81 | } |
| 82 | |
| 83 | // get an unused port for use for the proxy. the rest of the tests just |
| 84 | // pick a high-numbered port, but this should be safer, if ugly. could |
nothing calls this directly
no test coverage detected