(logger slog.Logger, inv *serpent.Invocation, cfg *codersdk.DeploymentValues)
| 2780 | } |
| 2781 | |
| 2782 | func ConfigureHTTPServers(logger slog.Logger, inv *serpent.Invocation, cfg *codersdk.DeploymentValues) (_ *HTTPServers, err error) { |
| 2783 | ctx := inv.Context() |
| 2784 | httpServers := &HTTPServers{} |
| 2785 | defer func() { |
| 2786 | if err != nil { |
| 2787 | // Always close the listeners if we fail. |
| 2788 | httpServers.Close() |
| 2789 | } |
| 2790 | }() |
| 2791 | // Validate bind addresses. |
| 2792 | if cfg.Address.String() != "" { |
| 2793 | if cfg.TLS.Enable { |
| 2794 | cfg.HTTPAddress = "" |
| 2795 | cfg.TLS.Address = cfg.Address |
| 2796 | } else { |
| 2797 | _ = cfg.HTTPAddress.Set(cfg.Address.String()) |
| 2798 | cfg.TLS.Address.Host = "" |
| 2799 | cfg.TLS.Address.Port = "" |
| 2800 | } |
| 2801 | } |
| 2802 | if cfg.TLS.Enable && cfg.TLS.Address.String() == "" { |
| 2803 | return nil, xerrors.Errorf("TLS address must be set if TLS is enabled") |
| 2804 | } |
| 2805 | if !cfg.TLS.Enable && cfg.HTTPAddress.String() == "" { |
| 2806 | return nil, xerrors.Errorf("TLS is disabled. Enable with --tls-enable or specify a HTTP address") |
| 2807 | } |
| 2808 | |
| 2809 | if cfg.AccessURL.String() != "" && |
| 2810 | !(cfg.AccessURL.Scheme == "http" || cfg.AccessURL.Scheme == "https") { |
| 2811 | return nil, xerrors.Errorf("access-url must include a scheme (e.g. 'http://' or 'https://)") |
| 2812 | } |
| 2813 | |
| 2814 | addrString := func(l net.Listener) string { |
| 2815 | listenAddrStr := l.Addr().String() |
| 2816 | // For some reason if 0.0.0.0:x is provided as the https |
| 2817 | // address, httpsListener.Addr().String() likes to return it as |
| 2818 | // an ipv6 address (i.e. [::]:x). If the input ip is 0.0.0.0, |
| 2819 | // try to coerce the output back to ipv4 to make it less |
| 2820 | // confusing. |
| 2821 | if strings.Contains(cfg.HTTPAddress.String(), "0.0.0.0") { |
| 2822 | listenAddrStr = strings.ReplaceAll(listenAddrStr, "[::]", "0.0.0.0") |
| 2823 | } |
| 2824 | return listenAddrStr |
| 2825 | } |
| 2826 | |
| 2827 | if cfg.HTTPAddress.String() != "" { |
| 2828 | httpServers.HTTPListener, err = net.Listen("tcp", cfg.HTTPAddress.String()) |
| 2829 | if err != nil { |
| 2830 | return nil, err |
| 2831 | } |
| 2832 | |
| 2833 | // We want to print out the address the user supplied, not the |
| 2834 | // loopback device. |
| 2835 | _, _ = fmt.Fprintf(inv.Stdout, "Started HTTP listener at %s\n", (&url.URL{Scheme: "http", Host: addrString(httpServers.HTTPListener)}).String()) |
| 2836 | |
| 2837 | // Set the http URL we want to use when connecting to ourselves. |
| 2838 | tcpAddr, tcpAddrValid := httpServers.HTTPListener.Addr().(*net.TCPAddr) |
| 2839 | if !tcpAddrValid { |
no test coverage detected