2019-02-08 16:37:02 +00:00
|
|
|
#pragma once
|
2019-02-08 13:28:39 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "net_tools.h"
|
|
|
|
|
2019-02-15 14:45:56 +00:00
|
|
|
enum socks5_state {
|
|
|
|
SOCKS5_STATE_NEW,
|
|
|
|
SOCKS5_STATE_ACK,
|
|
|
|
SOCKS5_STATE_RDY,
|
|
|
|
SOCKS5_STATE_ERR
|
|
|
|
};
|
|
|
|
|
2019-02-08 13:28:39 +00:00
|
|
|
enum cmd {
|
|
|
|
CMD_CONNECT = 0x01,
|
|
|
|
CMD_BIND = 0x02,
|
|
|
|
CMD_UDP_ASSOCIATE = 0x03
|
|
|
|
};
|
|
|
|
|
|
|
|
enum atyp {
|
|
|
|
ATYP_IPV4 = 0x01,
|
|
|
|
ATYP_DOMAINNAME = 0x03,
|
|
|
|
ATYP_IPV6 = 0x04
|
|
|
|
};
|
|
|
|
|
|
|
|
union socks5_addr {
|
|
|
|
struct {
|
|
|
|
uint8_t len;
|
|
|
|
char* str;
|
|
|
|
} dns;
|
|
|
|
uint8_t ipv4[4];
|
|
|
|
uint8_t ipv6[16];
|
|
|
|
};
|
|
|
|
|
2019-03-06 15:39:39 +00:00
|
|
|
enum socks5_rep {
|
|
|
|
SOCKS5_REP_SUCCESS,
|
|
|
|
SOCKS5_REP_GENERAL_FAILURE,
|
|
|
|
SOCKS5_REP_CONOTALLOWED,
|
|
|
|
SOCKS5_REP_NETUNREACH,
|
|
|
|
SOCKS5_REP_HOSTUNREACH,
|
|
|
|
SOCKS5_REP_COREFUSED,
|
|
|
|
SOCKS5_REP_TTLEXP,
|
|
|
|
SOCKS5_REP_CMDNOTSUP,
|
|
|
|
SOCKS5_REP_ADDRNOTSUP
|
|
|
|
};
|
|
|
|
|
2019-02-13 14:32:38 +00:00
|
|
|
static char* rep_msg[] = {
|
2019-02-12 20:45:15 +00:00
|
|
|
"Succeeded",
|
|
|
|
"General SOCKS server failure",
|
|
|
|
"Connection not allowed by ruleset",
|
|
|
|
"Network unreachable",
|
|
|
|
"Host unreachable",
|
|
|
|
"Connection refused",
|
|
|
|
"TTL expired",
|
|
|
|
"Command not supported",
|
|
|
|
"Address type not supported"
|
|
|
|
};
|
|
|
|
|
2019-02-08 13:28:39 +00:00
|
|
|
/*
|
|
|
|
* RFC 1928 Messages
|
|
|
|
* https://tools.ietf.org/html/rfc1928
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct client_handshake {
|
|
|
|
uint8_t ver;
|
|
|
|
uint8_t nmethods;
|
|
|
|
uint8_t methods[255];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct server_handshake {
|
|
|
|
uint8_t ver;
|
|
|
|
uint8_t method;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct client_request {
|
|
|
|
uint8_t ver;
|
|
|
|
uint8_t cmd;
|
|
|
|
uint8_t rsv;
|
|
|
|
uint8_t atyp;
|
|
|
|
uint8_t dst_addr_len;
|
|
|
|
char* dst_addr;
|
|
|
|
uint16_t port;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct server_reply {
|
|
|
|
uint8_t ver;
|
|
|
|
uint8_t rep;
|
|
|
|
uint8_t rsv;
|
|
|
|
uint8_t atyp;
|
|
|
|
union socks5_addr bind_addr;
|
|
|
|
uint16_t port;
|
|
|
|
};
|
|
|
|
|
2019-02-15 14:45:56 +00:00
|
|
|
int socks5_handshake_syn(int sock);
|
|
|
|
int socks5_handshake_ack(int sock);
|
2019-02-12 20:45:15 +00:00
|
|
|
int socks5_connect_dns(int sock, char* addr, uint16_t port);
|
|
|
|
int socks5_reply(int sock);
|
2019-03-06 15:39:39 +00:00
|
|
|
char* socks5_rep (enum socks5_rep rep);
|