Advertisement
Guest User

Untitled

a guest
Apr 30th, 2014
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void)setIPv4Shit:(SCNetworkServiceRef)service{
  2.    
  3.    
  4.     SCNetworkProtocolRef protoR = SCNetworkServiceCopyProtocol(service, kSCNetworkProtocolTypeIPv4);
  5.     CFDictionaryRef proxyDictR = SCNetworkProtocolGetConfiguration(protoR); // returns NULL if no configuration.
  6.    
  7.     const void *configMethodP = proxyDictR ? CFDictionaryGetValue(proxyDictR, kSCPropNetIPv4ConfigMethod) : NULL;
  8.    
  9. #define GetCasted(value, type) ((value) && (CFGetTypeID(value) == type##GetTypeID()) ? ((type##Ref)value) : NULL)
  10.    
  11.     CFStringRef configMethod = GetCasted(configMethodP, CFString);
  12.    
  13.     CFMutableDictionaryRef newProxyDictR;
  14.    
  15.     newProxyDictR = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  16.    
  17.     CFDictionarySetValue(newProxyDictR, kSCPropNetIPv4ConfigMethod, configMethod);
  18.     CFDictionarySetValue(newProxyDictR, kSCPropNetOverridePrimary, CFSTR("1"));
  19.    
  20.    
  21.     SCNetworkProtocolSetConfiguration(protoR, newProxyDictR);
  22.    
  23.     CFRelease(newProxyDictR);
  24.    
  25.    
  26. }
  27.  
  28.  
  29. -(void)connectnew{
  30.    
  31.     CFStringRef username = (__bridge CFStringRef)@"xxx";
  32.     CFStringRef password = (__bridge CFStringRef)@"xxx";
  33.     CFStringRef address = (__bridge CFStringRef)@"67.185.xxx.xxx";
  34.    
  35.     CFStringRef kAppName = CFSTR("MyProxService");
  36.     CFStringRef kVendorName = CFSTR("com.all.proxtool");
  37.    
  38.     OSStatus        authErr = noErr;
  39.    
  40.     int err = 0;
  41.    
  42.     CFStringRef mykeys[]  = {  kSCPropNetPPPAuthName//, kSCPropNetPPPAuthPassword
  43.                             ,kSCPropNetPPPCommRemoteAddress,kSCPropNetPPPCCPEnabled
  44.                             ,kSCPropNetPPPCCPMPPE40Enabled, kSCPropNetPPPCCPMPPE128Enabled
  45.                             ,kSCPropNetPPPCommDisplayTerminalWindow,kSCPropNetPPPCommRedialCount
  46.                             ,kSCPropNetPPPCommRedialEnabled,kSCPropNetPPPCommRedialInterval
  47.                             ,kSCPropNetPPPCommUseTerminalScript,kSCPropNetPPPDialOnDemand
  48.                             ,kSCPropNetPPPDisconnectOnFastUserSwitch,kSCPropNetPPPDisconnectOnIdle
  49.                             ,kSCPropNetPPPDisconnectOnIdleTimer,kSCPropNetPPPDisconnectOnLogout
  50.                             ,kSCPropNetPPPDisconnectOnSleep,kSCPropNetPPPIPCPCompressionVJ
  51.                             ,kSCPropNetPPPIdleReminder,kSCPropNetPPPIdleReminderTimer
  52.                             ,kSCPropNetPPPLCPEchoEnabled,kSCPropNetPPPLCPEchoFailure
  53.                             ,kSCPropNetPPPLCPEchoInterval,kSCPropNetPPPLogfile
  54.                             ,kSCPropNetPPPVerboseLogging };
  55.    
  56.     CFStringRef myvals[]  = { username//, password
  57.                             , address, CFSTR("1")
  58.                             , CFSTR("1"), CFSTR("1")
  59.                             , CFSTR("0"), CFSTR("1")
  60.                             , CFSTR("0"), CFSTR("5")
  61.                             , CFSTR("0"), CFSTR("0")
  62.                             , CFSTR("1"), CFSTR("0")
  63.                             , CFSTR("600"), CFSTR("1")
  64.                             , CFSTR("0"), CFSTR("0")
  65.                             , CFSTR("0"), CFSTR("1800")
  66.                             , CFSTR("1"), CFSTR("15")
  67.                             , CFSTR("20"), CFSTR("/var/log/ppp.log")
  68.                             , CFSTR("0") };
  69.    
  70.    
  71.     CFDictionaryRef myoptions  = CFDictionaryCreate (NULL,
  72.                                                      (const void **)&mykeys,
  73.                                                      (const void **)&myvals,
  74.                                                      24,
  75.                                                      &kCFTypeDictionaryKeyCallBacks,
  76.                                                      &kCFTypeDictionaryValueCallBacks);
  77.    
  78.    
  79.    
  80.     // Authority
  81.    
  82.     AuthorizationFlags rootFlags = kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights |
  83.                                     kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize;
  84.    
  85.     AuthorizationRef auth;
  86.    
  87.     authErr = AuthorizationCreate(NULL,
  88.                                   kAuthorizationEmptyEnvironment,
  89.                                   rootFlags,
  90.                                   &auth);
  91.    
  92.     // Create preferences for interface.
  93.    
  94.     SCPreferencesRef prefs;
  95.    
  96.     if (authErr == noErr) {
  97.         NSLog(@"here");
  98.         prefs = SCPreferencesCreateWithAuthorization(NULL, kVendorName, NULL, auth);
  99.     } else {
  100.         NSLog(@"there");
  101.  
  102.         prefs = SCPreferencesCreate(NULL, kVendorName, NULL);
  103.     }
  104.    
  105.     if (prefs == NULL) {
  106.    
  107.         NSLog(@"Could not create preferences\n");
  108.         err = SCError();
  109.  
  110.     } else {
  111.        
  112.         CFArrayRef servicesArray;
  113.        
  114.         servicesArray = SCNetworkServiceCopyAll(prefs);
  115.        
  116.         if (servicesArray == NULL) {
  117.            
  118.             NSLog(@"No network services");
  119.            
  120.             //   return -1;
  121.             SCPreferencesUnlock(prefs);
  122.            
  123.             if (servicesArray != NULL) {
  124.                
  125.                 CFRelease(servicesArray);
  126.                
  127.             }
  128.  
  129.         } else {
  130.            
  131.             SCNetworkServiceRef service;
  132.             CFIndex i, arraySize;
  133.             Boolean serviceFound = FALSE;
  134.             CFStringRef serviceToDial;
  135.            
  136.             arraySize = CFArrayGetCount(servicesArray);
  137.  
  138.             for (i = 0; i < arraySize; ++i) {
  139.                
  140.                 service = (SCNetworkServiceRef) CFArrayGetValueAtIndex(servicesArray, i);
  141.                
  142.                 CFStringRef serviceName = SCNetworkServiceGetName(service);
  143.                
  144.                 if (CFStringCompare(serviceName, kAppName, 0) == kCFCompareEqualTo) {
  145.                    
  146.                     serviceFound = TRUE;
  147.                     serviceToDial = SCNetworkServiceGetServiceID(service);
  148.                     break;
  149.                    
  150.                 }
  151.                
  152.             }
  153.            
  154.             SCNetworkInterfaceRef   pptpItfc;
  155.             SCNetworkInterfaceRef   iface;
  156.            
  157.             if (SCPreferencesLock(prefs, TRUE)){
  158.                
  159.                 if (!serviceFound){
  160.                    
  161.                     pptpItfc = SCNetworkInterfaceCreateWithInterface(kSCNetworkInterfaceIPv4,
  162.                                                                      kSCNetworkInterfaceTypePPTP);
  163.                    
  164.                     iface = SCNetworkInterfaceCreateWithInterface(pptpItfc, kSCNetworkInterfaceTypePPP);
  165.                    
  166.                     if (prefs && iface) {
  167.                        
  168.                         service = SCNetworkServiceCreate(prefs, iface);
  169.                        
  170.                         SCNetworkServiceSetName(service, kAppName);
  171.                        
  172.                         iface = SCNetworkServiceGetInterface(service);
  173.                        
  174.                         if (! SCNetworkInterfaceSetConfiguration(iface, myoptions)){
  175.                            
  176.                             NSLog(@"failed to set config");
  177.                            
  178.                            // err = SCError();
  179.                            
  180.                         } else {
  181.                            
  182.                             SCNetworkServiceEstablishDefaultConfiguration(service);
  183.                            
  184.                             SCNetworkSetRef set = SCNetworkSetCopyCurrent(prefs);
  185.                            
  186.                             if (set && SCNetworkSetAddService(set, service)) {
  187.                                
  188.                                 [self setIPv4Shit:service];
  189.                                
  190.                                 if (SCPreferencesCommitChanges(prefs)) {
  191.                                    
  192.                                     if (SCPreferencesApplyChanges(prefs)){
  193.                                        
  194.                                         SCPreferencesSynchronize(prefs);
  195.                                        
  196.                                         serviceToDial = SCNetworkServiceGetServiceID(service);
  197.                                        
  198.                                     }
  199.                                    
  200.                                 }
  201.                                
  202.                             }
  203.                            
  204.                            
  205.                         }
  206.                        
  207.                        
  208.                     }
  209.                    
  210.                 } else {
  211.                    
  212.                     iface = SCNetworkServiceGetInterface(service);
  213.                    
  214.                     if (! (iface && SCNetworkInterfaceSetConfiguration(iface, myoptions)) ){
  215.                        
  216.                         NSLog(@"failed to set config");
  217.                        
  218.                        // err = SCError();
  219.                        
  220.                     } else {
  221.                        
  222.                                 [self setIPv4Shit:service];
  223.                        
  224.                         if (SCPreferencesCommitChanges(prefs)) {
  225.                            
  226.                             if (SCPreferencesApplyChanges(prefs)){
  227.                                
  228.                                 SCPreferencesSynchronize(prefs);
  229.                                
  230.                                 serviceToDial = SCNetworkServiceGetServiceID(service);
  231.                                
  232.                             }
  233.                            
  234.                         }
  235.                        
  236.                     }
  237.                    
  238.                 }
  239.                
  240.  
  241.                
  242.  
  243.                
  244.                
  245.  
  246.                 SCPreferencesUnlock(prefs);
  247.                
  248.                 CFStringRef serviceName;
  249.                
  250.                 serviceName = SCNetworkServiceGetName(service);
  251.                
  252.                 NSLog(@"the service name is %@", serviceName);
  253.  
  254.                
  255.                 NSTask *task;
  256.                 task = [[NSTask alloc] init];
  257.                 [task setLaunchPath: @"/usr/sbin/networksetup"];
  258.                
  259.                 NSArray *arguments;
  260.                 arguments = [NSArray arrayWithObjects: @"-setpppoepassword", serviceName, password, nil];
  261.                 [task setArguments: arguments];
  262.                 NSLog(@"%@", arguments);
  263.                
  264.                 [task launch];
  265.                 [task waitUntilExit];
  266.                
  267.  
  268.                
  269.  
  270.             } else {
  271.                
  272.                 NSLog(@"Failed to get pref lock");
  273.                
  274.             }
  275.            
  276.            
  277.         }
  278.        
  279.     }
  280.    
  281.    
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement