Skip to content

Commit

Permalink
CONNECT CHAIN SEPARATOR CHANGED! Alterations/Bugfixes to better suppo…
Browse files Browse the repository at this point in the history
…rt streaming data using non-blocking io
  • Loading branch information
ColumPaget committed Mar 26, 2018
1 parent 77fabf8 commit 774a601
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 68 deletions.
8 changes: 4 additions & 4 deletions ConnectionChain.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ int SetGlobalConnectionChain(const char *Chain)
const char *ptr;
int result=TRUE, count=0;

ptr=GetToken(Chain, ",", &Token, 0);
ptr=GetToken(Chain, "|", &Token, 0);
while (ptr)
{
GetToken(Token,":",&Type,0);
if (MatchTokenFromList(Type, HopTypes, 0)==-1) result=FALSE;
count++;
ptr=GetToken(ptr, ",", &Token, 0);
ptr=GetToken(ptr, "|", &Token, 0);
}

GlobalConnectionChain=CopyStr(GlobalConnectionChain, Chain);
Expand Down Expand Up @@ -462,13 +462,13 @@ int STREAMProcessConnectHops(STREAM *S, const char *HopList)
int count=0;


ptr=GetToken(HopList, ",", &HopURL,0);
ptr=GetToken(HopList, "|", &HopURL,0);

