nop_90

Just a proof of concept.
my

python

  proxy blows up when you try and have 5-10 socks open at once Applause.
I will improve this one hopefully.

Intresting part about

erlang

  u can recompile and reload the code while server is running.


-module(proxyserver).
-export([start/0]).

start() ->
    {ok,Listen} = gen_tcp:listen(8000,
[binary,
  {active, true}]),
    spawn(fun() -> par_connect(Listen) end).

par_connect(Listen) ->
    {ok, Socket} = gen_tcp:accept(Listen),
    spawn(fun() -> par_connect(Listen) end),
    loop(Socket).

parse_header(Headers) ->
    dict:erase("Proxy-Connection",
dict:from_list(
  lists:map(
    fun(Header)->
    {ok,[Key,Value]}=

regex

 p:split(Header,": "Applause,
    {Key,Value}
    end
    ,
    Headers
  )))
.

split_payload(Bin,Headers)->
    case Bin of
[$ ,$ ,$ ,$ |Rest] ->
    {lists:reverse(Headers),Rest};
[H|Rest]->
    split_payload(Rest,[H|Headers]);
[] -> error
    end
    .

get_url(Bin) ->
    {Lines,Payload} = split_payload(binary_to_list(Bin),[]),
    [Request|Headers]= string:tokens(Lines," "Applause,
    HeaderDict = parse_header(Headers),
    %io:format("Host  = ~p~n" ,[dict:fetch("Host",HeaderDict)]),
    {ok,Socket} = gen_tcp:connect(dict:fetch("Host",HeaderDict),80,[binary, {packet, 0}]),
    ok = gen_tcp:send(Socket, Request++" " ),
    lists:foreach(fun({Key,Value})->
    ok = gen_tcp:send(Socket,Key++": "++Value++" "Applause
    end,
    dict:to_list(HeaderDict)),
    ok = gen_tcp:send(Socket,"Connection: close "Applause,
    ok = gen_tcp:send(Socket," " ),
    ok = gen_tcp:send(Socket,Payload),
    receive_data(Socket,[]).

receive_data(Socket,SoFar) ->
      receive
    {tcp,Socket,Bin} ->
          receive_data(Socket, [Bin|SoFar]);
    {tcp_closed,Socket} ->
        list_to_binary(lists:reverse(SoFar))
  end.

loop(Socket) ->
    receive
{tcp, Socket, Bin} ->
    gen_tcp:send(Socket,get_url(Bin)),
    loop(Socket);
{tcp_closed, Socket} ->
    ok
    end.

perkiset

Hey Nop - provided you find this language intriguing enough to continue, would you mind posting a bit on getting/configuring/compiling

erlang

  and gotchas that anyone might want to watch out for? Also, is it a "true" compile, or is it a pcode thang...? Is the code a standalone executable or does it need some type of RTL? Just how tweaked does a box need to be to make use of the language?

Thanks!
/p

nop_90

This is just off the top of my head. And i am still newbie
Basically to install an

erlang

  system on a

linux

  box it is as simple as downloading the sources and doing a ./configure make etc

It has a compiler that makes .beam files
these can be either made from like a make file (I have not figured out how to do that yet).
Or from its console.

In the above example you would start erl in same directory as proxyserver. (file should be named proxyserver.erl)

Erlang

  (BEAM) emulator version 5.5.4 [source] [async-threads:0] [hipe] [kernel-poll:false]
2> c(proxyserver). <--- this compiles the file
{ok,proxyserver}
3> proxyserver:start(). this calls the function start
<0.42.0>
4>

but the intresting part is that if i do changes to code
and recompile, changes are automatically made in server without restarting.

Also the language is "functional"
That means it require different way to solve problem,
for starter you can not reassign variable.
you rely on recursion heavily, but on the positive side you no longer will have logic bugs caused by side effects

Theoretically functional languages should scale better for multithreaded/multi

mac

 hine systems because u do not worry about global memory/ mutexes etc.

If i do endup using

erlang

  for something it will be because of its better performance. (If it really has that Applause).
Either way it is a

learn

 ing  experience.


Perkiset's Place Home   Politics @ Perkiset's