Skip to content

Commit 67af31b

Browse files
committed
Conflicts: main.cpp mainwindow.cpp proxy/rypipedata.cpp proxy/rypipedata.h rythem_zh_CN.ts
2 parents ad6e51c + 09fa343 commit 67af31b

15 files changed

+156
-84
lines changed

Rythem.pro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ OTHER_FILES += \
152152
remove.png \
153153
rythem_zh_CN.ts \
154154
rythem_zh_CN.qm \
155-
deploy.txt \
156155
qt_zh_CN.qm \
157156
setupproxy \
158-
publish
157+
publish \
158+
deploy
159159
CONFIG(release){
160160
#DEFINES += DEBUGTOFILE
161161
#message("debug")

deploy.txt renamed to deploy

File renamed without changes.

macdeploy

Lines changed: 0 additions & 6 deletions
This file was deleted.

main.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
#include "proxy/proxyautoconfig.h"
2323
#endif
2424

25-
#ifdef Q_OS_LINUX
26-
#include "proxy/proxyautoconfig.h"
27-
#endif
28-
2925
#include <QtNetwork>
3026
QList<QNetworkProxy> macQueryInternal(const QNetworkProxyQuery &query);
3127

@@ -72,12 +68,9 @@ int main(int argc, char *argv[])
7268
}
7369

7470

75-
#ifndef Q_OS_LINUX
7671
#ifdef Q_OS_MAC
7772
ProxyAutoConfig::instance();
7873
#endif
79-
#endif
80-
8174
a.setQuitOnLastWindowClosed(false);
8275
appPath = qApp->applicationDirPath();
8376

@@ -115,7 +108,11 @@ int main(int argc, char *argv[])
115108
server->connect(server,SIGNAL(pipeBegin(RyPipeData_ptr)),&w,SLOT(onNewPipe(RyPipeData_ptr)));
116109
server->connect(server,SIGNAL(pipeComplete(RyPipeData_ptr)),&w,SLOT(onPipeUpdate(RyPipeData_ptr)));
117110
server->connect(server,SIGNAL(pipeError(RyPipeData_ptr)),&w,SLOT(onPipeUpdate(RyPipeData_ptr)));
118-
server->listen(QHostAddress("127.0.0.1"),8889);
111+
bool isListenSuccess = server->listen(QHostAddress::Any,8889);
112+
if(!isListenSuccess){
113+
return 0;
114+
}
115+
qDebug()<<"listen success";
119116

120117
//w.showMaximized();
121118
w.show();

