Skip to content

Commit

Permalink
Refactor codes to remove redundancies
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Dec 3, 2024
1 parent 5f97a8a commit 2c8f4ae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 46 deletions.
69 changes: 24 additions & 45 deletions ip2proxy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ getpackageversion() ->
end.

getmoduleversion() ->
"3.3.2".
"3.3.3".

getdatabaseversion() ->
case ets:info(mymeta) of
Expand Down Expand Up @@ -389,37 +389,49 @@ searchtree(S, Ipnum, Dbtype, Low, High, BaseAddr, Colsize, Iptype, Mode) ->
end.

search4(S, Ipnum, Dbtype, Low, High, Baseaddr, Indexbaseaddr, Colsize, Mode) ->
if
Ipnum == 4294967295 ->
Ipnum2 = Ipnum - 1;
true ->
Ipnum2 = Ipnum
end,
if
Indexbaseaddr > 0 ->
Indexpos = ((Ipnum bsr 16) bsl 3) + Indexbaseaddr,
Indexpos = ((Ipnum2 bsr 16) bsl 3) + Indexbaseaddr,
case file:pread(S, Indexpos - 1, 8) of % 4 bytes for each IP From & IP To
eof ->
io:format("Error: IP address not found.~n", []),
{}; % return empty
{ok, R} ->
Low2 = readuint32row(R, 0),
High2 = readuint32row(R, 4),
searchtree(S, Ipnum, Dbtype, Low2, High2, Baseaddr, Colsize, ipv4, Mode)
searchtree(S, Ipnum2, Dbtype, Low2, High2, Baseaddr, Colsize, ipv4, Mode)
end;
true ->
searchtree(S, Ipnum, Dbtype, Low, High, Baseaddr, Colsize, ipv4, Mode)
searchtree(S, Ipnum2, Dbtype, Low, High, Baseaddr, Colsize, ipv4, Mode)
end.

search6(S, Ipnum, Dbtype, Low, High, Baseaddr, Indexbaseaddr, Colsize, Mode) ->
if
Ipnum == 340282366920938463463374607431768211455 ->
Ipnum2 = Ipnum - 1;
true ->
Ipnum2 = Ipnum
end,
if
Indexbaseaddr > 0 ->
Indexpos = ((Ipnum bsr 112) bsl 3) + Indexbaseaddr,
Indexpos = ((Ipnum2 bsr 112) bsl 3) + Indexbaseaddr,
case file:pread(S, Indexpos - 1, 8) of % 4 bytes for each IP From & IP To
eof ->
io:format("Error: IP address not found.~n", []),
{}; % return empty
{ok, R} ->
Low2 = readuint32row(R, 0),
High2 = readuint32row(R, 4),
searchtree(S, Ipnum, Dbtype, Low2, High2, Baseaddr, Colsize, ipv6, Mode)
searchtree(S, Ipnum2, Dbtype, Low2, High2, Baseaddr, Colsize, ipv6, Mode)
end;
true ->
searchtree(S, Ipnum, Dbtype, Low, High, Baseaddr, Colsize, ipv6, Mode)
searchtree(S, Ipnum2, Dbtype, Low, High, Baseaddr, Colsize, ipv6, Mode)
end.

getall(Ip) ->
Expand Down Expand Up @@ -522,53 +534,20 @@ query(Ip, Mode) ->
Result = case inet:parse_address(Ip) of
{ok, {X1, X2, X3, X4}} ->
Ipnum = (X1 bsl 24) + (X2 bsl 16) + (X3 bsl 8) + (X4),
if
Ipnum == 4294967295 ->
Ipnum2 = Ipnum - 1;
true ->
Ipnum2 = Ipnum
end,
search4(S, Ipnum2, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize, Mode);
search4(S, Ipnum, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize, Mode);
{ok, {X1, X2, X3, X4, X5, X6, X7, X8}} ->
Ipnum = (X1 bsl 112) + (X2 bsl 96) + (X3 bsl 80) + (X4 bsl 64) + (X5 bsl 48) + (X6 bsl 32) + (X7 bsl 16) + X8,
if
Ipnum >= Fromv4mapped andalso Ipnum =< Tov4mapped ->
Ipnum2 = Ipnum - Fromv4mapped,
if
Ipnum2 == 4294967295 ->
Ipnum3 = Ipnum2 - 1;
true ->
Ipnum3 = Ipnum2
end,
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize, Mode);
search4(S, (Ipnum - Fromv4mapped), Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize, Mode);
Ipnum >= From6to4 andalso Ipnum =< To6to4 ->
Ipnum2 = (Ipnum bsr 80) band Last32bits,
if
Ipnum2 == 4294967295 ->
Ipnum3 = Ipnum2 - 1;
true ->
Ipnum3 = Ipnum2
end,
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize, Mode);
search4(S, ((Ipnum bsr 80) band Last32bits), Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize, Mode);
Ipnum >= Fromteredo andalso Ipnum =< Toteredo ->
Ipnum2 = (bnot Ipnum) band Last32bits,
if
Ipnum2 == 4294967295 ->
Ipnum3 = Ipnum2 - 1;
true ->
Ipnum3 = Ipnum2
end,
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize, Mode);
search4(S, ((bnot Ipnum) band Last32bits), Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize, Mode);
true ->
if
Ipv6databasecount > 0 ->
if
Ipnum == 340282366920938463463374607431768211455 ->
Ipnum2 = Ipnum - 1;
true ->
Ipnum2 = Ipnum
end,
search6(S, Ipnum2, Databasetype, 0, Ipv6databasecount, Ipv6databaseaddr, Ipv6indexbaseaddr, Ipv6columnsize, Mode);
search6(S, Ipnum, Databasetype, 0, Ipv6databasecount, Ipv6databaseaddr, Ipv6indexbaseaddr, Ipv6columnsize, Mode);
true ->
#ip2proxyrecord{
country_short = Z,
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule IP2ProxyErlang.MixProject do
def project() do
[
app: :ip2proxy_erlang,
version: "3.3.2",
version: "3.3.3",
elixir: "~> 1.0",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand Down

0 comments on commit 2c8f4ae

Please sign in to comment.