2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_proxy65_lib.erl
|
|
|
|
%%% Author : Evgeniy Khramtsov <xram@jabber.ru>
|
|
|
|
%%% Purpose : SOCKS5 parsing library.
|
|
|
|
%%% Created : 12 Oct 2006 by Evgeniy Khramtsov <xram@jabber.ru>
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_proxy65_lib).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
-author('xram@jabber.ru').
|
|
|
|
|
|
|
|
-include("mod_proxy65.hrl").
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([unpack_init_message/1, unpack_auth_request/1,
|
|
|
|
unpack_request/1, make_init_reply/1, make_auth_reply/1,
|
|
|
|
make_reply/1, make_error_reply/1, make_error_reply/2]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
unpack_init_message(<<(?VERSION_5), N,
|
|
|
|
AuthMethodList:N/binary>>)
|
|
|
|
when N > 0, N < 256 ->
|
2006-10-28 04:04:55 +02:00
|
|
|
{ok, binary_to_list(AuthMethodList)};
|
2013-03-14 10:33:02 +01:00
|
|
|
unpack_init_message(_) -> error.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
unpack_auth_request(<<1, ULen, User:ULen/binary, PLen,
|
|
|
|
Pass:PLen/binary>>)
|
|
|
|
when ULen < 256, PLen < 256 ->
|
|
|
|
{(User), (Pass)};
|
|
|
|
unpack_auth_request(_) -> error.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
unpack_request(<<(?VERSION_5), CMD, RSV,
|
|
|
|
(?ATYP_DOMAINNAME), 40, SHA1:40/binary, 0, 0>>)
|
|
|
|
when CMD == (?CMD_CONNECT); CMD == (?CMD_UDP) ->
|
|
|
|
Command = if CMD == (?CMD_CONNECT) -> connect;
|
|
|
|
CMD == (?CMD_UDP) -> udp
|
2006-10-28 04:04:55 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
#s5_request{cmd = Command, rsv = RSV, sha1 = (SHA1)};
|
|
|
|
unpack_request(_) -> error.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
make_init_reply(Method) -> [?VERSION_5, Method].
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
make_auth_reply(true) -> [1, ?SUCCESS];
|
2007-06-27 13:05:14 +02:00
|
|
|
make_auth_reply(false) -> [1, ?ERR_NOT_ALLOWED].
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2008-07-24 20:03:28 +02:00
|
|
|
make_reply(#s5_request{rsv = RSV, sha1 = SHA1}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[?VERSION_5, ?SUCCESS, RSV, ?ATYP_DOMAINNAME,
|
|
|
|
byte_size(SHA1), SHA1, 0, 0].
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
make_error_reply(Request) ->
|
2007-06-27 13:05:14 +02:00
|
|
|
make_error_reply(Request, ?ERR_NOT_ALLOWED).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
make_error_reply(#s5_request{rsv = RSV, sha1 = SHA1},
|
|
|
|
Reason) ->
|
|
|
|
[?VERSION_5, Reason, RSV, ?ATYP_DOMAINNAME,
|
|
|
|
byte_size(SHA1), SHA1, 0, 0].
|