Created
October 28, 2010 20:44
-
-
Save DylanLukes/652290 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (int)handleConnectAttempt:(int)socket addr:(const struct sockaddr *)addr addrlen:(socklen_t)addrlen { | |
static BOOL javaStackTestConnect = NO; | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
// Ignore the first connect() attempt (java test) | |
static dispatch_once_t pred; | |
dispatch_once(&pred, ^{javaStackTestConnect = YES;}); | |
if (javaStackTestConnect) { | |
javaStackTestConnect = NO; | |
return connectReentry(socket, addr, addrlen); | |
} | |
char serv[NI_MAXSERV]; | |
// If the socket is not for minecraft, return without doing anything... | |
getnameinfo(addr, addrlen, NULL, 0, serv, NI_MAXSERV, NI_NUMERICSERV); | |
int port = atoi(serv); | |
if (port == 80) { | |
return connectReentry(socket, addr, addrlen); | |
} | |
// if socket is non-blocking... | |
BOOL wasNonBlocking = NO; | |
int flags = fcntl(socket,F_GETFL); | |
if (flags & O_NONBLOCK){ | |
wasNonBlocking = YES; | |
fcntl(socket, F_SETFL, flags & ~O_NONBLOCK); | |
} | |
// Call the original connect() | |
int retval = connectReentry(socket, addr, addrlen); | |
// Steal the original file descriptor (to server) | |
int original_fd = dup(socket); | |
// Give the client sockpair[0] (closing socket) | |
int sockpair[2]; | |
socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair); | |
dup2(sockpair[0], socket); | |
// Uncomment this to disable proxy | |
//dup2(original_fd, sockpair[1]); | |
// If the socket was non-blocking... | |
if (wasNonBlocking) { | |
// ...fix the socket to its original state | |
fcntl(socket, F_SETFL, O_NONBLOCK); | |
} | |
// Set up proxy | |
MHProxyDescriptorPair pair = {sockpair[1], original_fd}; | |
NSValue *pairValue = [NSValue value:&pair withObjCType:@encode(MHProxyDescriptorPair)]; | |
[NSThread detachNewThreadSelector:@selector(proxyThreadMain:) toTarget:self withObject:pairValue]; | |
[pool drain]; | |
return retval; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment