Guest User

Untitled

a guest
Jun 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. //
  2. // MHProxyFactory.m
  3. // MineHelmet
  4. //
  5. // Created by Dylan Lukes on 10/22/10.
  6. // Copyright 2010 Dylan Lukes. All rights reserved.
  7. //
  8.  
  9. #import "MHProxyManager.h"
  10. #import "APELite.h"
  11.  
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <netdb.h>
  15.  
  16. typedef struct{
  17. int client;
  18. int server;
  19. } MHProxyDescriptorPair;
  20.  
  21. @interface MHProxyManager (PrivateMethods)
  22.  
  23. - (int)handleConnectAttempt:(int)socket addr:(const struct sockaddr *)addr addrlen:(socklen_t)addrlen;
  24. - (void)proxyThreadMain:(NSValue *)pairValue;
  25.  
  26. @end
  27.  
  28. static MHProxyManager *instance_pointer;
  29. static int connect_override(int socket, const struct sockaddr *address, socklen_t addrlen){
  30. return [instance_pointer handleConnectAttempt:socket addr:address addrlen:addrlen];
  31. }
  32.  
  33. @implementation MHProxyManager
  34.  
  35. + (id)sharedInstance {
  36. static dispatch_once_t pred;
  37. static MHProxyManager *instance = nil;
  38.  
  39. dispatch_once(&pred, ^{ instance = [[self alloc] init]; });
  40. return instance;
  41. }
  42.  
  43. - (id)init {
  44. if (self = [super init]) {
  45. connectReentry = APEPatchCreate(connect, connect_override);
  46. instance_pointer = self;
  47. }
  48. return self;
  49. }
  50.  
  51. - (int)handleConnectAttempt:(int)socket addr:(const struct sockaddr *)addr addrlen:(socklen_t)addrlen {
  52. static BOOL javaStackTestConnect = NO;
  53.  
  54. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  55.  
  56. // Ignore the first connect() attempt (java test)
  57. static dispatch_once_t pred;
  58. dispatch_once(&pred, ^{javaStackTestConnect = YES;});
  59. if (javaStackTestConnect) {
  60. javaStackTestConnect = NO;
  61. return connectReentry(socket, addr, addrlen);
  62. }
  63.  
  64. char serv[NI_MAXSERV];
  65.  
  66. // If the socket is not for minecraft, return without doing anything...
  67. getnameinfo(addr, addrlen, NULL, 0, serv, NI_MAXSERV, NI_NUMERICSERV);
  68. int port = atoi(serv);
  69. if (port == 80) {
  70. return connectReentry(socket, addr, addrlen);
  71. }
  72.  
  73. // if socket is non-blocking...
  74. BOOL wasNonBlocking = NO;
  75. int flags = fcntl(socket,F_GETFL);
  76. if (flags & O_NONBLOCK){
  77. wasNonBlocking = YES;
  78. fcntl(socket, F_SETFL, flags & ~O_NONBLOCK);
  79. }
  80.  
  81. // Call the original connect()
  82. int retval = connectReentry(socket, addr, addrlen);
  83.  
  84. // Steal the original file descriptor (to server)
  85. int original_fd = dup(socket);
  86.  
  87. // Redirect socket to sockpair[0]
  88. int sockpair[2];
  89. socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair);
  90. dup2(sockpair[0], socket);
  91.  
  92. // If the socket was non-blocking...
  93. if (wasNonBlocking) {
  94. // ...fix the socket to its original state
  95. fcntl(socket, F_SETFL, O_NONBLOCK);
  96. }
  97.  
  98. // Set up proxy
  99. MHProxyDescriptorPair pair = {sockpair[1], original_fd};
  100. NSValue *pairValue = [NSValue value:&pair withObjCType:@encode(MHProxyDescriptorPair)];
  101.  
  102. [NSThread detachNewThreadSelector:@selector(proxyThreadMain:) toTarget:self withObject:pairValue];
  103.  
  104. [pool drain];
  105. return retval;
  106. }
  107.  
  108. - (void)proxyThreadMain:(NSValue *)pairValue{
  109. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  110.  
  111. [[NSThread currentThread] setName:@"MineHelmet Proxy Thread"];
  112.  
  113. MHProxyDescriptorPair pair;
  114. [pairValue getValue:&pair];
  115.  
  116. proxy = [[MHProxy alloc] initWithClientDescriptor:pair.client serverDescriptor:pair.server];
  117. // Run loop
  118. CFRunLoopRun();
  119. // When the proxy stops the loop, release it
  120. [proxy release];
  121. [pool drain];
  122. }
  123.  
  124. @end
Add Comment
Please, Sign In to add comment