Commit 81933021 authored by Бобби's avatar Бобби Committed by Cesanta Bot

Initial socks5 client & server implementation

PUBLISHED_FROM=05d3cca6223c963e7ae89dde3628fa8fad46e6bd
parent 6e3e5560
PROG = socks_server
# NOTE(lsm): -DMG_ENABLE_IPV6 won't work for the mingw build
MODULE_CFLAGS = -DMG_ENABLE_SOCKS=1 # -DMG_ENABLE_DEBUG=1
#SSL_LIB=openssl
include ../examples.mk
/*
* Copyright (c) 2017 Cesanta Software Limited
* All rights reserved
*
* Use curl to test, e.g.
* curl -i --socks5 127.0.0.1:1080 www.met.ie
*/
#include "mongoose.h"
static const char *s_listening_addr = "1080";
int main(void) {
struct mg_mgr mgr;
struct mg_connection *c;
mg_mgr_init(&mgr, NULL);
if ((c = mg_bind(&mgr, s_listening_addr, NULL)) == NULL) {
fprintf(stderr, "mg_bind(%s) failed\n", s_listening_addr);
exit(EXIT_FAILURE);
}
mg_set_protocol_socks(c);
printf("Starting socks5 proxy server on %s\n", s_listening_addr);
for (;;) {
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}
This diff is collapsed.
...@@ -3029,6 +3029,10 @@ struct { \ ...@@ -3029,6 +3029,10 @@ struct { \
#define MG_ENABLE_MQTT 1 #define MG_ENABLE_MQTT 1
#endif #endif
#ifndef MG_ENABLE_SOCKS
#define MG_ENABLE_SOCKS 0
#endif
#ifndef MG_ENABLE_MQTT_BROKER #ifndef MG_ENABLE_MQTT_BROKER
#define MG_ENABLE_MQTT_BROKER 0 #define MG_ENABLE_MQTT_BROKER 0
#endif #endif
...@@ -6032,3 +6036,69 @@ struct mg_connection *mg_sntp_get_time(struct mg_mgr *mgr, ...@@ -6032,3 +6036,69 @@ struct mg_connection *mg_sntp_get_time(struct mg_mgr *mgr,
#endif #endif
#endif /* CS_MONGOOSE_SRC_SNTP_H_ */ #endif /* CS_MONGOOSE_SRC_SNTP_H_ */
#ifdef MG_MODULE_LINES
#line 1 "mongoose/src/socks.h"
#endif
/*
* Copyright (c) 2017 Cesanta Software Limited
* All rights reserved
*/
#ifndef CS_MONGOOSE_SRC_SOCKS_H_
#define CS_MONGOOSE_SRC_SOCKS_H_
#if MG_ENABLE_SOCKS
#define MG_SOCKS_VERSION 5
/* SOCKS5 handshake methods */
enum mg_socks_handshake_method {
MG_SOCKS_HANDSHAKE_NOAUTH = 0, /* Handshake method - no authentication */
MG_SOCKS_HANDSHAKE_GSSAPI = 1, /* Handshake method - GSSAPI auth */
MG_SOCKS_HANDSHAKE_USERPASS = 2, /* Handshake method - user/password auth */
MG_SOCKS_HANDSHAKE_FAILURE = 0xff, /* Handshake method - failure */
};
/* SOCKS5 commands */
enum mg_socks_command {
MG_SOCKS_CMD_CONNECT = 1, /* Command: CONNECT */
MG_SOCKS_CMD_BIND = 2, /* Command: BIND */
MG_SOCKS_CMD_UDP_ASSOCIATE = 3, /* Command: UDP ASSOCIATE */
};
/* SOCKS5 address types */
enum mg_socks_address_type {
MG_SOCKS_ADDR_IPV4 = 1, /* Address type: IPv4 */
MG_SOCKS_ADDR_DOMAIN = 3, /* Address type: Domain name */
MG_SOCKS_ADDR_IPV6 = 4, /* Address type: IPv6 */
};
/* SOCKS5 response codes */
enum mg_socks_response {
MG_SOCKS_SUCCESS = 0, /* Response: success */
MG_SOCKS_FAILURE = 1, /* Response: failure */
MG_SOCKS_NOT_ALLOWED = 2, /* Response: connection not allowed */
MG_SOCKS_NET_UNREACHABLE = 3, /* Response: network unreachable */
MG_SOCKS_HOST_UNREACHABLE = 4, /* Response: network unreachable */
MG_SOCKS_CONN_REFUSED = 5, /* Response: network unreachable */
MG_SOCKS_TTL_EXPIRED = 6, /* Response: network unreachable */
MG_SOCKS_CMD_NOT_SUPPORTED = 7, /* Response: network unreachable */
MG_SOCKS_ADDR_NOT_SUPPORTED = 8, /* Response: network unreachable */
};
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* Turn the connection into the SOCKS server */
void mg_set_protocol_socks(struct mg_connection *c);
/* Create socks tunnel for the client connection */
struct mg_iface *mg_socks_mk_iface(struct mg_mgr *, const char *proxy_addr);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment