Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
brechtsanders committed Apr 17, 2017
1 parent 9d5e553 commit 60f71c8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 43 deletions.
4 changes: 3 additions & 1 deletion Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

2017-04-17 Brecht Sanders https://github.com/brechtsanders/

* proxysocket_connect with proxy set to NULL makes direct connection
* proxysocket_connect() with proxy set to NULL makes direct connection
* added proxysocketconfig_get_type_name() to get name of proxy type
* added proxysocketconfig_get_name_type() to get proxy type by name

0.1.3

Expand Down
51 changes: 14 additions & 37 deletions examples/ipify.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ void show_help ()
);
}

#define GET_PARAM() \
if (argv[i][2]) \
param = argv[i] + 2; \
else if (i + 1 < argc && argv[i + 1]) \
param = argv[++i]; \
else \
param = NULL;

int main (int argc, char* argv[])
{
//get command line parameters
Expand All @@ -66,61 +74,30 @@ int main (int argc, char* argv[])
show_help();
return 0;
case 't' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
else
param = NULL;
if (strcasecmp(param, "SOCKS4") == 0 || strcasecmp(param, "SOCKS4A") == 0)
proxytype = PROXYSOCKET_TYPE_SOCKS4;
else if (strcasecmp(param, "SOCKS5") == 0)
proxytype = PROXYSOCKET_TYPE_SOCKS5;
else if (strcasecmp(param, "WEB") == 0 || strcasecmp(param, "HTTP") == 0)
proxytype = PROXYSOCKET_TYPE_WEB_CONNECT;
else {
GET_PARAM()
if ((proxytype = proxysocketconfig_get_name_type(param)) == PROXYSOCKET_TYPE_INVALID) {
fprintf(stderr, "Invalid proxy type: %s\n", param);
show_help();
return 1;
}
break;
case 's' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
else
param = NULL;
GET_PARAM()
if (param)
proxyhost = param;
break;
case 'p' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
else
param = NULL;
GET_PARAM()
if (param)
proxyport = strtol(param, (char**)NULL, 10);
break;
case 'l' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
else
param = NULL;
GET_PARAM()
if (param)
proxyuser = param;
break;
case 'w' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
else
param = NULL;
GET_PARAM()
if (param)
proxypass = param;
break;
Expand Down
38 changes: 33 additions & 5 deletions src/proxysocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#ifdef _MSC_VER
#define va_copy(dst,src) ((dst) = (src))
#endif
#if defined(_WIN32) && !defined(__MINGW64_VERSION_MAJOR)
#define strcasecmp stricmp
#define strncasecmp strnicmp
#endif

#ifndef _WIN32
#define HAVE_VASPRINTF 1
Expand Down Expand Up @@ -162,11 +166,7 @@ int send_http_request (SOCKET sock, const char* request, char** response)
//get result code
if ((responseline = socket_receiveline(sock)) == NULL)
return -1;
#ifdef _WIN32
if (strnicmp(responseline, "HTTP/", 5) != 0)
#else
if (strncasecmp(responseline, "HTTP/", 5) != 0)
#endif
return -1;
p = responseline + 5;
while (*p && (isdigit(*p) || *p == '.'))
Expand Down Expand Up @@ -308,6 +308,35 @@ DLL_EXPORT_PROXYSOCKET const char* proxysocket_get_version_string ()
return PROXYSOCKET_VERSION_STRING;
}

DLL_EXPORT_PROXYSOCKET const char* proxysocketconfig_get_type_name (int proxytype)
{
switch (proxytype) {
case PROXYSOCKET_TYPE_NONE:
return "NONE";
case PROXYSOCKET_TYPE_SOCKS4:
return "SOCKS4A";
case PROXYSOCKET_TYPE_SOCKS5:
return "SOCKS5";
case PROXYSOCKET_TYPE_WEB_CONNECT:
return "WEB";
default:
return "INVALID";
}
}

DLL_EXPORT_PROXYSOCKET int proxysocketconfig_get_name_type (const char* proxytypename)
{
if (strcasecmp(proxytypename, "NONE") == 0 || strcasecmp(proxytypename, "DIRECT") == 0)
return PROXYSOCKET_TYPE_NONE;
if (strcasecmp(proxytypename, "SOCKS4A") == 0 || strcasecmp(proxytypename, "SOCKS4") == 0)
return PROXYSOCKET_TYPE_SOCKS4;
if (strcasecmp(proxytypename, "SOCKS5") == 0)
return PROXYSOCKET_TYPE_SOCKS5;
if (strcasecmp(proxytypename, "WEB") == 0 || strcasecmp(proxytypename, "HTTP") == 0)
return PROXYSOCKET_TYPE_WEB_CONNECT;
return PROXYSOCKET_TYPE_INVALID;
}

DLL_EXPORT_PROXYSOCKET int proxysocket_initialize ()
{
#ifdef __WIN32__
Expand Down Expand Up @@ -865,4 +894,3 @@ DLL_EXPORT_PROXYSOCKET char* socket_receiveline (SOCKET sock)
buf[bufpos] = 0;
return buf;
}

18 changes: 18 additions & 0 deletions src/proxysocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ extern "C" {
#define PROXYSOCKET_TYPE_SOCKS5 0x05
/*! \brief HTTP proxy */
#define PROXYSOCKET_TYPE_WEB_CONNECT 0x20
/*! \brief invalid proxy type */
#define PROXYSOCKET_TYPE_INVALID -1
/*! @} */

/*! \brief logging levels
Expand Down Expand Up @@ -95,6 +97,22 @@ DLL_EXPORT_PROXYSOCKET void proxysocket_get_version (int* pmajor, int* pminor, i
*/
DLL_EXPORT_PROXYSOCKET const char* proxysocket_get_version_string ();

/*! \brief get the textual name of a proxy type
* \param proxytype proxy type (one of the PROXYSOCKET_TYPE_ constants)
* \return name of the proxy type (NONE/SOCKS4A/SOCKS5/WEB/INVALID)
* \sa proxysocketconfig_create()
* \sa proxysocketconfig_get_name_type()
*/
DLL_EXPORT_PROXYSOCKET const char* proxysocketconfig_get_type_name (int proxytype);

/*! \brief get the textual name of a proxy type
* \param proxytypename name of the proxy type (NONE/DIRECT/SOCKS4/SOCKS4A/SOCKS5/WEB/HTTP)
* \return proxy type (one of the PROXYSOCKET_TYPE_ constants) or PROXYSOCKET_TYPE_INVALID on failure
* \sa proxysocketconfig_create()
* \sa proxysocketconfig_get_type_name()
*/
DLL_EXPORT_PROXYSOCKET int proxysocketconfig_get_name_type (const char* proxytypename);

/*! \brief initialize library, call once at the beginning of the main thread of the application
* \return zero on success
*/
Expand Down

0 comments on commit 60f71c8

Please sign in to comment.