Git Product home page Git Product logo

erl-tcpspy's Introduction

erl-tcpspy

Another erlang proxy, from a gist

-module(generic_proxy).

-export([run/0]).

-define(PORT_FROM, 63790). -define(PORT_TO, 6379). -define(BACKLOG, 10000).

run() -> {ok, Socket} = gen_tcp:listen(0, [ {backlog, ?BACKLOG}, {reuseaddr, true}, {port, ?PORT_FROM}, binary, {packet, 0}, {active, true} ]), accept(Socket).

accept(Socket) -> {ok, ClientSocket} = gen_tcp:accept(Socket), Handler = spawn(fun() -> receive go -> go end, {ok, TargetSocket} = gen_tcp:connect("127.0.0.1", ?PORT_TO, [ binary, {nodelay, true}, {packet, 0}, {active, true} ]), loop(TargetSocket, ClientSocket) end), gen_tcp:controlling_process(ClientSocket, Handler), Handler ! go, accept(Socket).

loop(TargetSocket, ClientSocket) -> Continue = receive {tcp, TargetSocket, Data} -> gen_tcp:send(ClientSocket, Data), true; {tcp, ClientSocket, Data} -> gen_tcp:send(TargetSocket, Data), true; {tcp_closed, _} -> gen_tcp:close(TargetSocket), gen_tcp:close(ClientSocket), false; X -> io:format("Unknown msg: ~p", [X]), gen_tcp:close(TargetSocket), gen_tcp:close(ClientSocket), false end, case Continue of true -> loop(TargetSocket, ClientSocket); false -> ok end.

erl-tcpspy's People

Contributors

begoon avatar nbrandaleone avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.