anchor.diff: -%% 简单高速加密的 socks5 代理服务器
  -%%
  -%% Client - 客户端：如浏览器
  -%% Front  - 代理前端：从浏览器等客户端接收数据，简单加密后转发给后端
  -%% Back   - 代理后端：从前端接收数据，解密后等同于Socks5服务器
  -%% Remote - 远程服务器：需要请求的目标服务器
  -%%
  -%% Client 与 Front  为本地通信
  -%% Back   与 Remote 为本地通信
  -%% Front  与 Back   为异地加密通信
  -%%
  -%% 加密算法为 字节 异或 0x66 (01100110)
  -
  +%%% 支持Socks5协议的高速加密通信的代理服务器脚本
   -module(proxy).
   -export([
       start/0
   ]).
   
   -export([
  -    back_start/0,
  +    back_start/0, back_start/1,
       back_accept/1,
       back_process/1,
   
  -    front_start/0,
  -    front_accept/1,
  -    front_process/1,
  -    forward/4,
  +    front_start/1,
  +    front_accept/3,
  +    front_process/3,
  +    forward/3,
  +
       flip/1,
       flip_recv/2, flip_recv/3,
  -    flip_send/2
  +    flip_send/2,
  +
  +    heart/0
   ]).
   
  --define(FRONT_PORT, 5050).
  --define(BACK_PORT, 5051).
  +-define(FRONT_PORT, '8780').
  +-define(BACK_PORT, '8781').
   -define(CONNECT_TIMEOUT, 5000).
  +
  +-define(PASSWORD, "abcd1234").
  +-define(PASSWORD_LENGTH, length(?PASSWORD)).
   
   %% socks constants
   -define(IPV4, 1).
  
positive.code:                 gen_tcp:close(Remote)
          end
      catch
          _:_ ->
              gen_tcp:close(Front)
      end.
  
  
  %% front，与back连接采用自定义协议，与客户端连接采用socks5
  %% client -- front，socks5 接收
  %% front -- back，Flip
  %% back -- front，Flip
  %% front -- client，Raw
  front_start() ->
      {ok, Socket} = gen_tcp:listen(?FRONT_PORT, [{reuseaddr, true},
                                                  {active, false},
                                                  binary]),
      front_accept(Socket).
  
  front_accept(Socket) ->
      {ok, Client} = gen_tcp:accept(Socket),
      spawn(?MODULE, front_process, [Client]),
      front_accept(Socket).
  
  front_process(Client) ->
      try
          From = self(),
  
          {ok, Back} = gen_tcp:connect("127.0.0.1", ?BACK_PORT, [{active, false},
                                                                   binary]),
          flip_send(Back, <<"abcd1234">>),
          {ok, <<"ok">>} = flip_recv(Back, 2),
  
          spawn(?MODULE, forward, [flip_send, Client, Back, From]),
          spawn(?MODULE, forward, [flip_recv, Back, Client, From]),
  
          receive
              {close} ->
  
negative.code:             {ok, Address} = inet:getaddr(Domain, inet),
              {ok, <<Port:16>>} = flip_recv(Client, 2)
      end,
      {ok, Remote} = gen_tcp:connect(Address,
                                     Port,
                                     [{active, false}, binary],
                                     ?CONNECT_TIMEOUT),
      ok = flip_send(Client, <<5, 0, 0, 1, 0:32, 0:16 >>),
      {ok, Remote}.
  
  start() ->
      spawn(?MODULE, back_start, [['0.0.0.0', '8781']]),
      spawn(?MODULE, front_start, [['127.0.0.1', '8781', '127.0.0.1', '8780']]),
      receive
          {close} ->
              exit({done})
      end.
  