Skip to content

Commit

Permalink
Use https proxy if configured
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed May 4, 2023
1 parent fc4adf7 commit 126a8a3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## Cldr Utils version 2.23.0

This is the changelog for Cldr Utils v2.23.0 released on May 4th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_utils/tags)

**Cldr Utils now requires Elixir 1.11 or later**

### Enhancements

* Adds support for https proxy for `Cldr.Http.get/2`. The proxy can be specified as an option to to `Cldr.Http.get/2`, as a configuration option under the `:ex_cldr[:https_proxy]` key, or from the environment variables `HTTPS_PROXY` or `https_proxy`. Thanks to @d-led for the PR and issue.

## Cldr Utils version 2.22.0

This is the changelog for Cldr Utils v2.22.0 released on March 25th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_utils/tags)
Expand Down
39 changes: 33 additions & 6 deletions lib/cldr/http/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ defmodule Cldr.Http do
This option may also be set with the
`CLDR_HTTP_CONNECTION_TIMEOUT` environment variable.
* `:https_proxy` is the URL of an https proxy to be used. The
default is `nil`.
### Returns
* `{:ok, body, headers}` if the return is successful.
Expand All @@ -189,6 +192,17 @@ defmodule Cldr.Http do
unidentified reasons. Please [open an issue](https://github.com/elixir-cldr/cldr/issues)
if this occurs.
### Https Proxy
`Cldr.Http.get/2` will look for a proxy URL in the following
locales in the order presented:
* `options[:https_proxy]
* `ex_cldr` compile-time configuration under the
key `ex_cldr` -> `:https_proxy`
* The environment variable `HTTPS_PROXY`
* The environment variable `https_proxy`
### Certificate stores
In order to keep dependencies to a minimum,
Expand Down Expand Up @@ -250,6 +264,12 @@ defmodule Cldr.Http do
hostname = String.to_charlist(URI.parse(url).host)
url = String.to_charlist(url)
http_options = http_opts(hostname, options)
https_proxy = https_proxy(options)

This comment has been minimized.

Copy link
@d-led

d-led May 4, 2023

this line seems to be problematic, failing to compile (elixir-cldr/cldr#201 (comment)) when https_proxy is not set set to an empty string.

This comment has been minimized.

Copy link
@d-led

d-led May 4, 2023

iex(1)> URI.parse("bla")
%URI{
  scheme: nil,
  userinfo: nil,
  host: nil,
  port: nil,
  path: "bla",
  query: nil,
  fragment: nil
}

iex(2)> URI.parse(" ")  
%URI{
  scheme: nil,
  userinfo: nil,
  host: nil,
  port: nil,
  path: " ",
  query: nil,
  fragment: nil
}

if https_proxy do
%{host: host, port: port} = URI.parse(https_proxy)
:httpc.set_options([{:https_proxy, {{String.to_charlist(host), port}, []}}])
end

case :httpc.request(:get, {url, headers}, http_options, []) do
{:ok, {{_version, 200, _}, headers, body}} ->
Expand Down Expand Up @@ -337,7 +357,7 @@ defmodule Cldr.Http do
|> Enum.reject(&is_nil/1)

@doc false
def certificate_store do
defp certificate_store do
@certificate_locations
|> Enum.find(&File.exists?/1)
|> raise_if_no_cacertfile!
Expand Down Expand Up @@ -376,7 +396,7 @@ defmodule Cldr.Http do
file
end

def http_opts(hostname, options) do
defp http_opts(hostname, options) do
default_timeout =
"CLDR_HTTP_TIMEOUT"
|> System.get_env(@cldr_default_timeout)
Expand Down Expand Up @@ -424,7 +444,7 @@ defmodule Cldr.Http do
end
end

def preferred_ciphers do
defp preferred_ciphers do
preferred_ciphers =
[
# Cipher suites (TLS 1.3): TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
Expand All @@ -447,21 +467,21 @@ defmodule Cldr.Http do
:ssl.filter_cipher_suites(preferred_ciphers, [])
end

def protocol_versions do
defp protocol_versions do
if otp_version() < 25 do
[:"tlsv1.2"]
else
[:"tlsv1.2", :"tlsv1.3"]
end
end

def preferred_eccs do
defp preferred_eccs do
# TLS curves: X25519, prime256v1, secp384r1
preferred_eccs = [:secp256r1, :secp384r1]
:ssl.eccs() -- (:ssl.eccs() -- preferred_eccs)
end

def secure_ssl? do
defp secure_ssl? do
case System.get_env(@cldr_unsafe_https) do
nil -> true
"FALSE" -> false
Expand All @@ -472,6 +492,13 @@ defmodule Cldr.Http do
end
end

defp https_proxy(options) do
options[:https_proxy] ||
Application.get_env(:ex_cldr, :https_proxy) ||
System.get_env("HTTPS_PROXY") ||
System.get_env("https_proxy")
end

def otp_version do
:erlang.system_info(:otp_release) |> List.to_integer
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Cldr.Utils.MixProject do
use Mix.Project

@version "2.22.0"
@version "2.23.0"
@source_url "https://github.com/elixir-cldr/cldr_utils"

def project do
Expand Down

0 comments on commit 126a8a3

Please sign in to comment.