Guest User

Untitled

a guest
Jan 31st, 2019
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. //
  2. // socks5.hpp
  3. //
  4.  
  5. #ifndef SOCKS5_HPP
  6. #define SOCKS5_HPP
  7.  
  8. #include <array>
  9. #include <string>
  10. #include <boost/asio/buffer.hpp>
  11. #include <boost/asio/ip/tcp.hpp>
  12. #include <sstream>
  13. #include <iostream>
  14.  
  15. namespace socks5 {
  16.  
  17. enum addr_type {
  18.     IPv4 = 0x01,
  19.     Domain = 0x03,
  20.     IPv6 = 0x04
  21. };
  22.  
  23. const unsigned char version = 0x05;
  24. const unsigned char password_ver = 0x01;
  25.  
  26. const unsigned char num_auth_supported = 1;
  27.  
  28. const unsigned char NO_AUTH = 0x00;
  29. const unsigned char PASSWORD = 0x02;
  30.  
  31. unsigned char null_byte = 0;
  32.  
  33. class request {
  34.  
  35.     public:
  36.  
  37.         enum command {
  38.             connect = 0x01,
  39.             bind = 0x02,
  40.             assoc_port = 0x03
  41.         };
  42.  
  43.         std::array<boost::asio::const_buffer, 4> greeting() {
  44.             return {
  45.                 {
  46.                     boost::asio::buffer(&version, 1),
  47.                     boost::asio::buffer(&num_auth_supported, 1),
  48.                     boost::asio::buffer(&NO_AUTH, 1),
  49.                     //boost::asio::buffer(&PASSWORD, 1)
  50.                 }
  51.             };
  52.         }
  53.  
  54.         std::array<boost::asio::const_buffer, 5> password(std::string username, std::string password) {
  55.             int usernameLen = username.length();
  56.             int passwordLen = password.length();
  57.  
  58.             return {
  59.                 {
  60.                     boost::asio::buffer(&password_ver, 1),
  61.                     boost::asio::buffer(&usernameLen, 1),
  62.                     boost::asio::buffer(username, 255),
  63.                     boost::asio::buffer(&passwordLen, 1),
  64.                     boost::asio::buffer(password, 255)
  65.                 }
  66.             };
  67.         }
  68.        
  69.         std::vector<boost::asio::const_buffer> conn(unsigned char cmd, unsigned char type, std::string hostname, int port) {
  70.             int cmd_ = 0x04;
  71.             int type_ = 0x03;
  72.             std::vector<boost::asio::const_buffer> buffers = {
  73.                 //{
  74.                     boost::asio::buffer(&version, 1),
  75.                     boost::asio::buffer(&cmd_, 1),
  76.                     boost::asio::buffer(&null_byte, 1),
  77.                     boost::asio::buffer(&type_, 1)
  78.                 //}
  79.             };
  80.  
  81.             std::cout << "Conn:" << cmd << std::endl << "type:" << type << std::endl;
  82.  
  83.             if (type_ == IPv4) {
  84.                 int byte1=0x5d, byte2=0xb8, byte3=0xd8, byte4=0x22;
  85.                 char dot;
  86.                 std::istringstream s(hostname);  // input stream that now contains the ip address string
  87.  
  88.                 //s >> byte1 >> dot >> byte2 >> dot >> byte3 >> dot >> byte4 >> dot;
  89.                 // buffers.push_back(boost::asio::buffer({
  90.                 //  (unsigned char)byte1,
  91.                 //  (unsigned char)byte2,
  92.                 //  (unsigned char)byte3,
  93.                 //  (unsigned char)byte4
  94.                 // }));
  95.                 buffers.push_back(boost::asio::buffer(&byte1, 1)); 
  96.                 buffers.push_back(boost::asio::buffer(&byte2, 1));         
  97.                 buffers.push_back(boost::asio::buffer(&byte3, 1));         
  98.                 buffers.push_back(boost::asio::buffer(&byte4, 1));         
  99.  
  100.             } else if (type_ == IPv6) {
  101.                 int byte1,
  102.                     byte2,
  103.                     byte3,
  104.                     byte4,
  105.                     byte5,
  106.                     byte6,
  107.                     byte7,
  108.                     byte8,
  109.                     byte9,
  110.                     byte10,
  111.                     byte11,
  112.                     byte12,
  113.                     byte13,
  114.                     byte14,
  115.                     byte15,
  116.                     byte16;
  117.                 char sep;
  118.                 std::istringstream s(hostname);  // input stream that now contains the ip address string
  119.  
  120.                 s >>
  121.                     byte1 >> sep >>
  122.                     byte2 >> sep >>
  123.                     byte3 >> sep >>
  124.                     byte4 >> sep >>
  125.                     byte5 >> sep >>
  126.                     byte6 >> sep >>
  127.                     byte7 >> sep >>
  128.                     byte8 >> sep >>
  129.                     byte9 >> sep >>
  130.                     byte10 >> sep >>
  131.                     byte11 >> sep >>
  132.                     byte12 >> sep >>
  133.                     byte13 >> sep >>
  134.                     byte14 >> sep >>
  135.                     byte15 >> sep >>
  136.                     byte16;
  137.  
  138.                 buffers.push_back(boost::asio::buffer({
  139.                     (unsigned char)byte1,
  140.                     (unsigned char)byte2,
  141.                     (unsigned char)byte3,
  142.                     (unsigned char)byte4,
  143.                     (unsigned char)byte5,
  144.                     (unsigned char)byte6,
  145.                     (unsigned char)byte7,
  146.                     (unsigned char)byte8,
  147.                     (unsigned char)byte9,
  148.                     (unsigned char)byte10,
  149.                     (unsigned char)byte11,
  150.                     (unsigned char)byte12,
  151.                     (unsigned char)byte13,
  152.                     (unsigned char)byte14,
  153.                     (unsigned char)byte15,
  154.                     (unsigned char)byte16
  155.                 }));
  156.             } else if (type_ == Domain) {
  157.                 int hostnameLen = hostname.length();
  158.                 buffers.push_back(boost::asio::buffer(&hostnameLen, 1));
  159.                 buffers.push_back(boost::asio::buffer(hostname, 255));
  160.  
  161.                 std::cout << hostnameLen << std::endl << hostname << std::endl;
  162.             }
  163.  
  164.             unsigned char port_high_byte_ = (port >> 8) & 0xff;
  165.             unsigned char port_low_byte_ = port & 0xff;
  166.  
  167.             buffers.push_back(boost::asio::buffer(&port_high_byte_, 1));
  168.             buffers.push_back(boost::asio::buffer(&port_low_byte_, 1));
  169.  
  170.             std::cout << buffers[0].data();
  171.             return buffers;
  172.         }          
  173.  
  174. };
  175.  
  176. class reply {
  177.  
  178.     private:
  179.         int hostnameLen;
  180.         int hostname;
  181.         int port_nbo;
  182.  
  183.         enum state {
  184.             AUTH_CHOICE = 0,
  185.             PASSWORD = 1,
  186.             CONNECT = 2
  187.         };
  188.  
  189.         state state;
  190.  
  191.     public:
  192.  
  193.         unsigned char ver;
  194.         unsigned char auth;
  195.         unsigned char auth_ver;
  196.         unsigned char status_;
  197.         addr_type type;
  198.  
  199.         bool success() {
  200.             return ver == version &&
  201.                 (state == AUTH_CHOICE ? state == NO_AUTH || state == PASSWORD : true) &&
  202.                 (state == PASSWORD ? status_ == 0x00 : true) &&
  203.                 (state == CONNECT ? status_ == 0x00 : true);
  204.         }
  205.  
  206.         std::string status() const {
  207.             if (state == AUTH_CHOICE) {
  208.                 if (auth == 0xFF) {
  209.                     return "No acceptable auth methods";
  210.                 }
  211.             } else if (state == PASSWORD) {
  212.                 return status_ == 0x00 ? "Auth success" : "Auth error: "+std::to_string(status_);
  213.             } else if (state == CONNECT) {
  214.                 return status_ == 0x00 ? "Connect success" : "Connect error: "+std::to_string(status_);
  215.             }
  216.             return "";
  217.         }
  218.  
  219.         std::array<boost::asio::mutable_buffer, 5> auth_choice() {
  220.             state = AUTH_CHOICE;
  221.             return {
  222.                 {
  223.                     boost::asio::buffer(&ver, 1),
  224.                     boost::asio::buffer(&auth, 1)
  225.                 }
  226.             };
  227.         }
  228.  
  229.         std::array<boost::asio::mutable_buffer, 5> password_verif() {
  230.             state = PASSWORD;
  231.             return {
  232.                 {
  233.                     boost::asio::buffer(&auth_ver, 1),
  234.                     boost::asio::buffer(&status_, 1)
  235.                 }
  236.             };
  237.         }
  238.  
  239.         std::vector<boost::asio::mutable_buffer> connect_reply() {
  240.             state = CONNECT;
  241.             std::vector<boost::asio::mutable_buffer> buffers = {
  242.                 {
  243.                     boost::asio::buffer(&ver, 1),
  244.                     boost::asio::buffer(&status_, 1),
  245.                     boost::asio::buffer(&null_byte, 1),
  246.                     boost::asio::buffer(&type, 1)
  247.                 }
  248.             };
  249.  
  250.             if (type == IPv4) {
  251.                 buffers.push_back(boost::asio::buffer(&hostname, 4));
  252.             } else if (type == IPv6) {
  253.                 buffers.push_back(boost::asio::buffer(&hostname, 16));
  254.             } else if (type == Domain) {
  255.                 buffers.push_back(boost::asio::buffer(&hostnameLen, 1));
  256.                 buffers.push_back(boost::asio::buffer(&hostname, 255));
  257.             }
  258.  
  259.             buffers.push_back(boost::asio::buffer(&port_nbo, 2));
  260.  
  261.             return buffers;
  262.         }      
  263. };
  264.  
  265. } // namespace socks5
  266.  
  267. #endif // SOCKS5_HPP
Add Comment
Please, Sign In to add comment