Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/api/erotik.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
-spec api_connect(string(), string(), integer(), string(), string()) -> {ok, pid()}.
api_connect(Name, Host, Port, Login, Password) ->
Config = [{host, Host}, {port, Port}, {login, Login}, {password, Password}],
me_connector:start_link(Name, {api, Config}).
{ok, Pid} = me_connector:start_link(Name, []),
ok = gen_server:call(Pid, {connect, {api, Config}}, 10 * 1000),
{ok, Pid}.

%% same as api_connect, but with timeout in milliseconds
-spec api_connect(string(), string(), integer(), string(), string(), integer()) -> {ok, pid()}.
api_connect(Name, Host, Port, Login, Password, Timeout) ->
Config = [{host, Host}, {port, Port}, {login, Login}, {password, Password}, {timeout, Timeout}],
me_connector:start_link(Name, {api, Config}).
{ok, Pid} = me_connector:start_link(Name, []),
ok = gen_server:call(Pid, {connect, {api, Config}}, 10 * 1000),
{ok, Pid}.

-spec ssh_connect(string(), string(), integer(), string(), string()) -> {ok, pid()}.
ssh_connect(Name, Host, Port, Login, Password) ->
Expand Down
27 changes: 20 additions & 7 deletions src/core/me_connector.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
socket :: port(),
ssh_ref :: pid(),
channel_ids :: dict:dict()
channel_ids :: dict()
}).

%%%===================================================================
Expand Down Expand Up @@ -96,6 +96,11 @@ handle_call({command, List}, _From, State = #state{socket = S}) when S /= undefi
handle_call({command, List}, From, State = #state{ssh_ref = SSHRef, channel_ids = Dict}) -> %working through ssh
ChannelId = me_logic:send_command(SSHRef, List),
{noreply, State#state{channel_ids = dict:append(ChannelId, From, Dict)}};
handle_call({connect, {api, Config}}, _From, State) ->
{Host, Port, Login, Password, Timeout} = parse_params(Config),
% {ok, Socket} = gen_tcp:connect(Host, Port, [{active, false}], Timeout), %TODO change to active true and stream dencoding
{ok, Socket} = login(Login, Password, Host, Port, Timeout),
{reply, ok, State#state{socket = Socket}};
handle_call(disconnect, _From, State = #state{socket = Socket, ssh_ref = SShRef}) ->
me_ssh:close(SShRef),
me_api:close(Socket),
Expand All @@ -118,11 +123,6 @@ handle_cast({connect, {ssh, Config}}, State) ->
{Host, Port, Login, Password, Timeout} = parse_params(Config),
{ok, SSHRef} = me_ssh:connect(Host, Port, Login, Password, Timeout),
{noreply, State#state{ssh_ref = SSHRef, channel_ids = dict:new()}};
handle_cast({connect, {api, Config}}, State) ->
{Host, Port, Login, Password, Timeout} = parse_params(Config),
{ok, Socket} = gen_tcp:connect(Host, Port, [{active, false}], Timeout), %TODO change to active true and stream dencoding
ok = me_logic:do_login(Socket, Login, Password),
{noreply, State#state{socket = Socket}};
handle_cast(halt, State) ->
{stop, normal, State};
handle_cast(_Request, State) ->
Expand Down Expand Up @@ -196,4 +196,17 @@ parse_params(Config) ->
Login = proplists:get_value(login, Config),
Password = proplists:get_value(password, Config),
Timeout = proplists:get_value(timeout, Config, infinity),
{Host, Port, Login, Password, Timeout}.
{Host, Port, Login, Password, Timeout}.

login(Login, Password, Host, Port, Timeout) ->
{ok, Socket} = gen_tcp:connect(Host, Port, [{active, false}], Timeout), %TODO change to active true and stream dencoding
case me_logic:do_login(Socket, Login, Password) of
ok ->
io:format("Connected~n"),
{ok, Socket};
err ->
ok = gen_tcp:close(Socket),
timer:sleep(1000),
io:format("Reconnecting~n"),
login(Login, Password, Host, Port, Timeout)
end.
8 changes: 6 additions & 2 deletions src/core/me_logic.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ do_login(Socket, Login, Password) ->
Hash = count_hash(Password, Salt),
LoginRequest = form_login_sentence(Login, Hash),
me_api:write_sentence(Socket, LoginRequest),
{done, ["!done"]} = me_api:read_sentence(Socket),
ok.
case me_api:read_sentence(Socket) of
{done, ["!done"]} ->
ok;
{trap, ["!trap", "=message=cannot log in"]} ->
err
end.

%% @private
get_salt({done, LoginGreeting}) ->
Expand Down