@@ -136,12 +136,42 @@ bool setProxy(const QSettings& proxySetting){
136
136
#endif
137
137
#ifdef Q_OS_MAC
138
138
139
+ const char * myCFStringCStringPtr (CFStringRef aString,CFStringEncoding encoding,bool * needFree = 0 ){
140
+ if (needFree!=0 ){
141
+ *needFree = false ;
142
+ }
143
+ if (!aString){
144
+ qDebug ()<<" myCFStringCStringPtr source is NULL" ;
145
+ return NULL ;
146
+ }
147
+ const char * ret = CFStringGetCStringPtr (aString,encoding);
148
+ if (ret == NULL ){
149
+
150
+ CFIndex length = CFStringGetLength (aString);
151
+ CFIndex maxSize =
152
+ CFStringGetMaximumSizeForEncoding (length,
153
+ kCFStringEncodingUTF8 );
154
+ char *buffer = (char *)malloc (maxSize);
155
+
156
+ if (CFStringGetCString (aString, buffer, maxSize,
157
+ kCFStringEncodingUTF8 )) {
158
+ if (needFree != 0 ){
159
+ *needFree = true ;
160
+ }
161
+ return buffer;
162
+ }
163
+ free (buffer);
164
+ return NULL ;
165
+ }
166
+ return ret;
167
+ }
168
+
139
169
QString getServiceName (){
140
170
// get primary serverId
141
171
char *serviceName = NULL ;
142
172
CFStringRef primaryServiceId=NULL ;
143
173
SCDynamicStoreRef dynamicStoreDomainState = SCDynamicStoreCreate (NULL ,
144
- CFSTR (" myApplicationName " ),
174
+ CFSTR (" Rythem " ),
145
175
NULL ,
146
176
NULL );
147
177
// get primary interface
@@ -154,25 +184,49 @@ QString getServiceName(){
154
184
n += CFStringGetLength (kSCEntNetIPv4 );
155
185
n += 3 ;// for three '/'
156
186
char *netIp4KeyStr = (char *)malloc (sizeof (char )*(n+1 ));
187
+
188
+ bool stateNeedFree = false ;
189
+ const char *domainState = myCFStringCStringPtr (kSCDynamicStoreDomainState , kCFStringEncodingUTF8 ,&stateNeedFree);
190
+ bool networkNeedFree = false ;
191
+ const char *network = myCFStringCStringPtr (kSCCompNetwork , kCFStringEncodingUTF8 ,&networkNeedFree);
192
+ bool globalNeedFree = false ;
193
+ const char *global = myCFStringCStringPtr (kSCCompGlobal , kCFStringEncodingUTF8 ,&globalNeedFree);
194
+ bool ipv4NeedFree = false ;
195
+ const char *ipv4 = myCFStringCStringPtr (kSCEntNetIPv4 , kCFStringEncodingUTF8 ,&ipv4NeedFree);
196
+
157
197
sprintf (netIp4KeyStr, " %s/%s/%s/%s" ,
158
- CFStringGetCStringPtr (kSCDynamicStoreDomainState , kCFStringEncodingUTF8 ),
159
- CFStringGetCStringPtr (kSCCompNetwork , kCFStringEncodingUTF8 ),
160
- CFStringGetCStringPtr (kSCCompGlobal , kCFStringEncodingUTF8 ),
161
- CFStringGetCStringPtr (kSCEntNetIPv4 , kCFStringEncodingUTF8 ));
198
+ domainState,
199
+ network,
200
+ global,
201
+ ipv4);
202
+ if (stateNeedFree){
203
+ free ((char *)domainState);
204
+ }
205
+ if (networkNeedFree){
206
+ free ((char *)network);
207
+ }
208
+ if (globalNeedFree){
209
+ free ((char *)global);
210
+ }
211
+ if (ipv4NeedFree){
212
+ free ((char *)ipv4);
213
+ }
214
+
162
215
163
216
CFStringRef netIpv4KeyCFStr = CFStringCreateWithCString (NULL , netIp4KeyStr, kCFStringEncodingUTF8 );
164
217
CFPropertyListRef netIpv4List =SCDynamicStoreCopyValue (dynamicStoreDomainState, netIpv4KeyCFStr);
165
218
if (netIpv4List) {
166
219
primaryServiceId = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef)netIpv4List, kSCDynamicStorePropNetPrimaryService );
220
+ if (primaryServiceId){
221
+ CFRetain (primaryServiceId);
222
+ }
223
+ CFRelease (netIpv4List);
167
224
}
168
- if (primaryServiceId){
169
- CFRetain (primaryServiceId);
170
- }
171
- CFRelease (netIpv4List);
172
225
CFRelease (netIpv4KeyCFStr);
173
226
free (netIp4KeyStr);
174
227
}
175
228
if (!primaryServiceId){
229
+ qDebug ()<<" no service name found" ;
176
230
return QString ();
177
231
}
178
232
CFShow (primaryServiceId);
@@ -189,12 +243,20 @@ QString getServiceName(){
189
243
190
244
CFStringRef serviceId = SCNetworkServiceGetServiceID (service);
191
245
CFShow (serviceId);
246
+ CFShow (primaryServiceId);
192
247
if ( CFStringCompare (primaryServiceId,serviceId,0 ) == kCFCompareEqualTo ){
193
248
CFStringRef name = SCNetworkServiceGetName (service);// Wi-Fi "Bluetooth DUN" etc.
194
249
if (name){
195
250
unsigned long l = CFStringGetLength (name) + 1 ;
251
+ CFShow (name);
196
252
serviceName = (char *)malloc (l*sizeof (char ));
197
- sprintf (serviceName, " %s" ,CFStringGetCStringPtr (name, kCFStringEncodingUTF8 ));
253
+ bool needFree = false ;
254
+ const char * theName = myCFStringCStringPtr (name, kCFStringEncodingUTF8 ,&needFree);
255
+ sprintf (serviceName, " %s" ,theName);
256
+ if (needFree){
257
+ free ((char *)theName);
258
+ }
259
+
198
260
}
199
261
// break;
200
262
}else {
@@ -219,6 +281,10 @@ QString getServiceName(){
219
281
220
282
bool execScript (const QString &script){
221
283
QString service = getServiceName ();
284
+ if (service == NULL || service.isEmpty ()){
285
+ // TODO 提示
286
+ return false ;
287
+ }
222
288
QProcess process;
223
289
qDebug ()<<" script" <<script.arg (service);
224
290
QStringList args = script.arg (service).split (" " );
@@ -251,6 +317,10 @@ bool disableAutoProxyAndSetProxyForService(const QString& host, const int port){
251
317
bool execProxysetting (const QString &pacUrl=QString()){
252
318
QProcess process;
253
319
QString service = getServiceName ();
320
+ if (service == NULL || service.isEmpty ()){
321
+ // TODO 提示
322
+ return false ;
323
+ }
254
324
QString script = " proxysetting" ;
255
325
if (pacUrl.isEmpty ()){
256
326
script = QString (appPath+" /proxysetting --disablepac %1" ).arg (service);
@@ -415,14 +485,7 @@ MainWindow::MainWindow(QWidget *parent) :
415
485
connect (ui->ActionCapture ,SIGNAL (triggered ()),SLOT (toggleCapture ()));
416
486
connect (ui->actionRemoveAll ,SIGNAL (triggered ()),this ,SLOT (onActionRemoveAll ()));
417
487
connect (ui->actionWaterfall , SIGNAL (triggered ()), this , SLOT (onWaterfallActionTriggered ()));
418
- /*
419
- #ifdef Q_WS_MAC
420
- // TODO: mac下需手动设置代理
421
- ui->ActionCapture->setEnabled(false);
422
- ui->ActionCapture->setText(tr("SetupProxyManually"));
423
- ui->ActionCapture->setToolTip(tr("non-windows OS need to set proxy to:127.0.0.1:8889 manually"));
424
- #endif
425
- */
488
+
426
489
checker = new RyUpdateChecker (this );
427
490
QTimer timer;
428
491
timer.singleShot (1000 ,this ,SLOT (checkNewVersion ()));
@@ -662,7 +725,7 @@ void MainWindow::onWaterfallActionTriggered(){
662
725
663
726
void MainWindow::toggleProxy (){
664
727
QMutexLocker locker (&proxyMutex);
665
- #ifdef Q_WS_MAC
728
+ #ifdef Q_OS_MAC
666
729
bool setProxySuccess = false ;
667
730
if (_isUsingCapture){
668
731
if (_previousProxyInfo.isUsingPac == " 1" ){
@@ -678,17 +741,22 @@ void MainWindow::toggleProxy(){
678
741
int isPacEnabled = 0 ;
679
742
// CFStringRef CFPacUrl = (CFStringRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesProxyAutoConfigURLString);
680
743
// if(CFPacUrl){
681
- // _previousProxyInfo.pacUrl = QString::fromUtf8( CFStringGetCStringPtr (CFPacUrl,kCFStringEncodingUTF8) );
744
+ // _previousProxyInfo.pacUrl = QString::fromUtf8( myCFStringCStringPtr (CFPacUrl,kCFStringEncodingUTF8) );
682
745
// }
683
746
if (proxies){
684
- CFNumberRef pacEnabled ;
747
+ CFNumberRef tmpRef ;
685
748
// kSCPropNetProxiesHTTPSProxy
686
- if ((pacEnabled = (CFNumberRef)CFDictionaryGetValue (proxies, kSCPropNetProxiesProxyAutoConfigEnable ))){
687
- if (CFNumberGetValue (pacEnabled, kCFNumberIntType , &pacEnabled) && pacEnabled){
749
+ if ((tmpRef = (CFNumberRef)CFDictionaryGetValue (proxies, kSCPropNetProxiesProxyAutoConfigEnable ))){
750
+ CFNumberRef pacEnabled;
751
+ CFNumberGetValue (pacEnabled, kCFNumberIntType , &pacEnabled);
752
+ if (!pacEnabled){
753
+ CFNumberGetValue (pacEnabled, kCFNumberSInt32Type , &pacEnabled);
754
+ }
755
+ if (pacEnabled){
688
756
isPacEnabled = 1 ;
689
757
_previousProxyInfo.isUsingPac = QString (" 1" );
690
758
CFStringRef CFPacUrl = (CFStringRef)CFDictionaryGetValue (proxies, kSCPropNetProxiesProxyAutoConfigURLString );
691
- _previousProxyInfo.pacUrl = QString::fromUtf8 ( CFStringGetCStringPtr (CFPacUrl,kCFStringEncodingUTF8 ) );
759
+ _previousProxyInfo.pacUrl = QString::fromUtf8 ( myCFStringCStringPtr (CFPacUrl,kCFStringEncodingUTF8 ) );
692
760
}
693
761
}
694
762
}
@@ -703,7 +771,7 @@ void MainWindow::toggleProxy(){
703
771
if (httpEnabled && CFNumberGetValue (httpEnabled, kCFNumberIntType , &tmp) && tmp){
704
772
CFStringRef host = (CFStringRef)CFDictionaryGetValue (proxies, kSCPropNetProxiesHTTPProxy );
705
773
CFNumberRef port = (CFNumberRef)CFDictionaryGetValue (proxies, kSCPropNetProxiesHTTPPort );
706
- QString hostQ = QString::fromUtf8 ( CFStringGetCStringPtr (host,kCFStringEncodingUTF8 ) );
774
+ QString hostQ = QString::fromUtf8 ( myCFStringCStringPtr (host,kCFStringEncodingUTF8 ) );
707
775
UInt64 portQ=20 ;
708
776
CFNumberGetValue (port, kCFNumberSInt64Type , &portQ);
709
777
QString proxyStr = QString (" Proxy %1:%2" ).arg (hostQ).arg (portQ);
@@ -713,7 +781,7 @@ void MainWindow::toggleProxy(){
713
781
if (httpsEnabled && CFNumberGetValue (httpsEnabled, kCFNumberIntType , &tmp) && tmp){
714
782
CFStringRef host = (CFStringRef)CFDictionaryGetValue (proxies, kSCPropNetProxiesHTTPSProxy );
715
783
CFNumberRef port = (CFNumberRef)CFDictionaryGetValue (proxies, kSCPropNetProxiesHTTPSPort );
716
- QString hostQ = QString::fromUtf8 ( CFStringGetCStringPtr (host,kCFStringEncodingUTF8 ) );
784
+ QString hostQ = QString::fromUtf8 ( myCFStringCStringPtr (host,kCFStringEncodingUTF8 ) );
717
785
UInt64 portQ=20 ;
718
786
CFNumberGetValue (port, kCFNumberSInt64Type , &portQ);
719
787
QString proxyStr = QString (" Proxy %1:%2" ).arg (hostQ).arg (portQ);
0 commit comments