From ec8a560d83bdd351f3a1412c1b83274d5be223ac Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Wed, 6 May 2020 08:22:35 +0200 Subject: [PATCH 1/8] WIP add apns4erl & prep for iOS13 push reqs --- apps/roster/src/api/push/ios13.erl | 174 +++++++++++++++++++++++ apps/roster/src/api/push/push_api.erl | 31 +++- apps/roster/src/protocol/roster_push.erl | 29 ++-- apps/roster/src/roster.app.src | 2 +- rebar.config | 5 +- rebar.lock | 8 ++ sys.config | 1 + 7 files changed, 231 insertions(+), 19 deletions(-) create mode 100644 apps/roster/src/api/push/ios13.erl diff --git a/apps/roster/src/api/push/ios13.erl b/apps/roster/src/api/push/ios13.erl new file mode 100644 index 000000000..1a2a927ec --- /dev/null +++ b/apps/roster/src/api/push/ios13.erl @@ -0,0 +1,174 @@ +-module(ios13). +-include_lib("kernel/include/logger.hrl"). +-include("roster.hrl"). +-include_lib("roster/include/static/push_notification_var.hrl"). + +-export([start/0]). +-export([description/0, notify/6, test_push_notification/0]). + +description() -> "iOS 13.x Push Notifications Module". + +-define(APNS_CERT_DIR, proplists:get_value(apns_cert_dir, + push_api_opts(), + default_cert_dir())). + +-define(APNS_PORT, proplists:get_value(apns_port, push_api_opts(), 443)). + +-define(GATEWAY_LIST, [ + {<<"SANDBOX">>, "api.sandbox.push.apple.com"}, + {<<"LIVE">>, "api.push.apple.com"}]). + +-define(BANDLE_LIST, [ + {<<"com.nynja.mobile.communicator">>, {"cert_prod.pem", "key_prod.pem"}}, + {<<"com.nynja.rс.mobile.communicator">>, {"cert_prod.pem", "key_prod.pem"}}, + {<<"com.nynja.dev.mobile.communicator">>, {"cert_dev.pem", "key_dev.pem"}} + ]). + +start() -> + dbg:tracer(), + dbg:tpl(?MODULE, x), + dbg:tp(apns, x), + dbg:tp(gun, x), + dbg:tp(gen_tcp,connect,x), + dbg:p(all,[c]), + try start(detect_context()) + after + timer:sleep(1000), + dbg:ctpl('_'), + dbg:ctp('_'), + dbg:stop() + end. + +start(Context) -> + Conn = #{ name => ios13 + , apple_host => get_host(Context) + , apple_port => ?APNS_PORT + , certfile => certfile(Context) + , keyfile => keyfile(Context) + , type => cert }, + apns:connect(Conn). + +detect_context() -> + proplists:get_value(context, push_api_opts(), dev). + +get_host(Context) -> + Key = case Context of + dev -> + <<"SANDBOX">>; + prod -> + <<"LIVE">> + end, + proplists:get_value(Key, ?GATEWAY_LIST). + +push_api_opts() -> + application:get_env(roster, push_api, []). + +certfile(Context) -> + Base = case Context of + dev -> "cert_dev.pem"; + prod -> "cert_prod.pem"; + rc -> "cert_rc.pem" + end, + filename:join(?APNS_CERT_DIR, Base). + +keyfile(Context) -> + Base = case Context of + dev -> "key_dev.pem"; + prod -> "key_prod.pem"; + rc -> "key_rc.pem" + end, + filename:join(?APNS_CERT_DIR, Base). + +default_cert_dir() -> + filename:join(code:priv_dir(roster), "apns_certificates"). + +%% ------------------------------------------------------------------ +%% Ios Push Notifications +%% ------------------------------------------------------------------ + +notify(Alert, Custom, Type, DeviceId, SessionSettings, ConnSettings) + when is_binary(DeviceId) -> + notify(Alert, Custom, Type, binary_to_list(DeviceId), SessionSettings, ConnSettings); +notify(A, C, T, DeviceId, SessionSettings, ConnSettings) -> + [Alert, Custom, Type] = [iolist_to_binary([L]) || L <- [A, C, T]], + + Aps = #{ nynja => #{ model => Custom + , type => Type + , title => Alert + , dns => get_data_from_feature(SessionSettings, ?FKPN_SERVER_DNS) + , version => <> } }, + + %% Use DeviceId or FormattedDeviceId?? + %% FormattedDeviceId = list_to_integer(DeviceId, 16), + send_push(DeviceId, #{aps => Aps}, ConnSettings), + ok. + +%% ------------------------------------------------------------------ +%% Helpers +%% ------------------------------------------------------------------ + +send_push(DeviceId, Msg, Pid) when is_pid(Pid) -> + apns:push_notification(Pid, DeviceId, Msg). + +get_data_from_feature(SessionSettings, Key) -> + case lists:keyfind(Key, #'Feature'.key, SessionSettings) of + #'Feature'{value = Value} -> Value; + _ -> [] + end. + +%% get_bandle(SessionSettings) -> +%% [H|_]=get_from_session(SessionSettings, ?FKPN_BANDLE, ?BANDLE_LIST), +%% H. + +%% get_from_session(SessionSettings, Key, AcceptedValues) -> +%% case get_data_from_feature(SessionSettings, Key) of +%% [] -> +%% AcceptedValues; +%% FoundValue -> +%% Filtered = lists:filter( +%% fun(X) -> +%% element(1,X) == FoundValue +%% end, AcceptedValues), +%% case Filtered of +%% [] -> +%% AcceptedValues; +%% _ -> +%% Filtered +%% end +%% end. + +%% ------------------------------------------------------------------ +%% Tests +%% ------------------------------------------------------------------ + +%% Liubov's phone +-define(APNS_TEST_DEVICE_ID, "f9e7bedd8d46079c51a5aee1f951bbafc68ec541d68a56a0aa709214263cf138"). +%% Anton's phone dev +%% -define(APNS_TEST_DEVICE_ID, "55e9a60ffde1701ba701ea653ba6c0dfa4e515de7d56aa2039d72a904f353e54"). +%% Anton's phone rc +%% -define(APNS_TEST_DEVICE_ID, "a34830e7199ff499e986d2bc3ab0555b0acebe499f9a9f0445701b7dbe2a6722"). + +%% TODO: run tests +test_push_notification() -> + {ok, Pid} = start(dev), + SessionSettings = + [ + #'Feature'{ id = <<"ID_Sandbox">> + , key = <<"APNS_GATEWAY">> + , value = <<"SANDBOX">> + , group = <<"AUTH_DATA">>} + , #'Feature'{ id = <<"ID_Dns">> + , key = <<"SERVER_DNS">> + , value = <<"SomeDNSValue">> + , group = <<"AUTH_DATA">>} + , #'Feature'{ id = <<"ID_Bandle">> + , key = <<"IOS_BANDLE">> + , value = <<"com.nynja.mobile.communicator">> + , group = <<"AUTH_DATA">>} + ], + Msg = lists:concat(["Test it! ", vox_api:generate_random_data(4)]), + Custom = <<"g2gSZAAHTWVzc2FnZWEQZAAFY2hhaW5oA2QAA3AycG0AAAAOMzgw" + "NjM4MDk1MTU4XzdtAAAADjM4MDk5NDM4Mjc5OF84ampqam0AAAAO" + "MzgwOTk0MzgyNzk4XzhtAAAADjM4MDYzODA5NTE1OF83am4GAD5B" + "RlNeAWpqbAAAAAFoBmQABERlc2NqbQAAAARIaGhoYQBqampqamQABHNlbnQ=">>, + notify(Msg, Custom, <<"message">>, ?APNS_TEST_DEVICE_ID, SessionSettings, Pid). diff --git a/apps/roster/src/api/push/push_api.erl b/apps/roster/src/api/push/push_api.erl index 41990451b..a0d3862c9 100644 --- a/apps/roster/src/api/push/push_api.erl +++ b/apps/roster/src/api/push/push_api.erl @@ -1,14 +1,37 @@ -module(push_api). --export([description/0, fcm_notify/3, apns_notify/5]). +-export([start/0]). +-export([description/0, fcm_notify/4, apns_notify/6]). description() -> "Mobile Push Notifications Module. Wrapper for IOS and Android". -compile(export_all). +-include("roster.hrl"). -fcm_notify(MessageTitle, MessageBody, DeviceId) -> +start() -> + {ok, IOS13} = ios13:start(), + {ok, #{ ios13 => IOS13 + , android => [] + , ios => [] }}. + +fcm_notify(MessageTitle, MessageBody, DeviceId, _ConnState) -> android:notify(MessageTitle, MessageBody, DeviceId). -apns_notify(Alert, Custom, Type, DeviceId, SessionSettings) -> - ios:notify(Alert, Custom, Type, DeviceId, SessionSettings). \ No newline at end of file +apns_notify(Alert, Custom, Type, DeviceId, SessionSettings, ConnState) -> + case ios_version(SessionSettings) of + ios -> + ios:notify(Alert, Custom, Type, DeviceId, SessionSettings); + ios13 -> + IOS13St = maps:get(ios13, ConnState), + ios13:notify(Alert, Custom, Type, DeviceId, SessionSettings, IOS13St) + end. + +ios_version(Settings) -> + case [X || #'Feature'{key = <<"OS">>, value = <<"iOS 13", _/binary>>} = X + <- Settings] of + [_|_] -> + ios13; + [] -> + ios + end. diff --git a/apps/roster/src/protocol/roster_push.erl b/apps/roster/src/protocol/roster_push.erl index b6e67162c..2e91db72b 100644 --- a/apps/roster/src/protocol/roster_push.erl +++ b/apps/roster/src/protocol/roster_push.erl @@ -5,32 +5,37 @@ -include_lib("kvs/include/kvs.hrl"). -compile(export_all). -start() -> n2o_async:start(#handler{module = ?MODULE, class = system, group = roster, name = ?MODULE, state = []}). +start() -> + {ok, ConnState} = push_api:start(), + n2o_async:start(#handler{module = ?MODULE, class = system, group = roster, name = ?MODULE, state = #{conn_state => ConnState}}). proc(init, #handler{name = ?MODULE} = Async) -> ?LOG_INFO("ASYNC", []), {ok, Async}; -proc({async_push, Session, Payload, PushAlert, PushType}, #handler{} = H) -> - send_push_notification(Session, Payload, PushAlert, PushType), +proc({async_push, Session, Payload, PushAlert, PushType}, #handler{state = HS} = H) -> + send_push_notification(Session, Payload, PushAlert, PushType, HS), {reply, [], H}. %% TODO prettify variables naming -send_push_notification(#'Auth'{os = OS, push = PushToken, user_id = PhoneId, settings = AuthSettings}, Payload, PushAlert, PushType) -> +send_push_notification(#'Auth'{ os = OS + , push = PushToken + , user_id = PhoneId + , settings = AuthSettings}, Payload, PushAlert, PushType, HS) -> case PushToken of [] -> skip; _ -> ?LOG_INFO("~p:~p:~pPushAlert:~p", [PhoneId, OS, binary:part(PushToken, 0, erlang:min(25, size(PushToken))), PushAlert]), - send_push_notification(OS, PushToken, Payload, PushAlert, PushType, AuthSettings) + send_push_notification(OS, PushToken, Payload, PushAlert, PushType, AuthSettings, HS) end. -send_push_notification(ios, Push, Payload, PushAlert, <<"calling">>, AuthSettings) -> - push_api:apns_notify(PushAlert, Payload, <<"calling">>, Push, AuthSettings); -send_push_notification(ios, Push, Payload, PushAlert, PushType, AuthSettings) -> +send_push_notification(ios, Push, Payload, PushAlert, <<"calling">>, AuthSettings, HS) -> + push_api:apns_notify(PushAlert, Payload, <<"calling">>, Push, AuthSettings, HS); +send_push_notification(ios, Push, Payload, PushAlert, PushType, AuthSettings, HS) -> DecodedPayload = base64:encode(term_to_binary(Payload)), - push_api:apns_notify(PushAlert, DecodedPayload, PushType, Push, AuthSettings); -send_push_notification(android, Push, Payload, PushAlert, PushType, _) -> + push_api:apns_notify(PushAlert, DecodedPayload, PushType, Push, AuthSettings, HS); +send_push_notification(android, Push, Payload, PushAlert, PushType, _AuthSettings, HS) -> PushModel = #push{model = Payload, type = PushType, alert = PushAlert, title = PushAlert, badge = 1}, AndroidPush = http_uri:encode(binary_to_list(base64:encode(term_to_binary(PushModel)))), - push_api:fcm_notify(PushAlert, AndroidPush, Push); -send_push_notification(_, _, _, _, _, _) -> skip. \ No newline at end of file + push_api:fcm_notify(PushAlert, AndroidPush, Push, HS); +send_push_notification(_, _, _, _, _, _, _) -> skip. diff --git a/apps/roster/src/roster.app.src b/apps/roster/src/roster.app.src index 05a7bdae9..bf95b6384 100644 --- a/apps/roster/src/roster.app.src +++ b/apps/roster/src/roster.app.src @@ -5,7 +5,7 @@ {applications, [kernel,stdlib, mnesia, crypto, inets, ssl, ibrowse, cowboy, mochiweb, gen_smtp, kvs, nitro, n2o, emqttc, emqttd, bpe, - jose, jsx, uuid, erlydtl, jwt, + jose, jsx, uuid, erlydtl, jwt, apns, mini_s3, qdate, rest, enenra, locus, prometheus, libphonenumber_erlang]}, {mod, {roster, []}}, diff --git a/rebar.config b/rebar.config index 530a432ce..c1adc0a3f 100644 --- a/rebar.config +++ b/rebar.config @@ -15,6 +15,7 @@ {emqttc, {git, "git://github.com/NYNJA-MC/emqttc", {branch,"master"}}}, {rest, {git, "git://github.com/synrc/rest", {tag,"5.10"}}}, {gen_smtp, {git, "git://github.com/voxoz/gen_smtp", {branch,"master"}}}, + {apns, {git, "git://github.com/inaka/apns4erl", {tag, "2.3.1"}}}, {emq_dashboard, {git, "https://github.com/synrc/emq_dashboard", {branch,"master"}}}, {opencensus, {git, "https://github.com/census-instrumentation/opencensus-erlang", {ref, "7fb276f"}}}, {libphonenumber_erlang, {git, "https://github.com/marinakr/libphonenumber_erlang.git", {branch,"master"}}}, @@ -73,7 +74,7 @@ {relx, [{release, {server, {cmd, "git log --pretty=format:'1.2-%h' -n 1"}}, %% Copied from .applist in older version. Should be cleaned up. - [kernel, stdlib, sasl, crypto, inets, os_mon, + [kernel, stdlib, sasl, crypto, inets, os_mon, runtime_tools, fs, gproc, gen_logger, compiler, mnesia, kvs, esockd, @@ -82,7 +83,7 @@ certifi,ibrowse,asn1,xmerl,counters,ctx, wts,syntax_tools,qdate_localtime, libphonenumber_erlang,syn,cowlib,jiffy,idna,parse_trans, - goldrush, public_key,bpe,{lager,load},ssl,ranch, + goldrush, public_key,bpe,{lager,load},ssl,ranch,gun,apns, ssl_verify_fun,locus,emqttd,hackney,roster,service,active, cowboy,emq_dashboard,emqttc,enenra,envy,uuid,erlydtl,forms, gen_smtp, jwt, mini_s3, nitro, opencensus, diff --git a/rebar.lock b/rebar.lock index 1b30e6623..8626ad330 100644 --- a/rebar.lock +++ b/rebar.lock @@ -3,6 +3,11 @@ {git,"git://github.com/synrc/active", {ref,"cdd8f2b0f62b9785673bdbea7be90e1ae1ca1c02"}}, 0}, + {<<"apns">>, + {git,"git://github.com/inaka/apns4erl", + {ref,"c7ee524c349ddc2fd97a4a3dbdb4b3c2c5aaf089"}}, + 0}, + {<<"apns4erl">>,{pkg,<<"apns4erl">>,<<"2.3.1">>},0}, {<<"base64url">>, {git,"https://github.com/dvv/base64url.git", {ref,"f2c64ed8b9bebc536fad37ad97243452b674b837"}}, @@ -86,6 +91,7 @@ {git,"https://github.com/uwiger/gproc", {ref,"1d16f5e6d7cf616eec4395f2385e3a680a4ffc9f"}}, 0}, + {<<"gun">>,{pkg,<<"gun">>,<<"1.3.0">>},1}, {<<"hackney">>, {git,"https://github.com/benoitc/hackney", {ref,"3c32f04ff0783479992a5d11ec0f4a2d09ba922a"}}, @@ -200,9 +206,11 @@ 0}]}. [ {pkg_hash,[ + {<<"apns4erl">>, <<"C7242DC64DD035BA1BBA2BB46895CCCD204853AF0FC232C4200BA1FE457E2ED7">>}, {<<"certifi">>, <<"75424FF0F3BAACCFD34B1214184B6EF616D89E420B258BB0A5EA7D7BC628F7F0">>}, {<<"cf">>, <<"5CB902239476E141EA70A740340233782D363A31EEA8AD37049561542E6CD641">>}, {<<"erlware_commons">>, <<"0CE192AD69BC6FD0880246D852D0ECE17631E234878011D1586E053641ED4C04">>}, + {<<"gun">>, <<"18E5D269649C987AF95AEC309F68A27FFC3930531DD227A6EAA0884D6684286E">>}, {<<"idna">>, <<"689C46CBCDF3524C44D5F3DDE8001F364CD7608A99556D8FBD8239A5798D4C10">>}, {<<"metrics">>, <<"25F094DEA2CDA98213CECC3AEFF09E940299D950904393B2A29D191C346A8486">>}, {<<"mimerl">>, <<"67E2D3F571088D5CFD3E550C383094B47159F3EEE8FFA08E64106CDF5E981BE3">>}, diff --git a/sys.config b/sys.config index c319338ae..ca62fb38d 100644 --- a/sys.config +++ b/sys.config @@ -95,6 +95,7 @@ {app_credentials, "etc/certs/transcribe-dacb4306ab76.json"} ]}, {push_api,[ + {context, dev}, {fcm_server_key,<<"AAAAAzb6_Zg:APA91bGN0jYv_4iqyk8IC4xUdPYXh0yPsTF9YYj_gd9oebRr_ZEoLuC5hCD9RfdqA3Y3AF_P_WbelqvzvgR3RsX_mHBLynV14Q6HakXAtrY_eWLK2xqamF2OC9uBXfKgxTFFqmyr1Kbw">>}, {apns_cert_dir,<<"apns_certificates">>}, {apns_port,2195}]}, -- GitLab From 148c51b05b1b9d7af2cf8014e96e51e9d8b268be Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Wed, 6 May 2020 08:44:52 +0200 Subject: [PATCH 2/8] fix paths to pem files --- apps/roster/src/api/push/ios13.erl | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/apps/roster/src/api/push/ios13.erl b/apps/roster/src/api/push/ios13.erl index 1a2a927ec..5a0beeda1 100644 --- a/apps/roster/src/api/push/ios13.erl +++ b/apps/roster/src/api/push/ios13.erl @@ -8,9 +8,7 @@ description() -> "iOS 13.x Push Notifications Module". --define(APNS_CERT_DIR, proplists:get_value(apns_cert_dir, - push_api_opts(), - default_cert_dir())). +-define(APNS_CERT_DIR, apns_cert_dir()). -define(APNS_PORT, proplists:get_value(apns_port, push_api_opts(), 443)). @@ -79,6 +77,21 @@ keyfile(Context) -> end, filename:join(?APNS_CERT_DIR, Base). +apns_cert_dir() -> + D = proplists:get_value(apns_cert_dir, push_api_opts(), + default_cert_dir()), + Priv = code:priv_dir(roster), + case D of + "apps/roster/" ++ _ -> + filename:join(Priv, filename:basename(D)); + "priv/" ++ Rest -> + filename:join(Priv, Rest); + "/" ++ _ -> + D; + Rel -> + filename:join(Priv, Rel) + end. + default_cert_dir() -> filename:join(code:priv_dir(roster), "apns_certificates"). -- GitLab From f1b137e9f52dd670696574e4c7455e3a31eea212 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Wed, 6 May 2020 10:44:59 +0200 Subject: [PATCH 3/8] fix cert_dir and port defaults --- apps/roster/src/api/push/ios13.erl | 38 ++++++++++++------------------ sys.config | 3 +-- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/apps/roster/src/api/push/ios13.erl b/apps/roster/src/api/push/ios13.erl index 5a0beeda1..80892fed4 100644 --- a/apps/roster/src/api/push/ios13.erl +++ b/apps/roster/src/api/push/ios13.erl @@ -23,19 +23,7 @@ description() -> "iOS 13.x Push Notifications Module". ]). start() -> - dbg:tracer(), - dbg:tpl(?MODULE, x), - dbg:tp(apns, x), - dbg:tp(gun, x), - dbg:tp(gen_tcp,connect,x), - dbg:p(all,[c]), - try start(detect_context()) - after - timer:sleep(1000), - dbg:ctpl('_'), - dbg:ctp('_'), - dbg:stop() - end. + start(detect_context()). start(Context) -> Conn = #{ name => ios13 @@ -81,20 +69,24 @@ apns_cert_dir() -> D = proplists:get_value(apns_cert_dir, push_api_opts(), default_cert_dir()), Priv = code:priv_dir(roster), - case D of - "apps/roster/" ++ _ -> - filename:join(Priv, filename:basename(D)); - "priv/" ++ Rest -> - filename:join(Priv, Rest); - "/" ++ _ -> - D; - Rel -> - filename:join(Priv, Rel) - end. + Res = case D of + "apps/roster/" ++ _ -> + filename:join(Priv, filename:basename(D)); + "priv/" ++ Rest -> + filename:join(Priv, Rest); + "/" ++ _ -> + D; + Rel -> + filename:join(Priv, Rel) + end, + string(Res). default_cert_dir() -> filename:join(code:priv_dir(roster), "apns_certificates"). +string(S) -> + binary_to_list(iolist_to_binary(S)). + %% ------------------------------------------------------------------ %% Ios Push Notifications %% ------------------------------------------------------------------ diff --git a/sys.config b/sys.config index ca62fb38d..6e0bad93a 100644 --- a/sys.config +++ b/sys.config @@ -97,8 +97,7 @@ {push_api,[ {context, dev}, {fcm_server_key,<<"AAAAAzb6_Zg:APA91bGN0jYv_4iqyk8IC4xUdPYXh0yPsTF9YYj_gd9oebRr_ZEoLuC5hCD9RfdqA3Y3AF_P_WbelqvzvgR3RsX_mHBLynV14Q6HakXAtrY_eWLK2xqamF2OC9uBXfKgxTFFqmyr1Kbw">>}, - {apns_cert_dir,<<"apns_certificates">>}, - {apns_port,2195}]}, + {apns_cert_dir,<<"apns_certificates">>}]}, {job_delay, 60}, %% 1 mins {auth_ttl, 900}, %% 15 mins {auth_check_ip, false}, -- GitLab From ce9074a0fc16c296b159c4bb92e5f778da1abc2e Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Wed, 6 May 2020 10:55:10 +0200 Subject: [PATCH 4/8] remove runtime_tools again --- rebar.config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rebar.config b/rebar.config index c1adc0a3f..d9c1f9fcf 100644 --- a/rebar.config +++ b/rebar.config @@ -74,7 +74,7 @@ {relx, [{release, {server, {cmd, "git log --pretty=format:'1.2-%h' -n 1"}}, %% Copied from .applist in older version. Should be cleaned up. - [kernel, stdlib, sasl, crypto, inets, os_mon, runtime_tools, + [kernel, stdlib, sasl, crypto, inets, os_mon, fs, gproc, gen_logger, compiler, mnesia, kvs, esockd, -- GitLab From 2ad0284cbbf84df3b090d8e2902921f91a98c530 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Thu, 7 May 2020 16:49:53 +0200 Subject: [PATCH 5/8] Address review comments --- apps/roster/src/api/push/ios13.erl | 15 +-------------- sys.config | 2 +- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/apps/roster/src/api/push/ios13.erl b/apps/roster/src/api/push/ios13.erl index 80892fed4..56a7b8748 100644 --- a/apps/roster/src/api/push/ios13.erl +++ b/apps/roster/src/api/push/ios13.erl @@ -69,24 +69,11 @@ apns_cert_dir() -> D = proplists:get_value(apns_cert_dir, push_api_opts(), default_cert_dir()), Priv = code:priv_dir(roster), - Res = case D of - "apps/roster/" ++ _ -> - filename:join(Priv, filename:basename(D)); - "priv/" ++ Rest -> - filename:join(Priv, Rest); - "/" ++ _ -> - D; - Rel -> - filename:join(Priv, Rel) - end, - string(Res). + filename:join(Priv, D). default_cert_dir() -> filename:join(code:priv_dir(roster), "apns_certificates"). -string(S) -> - binary_to_list(iolist_to_binary(S)). - %% ------------------------------------------------------------------ %% Ios Push Notifications %% ------------------------------------------------------------------ diff --git a/sys.config b/sys.config index 6e0bad93a..83e75cc9c 100644 --- a/sys.config +++ b/sys.config @@ -97,7 +97,7 @@ {push_api,[ {context, dev}, {fcm_server_key,<<"AAAAAzb6_Zg:APA91bGN0jYv_4iqyk8IC4xUdPYXh0yPsTF9YYj_gd9oebRr_ZEoLuC5hCD9RfdqA3Y3AF_P_WbelqvzvgR3RsX_mHBLynV14Q6HakXAtrY_eWLK2xqamF2OC9uBXfKgxTFFqmyr1Kbw">>}, - {apns_cert_dir,<<"apns_certificates">>}]}, + {apns_cert_dir, "apns_certificates"}]}, {job_delay, 60}, %% 1 mins {auth_ttl, 900}, %% 15 mins {auth_check_ip, false}, -- GitLab From 99a5f5bef0a0b1f3cb59141aaf67c7dbf94a7850 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Mon, 11 May 2020 14:11:32 +0200 Subject: [PATCH 6/8] Use own apns4erl fork; add status debug function --- apps/roster/src/api/push/ios13.erl | 57 +++++++++++++++++++++++++++++- rebar.config | 2 +- rebar.lock | 4 +-- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/apps/roster/src/api/push/ios13.erl b/apps/roster/src/api/push/ios13.erl index 56a7b8748..a071847c3 100644 --- a/apps/roster/src/api/push/ios13.erl +++ b/apps/roster/src/api/push/ios13.erl @@ -6,6 +6,8 @@ -export([start/0]). -export([description/0, notify/6, test_push_notification/0]). +-export([status/0]). + description() -> "iOS 13.x Push Notifications Module". -define(APNS_CERT_DIR, apns_cert_dir()). @@ -31,9 +33,19 @@ start(Context) -> , apple_port => ?APNS_PORT , certfile => certfile(Context) , keyfile => keyfile(Context) - , type => cert }, + , type => cert + , gun => #{ transport => tls + , http2_opts => #{ keepalive => 30000 } + } + }, apns:connect(Conn). +%% For debugging purposes. Should be replaced by a more +%% structured solution. +status() -> + Ch = supervisor:which_children(apns_sup), + [ch_status(Pid) || {_, Pid, worker, [apns_connection]} <- Ch]. + detect_context() -> proplists:get_value(context, push_api_opts(), dev). @@ -129,6 +141,49 @@ get_data_from_feature(SessionSettings, Key) -> %% end %% end. +ch_status(Pid) -> + case sys:get_status(Pid) of + {status, _, {module, gen_statem}, + [_, running, _, _, + [ {header, _} + , {data, _} + , {data, [{"State", {Status, St}}]} + ]]} -> + ch_status(Pid, Status, St); + _Other -> + {Pid, undefined} + end. + +ch_status(Pid, connected, St) -> + GunPid = maps:get(gun_pid, St), + {Pid, #{ status => connected + , state => St + , gun => gun_status(GunPid) }}; +ch_status(Pid, Other, St) -> + {Pid, Other, St}. + +gun_status(Pid) -> + case sys:get_status(Pid) of + {status, _, {module, _}, + [ _, running, _, _ + , {loop, State}] + } -> + case State of + _ when element(1, State) == state -> + case [M || M <- tuple_to_list(State), + is_map(M)] of + [GunOpts] -> + GunOpts; + _ -> + undefined + end; + _ -> + undefined + end; + _Other -> + undefined + end. + %% ------------------------------------------------------------------ %% Tests %% ------------------------------------------------------------------ diff --git a/rebar.config b/rebar.config index d9c1f9fcf..7e9ad4546 100644 --- a/rebar.config +++ b/rebar.config @@ -15,7 +15,7 @@ {emqttc, {git, "git://github.com/NYNJA-MC/emqttc", {branch,"master"}}}, {rest, {git, "git://github.com/synrc/rest", {tag,"5.10"}}}, {gen_smtp, {git, "git://github.com/voxoz/gen_smtp", {branch,"master"}}}, - {apns, {git, "git://github.com/inaka/apns4erl", {tag, "2.3.1"}}}, + {apns, {git, "git://github.com/NYNJA-MC/apns4erl", {ref, "6724edcf073f512a01a0b7652223d173db2f2fe0"}}}, {emq_dashboard, {git, "https://github.com/synrc/emq_dashboard", {branch,"master"}}}, {opencensus, {git, "https://github.com/census-instrumentation/opencensus-erlang", {ref, "7fb276f"}}}, {libphonenumber_erlang, {git, "https://github.com/marinakr/libphonenumber_erlang.git", {branch,"master"}}}, diff --git a/rebar.lock b/rebar.lock index 8626ad330..a9acb9def 100644 --- a/rebar.lock +++ b/rebar.lock @@ -4,8 +4,8 @@ {ref,"cdd8f2b0f62b9785673bdbea7be90e1ae1ca1c02"}}, 0}, {<<"apns">>, - {git,"git://github.com/inaka/apns4erl", - {ref,"c7ee524c349ddc2fd97a4a3dbdb4b3c2c5aaf089"}}, + {git,"git://github.com/NYNJA-MC/apns4erl", + {ref,"6724edcf073f512a01a0b7652223d173db2f2fe0"}}, 0}, {<<"apns4erl">>,{pkg,<<"apns4erl">>,<<"2.3.1">>},0}, {<<"base64url">>, -- GitLab From 0eb4bdf5b5f0be0992ba9554ff87d01c2933af22 Mon Sep 17 00:00:00 2001 From: Tobias Lindahl Date: Wed, 27 May 2020 14:35:10 +0200 Subject: [PATCH 7/8] Remove obsolete apns4erl references --- rebar.lock | 2 -- 1 file changed, 2 deletions(-) diff --git a/rebar.lock b/rebar.lock index a9acb9def..198e300be 100644 --- a/rebar.lock +++ b/rebar.lock @@ -7,7 +7,6 @@ {git,"git://github.com/NYNJA-MC/apns4erl", {ref,"6724edcf073f512a01a0b7652223d173db2f2fe0"}}, 0}, - {<<"apns4erl">>,{pkg,<<"apns4erl">>,<<"2.3.1">>},0}, {<<"base64url">>, {git,"https://github.com/dvv/base64url.git", {ref,"f2c64ed8b9bebc536fad37ad97243452b674b837"}}, @@ -206,7 +205,6 @@ 0}]}. [ {pkg_hash,[ - {<<"apns4erl">>, <<"C7242DC64DD035BA1BBA2BB46895CCCD204853AF0FC232C4200BA1FE457E2ED7">>}, {<<"certifi">>, <<"75424FF0F3BAACCFD34B1214184B6EF616D89E420B258BB0A5EA7D7BC628F7F0">>}, {<<"cf">>, <<"5CB902239476E141EA70A740340233782D363A31EEA8AD37049561542E6CD641">>}, {<<"erlware_commons">>, <<"0CE192AD69BC6FD0880246D852D0ECE17631E234878011D1586E053641ED4C04">>}, -- GitLab From 3aa756cdb6de7528de1e73500861e446a5ba1558 Mon Sep 17 00:00:00 2001 From: Tobias Lindahl Date: Thu, 28 May 2020 16:39:04 +0200 Subject: [PATCH 8/8] Reboot the apns refactoring effort * remove unnecessary layer push_api.erl * remove split on different ios versions * better naming of roster_apns_api * introduce roster:deployment_context() to find if we are dev/staging/prod --- apps/roster/src/api/push/ios.erl | 118 ---------- apps/roster/src/api/push/ios13.erl | 221 ------------------- apps/roster/src/api/push/push_api.erl | 37 ---- apps/roster/src/api/push/roster_apns_api.erl | 149 +++++++++++++ apps/roster/src/protocol/roster_push.erl | 26 ++- apps/roster/src/roster.erl | 16 ++ sys.config | 6 +- 7 files changed, 187 insertions(+), 386 deletions(-) delete mode 100644 apps/roster/src/api/push/ios.erl delete mode 100644 apps/roster/src/api/push/ios13.erl delete mode 100644 apps/roster/src/api/push/push_api.erl create mode 100644 apps/roster/src/api/push/roster_apns_api.erl diff --git a/apps/roster/src/api/push/ios.erl b/apps/roster/src/api/push/ios.erl deleted file mode 100644 index 07a878027..000000000 --- a/apps/roster/src/api/push/ios.erl +++ /dev/null @@ -1,118 +0,0 @@ --module(ios). --include_lib("kernel/include/logger.hrl"). --include("roster.hrl"). --include_lib("roster/include/static/push_notification_var.hrl"). - --export([description/0, notify/5, test_push_notification/0]). - -description() -> "IOS Push Notifications Module". - --define(APNS_CERT_DIR, proplists:get_value(apns_cert_dir, application:get_env(roster, push_api, []))). --define(APNS_PORT, proplists:get_value(apns_port, application:get_env(roster, push_api, []))). - --define(GATEWAY_LIST, [ - {<<"SANDBOX">>, "gateway.sandbox.push.apple.com"}, - {<<"LIVE">>, "gateway.push.apple.com"}]). - --define(BANDLE_LIST, [ - {<<"com.nynja.mobile.communicator">>, {"cert_prod.pem", "key_prod.pem"}}, - {<<"com.nynja.rс.mobile.communicator">>, {"cert_prod.pem", "key_prod.pem"}}, - {<<"com.nynja.dev.mobile.communicator">>, {"cert_dev.pem", "key_dev.pem"}} - ]). - -%% ------------------------------------------------------------------ -%% Ios Push Notifications -%% ------------------------------------------------------------------ - -notify(Alert, Custom, Type, DeviceId, SessionSettings) when is_binary(DeviceId) -> - notify(Alert, Custom, Type, binary_to_list(DeviceId), SessionSettings); -notify(A, C, T, DeviceId, SessionSettings) -> - [Alert, Custom, Type] = [iolist_to_binary([L]) || L <- [A, C, T]], - application:ensure_started(ssl), - -%% create aps json - Aps = jsx:encode([{<<"model">>, Custom}, {<<"type">>, Type}, {<<"title">>, Alert}, - {<<"dns">>, get_data_from_feature(SessionSettings, ?FKPN_SERVER_DNS)}, {<<"version">>, <>}]), - -%% create ios payload string - PayloadString = binary_to_list(iolist_to_binary(["{\"aps\": {\"nynja\": ", Aps, "}}"])), -% ?LOG_INFO("PayloadString ~p~n~n", [PayloadString]), - -%% prepare push data - Payload = list_to_binary(PayloadString), - PayloadLength = size(Payload), - FormattedDeviceId = list_to_integer(DeviceId, 16), - Packet = <<0:8, 32:16/big, FormattedDeviceId:256/big, PayloadLength:16/big, Payload/binary>>, - {_, {CertFile, KeyFile}} = get_bandle(SessionSettings), - ?LOG_INFO("CertFile: ~p", [CertFile]), - Options = [{certfile, path_to_pem_file(CertFile)}, {keyfile, path_to_pem_file(KeyFile)}, {mode, binary}], - [send_push(Addr, Packet, Options, 1) || {_, Addr} <- get_gateway(SessionSettings)], - ok. - -%% ------------------------------------------------------------------ -%% Helpers -%% ------------------------------------------------------------------ - -send_push(Addr, Payload, Options, Attempt) -> - ?LOG_INFO("Addr: ~p, Attempt:~p", [Addr, Attempt]), -%% NOTE set Duration = Attempt * 100 for tests - Duration = Attempt * 500, - {Status, Socket} = ssl:connect(Addr, ?APNS_PORT, Options, Duration), - case Status of - ok -> - ssl:send(Socket, Payload), - ssl:close(Socket), - ?LOG_INFO("Push sent", []); - error -> - if - Attempt > 10 -> - ?LOG_INFO("Final error", []); - true -> - timer:sleep(Duration), - ?LOG_INFO("Error with socket opening. Reason:~p", [Socket]), - send_push(Addr, Payload, Options, Attempt + 1) - end - end. - -path_to_pem_file(FileName) -> - PrivDir = code:priv_dir(roster), - filename:join([PrivDir,?APNS_CERT_DIR, FileName]). - -get_data_from_feature(SessionSettings, Key) -> - case lists:keyfind(Key, #'Feature'.key, SessionSettings) of - #'Feature'{value = Value} -> Value; - _ -> [] - end. - -get_bandle(SessionSettings) -> - [H|_]=get_from_session(SessionSettings, ?FKPN_BANDLE, ?BANDLE_LIST), - H. - -get_gateway(SessionSettings) -> - get_from_session(SessionSettings, ?FKPN_GATEWAY, ?GATEWAY_LIST). - -get_from_session(SessionSettings, Key, AcceptedValues) -> - case get_data_from_feature(SessionSettings, Key) of - [] -> AcceptedValues; - FoundValue -> Filtered = lists:filter(fun(X) -> element(1,X) == FoundValue end, AcceptedValues), - case Filtered of [] -> AcceptedValues; _ -> Filtered end - end. - -%% ------------------------------------------------------------------ -%% Tests -%% ------------------------------------------------------------------ - -%% Liubov's phone --define(APNS_TEST_DEVICE_ID, "f9e7bedd8d46079c51a5aee1f951bbafc68ec541d68a56a0aa709214263cf138"). -%% Anton's phone dev -%% -define(APNS_TEST_DEVICE_ID, "55e9a60ffde1701ba701ea653ba6c0dfa4e515de7d56aa2039d72a904f353e54"). -%% Anton's phone rc -%% -define(APNS_TEST_DEVICE_ID, "a34830e7199ff499e986d2bc3ab0555b0acebe499f9a9f0445701b7dbe2a6722"). - -test_push_notification() -> - SessionSettings = [#'Feature'{id = <<"ID_Sandbox">>, key = <<"APNS_GATEWAY">>, value = <<"SANDBOX">>, group = <<"AUTH_DATA">>}, - #'Feature'{id = <<"ID_Dns">>, key = <<"SERVER_DNS">>, value = <<"SomeDNSValue">>, group = <<"AUTH_DATA">>}, - #'Feature'{id = <<"ID_Bandle">>, key = <<"IOS_BANDLE">>,value = <<"com.nynja.mobile.communicator">>, group = <<"AUTH_DATA">>}], - Msg = lists:concat(["Test it! ", vox_api:generate_random_data(4)]), - Custom = <<"g2gSZAAHTWVzc2FnZWEQZAAFY2hhaW5oA2QAA3AycG0AAAAOMzgwNjM4MDk1MTU4XzdtAAAADjM4MDk5NDM4Mjc5OF84ampqam0AAAAOMzgwOTk0MzgyNzk4XzhtAAAADjM4MDYzODA5NTE1OF83am4GAD5BRlNeAWpqbAAAAAFoBmQABERlc2NqbQAAAARIaGhoYQBqampqamQABHNlbnQ=">>, - notify(Msg, Custom, <<"message">>, ?APNS_TEST_DEVICE_ID, SessionSettings). diff --git a/apps/roster/src/api/push/ios13.erl b/apps/roster/src/api/push/ios13.erl deleted file mode 100644 index a071847c3..000000000 --- a/apps/roster/src/api/push/ios13.erl +++ /dev/null @@ -1,221 +0,0 @@ --module(ios13). --include_lib("kernel/include/logger.hrl"). --include("roster.hrl"). --include_lib("roster/include/static/push_notification_var.hrl"). - --export([start/0]). --export([description/0, notify/6, test_push_notification/0]). - --export([status/0]). - -description() -> "iOS 13.x Push Notifications Module". - --define(APNS_CERT_DIR, apns_cert_dir()). - --define(APNS_PORT, proplists:get_value(apns_port, push_api_opts(), 443)). - --define(GATEWAY_LIST, [ - {<<"SANDBOX">>, "api.sandbox.push.apple.com"}, - {<<"LIVE">>, "api.push.apple.com"}]). - --define(BANDLE_LIST, [ - {<<"com.nynja.mobile.communicator">>, {"cert_prod.pem", "key_prod.pem"}}, - {<<"com.nynja.rс.mobile.communicator">>, {"cert_prod.pem", "key_prod.pem"}}, - {<<"com.nynja.dev.mobile.communicator">>, {"cert_dev.pem", "key_dev.pem"}} - ]). - -start() -> - start(detect_context()). - -start(Context) -> - Conn = #{ name => ios13 - , apple_host => get_host(Context) - , apple_port => ?APNS_PORT - , certfile => certfile(Context) - , keyfile => keyfile(Context) - , type => cert - , gun => #{ transport => tls - , http2_opts => #{ keepalive => 30000 } - } - }, - apns:connect(Conn). - -%% For debugging purposes. Should be replaced by a more -%% structured solution. -status() -> - Ch = supervisor:which_children(apns_sup), - [ch_status(Pid) || {_, Pid, worker, [apns_connection]} <- Ch]. - -detect_context() -> - proplists:get_value(context, push_api_opts(), dev). - -get_host(Context) -> - Key = case Context of - dev -> - <<"SANDBOX">>; - prod -> - <<"LIVE">> - end, - proplists:get_value(Key, ?GATEWAY_LIST). - -push_api_opts() -> - application:get_env(roster, push_api, []). - -certfile(Context) -> - Base = case Context of - dev -> "cert_dev.pem"; - prod -> "cert_prod.pem"; - rc -> "cert_rc.pem" - end, - filename:join(?APNS_CERT_DIR, Base). - -keyfile(Context) -> - Base = case Context of - dev -> "key_dev.pem"; - prod -> "key_prod.pem"; - rc -> "key_rc.pem" - end, - filename:join(?APNS_CERT_DIR, Base). - -apns_cert_dir() -> - D = proplists:get_value(apns_cert_dir, push_api_opts(), - default_cert_dir()), - Priv = code:priv_dir(roster), - filename:join(Priv, D). - -default_cert_dir() -> - filename:join(code:priv_dir(roster), "apns_certificates"). - -%% ------------------------------------------------------------------ -%% Ios Push Notifications -%% ------------------------------------------------------------------ - -notify(Alert, Custom, Type, DeviceId, SessionSettings, ConnSettings) - when is_binary(DeviceId) -> - notify(Alert, Custom, Type, binary_to_list(DeviceId), SessionSettings, ConnSettings); -notify(A, C, T, DeviceId, SessionSettings, ConnSettings) -> - [Alert, Custom, Type] = [iolist_to_binary([L]) || L <- [A, C, T]], - - Aps = #{ nynja => #{ model => Custom - , type => Type - , title => Alert - , dns => get_data_from_feature(SessionSettings, ?FKPN_SERVER_DNS) - , version => <> } }, - - %% Use DeviceId or FormattedDeviceId?? - %% FormattedDeviceId = list_to_integer(DeviceId, 16), - send_push(DeviceId, #{aps => Aps}, ConnSettings), - ok. - -%% ------------------------------------------------------------------ -%% Helpers -%% ------------------------------------------------------------------ - -send_push(DeviceId, Msg, Pid) when is_pid(Pid) -> - apns:push_notification(Pid, DeviceId, Msg). - -get_data_from_feature(SessionSettings, Key) -> - case lists:keyfind(Key, #'Feature'.key, SessionSettings) of - #'Feature'{value = Value} -> Value; - _ -> [] - end. - -%% get_bandle(SessionSettings) -> -%% [H|_]=get_from_session(SessionSettings, ?FKPN_BANDLE, ?BANDLE_LIST), -%% H. - -%% get_from_session(SessionSettings, Key, AcceptedValues) -> -%% case get_data_from_feature(SessionSettings, Key) of -%% [] -> -%% AcceptedValues; -%% FoundValue -> -%% Filtered = lists:filter( -%% fun(X) -> -%% element(1,X) == FoundValue -%% end, AcceptedValues), -%% case Filtered of -%% [] -> -%% AcceptedValues; -%% _ -> -%% Filtered -%% end -%% end. - -ch_status(Pid) -> - case sys:get_status(Pid) of - {status, _, {module, gen_statem}, - [_, running, _, _, - [ {header, _} - , {data, _} - , {data, [{"State", {Status, St}}]} - ]]} -> - ch_status(Pid, Status, St); - _Other -> - {Pid, undefined} - end. - -ch_status(Pid, connected, St) -> - GunPid = maps:get(gun_pid, St), - {Pid, #{ status => connected - , state => St - , gun => gun_status(GunPid) }}; -ch_status(Pid, Other, St) -> - {Pid, Other, St}. - -gun_status(Pid) -> - case sys:get_status(Pid) of - {status, _, {module, _}, - [ _, running, _, _ - , {loop, State}] - } -> - case State of - _ when element(1, State) == state -> - case [M || M <- tuple_to_list(State), - is_map(M)] of - [GunOpts] -> - GunOpts; - _ -> - undefined - end; - _ -> - undefined - end; - _Other -> - undefined - end. - -%% ------------------------------------------------------------------ -%% Tests -%% ------------------------------------------------------------------ - -%% Liubov's phone --define(APNS_TEST_DEVICE_ID, "f9e7bedd8d46079c51a5aee1f951bbafc68ec541d68a56a0aa709214263cf138"). -%% Anton's phone dev -%% -define(APNS_TEST_DEVICE_ID, "55e9a60ffde1701ba701ea653ba6c0dfa4e515de7d56aa2039d72a904f353e54"). -%% Anton's phone rc -%% -define(APNS_TEST_DEVICE_ID, "a34830e7199ff499e986d2bc3ab0555b0acebe499f9a9f0445701b7dbe2a6722"). - -%% TODO: run tests -test_push_notification() -> - {ok, Pid} = start(dev), - SessionSettings = - [ - #'Feature'{ id = <<"ID_Sandbox">> - , key = <<"APNS_GATEWAY">> - , value = <<"SANDBOX">> - , group = <<"AUTH_DATA">>} - , #'Feature'{ id = <<"ID_Dns">> - , key = <<"SERVER_DNS">> - , value = <<"SomeDNSValue">> - , group = <<"AUTH_DATA">>} - , #'Feature'{ id = <<"ID_Bandle">> - , key = <<"IOS_BANDLE">> - , value = <<"com.nynja.mobile.communicator">> - , group = <<"AUTH_DATA">>} - ], - Msg = lists:concat(["Test it! ", vox_api:generate_random_data(4)]), - Custom = <<"g2gSZAAHTWVzc2FnZWEQZAAFY2hhaW5oA2QAA3AycG0AAAAOMzgw" - "NjM4MDk1MTU4XzdtAAAADjM4MDk5NDM4Mjc5OF84ampqam0AAAAO" - "MzgwOTk0MzgyNzk4XzhtAAAADjM4MDYzODA5NTE1OF83am4GAD5B" - "RlNeAWpqbAAAAAFoBmQABERlc2NqbQAAAARIaGhoYQBqampqamQABHNlbnQ=">>, - notify(Msg, Custom, <<"message">>, ?APNS_TEST_DEVICE_ID, SessionSettings, Pid). diff --git a/apps/roster/src/api/push/push_api.erl b/apps/roster/src/api/push/push_api.erl deleted file mode 100644 index a0d3862c9..000000000 --- a/apps/roster/src/api/push/push_api.erl +++ /dev/null @@ -1,37 +0,0 @@ --module(push_api). - --export([start/0]). --export([description/0, fcm_notify/4, apns_notify/6]). - -description() -> "Mobile Push Notifications Module. Wrapper for IOS and Android". - --compile(export_all). - --include("roster.hrl"). - -start() -> - {ok, IOS13} = ios13:start(), - {ok, #{ ios13 => IOS13 - , android => [] - , ios => [] }}. - -fcm_notify(MessageTitle, MessageBody, DeviceId, _ConnState) -> - android:notify(MessageTitle, MessageBody, DeviceId). - -apns_notify(Alert, Custom, Type, DeviceId, SessionSettings, ConnState) -> - case ios_version(SessionSettings) of - ios -> - ios:notify(Alert, Custom, Type, DeviceId, SessionSettings); - ios13 -> - IOS13St = maps:get(ios13, ConnState), - ios13:notify(Alert, Custom, Type, DeviceId, SessionSettings, IOS13St) - end. - -ios_version(Settings) -> - case [X || #'Feature'{key = <<"OS">>, value = <<"iOS 13", _/binary>>} = X - <- Settings] of - [_|_] -> - ios13; - [] -> - ios - end. diff --git a/apps/roster/src/api/push/roster_apns_api.erl b/apps/roster/src/api/push/roster_apns_api.erl new file mode 100644 index 000000000..9ec8c202a --- /dev/null +++ b/apps/roster/src/api/push/roster_apns_api.erl @@ -0,0 +1,149 @@ +%%%------------------------------------------------------------------- +%%% @doc Client interface to apns +%%% +%%% @end +%%%------------------------------------------------------------------- +-module(roster_apns_api). + +-include_lib("kernel/include/logger.hrl"). +-include_lib("roster/include/roster.hrl"). +-include_lib("roster/include/static/push_notification_var.hrl"). + +-export([ notify/6 + , start/1 + ]). + +-type context() :: 'development' | 'production' | 'staging'. +-record(roster_apns_api_state, { conn_pid :: pid() + , context :: context() + }). + +-define(BUNDLE_ENV, + #{ production => <<"com.nynja.mobile.communicator">> + , staging => <<"com.nynja.rс.mobile.communicator">> + , development => <<"com.nynja.dev.mobile.communicator">>}). + +-define(GATEWAY_ENV, + #{ production => <<"LIVE">> + , staging => <<"LIVE">> + , development => <<"SANDBOX">>}). + +-define(DNS_ENV, + #{ production => <<"im.nynja.net">> + , staging => <<"im.staging.nynja.net">> + , development => <<"im.dev.nynja.net">>}). + +%%%=================================================================== +%%% API +%%%=================================================================== + +start(Context) -> + %% TODO: Move the certs away from priv_dir + CertDir = filename:join(code:priv_dir(roster), get_from_config(apns_cert_dir)), + Conn = #{ name => ios_http2 + , apple_host => host(Context) + , apple_port => get_from_config(apns_http_port) + , certfile => certfile(CertDir, Context) + , keyfile => keyfile(CertDir, Context) + , type => cert + , gun => #{ transport => tls + , http2_opts => #{ keepalive => 30000 } + } + }, + ?LOG_INFO("Connecting to apns with config ~p", [Conn]), + case apns:connect(Conn) of + {ok, ConnPid} -> + %% TODO: We should monitor the connection, + %% or at least deal with apns monitor messages + {ok, #roster_apns_api_state{ context = Context, conn_pid = ConnPid}}; + {error, What} -> + error({could_not_start_apns, What}) + end. + + +notify(A, C, T, DeviceId, SessionSettings, State) -> + Context = State#roster_apns_api_state.context, + case session_settings_errors(SessionSettings, Context) of + [] -> + [Alert, Custom, Type] = [iolist_to_binary([L]) || L <- [A, C, T]], + DNS = get_data_from_feature(SessionSettings, ?FKPN_SERVER_DNS), + Aps = #{ nynja => #{ model => Custom + , type => Type + , title => Alert + , dns => DNS + %% TODO: Is this needed? + , version => <> + } + }, + ConnPid = State#roster_apns_api_state.conn_pid, + %% TODO: Handle response codes + apns:push_notification(ConnPid, DeviceId, Aps); + What -> + ?LOG_INFO("Bad session apns settings: ~p (~p)", + [What, SessionSettings]), + ok + end. + +%%%=================================================================== +%%% Internal functions +%%%=================================================================== + +%%%=================================================================== +%%% Initialisation of connection + +get_from_config(Key) -> + Env = application:get_env(roster, push_api, []), + proplists:get_value(Key, Env). + +certfile(CertDir, Context) -> + Base = case Context of + development -> "cert_dev.pem"; + production -> "cert_prod.pem"; + staging -> "cert_rc.pem" + end, + FN = filename:join(CertDir, Base), + case filelib:is_file(FN) of + true -> FN; + false -> error({no_apns_cert_file, FN}) + end. + +keyfile(CertDir, Context) -> + Base = case Context of + development -> "key_dev.pem"; + production -> "key_prod.pem"; + staging -> "key_rc.pem" + end, + FN = filename:join(CertDir, Base), + case filelib:is_file(FN) of + true -> FN; + false -> error({no_apns_key_file, FN}) + end. + +host(Context) -> + case Context of + development -> "api.sandbox.push.apple.com"; + staging -> "api.sandbox.push.apple.com"; + production -> "api.push.apple.com" + end. + +%%%=================================================================== +%%% Client verification + +%% TODO: This could probably go away once the settings have been tested +%% on all environments +session_settings_errors(SessionSettings, Context) -> + Bundle = get_data_from_feature(SessionSettings, ?FKPN_BANDLE), + Gateway = get_data_from_feature(SessionSettings, ?FKPN_GATEWAY), + DNS = get_data_from_feature(SessionSettings, ?FKPN_SERVER_DNS), + ExpectedBundle = maps:get(Context, ?BUNDLE_ENV), + ExpectedGateway = maps:get(Context, ?GATEWAY_ENV), + ExpectedDNS = maps:get(Context, ?DNS_ENV), + [bad_bundle || ExpectedBundle /= Bundle andalso Bundle /= []] ++ + [bad_gateway || ExpectedGateway /= Gateway andalso Gateway /= []] ++ + [bad_dns || ExpectedDNS /= DNS andalso DNS /= []]. + +get_data_from_feature(SessionSettings, Key) -> + case lists:keyfind(Key, #'Feature'.key, SessionSettings) of + #'Feature'{value = Value} -> Value; + _ -> [] + end. diff --git a/apps/roster/src/protocol/roster_push.erl b/apps/roster/src/protocol/roster_push.erl index 2e91db72b..b228df808 100644 --- a/apps/roster/src/protocol/roster_push.erl +++ b/apps/roster/src/protocol/roster_push.erl @@ -6,8 +6,17 @@ -compile(export_all). start() -> - {ok, ConnState} = push_api:start(), - n2o_async:start(#handler{module = ?MODULE, class = system, group = roster, name = ?MODULE, state = #{conn_state => ConnState}}). + Context = roster:deployment_context(), + {ok, Ios} = roster_apns_api:start(Context), + ConnState = #{ android => [] + , ios => Ios}, + n2o_async:start(#handler{ module = ?MODULE + , class = system + , group = roster + , name = ?MODULE + , state = #{conn_state => ConnState}}). + +%% TODO: Handle apns connection messages in proc/2 proc(init, #handler{name = ?MODULE} = Async) -> ?LOG_INFO("ASYNC", []), @@ -17,7 +26,6 @@ proc({async_push, Session, Payload, PushAlert, PushType}, #handler{state = HS} = send_push_notification(Session, Payload, PushAlert, PushType, HS), {reply, [], H}. -%% TODO prettify variables naming send_push_notification(#'Auth'{ os = OS , push = PushToken , user_id = PhoneId @@ -29,13 +37,13 @@ send_push_notification(#'Auth'{ os = OS [PhoneId, OS, binary:part(PushToken, 0, erlang:min(25, size(PushToken))), PushAlert]), send_push_notification(OS, PushToken, Payload, PushAlert, PushType, AuthSettings, HS) end. -send_push_notification(ios, Push, Payload, PushAlert, <<"calling">>, AuthSettings, HS) -> - push_api:apns_notify(PushAlert, Payload, <<"calling">>, Push, AuthSettings, HS); + send_push_notification(ios, Push, Payload, PushAlert, PushType, AuthSettings, HS) -> - DecodedPayload = base64:encode(term_to_binary(Payload)), - push_api:apns_notify(PushAlert, DecodedPayload, PushType, Push, AuthSettings, HS); -send_push_notification(android, Push, Payload, PushAlert, PushType, _AuthSettings, HS) -> + EncodedPayload = base64:encode(term_to_binary(Payload)), + IOS = maps:get(ios, HS), + roster_apns_api:notify(PushAlert, EncodedPayload, PushType, Push, AuthSettings, IOS); +send_push_notification(android, Push, Payload, PushAlert, PushType, _AuthSettings,_HS) -> PushModel = #push{model = Payload, type = PushType, alert = PushAlert, title = PushAlert, badge = 1}, AndroidPush = http_uri:encode(binary_to_list(base64:encode(term_to_binary(PushModel)))), - push_api:fcm_notify(PushAlert, AndroidPush, Push, HS); + android:notify(PushAlert, AndroidPush, Push); send_push_notification(_, _, _, _, _, _, _) -> skip. diff --git a/apps/roster/src/roster.erl b/apps/roster/src/roster.erl index b40dafc87..0ed71f317 100644 --- a/apps/roster/src/roster.erl +++ b/apps/roster/src/roster.erl @@ -59,6 +59,7 @@ atoms() -> [android, ios, contact, signup, signin, welcome]. init([]) -> {ok, {{one_for_one, 5, 10}, []}}. start(_, _) -> atoms(), + ensure_deployment_context_is_set(), try load([]) catch Error:Reason -> @@ -96,6 +97,21 @@ start(_, _) -> google_api:start(), A. +ensure_deployment_context_is_set() -> + case deployment_context() of + development -> ok; + staging -> ok; + production -> ok; + Other -> + error({illegal_deployment_context, Other, + <<"Set this using os environment" + " variable DEPLOYMENT_CONTEXT">>}) + end. + +-spec deployment_context() -> 'development' | 'staging' | 'production'. +deployment_context() -> + application:get_env(roster, deployment_context, undefined). + execution_time(StartTime) -> TimeInMicroSec = (os:system_time() - StartTime)/1000, list_to_integer(float_to_list(TimeInMicroSec,[{decimals,0}])). diff --git a/sys.config b/sys.config index 83e75cc9c..368dc10f6 100644 --- a/sys.config +++ b/sys.config @@ -50,6 +50,9 @@ {review,[{host,"ns.synrc.com"}]}, {roster, [ + %% TODO: Move this to sys.config.src and use environment variable. + %% Don't merge with this still here! + {deployment_context, development}, %% Must be set to 'development' | 'staging' | 'production'. {health_endpoint_accept_traffic, true}, {freeze_time, 1000}, {upload,"./storage"}, @@ -97,7 +100,8 @@ {push_api,[ {context, dev}, {fcm_server_key,<<"AAAAAzb6_Zg:APA91bGN0jYv_4iqyk8IC4xUdPYXh0yPsTF9YYj_gd9oebRr_ZEoLuC5hCD9RfdqA3Y3AF_P_WbelqvzvgR3RsX_mHBLynV14Q6HakXAtrY_eWLK2xqamF2OC9uBXfKgxTFFqmyr1Kbw">>}, - {apns_cert_dir, "apns_certificates"}]}, + {apns_cert_dir, "apns_certificates"}, + {apns_http_port, 443}]}, {job_delay, 60}, %% 1 mins {auth_ttl, 900}, %% 15 mins {auth_check_ip, false}, -- GitLab