Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Completed different IP type fallbacks in case SOCKS5 proxy doesn't su…
Browse files Browse the repository at this point in the history
…pport it

Put locks on every UProxy while it's processing
  • Loading branch information
TETYYS committed Nov 12, 2016
1 parent 75a6f1f commit e1e3402
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 177 deletions.
22 changes: 19 additions & 3 deletions DNS.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdbool.h>
#include "ProxyRequest.h"
#include "Config.h"
#include "CPH_Threads.h"

static void DNSResolveCallError(DNS_LOOKUP_ASYNC_EX *Ex, enum dns_error Error)
{
Expand All @@ -24,7 +25,7 @@ static void DNSResolveCallError(DNS_LOOKUP_ASYNC_EX *Ex, enum dns_error Error)
static void DNSResolveLevEventToDNSProcess(evutil_socket_t fd, short what, void *arg)
{
DNS_LOOKUP_ASYNC_EX *ex = (DNS_LOOKUP_ASYNC_EX*)arg;
Log(LOG_LEVEL_DEBUG, "DNS process WHAT %d", what);
Log(LOG_LEVEL_DEBUG, "DNS process WHAT %d ipv6? %s", what, ex->ipv6 ? "ye" : "nah");
if (what == EV_TIMEOUT) {
DNSResolveCallError(ex, DNS_TIMEOUT);
goto free;
Expand All @@ -35,7 +36,7 @@ static void DNSResolveLevEventToDNSProcess(evutil_socket_t fd, short what, void
if (ex->resolveDone)
goto free;

Log(LOG_LEVEL_DEBUG, "DNS process WHAT %d END", what);
Log(LOG_LEVEL_DEBUG, "DNS process WHAT %d END ipv6? %s", what, ex->ipv6 ? "ye" : "nah");

return;
free:
Expand All @@ -44,9 +45,23 @@ static void DNSResolveLevEventToDNSProcess(evutil_socket_t fd, short what, void
event_free(ex->evDNS);
if (ex->fxFreed != NULL)
((FxDnsFreed)ex->fxFreed)(ex);

pthread_mutex_destroy(&(ex->preDoneLock));
free(ex);
}

void DNSDone(struct dns_cb_data *Data)
{
DNS_LOOKUP_ASYNC_EX *ex = (DNS_LOOKUP_ASYNC_EX*)Data->context;
Log(LOG_LEVEL_DEBUG, "Waiting for preDoneLock...");
pthread_mutex_lock(&(ex->preDoneLock)); {
Log(LOG_LEVEL_DEBUG, "PRE CALL");
ex->fxDone(Data);
Log(LOG_LEVEL_DEBUG, "POST CALL");
} pthread_mutex_unlock(&(ex->preDoneLock));
Log(LOG_LEVEL_DEBUG, "Unlocked preDoneLock");
}

DNS_LOOKUP_ASYNC_EX *DNSResolveAsync(void *Ex, char *Domain, bool IPv6, dns_callback_t fxDone, FxDnsFreed fxFree)
{
DNS_LOOKUP_ASYNC_EX *ex = malloc(sizeof(DNS_LOOKUP_ASYNC_EX));
Expand All @@ -55,6 +70,7 @@ DNS_LOOKUP_ASYNC_EX *DNSResolveAsync(void *Ex, char *Domain, bool IPv6, dns_call
ex->evDNS = NULL;
ex->resolveDone = false;
ex->fxDone = fxDone;
pthread_mutex_init(&(ex->preDoneLock), NULL);
ex->fxFreed = fxFree;
ex->ipv6 = IPv6;
Log(LOG_LEVEL_DEBUG, "DNSResolveAsync EX: obj %p fx %p", Ex, fxDone);
Expand All @@ -69,7 +85,7 @@ DNS_LOOKUP_ASYNC_EX *DNSResolveAsync(void *Ex, char *Domain, bool IPv6, dns_call
ex->dnsCtx = dnsCtx;
Log(LOG_LEVEL_DEBUG, "DNSResolveAsync EX: ctx %p", dnsCtx);

dns_queue(dnsCtx, ex, Domain, IPv6 ? DNS_RR_TYPE_AAAA : DNS_RR_TYPE_A, fxDone);
dns_queue(dnsCtx, ex, Domain, IPv6 ? DNS_RR_TYPE_AAAA : DNS_RR_TYPE_A, DNSDone);

Log(LOG_LEVEL_DEBUG, "DNS FD %d", dns_get_fd(dnsCtx));

Expand Down
6 changes: 5 additions & 1 deletion DNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

#include "tadns.h"
#include <stdbool.h>
#include "CPH_Threads.h"

typedef struct _DNS_LOOKUP_ASYNC_EX {
void *object;
struct event *evDNS;
struct dns *dnsCtx;
bool resolveDone;

pthread_mutex_t preDoneLock;
dns_callback_t fxDone;
void *fxFreed;

bool resolveDone;
bool ipv6;
} DNS_LOOKUP_ASYNC_EX;

Expand Down
11 changes: 3 additions & 8 deletions Interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,8 @@ static bool AuthVerify(char *Buff, struct evbuffer *OutBuff, int Fd, WEB_INTERFA
goto end; // Auth expired
}
} else {
if (AuthWebList[x]->expiry < (GetUnixTimestampMilliseconds() / 1000)) {
if (AuthWebList[x]->expiry < (GetUnixTimestampMilliseconds() / 1000))
AuthWebRemove(x);
}
}
}

Expand Down Expand Up @@ -597,7 +596,7 @@ void InterfaceRawReverseDNS(struct bufferevent *BuffEvent, char *Buff)
return;
}

char *rDNS = ReverseDNS(proxy->ip);
char *rDNS = ReverseDNS(proxy->ip); // TODO: async?
if (rDNS == NULL) {
evbuffer_add(headers, "3", 1); // To Content-Length
evbuffer_add(body, "N/A", 3);
Expand Down Expand Up @@ -659,7 +658,6 @@ static void InterfaceRawRecheckStage2(UNCHECKED_PROXY *UProxy)
evbuffer_free(body);

BufferEventFreeOnWrite(buffEvent);
return;
}

void InterfaceRawRecheck(struct bufferevent *BuffEvent, char *Buff)
Expand Down Expand Up @@ -966,7 +964,7 @@ static bool InterfaceRawGetCustomPageStage2(UNCHECKED_PROXY *UProxy, UPROXY_CUST
{
Log(LOG_LEVEL_DEBUG, "CustomPage stage 2");
struct bufferevent *buffEvent = (struct bufferevent*)UProxy->singleCheckCallbackExtraData;

if (!UProxy->checkSuccess) {
bufferevent_write(buffEvent, "HTTP/1.1 504 Gateway Timeout\r\nContent-Length: 24\r\nContent-Type: text/html\r\n\r\nProxy connection failure", 101);
Log(LOG_LEVEL_DEBUG, "BuffEvent free %p", buffEvent);
Expand Down Expand Up @@ -1012,8 +1010,6 @@ static bool InterfaceRawGetCustomPageStage2(UNCHECKED_PROXY *UProxy, UPROXY_CUST
} free(data);
bufferevent_write(buffEvent, "\r\n", 2);

pthread_mutex_unlock(&(UProxy->processing));

if (Stage == UPROXY_CUSTOM_PAGE_STAGE_END) {
struct evbuffer *foot = evbuffer_new(); {
HTML_TEMPALTE_TABLE_INFO tableInfo;
Expand Down Expand Up @@ -1055,7 +1051,6 @@ static bool InterfaceRawGetCustomPageStage2Render(UNCHECKED_PROXY *UProxy, UPROX
}

bufferevent_write_buffer(buffEvent, bufferevent_get_input(UProxy->assocBufferEvent));
pthread_mutex_unlock(&(UProxy->processing));

if (Stage == UPROXY_CUSTOM_PAGE_STAGE_END) {
Log(LOG_LEVEL_DEBUG, "BuffEvent free %p", buffEvent);
Expand Down
2 changes: 1 addition & 1 deletion ProxyLists.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ typedef struct _UNCHECKED_PROXY {
// Universal stages - 5, 6, 7, 8
UPROXY_STAGE stage;

// This one blocks EVWrite called timeout event in case WServer is processing UProxy while EVWrite timeout event tries to free it
// This one blocks EVWrite called timeout event in case the Server is processing UProxy while EVWrite timeout event tries to free it
pthread_mutex_t processing;

struct event *timeout;
Expand Down
Loading

0 comments on commit e1e3402

Please sign in to comment.