写了一个加密的端口映射工具

写了一个加密的端口映射工具

目的

做某些你知道的事情

Features

加密方法

我们用了一个最简单的取反操作,这样的好处是加密和解密使用同一套代码

基本原理

这个工具只是做数据加密,一般要和第三方proxy一起使用,比如和socks代理一起用

用法:

代码

#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/thread.hpp>
#include <boost/asio/spawn.hpp>
#include <vector>
#include <assert.h>

using boost::asio::ip::tcp;

namespace {

    // ref: https://www.boost.org/doc/libs/1_74_0/doc/html/boost_asio/example/cpp03/spawn/echo_server.cpp

    class session : public boost::enable_shared_from_this<session>
    {
        std::string remote_url_;
        int  remote_port_;
    public:
        explicit session(boost::asio::io_context& io_context,
            std::string const& remote_url, int  remote_port)
            : io_context_(io_context),
            remote_url_(remote_url),
            remote_port_(remote_port),
            strand_(boost::asio::make_strand(io_context)),
            socket_(io_context),
            sock_remote_(io_context_)
        {
        }

        tcp::socket& socket()
        {
            return socket_;
        }

        void go()
        {
            boost::asio::spawn(strand_,
                boost::bind(&session::echo,
                    shared_from_this(), boost::placeholders::_1));
        }

    private:

        void echo(boost::asio::yield_context yield)
        {
            tcp::resolver::results_type endpoints;
            tcp::resolver resolver(io_context_);
            char port[100];
            sprintf(port, "%d", remote_port_);
            endpoints = resolver.async_resolve(tcp::v4(), remote_url_, port, yield);

            boost::asio::async_connect(sock_remote_, endpoints, yield);
            
            boost::asio::spawn(strand_,
                boost::bind(&session::copy_sock_1, shared_from_this(), boost::placeholders::_1));

            boost::asio::spawn(strand_,
                boost::bind(&session::copy_sock_2, shared_from_this(), boost::placeholders::_1));
        }

        void copy_sock_1(boost::asio::yield_context yield)
        {
            tcp::socket& a = socket_;
            tcp::socket& b = sock_remote_;
            copy_sock(a, b, yield);
        }

        void copy_sock_2(boost::asio::yield_context yield)
        {
            tcp::socket& b = socket_;
            tcp::socket& a = sock_remote_;
            copy_sock(a, b, yield);
        }

        static void encrypt(uint8_t data[], size_t len)
        {
            for (size_t i = 0; i < len; i++)
            {
                data[i] = ~data[i];
            }
        }
        static void copy_sock(tcp::socket& a, tcp::socket& b, boost::asio::yield_context yield)
        {
            std::cout << "copy_sock \n";
            boost::system::error_code ec;
            while (!ec)
            {
                uint8_t data[512];

                size_t len = a.async_read_some(boost::asio::buffer(data), yield[ec]);

                std::cout << "received " << len << " bytes\n";
                if (len > 0)
                {
                    encrypt(data, len);
                    b.async_write_some(boost::asio::buffer(data, len), yield[ec]);
                }
            }
        }
        boost::asio::io_context& io_context_;
        boost::asio::strand<boost::asio::io_context::executor_type> strand_;
        tcp::socket socket_;
        tcp::socket sock_remote_;
    };

    void do_accept(boost::asio::io_context& io_context, 
        int local_port, std::string const& remote_url, int  remote_port,
        boost::asio::yield_context yield)
    {
        tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), local_port));

        for (;;)
        {
            tcp::socket socket(io_context);
            boost::shared_ptr<session> new_session(new session(io_context, remote_url, remote_port));
            acceptor.async_accept(new_session->socket(), yield);
            new_session->go();
        }
    }

}   // namespace

void portTrans(int local_port, std::string const& remote_url, int  remote_port)
{
    
    boost::asio::io_context io_context;
        
    boost::asio::spawn(io_context,
        [&io_context, local_port, remote_url, remote_port](boost::asio::yield_context yield) {
        do_accept(io_context, local_port, remote_url, remote_port, yield);
    });

    io_context.run();
    
}

Powered by Jekyll and Theme by solid

本站总访问量