erlang网络库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.2 KiB

  1. -module(ntPptAcceptor).
  2. -include("eNet.hrl").
  3. -include("ntCom.hrl").
  4. -include("proxyPt.hrl").
  5. -compile(inline).
  6. -compile({inline_size, 128}).
  7. -export([
  8. start_link/7
  9. , pptAndHS/5
  10. , init/1
  11. , handleMsg/2
  12. , init_it/2
  13. , system_code_change/4
  14. , system_continue/3
  15. , system_get_state/1
  16. , system_terminate/4
  17. ]).
  18. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% genActor start %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  19. -spec start_link(list(), timeout(), boolean(), timeout(), inet:socket(), module(), [proc_lib:spawn_option()]) -> {ok, pid()}.
  20. start_link(SslOpts, SslHSTet, ProxyPt, ProxyPtTet, LSock, ConMod, SpawnOpts) ->
  21. proc_lib:start_link(?MODULE, init_it, [self(), {SslOpts, SslHSTet, ProxyPt, ProxyPtTet, LSock, ConMod}], infinity, SpawnOpts).
  22. init_it(Parent, Args) ->
  23. process_flag(trap_exit, true),
  24. modInit(Parent, Args).
  25. -spec system_code_change(term(), module(), undefined | term(), term()) -> {ok, term()}.
  26. system_code_change(State, _Module, _OldVsn, _Extra) ->
  27. {ok, State}.
  28. -spec system_continue(pid(), [], {module(), atom(), pid(), term()}) -> ok.
  29. system_continue(_Parent, _Debug, {Parent, State}) ->
  30. loop(Parent, State).
  31. -spec system_get_state(term()) -> {ok, term()}.
  32. system_get_state(State) ->
  33. {ok, State}.
  34. -spec system_terminate(term(), pid(), [], term()) -> none().
  35. system_terminate(Reason, _Parent, _Debug, _State) ->
  36. exit(Reason).
  37. modInit(Parent, Args) ->
  38. case init(Args) of
  39. {ok, State} ->
  40. proc_lib:init_ack(Parent, {ok, self()}),
  41. loop(Parent, State);
  42. {stop, Reason} ->
  43. proc_lib:init_ack(Parent, {error, Reason}),
  44. exit(Reason)
  45. end.
  46. loop(Parent, State) ->
  47. receive
  48. {system, From, Request} ->
  49. sys:handle_system_msg(Request, From, Parent, ?MODULE, [], {Parent, State});
  50. {'EXIT', Parent, Reason} ->
  51. exit(Reason);
  52. Msg ->
  53. case handleMsg(Msg, State) of
  54. {ok, NewState} ->
  55. loop(Parent, NewState);
  56. {stop, Reason} ->
  57. exit(Reason)
  58. end
  59. end.
  60. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% genActor end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  61. -record(state, {
  62. lSock
  63. , sslOpts
  64. , sslHSTet
  65. , proxyPt
  66. , proxyPtTet
  67. , ref
  68. , conMod
  69. , sockMod
  70. }).
  71. -spec init(Args :: term()) -> ok.
  72. init({SslOpts, SslHSTet, ProxyPt, ProxyPtTet, LSock, ConMod}) ->
  73. case prim_inet:async_accept(LSock, -1) of
  74. {ok, Ref} ->
  75. {ok, SockMod} = inet_db:lookup_socket(LSock),
  76. {ok, #state{lSock = LSock, sslOpts = SslOpts, sslHSTet = SslHSTet, proxyPt = ProxyPt, proxyPtTet = ProxyPtTet, ref = Ref, conMod = ConMod, sockMod = SockMod}};
  77. {error, Reason} ->
  78. ?ntErr("init prim_inet:async_accept error ~p~n", [Reason]),
  79. {stop, Reason}
  80. end.
  81. handleMsg({inet_async, LSock, Ref, Msg}, #state{lSock = LSock, sslOpts = SslOpts, sslHSTet = SslHSTet, proxyPt = ProxyPt, proxyPtTet = ProxyPtTet, ref = Ref, conMod = ConMod, sockMod = SockMod} = State) ->
  82. case Msg of
  83. {ok, Sock} ->
  84. %% make it look like gen_tcp:accept
  85. inet_db:register_socket(Sock, SockMod),
  86. try ConMod:newConn(Sock) of
  87. {ok, Pid} ->
  88. gen_tcp:controlling_process(Sock, Pid),
  89. Pid ! {?mSockReady, Sock, SslOpts, SslHSTet, ProxyPt, ProxyPtTet},
  90. newAsyncAccept(LSock, State);
  91. {close, Reason} ->
  92. ?ntErr("handleMsg ConMod:newAcceptor return close ~p~n", [Reason]),
  93. catch port_close(Sock),
  94. newAsyncAccept(LSock, State);
  95. _Ret ->
  96. ?ntErr("ConMod:newAcceptor return error ~p~n", [_Ret]),
  97. {stop, error_ret}
  98. catch
  99. E:R:S ->
  100. ?ntErr("CliMod:newConnect crash: ~p:~p~n~p~n ~n ", [E, R, S]),
  101. newAsyncAccept(LSock, State)
  102. end;
  103. {error, closed} ->
  104. % ?ntErr("error, closed listen sock error ~p~n", [closed]),
  105. {stop, normal};
  106. {error, Reason} ->
  107. ?ntErr("listen sock error ~p~n", [Reason]),
  108. {stop, {lsock, Reason}}
  109. end;
  110. handleMsg(_Msg, State) ->
  111. ?ntErr("~p receive unexpected ~p msg: ~p", [?MODULE, self(), _Msg]),
  112. {ok, State}.
  113. newAsyncAccept(LSock, State) ->
  114. case prim_inet:async_accept(LSock, -1) of
  115. {ok, Ref} ->
  116. {ok, State#state{ref = Ref}};
  117. {error, Reason} ->
  118. ?ntErr("~p prim_inet:async_accept error ~p~n", [?MODULE, Reason]),
  119. {stop, Reason}
  120. end.
  121. pptAndHS(OSock, SslOpts, SslHSTet, ProxyPt, ProxyPtTet) ->
  122. PptRet =
  123. case ProxyPt of
  124. true ->
  125. nt_proxy_protocol:recv(OSock, ProxyPtTet);
  126. _ ->
  127. {ok, OSock, #proxy_socket{}}
  128. end,
  129. case PptRet of
  130. {ok, TemSock, ProxySock} ->
  131. case SslOpts /= undefined of
  132. true ->
  133. case ssl:handshake(TemSock, SslOpts, SslHSTet) of
  134. {ok, SslSock} ->
  135. {ok, SslSock, ProxySock};
  136. {ok, SslSock, _Ext} -> %% OTP 21.0
  137. {ok, SslSock, ProxySock};
  138. {error, _} = Err -> Err
  139. end;
  140. _ ->
  141. PptRet
  142. end;
  143. _ ->
  144. PptRet
  145. end.