//Note we check 'StrValid' not just whether ptr is null. This is because ptr will be an empty string
//for the last token, and we don't want to process th last token, which will be the 'actual' connection
while (StrValid(ptr))
{
ptr=GetToken(ptr, ",", &NextHop,0);
ptr=GetToken(ptr, "|", &NextHop,0);
ParseConnectDetails(NextHop, NULL, &Dest, &Token, NULL, NULL, NULL);
Dest=MCatStr(Dest,":",Token,NULL);

Expand Down
15 changes: 11 additions & 4 deletions ConnectionChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ Copyright (c) 2015 Colum Paget <[email protected]>
#include "includes.h"

/*
-------------------------------------- PLEASE NOTE ------------------------------------
Separation character has had to be changed from ',' to '|' because commas can occur validly in URLS
---------------------------------------------------------------------------------------
The only function a user should be concerned with here is 'SetGlobalConnectionChain'. This allows setting
a comma-seperated list of 'connection hops' (proxy servers) that are to be used for all connections in
a PIPE-seperated list of 'connection hops' (proxy servers) that are to be used for all connections in
the program. e.g.
if (! SetGlobalConnectionChain("socks5:127.0.0.1,https:89.40.24.6:8080,socks5:16.22.22.1:1080))
if (! SetGlobalConnectionChain("socks5:127.0.0.1|https:89.40.24.6:8080|socks5:16.22.22.1:1080))
{
printf("ERROR! Failed to SetGlobalConnectionChain\n");
}
Expand Down Expand Up @@ -47,12 +54,12 @@ Http.h also use connection chains.
If you only want to use a proxy chain on a particular connection instead of setting a global connection chain,
then open the connection with 'STREAMOpen' or 'STREAMConnect' and list all the hops before the connection URL. e.g.
S=STREAMOpen("socks5:user:[email protected],https:89.40.24.6:1080,tcp:mail.test.com:25");
S=STREAMOpen("socks5:user:[email protected]|https:89.40.24.6:1080|tcp:mail.test.com:25");
or
S=STREAMCreate();
S=STREAMConnect(S,"socks5:user:[email protected],https:89.40.24.6:1080,tcp:mail.test.com:25", 0);
S=STREAMConnect(S,"socks5:user:[email protected]|https:89.40.24.6:1080|tcp:mail.test.com:25", 0);
these will first connect to a socks5 proxy at 127.0.0.1, then an https CONNECT proxy at 89.40.24.6, and finally
to the destiation host 'mail.test.com'.
Expand Down
63 changes: 54 additions & 9 deletions DataParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,45 @@ const char *ParserYAMLItems(int ParserType, const char *Doc, ListNode *Parent, i



/*
Parse callbacks for config files of the form:
name=value
name value
name: value
type name
{
name=value
name value
name: value
}
*/

#define CONFIG_FILE_TOKENS " | |#|=|:|;|{|}|\r|\n"

int ParserConfigCheckForBrace(const char **Data)
{
char *Token=NULL;
const char *ptr;
int result=FALSE;

ptr=*Data;
if (! ptr) return(FALSE);

while (isspace(*ptr)) ptr++;
GetToken(ptr, CONFIG_FILE_TOKENS, &Token,GETTOKEN_MULTI_SEP|GETTOKEN_INCLUDE_SEP|GETTOKEN_HONOR_QUOTES);
if (strcmp(Token, "{")==0)
{
*Data=ptr;
result=TRUE;
}
DestroyString(Token);

return(result);
}



const char *ParserConfigItems(int ParserType, const char *Doc, ListNode *Parent, int IndentLevel)
{
Expand All @@ -188,14 +227,14 @@ const char *ParserConfigItems(int ParserType, const char *Doc, ListNode *Parent,
ListNode *Node;
int BreakOut=FALSE;

#define CONFIG_FILE_TOKENS " | |#|=|:|;|{|}|\r|\n"

ptr=Doc;
while (ptr && (! BreakOut))
{
while (isspace(*ptr)) ptr++;
//while (isspace(*ptr)) ptr++;
ptr=GetToken(ptr, CONFIG_FILE_TOKENS, &Token,GETTOKEN_MULTI_SEP|GETTOKEN_INCLUDE_SEP|GETTOKEN_HONOR_QUOTES);

printf("CFT: %s\n",Token);
switch (*Token)
{
case '#':
Expand All @@ -210,23 +249,29 @@ const char *ParserConfigItems(int ParserType, const char *Doc, ListNode *Parent,
BreakOut=TRUE;
break;

case '\r':
case '\n':
case ';':
//just ignore
break;

case ' ':
case ' ':
while (isspace(*ptr)) ptr++;

// if it's not a knowwn token next then it must be a config line of the form
//"name value", so we fall through
if (strchr(CONFIG_FILE_TOKENS, *ptr)) break;

case ':':
case '=':
ptr=GetToken(ptr,"\n|;|}",&Token,GETTOKEN_MULTI_SEP | GETTOKEN_INCLUDE_SEP | GETTOKEN_HONOR_QUOTES);
break;

case '\r':
case '\n':
printf("EOL\n");
if (ParserConfigCheckForBrace(&ptr))
{
PrevToken=MCatStr(PrevToken, " ", Token, NULL);
break;
}


case ';':
StripLeadingWhitespace(Token);
StripTrailingWhitespace(Token);
Node=ListAddNamedItem(Parent, PrevToken, CopyStr(NULL, Token));
Expand Down
5 changes: 2 additions & 3 deletions Http.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,14 @@ void HTTPInfoSetURL(HTTPInfoStruct *Info, const char *Method, const char *iURL)
const char *p_URL, *ptr;

ptr=GetToken(iURL, "\\S", &URL, 0);
p_URL=strrchr(URL,',');
p_URL=strrchr(URL,'|');
if (p_URL)
{
Info->ConnectionChain=CopyStrLen(Info->ConnectionChain, URL, p_URL - URL);
p_URL++;
}
else p_URL=URL;


if (strcasecmp(Method,"POST")==0) ParseURL(p_URL, &Proto, &Info->Host, &Token, &User, &Pass, &Info->Doc, &Args);
else ParseURL(p_URL, &Proto, &Info->Host, &Token, &User, &Pass, &Info->Doc, NULL);
if (StrValid(Token)) Info->Port=atoi(Token);
Expand Down Expand Up @@ -1141,7 +1140,7 @@ STREAM *HTTPSetupConnection(HTTPInfoStruct *Info, int ForceHTTPS)
}
}

if (StrValid(Info->ConnectionChain)) Tempstr=FormatStr(Tempstr,"%s,%s:%s:%d/",Info->ConnectionChain,Proto,Host,Port);
if (StrValid(Info->ConnectionChain)) Tempstr=FormatStr(Tempstr,"%s|%s:%s:%d/",Info->ConnectionChain,Proto,Host,Port);
else Tempstr=FormatStr(Tempstr,"%s:%s:%d/",Proto,Host,Port);

if (STREAMConnect(S,Tempstr,""))
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CC = gcc
VERSION = 3.2
CFLAGS = -g -O2 -mmmx -msse -msse2
LIBS = -lcrypto -lssl -lc -lc -lc -lc -lc
FLAGS=$(LDFLAGS) $(CPPFLAGS) $(CFLAGS) -fPIC -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DSTDC_HEADERS=1 -D_FILE_OFFSET_BITS=64 -DHAVE_LIBC=1 -DHAVE_PTSNAME_R=1 -DHAVE_LIBC=1 -DHAVE_UMOUNT2=1 -DHAVE_LIBC=1 -DHAVE_UMOUNT=1 -DHAVE_LIBC=1 -DHAVE_MKOSTEMP=1 -DHAVE_LIBC=1 -DHAVE_XATTR=1 -DHAVE_LIBSSL=1 -DHAVE_LIBCRYPTO=1 -DHAVE_EVP_BF_CBC=1 -DHAVE_EVP_RC2_CBC=1 -DHAVE_EVP_RC4=1 -DHAVE_EVP_DES_CBC=1 -DHAVE_EVP_DESX_CBC=1 -DHAVE_EVP_CAST5_CBC=1 -DHAVE_EVP_IDEA_CBC=1 -DHAVE_EVP_AES_128_CBC=1 -DHAVE_EVP_AES_256_CBC=1 -DHAVE_X509_CHECK_HOST=1 -DHAVE_DECL_OPENSSL_ADD_ALL_ALGORITHMS=1 -DHAVE_OPENSSL_ADD_ALL_ALGORITHMS=1 -DHAVE_DECL_SSL_SET_TLSEXT_HOST_NAME=1 -DHAVE_SSL_SET_TLSEXT_HOST_NAME=1 -DHAVE_MADVISE -DHAVE_MADVISE_NOFORK -DHAVE_MADVISE_DONTDUMP -DHAVE_MLOCK
LIBS = -lcrypto -lssl -lc -lc -lc -lc
FLAGS=$(LDFLAGS) $(CPPFLAGS) $(CFLAGS) -fPIC -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DSTDC_HEADERS=1 -D_FILE_OFFSET_BITS=64 -DHAVE_LIBC=1 -DHAVE_PTSNAME_R=1 -DHAVE_LIBC=1 -DHAVE_UMOUNT2=1 -DHAVE_LIBC=1 -DHAVE_UMOUNT=1 -DHAVE_LIBC=1 -DHAVE_XATTR=1 -DHAVE_LIBSSL=1 -DHAVE_LIBCRYPTO=1 -DHAVE_EVP_BF_CBC=1 -DHAVE_EVP_RC2_CBC=1 -DHAVE_EVP_RC4=1 -DHAVE_EVP_DES_CBC=1 -DHAVE_EVP_DESX_CBC=1 -DHAVE_EVP_CAST5_CBC=1 -DHAVE_EVP_IDEA_CBC=1 -DHAVE_EVP_AES_128_CBC=1 -DHAVE_EVP_AES_256_CBC=1 -DHAVE_X509_CHECK_HOST=1 -DHAVE_DECL_OPENSSL_ADD_ALL_ALGORITHMS=1 -DHAVE_OPENSSL_ADD_ALL_ALGORITHMS=1 -DHAVE_DECL_SSL_SET_TLSEXT_HOST_NAME=1 -DHAVE_SSL_SET_TLSEXT_HOST_NAME=1 -DHAVE_MADVISE -DHAVE_MADVISE_NOFORK -DHAVE_MADVISE_DONTDUMP -DHAVE_MLOCK
prefix=/usr/local
OBJ=String.o List.o Socket.o UnixSocket.o Stream.o Errors.o Unicode.o Terminal.o FileSystem.o GeneralFunctions.o DataProcessing.o Pty.o Log.o Http.o Smtp.o inet.o Expect.o base64.o crc32.o md5c.o sha1.o sha2.o whirlpool.o jh_ref.o Hash.o Ssh.o Compression.o OAuth.o LibSettings.o Vars.o Time.o Markup.o SpawnPrograms.o Tokenizer.o PatternMatch.o URL.o DataParser.o ConnectionChain.o OpenSSL.o Process.o Encodings.o RawData.o SecureMem.o CommandLineParser.o

Expand Down
4 changes: 2 additions & 2 deletions Socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1180,10 +1180,10 @@ int STREAMConnect(STREAM *S, const char *URL, const char *Config)
//if URL is a comma-seperated list then it's a list of 'connection hops' through proxies
if (StrValid(GlobalConnectionChain))
{
Value=MCopyStr(Value,GlobalConnectionChain,",",URL);
Value=MCopyStr(Value,GlobalConnectionChain,"|",URL);
result=STREAMProcessConnectHops(S, Value);
}
else if (strchr(URL, ',')) result=STREAMProcessConnectHops(S, URL);
else if (strchr(URL, '|')) result=STREAMProcessConnectHops(S, URL);
else result=STREAMDirectConnect(S, URL, 0);


Expand Down
Loading

0 comments on commit 774a601

Please sign in to comment.