From 1253fdc844f8b1d313d98a811d670dea2f78caf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D1=80=D0=BE?= =?UTF-8?q?=D1=85=D0=BE=D1=80=D0=BE=D0=B2?= Date: Mon, 22 Oct 2018 22:03:12 +0200 Subject: [PATCH] Add user-agent to outgoing HTTP requests --- src/mtp_config.erl | 14 +++++++++++--- src/mtproto_proxy.app.src | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/mtp_config.erl b/src/mtp_config.erl index 154fb80..646fb2f 100644 --- a/src/mtp_config.erl +++ b/src/mtp_config.erl @@ -124,11 +124,11 @@ update(State, _) -> end. update_key(Tab) -> - {ok, {{_, 200, _}, _, Body}} = httpc:request(?SECRET_URL), + {ok, Body} = http_get(?SECRET_URL), true = ets:insert(Tab, {key, list_to_binary(Body)}). update_config(Tab) -> - {ok, {{_, 200, _}, _, Body}} = httpc:request(?CONFIG_URL), + {ok, Body} = http_get(?CONFIG_URL), Downstreams = parse_config(Body), Range = get_range(Downstreams), update_downstreams(Downstreams, Tab), @@ -174,7 +174,7 @@ update_ip() -> update_ip([Url | Fallbacks]) -> try - {ok, {{_, 200, _}, _, Body}} = httpc:request(Url), + {ok, Body} = http_get(Url), IpStr= string:trim(Body), {ok, _} = inet:parse_ipv4strict_address(IpStr), %assert application:set_env(?APP, external_ip, IpStr) @@ -186,6 +186,14 @@ update_ip([Url | Fallbacks]) -> update_ip([]) -> error(ip_lookup_failed). +http_get(Url) -> + {ok, Vsn} = application:get_key(mtproto_proxy, vsn), + UserAgent = "MTProtoProxy/" ++ Vsn ++ " (+https://github.com/seriyps/mtproto_proxy)", + Headers = [{"User-Agent", UserAgent}], + {ok, {{_, 200, _}, _, Body}} = + httpc:request(get, {Url, Headers}, [{timeout, 3000}], []), + {ok, Body}. + -ifdef(TEST). -include_lib("eunit/include/eunit.hrl"). diff --git a/src/mtproto_proxy.app.src b/src/mtproto_proxy.app.src index 368164e..a3b447c 100644 --- a/src/mtproto_proxy.app.src +++ b/src/mtproto_proxy.app.src @@ -1,6 +1,6 @@ {application, mtproto_proxy, [{description, "An OTP application"}, - {vsn, "0.2.0"}, + {vsn, "0.3.0"}, {registered, []}, {mod, { mtproto_proxy_app, []}}, {applications,