mainwindow.cpp

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,42 @@ bool setProxy(const QSettings& proxySetting){
136136
#endif
137137
#ifdef Q_OS_MAC
138138

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+
139169
QString getServiceName(){
140170
// get primary serverId
141171
char *serviceName = NULL;
142172
CFStringRef primaryServiceId=NULL;
143173
SCDynamicStoreRef dynamicStoreDomainState = SCDynamicStoreCreate(NULL,
144-
CFSTR("myApplicationName"),
174+
CFSTR("Rythem"),
145175
NULL,
146176
NULL);
147177
// get primary interface
@@ -154,25 +184,49 @@ QString getServiceName(){
154184
n += CFStringGetLength(kSCEntNetIPv4);
155185
n += 3;// for three '/'
156186
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+
157197
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+
162215

163216
CFStringRef netIpv4KeyCFStr = CFStringCreateWithCString(NULL, netIp4KeyStr, kCFStringEncodingUTF8);
164217
CFPropertyListRef netIpv4List =SCDynamicStoreCopyValue(dynamicStoreDomainState, netIpv4KeyCFStr);
165218
if (netIpv4List) {
166219
primaryServiceId = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)netIpv4List, kSCDynamicStorePropNetPrimaryService);
220+
if(primaryServiceId){
221+
CFRetain(primaryServiceId);
222+
}
223+
CFRelease(netIpv4List);
167224
}
168-
if(primaryServiceId){
169-
CFRetain(primaryServiceId);
170-
}
171-
CFRelease(netIpv4List);
172225
CFRelease(netIpv4KeyCFStr);
173226
free(netIp4KeyStr);
174227
}
175228
if(!primaryServiceId){
229+
qDebug()<<"no service name found";
176230
return QString();
177231
}
178232
CFShow(primaryServiceId);
@@ -189,12 +243,20 @@ QString getServiceName(){
189243

190244
CFStringRef serviceId = SCNetworkServiceGetServiceID(service);
191245
CFShow(serviceId);
246+
CFShow(primaryServiceId);
192247
if( CFStringCompare(primaryServiceId,serviceId,0) == kCFCompareEqualTo ){
193248
CFStringRef name = SCNetworkServiceGetName(service);// Wi-Fi "Bluetooth DUN" etc.
194249
if(name){
195250
unsigned long l = CFStringGetLength(name) + 1;
251+
CFShow(name);
196252
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+
198260
}
199261
//break;
200262
}else{
@@ -219,6 +281,10 @@ QString getServiceName(){
219281

220282
bool execScript(const QString &script){
221283
QString service = getServiceName();
284+
if(service == NULL || service.isEmpty()){
285+
// TODO 提示
286+
return false;
287+
}
222288
QProcess process;
223289
qDebug()<<"script"<<script.arg(service);
224290
QStringList args = script.arg(service).split(" ");
@@ -251,6 +317,10 @@ bool disableAutoProxyAndSetProxyForService(const QString& host, const int port){
251317
bool execProxysetting(const QString &pacUrl=QString()){
252318
QProcess process;
253319
QString service = getServiceName();
320+
if(service == NULL || service.isEmpty()){
321+
// TODO 提示
322+
return false;
323+
}
254324
QString script = "proxysetting";
255325
if(pacUrl.isEmpty()){
256326
script = QString(appPath+"/proxysetting --disablepac %1").arg(service);
@@ -415,14 +485,7 @@ MainWindow::MainWindow(QWidget *parent) :
415485
connect(ui->ActionCapture,SIGNAL(triggered()),SLOT(toggleCapture()));
416486
connect(ui->actionRemoveAll,SIGNAL(triggered()),this,SLOT(onActionRemoveAll()));
417487
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+
426489
checker = new RyUpdateChecker(this);
427490
QTimer timer;
428491
timer.singleShot(1000,this,SLOT(checkNewVersion()));
@@ -662,7 +725,7 @@ void MainWindow::onWaterfallActionTriggered(){
662725

663726
void MainWindow::toggleProxy(){
664727
QMutexLocker locker(&proxyMutex);
665-
#ifdef Q_WS_MAC
728+
#ifdef Q_OS_MAC
666729
bool setProxySuccess = false;
667730
if(_isUsingCapture){
668731
if(_previousProxyInfo.isUsingPac == "1"){
@@ -678,17 +741,22 @@ void MainWindow::toggleProxy(){
678741
int isPacEnabled = 0;
679742
//CFStringRef CFPacUrl = (CFStringRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesProxyAutoConfigURLString);
680743
//if(CFPacUrl){
681-
// _previousProxyInfo.pacUrl = QString::fromUtf8( CFStringGetCStringPtr(CFPacUrl,kCFStringEncodingUTF8) );
744+
// _previousProxyInfo.pacUrl = QString::fromUtf8( myCFStringCStringPtr(CFPacUrl,kCFStringEncodingUTF8) );
682745
//}
683746
if (proxies){
684-
CFNumberRef pacEnabled;
747+
CFNumberRef tmpRef;
685748
//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){
688756
isPacEnabled = 1;
689757
_previousProxyInfo.isUsingPac = QString("1");
690758
CFStringRef CFPacUrl = (CFStringRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesProxyAutoConfigURLString);
691-
_previousProxyInfo.pacUrl = QString::fromUtf8( CFStringGetCStringPtr(CFPacUrl,kCFStringEncodingUTF8) );
759+
_previousProxyInfo.pacUrl = QString::fromUtf8( myCFStringCStringPtr(CFPacUrl,kCFStringEncodingUTF8) );
692760
}
693761
}
694762
}
@@ -703,7 +771,7 @@ void MainWindow::toggleProxy(){
703771
if (httpEnabled && CFNumberGetValue(httpEnabled, kCFNumberIntType, &tmp) && tmp){
704772
CFStringRef host = (CFStringRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesHTTPProxy);
705773
CFNumberRef port = (CFNumberRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesHTTPPort);
706-
QString hostQ = QString::fromUtf8( CFStringGetCStringPtr(host,kCFStringEncodingUTF8) );
774+
QString hostQ = QString::fromUtf8( myCFStringCStringPtr(host,kCFStringEncodingUTF8) );
707775
UInt64 portQ=20;
708776
CFNumberGetValue(port, kCFNumberSInt64Type, &portQ);
709777
QString proxyStr = QString("Proxy %1:%2").arg(hostQ).arg(portQ);
@@ -713,7 +781,7 @@ void MainWindow::toggleProxy(){
713781
if (httpsEnabled && CFNumberGetValue(httpsEnabled, kCFNumberIntType, &tmp) && tmp){
714782
CFStringRef host = (CFStringRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesHTTPSProxy);
715783
CFNumberRef port = (CFNumberRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesHTTPSPort);
716-
QString hostQ = QString::fromUtf8( CFStringGetCStringPtr(host,kCFStringEncodingUTF8) );
784+
QString hostQ = QString::fromUtf8( myCFStringCStringPtr(host,kCFStringEncodingUTF8) );
717785
UInt64 portQ=20;
718786
CFNumberGetValue(port, kCFNumberSInt64Type, &portQ);
719787
QString proxyStr = QString("Proxy %1:%2").arg(hostQ).arg(portQ);

proxy/ryconnection.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ void RyConnection::onRequestReadyRead(){
9898
_receivingPerformance.requestBegin = QDateTime::currentMSecsSinceEpoch();
9999
}
100100
QByteArray newContent = _requestSocket->readAll();
101+
qDebug()<<newContent;
101102
if(_isConnectTunnel){
102103
// https隧道,直接透传不解析
103104
// TODO: decrypt https tunnel
@@ -576,8 +577,10 @@ bool RyConnection::checkLocalWebServer(RyPipeData_ptr& pipe){
576577
QString host = pipe->host;
577578
quint16 port = pipe->port;
578579
// check if is request to self
579-
//qDebug()<<host<<QString::number(port);
580-
if(host == RyProxyServer::instance()->serverAddress().toString()
580+
qDebug()<<"checking local:"<<host<<QString::number(port);
581+
582+
QHostAddress h(host);
583+
if(h.isLoopback()
581584
&& port == RyProxyServer::instance()->serverPort()){
582585
// TODO: move these mapping to global util
583586
qDebug()<<"simple http"<<pipe->path;

proxy/rypipedata.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,21 @@ bool RyPipeData::parseRequestHeader(const QByteArray& headers){
134134
path = "/";
135135
port = 80;//TODO
136136

137-
QString withouProtocol = fullUrl;
137+
QString withoutProtocol = fullUrl;
138138
int indexOfHost = fullUrl.indexOf("://");
139139
if(indexOfHost!=-1){
140-
withouProtocol = fullUrl.mid(indexOfHost+3);
140+
withoutProtocol = fullUrl.mid(indexOfHost+3);
141141
}
142142

143143
if(indexOfHost==-1){
144144
// request after CONNECT tunnel
145145
path = fullUrl;
146146
}
147-
QString hostAndPort = withouProtocol;
148-
int indexOfPath = withouProtocol.indexOf("/");
147+
QString hostAndPort = withoutProtocol;
148+
int indexOfPath = withoutProtocol.indexOf("/");
149149
if(indexOfPath!=-1){
150-
hostAndPort = withouProtocol.left(indexOfPath);
151-
path = withouProtocol.mid(indexOfPath);
150+
hostAndPort = withoutProtocol.left(indexOfPath);
151+
path = withoutProtocol.mid(indexOfPath);
152152
}
153153
int indexOfPort = hostAndPort.indexOf(":");
154154
host = hostAndPort;
@@ -159,6 +159,8 @@ bool RyPipeData::parseRequestHeader(const QByteArray& headers){
159159
if(method == "CONNECT"){
160160
isConnectTunnel = true;
161161
fullUrl.prepend("http://");
162+
}else if(host == hostAndPort){
163+
host = "";
162164
}
163165

164166
//qDebug()<<"host="<<host<<port;
@@ -178,10 +180,10 @@ bool RyPipeData::parseRequestHeader(const QByteArray& headers){
178180
if(headerName == "Host"){
179181
int d = value.indexOf(":");
180182
if(d!=-1){
181-
host = value.left(d);
183+
virtualHost = value.left(d);
182184
port = value.mid(d+1).trimmed().toInt();
183185
}else{
184-
host = value;
186+
virtualHost = value;
185187
}
186188
}
187189

@@ -190,7 +192,9 @@ bool RyPipeData::parseRequestHeader(const QByteArray& headers){
190192
_dataToSend.append(value);
191193
_dataToSend.append("\r\n");
192194
}
193-
195+
if(host.isEmpty()){
196+
host = virtualHost;
197+
}
194198
if(path == fullUrl){
195199
if(method!="CONNECT"){
196200
if(port == 80){
@@ -258,7 +262,7 @@ bool RyPipeData::parseResponseHeader(const QByteArray& headers){
258262
}
259263

260264

261-
const QByteArray& RyPipeData::dataToSend(bool sendToProxy)const{
265+
const QByteArray RyPipeData::dataToSend(bool sendToProxy)const{
262266
if(sendToProxy){
263267
return _sigToSendForProxy + _dataToSend;
264268
}else{

proxy/rypipedata.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class RyPipeData : public QObject
2323
bool parseRequestHeader(const QByteArray&);
2424
bool parseResponseHeader(const QByteArray&);
2525

26-
const QByteArray& dataToSend(bool sendToProxy = false)const;
26+
const QByteArray dataToSend(bool sendToProxy = false)const;
2727
bool appendRequestBody(QByteArray* newData);
2828

2929
int readChunk(int chunkSize,QByteArray* chunkData,QByteArray* unChunkDes);
@@ -41,6 +41,7 @@ class RyPipeData : public QObject
4141
int socketHandle; // socket really handle
4242
quint64 socketConnectionId;//connection id in rythem
4343
QString host;
44+
QString virtualHost; // for header
4445
QString replacedHost;
4546
quint16 port;
4647
QString method;

proxy/ryproxyserver.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ QTcpSocket* RyProxyServer::getSocket(QString address,quint16 port,bool* isFromCa
105105
//
106106
// when new connection come and has the same handle.
107107
// reuse old RyConnection
108-
void RyProxyServer::incomingConnection(int handle){
108+
void RyProxyServer::incomingConnection(qintptr handle){
109+
qDebug()<<"incomingConnection";
109110
if(isStoping){
110111
qDebug()<<"incommingConnection "<<isStoping;
111112
return;

0 commit comments

Comments
 (0)