| 1 | #include "cNetStat.h" |
| 2 | #include <QDir> |
| 3 | #include <QFile> |
| 4 | #include <QTextStream> |
| 5 | #include <QDebug> |
| 6 | #include <QProcess> |
| 7 | #include <QRegExp> |
| 8 | #include <QRegularExpression> |
| 9 | |
| 10 | cNetStat::cNetStat(){ |
| 11 | |
| 12 | } |
| 13 | |
| 14 | |
| 15 | void cNetStat::run(){ |
| 16 | getNetStat(); |
| 17 | emit endReadStat(save_netstat); |
| 18 | } |
| 19 | |
| 20 | QVector<sNetStat> cNetStat::getTable(){ |
| 21 | sNetStat netstat; |
| 22 | QVector<sNetStat> result; |
| 23 | |
| 24 | |
| 25 | #if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) |
| 26 | QVector<QString> prots; // Протоколы |
| 27 | QList<QString> states; // Статусы подключений |
| 28 | |
| 29 | // Список протоколов |
| 30 | prots << "tcp" << "udp" << "tcp6" << "udp6" << "icmp"; |
| 31 | |
| 32 | // Массив статусов подключений |
| 33 | states << "" << "ESTABLISHED" << "SYN_SENT" << "SYN_RECV" << "FIN_WAIT1" << "FIN_WAIT2"; |
| 34 | states << "TIME_WAIT" << "CLOSE" << "CLOSE_WAIT" << "LAST_ACK" << "LISTEN"; |
| 35 | states << "CLOSING" << "CLOSED"; |
| 36 | |
| 37 | // Обновление списка процесов |
| 38 | ProcessListLinux proc; |
| 39 | proc.initProccessList(); |
| 40 | |
| 41 | |
| 42 | for (int p=0;p<prots.size();p++){ |
| 43 | QFile file("/proc/net/"+prots.at(p)); |
| 44 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){ |
| 45 | qDebug() << file.errorString(); |
| 46 | return result; |
| 47 | } |
| 48 | |
| 49 | QTextStream in(&file); |
| 50 | QStringList buf = in.readAll().split("\n"); |
| 51 | file.close(); |
| 52 | for (int i=1;i<buf.size()-1;i++){ |
| 53 | //("", "sl", "local_address", "rem_address", "st", "tx_queue", "rx_queue", "tr", "tm->when", "retrnsmt", "uid", "timeout", "inode", "") |
| 54 | //("", "18:", "4B80A8C0:DA90", "5480A8C0:008B", "01", "00000000:00000000", "02:000A6A42", "00000000", "500", "0", "7793475", "2", "d6798fc0", "20", "3", "32", "10", "-1", "") |
| 55 | buf[i] = " "+buf.at(i); |
| 56 | QStringList ns = buf.at(i).split(QRegularExpression("\\s+")); |
| 57 | |
| 58 | netstat.prot=prots.at(p).toUpper(); |
| 59 | netstat.local_address=convAddress(ns.at(2),netstat.prot); |
| 60 | netstat.rem_address=convAddress(ns.at(3),netstat.prot); |
| 61 | netstat.stat=states[ns.at(4).toInt(0,16)]; |
| 62 | netstat.process = proc.getProc(ns.at(10)); |
| 63 | |
| 64 | result.append(netstat); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #endif |
| 69 | |
| 70 | |
| 71 | #if defined(Q_OS_WIN) |
| 72 | //Имя Локальный адрес Внешний адрес Состояние PID |
| 73 | //TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 3480 |
| 74 | //TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1708 |
| 75 | |
| 76 | ProcessListWindows proc; |
| 77 | proc.initProccessList(); |
| 78 | |
| 79 | |
| 80 | QProcess *myProcess = new QProcess(); |
| 81 | myProcess->start("netstat.exe", QStringList() << "-n" << "-a" << "-o"); |
| 82 | |
| 83 | if (myProcess->waitForFinished(2000)==false){ |
| 84 | qDebug() << myProcess->errorString(); |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | QStringList table = QString(myProcess->readAll()).split("\n"); |
| 89 | for (int t=3;t<table.size();t++){ |
| 90 | QStringList ns = table.at(t).split(QRegExp("\\s+")); |
| 91 | |
| 92 | //qDebug() << ns; |
| 93 | |
| 94 | if (ns.size()<2) continue; |
| 95 | |
| 96 | if (ns.at(1)=="TCP"){ |
| 97 | netstat.prot=ns.at(1); |
| 98 | netstat.local_address=ns.at(2); |
| 99 | netstat.rem_address=ns.at(3); |
| 100 | netstat.stat=ns.at(4); |
| 101 | netstat.process = proc.getProc(ns.at(5)); |
| 102 | result.append(netstat); |
| 103 | } |
| 104 | if (ns.at(1)=="UDP"){ |
| 105 | netstat.prot=ns.at(1); |
| 106 | netstat.local_address=ns.at(2); |
| 107 | netstat.rem_address=ns.at(3); |
| 108 | netstat.stat=""; |
| 109 | netstat.process = proc.getProc(ns.at(4)); |
| 110 | |
| 111 | netstat.rem_address.replace("*:*","0.0.0.0:0"); |
| 112 | netstat.rem_address.replace("*:*","0.0.0.0:0"); |
| 113 | result.append(netstat); |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | |
| 118 | #endif |
| 119 | |
| 120 | |
| 121 | #if defined(Q_OS_OS2) |
| 122 | //SOCK TYPE FOREIGN LOCAL FOREIGN STATE |
| 123 | // PORT PORT HOST |
| 124 | //2302 STREAM 0 6881 0.0.0.0 LISTEN |
| 125 | //2304 DGRAM 0 6881 0.0.0.0 UDP |
| 126 | |
| 127 | QProcess *myProcess = new QProcess(); |
| 128 | myProcess->start("netstat", QStringList() << "-s"); |
| 129 | |
| 130 | if (myProcess->waitForFinished(2000)==false){ |
| 131 | qDebug() << myProcess->errorString(); |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | QString procData = myProcess->readAll(); |
| 136 | QStringList table = QString(procData).split("\n"); |
| 137 | |
| 138 | for (int t=10;t<table.size();t++){ |
| 139 | |
| 140 | QStringList ns = table.at(t).split(QRegExp("\\s+")); |
| 141 | |
| 142 | /* |
| 143 | writeLog(table.at(t)); |
| 144 | QString d=""; |
| 145 | for (int l=0;l<ns.size();l++){ |
| 146 | d+=ns.at(l)+" {"+QString::number(l)+"}| "; |
| 147 | } |
| 148 | writeLog("["+QString::number(ns.size())+"]"); |
| 149 | writeLog(d); |
| 150 | writeLog("-----\n"); |
| 151 | */ |
| 152 | |
| 153 | if (ns.size()!=8) return result; |
| 154 | |
| 155 | |
| 156 | QString foreign_port=ns.at(3); |
| 157 | if (foreign_port.split("..").size()==2) foreign_port=foreign_port.split("..").at(1); |
| 158 | |
| 159 | QString local_port=ns.at(4); |
| 160 | if (local_port.split("..").size()==2) local_port=local_port.split("..").at(1); |
| 161 | |
| 162 | if (ns.at(6)=="CLOSED") continue; |
| 163 | |
| 164 | if (ns.at(6)=="UDP"){ |
| 165 | netstat.stat="CLOSE"; |
| 166 | netstat.prot="UDP"; |
| 167 | }else{ |
| 168 | netstat.stat=ns.at(6); |
| 169 | netstat.prot="TCP"; |
| 170 | } |
| 171 | |
| 172 | netstat.local_address="0.0.0.0:"+local_port; |
| 173 | netstat.rem_address=ns.at(5)+":"+foreign_port; |
| 174 | netstat.process = sProccess(); |
| 175 | result.append(netstat); |
| 176 | |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | QVector<sNetStat> cNetStat::getNetStat(){ |
| 188 | |
| 189 | // Получение данных о сетевых процессах |
| 190 | sNetStat netstat; |
| 191 | QVector<sNetStat> result; |
| 192 | |
| 193 | |
| 194 | |
| 195 | QVector<sNetStat> TableNet = getTable(); |
| 196 | |
| 197 | for (int i=0;i<TableNet.size();i++){ |
| 198 | netstat=TableNet.at(i); |
| 199 | |
| 200 | if (netstat.local_address.split(":").at(0)=="0.0.0.0") netstat.local_address="*:"+netstat.local_address.split(":").at(1); |
| 201 | if (netstat.rem_address=="0.0.0.0:0") netstat.rem_address="*"; |
| 202 | |
| 203 | |
| 204 | netstat.operation=-1; |
| 205 | if (save_netstat.size()>0){ |
| 206 | for (int s=0;s<save_netstat.size();s++){ |
| 207 | if (netstat.local_address==save_netstat.at(s).local_address and netstat.rem_address==save_netstat.at(s).rem_address){ |
| 208 | if (QTime::currentTime() < save_netstat.at(s).timeEvent.addSecs(5)){ |
| 209 | netstat.operation=save_netstat.at(s).operation; |
| 210 | }else{ |
| 211 | netstat.operation=0; |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | if (netstat.operation==-1){ |
| 217 | netstat.operation=1; |
| 218 | netstat.timeEvent=QTime::currentTime(); |
| 219 | } |
| 220 | }else{ |
| 221 | netstat.operation=0; |
| 222 | } |
| 223 | result.append(netstat); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | // Показ старых подключений |
| 228 | bool isExistProc=false; |
| 229 | for (int p=0;p<save_netstat.size();p++){ |
| 230 | isExistProc=false; |
| 231 | for (int n=0;n<result.size();n++){ |
| 232 | if (save_netstat.at(p).local_address==result.at(n).local_address and save_netstat.at(p).rem_address==result.at(n).rem_address){ |
| 233 | isExistProc=true; |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | if (isExistProc==false){ |
| 238 | if (save_netstat.at(p).operation==2){ |
| 239 | if (QTime::currentTime() < save_netstat.at(p).timeEvent.addSecs(5)){ |
| 240 | netstat=save_netstat.at(p); |
| 241 | result.append(netstat); |
| 242 | } |
| 243 | }else{ |
| 244 | netstat=save_netstat.at(p); |
| 245 | netstat.operation=2; |
| 246 | netstat.timeEvent=QTime::currentTime(); |
| 247 | result.append(netstat); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | } |
| 252 | |
| 253 | |
| 254 | |
| 255 | save_netstat=result; |
| 256 | |
| 257 | return result; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | |
| 262 | QString cNetStat::convAddress(QString address,QString ipv){ |
| 263 | // Конвертирование IP адреса из 16-ричной в 10-ричную систему. |
| 264 | QString result=""; |
| 265 | QStringList octet; |
| 266 | bool ok; |
| 267 | |
| 268 | QString a= address.split(":").at(0); |
| 269 | QString p= address.split(":").at(1); |
| 270 | |
| 271 | |
| 272 | |
| 273 | if (ipv=="TCP6" or ipv=="UDP6" or ipv=="ICMP6"){ |
| 274 | // Незнаю как тут правильно |
| 275 | for (int i=16;i>0;i=i-4){ |
| 276 | octet.append(a.mid(i-4,4)); |
| 277 | } |
| 278 | result=octet.join(":"); |
| 279 | }else{ |
| 280 | for (int i=8;i>0;i=i-2){ |
| 281 | octet.append(QString::number(a.mid(i-2,2).toInt(&ok,16))); |
| 282 | } |
| 283 | result=octet.join("."); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | result+=":"+QString::number(p.toInt(&ok,16)); |
| 288 | |
| 289 | return result; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | void cNetStat::writeLog(QString str){ |
| 294 | QFile file(QDir::currentPath()+"/debug.log"); |
| 295 | |
| 296 | if (!file.open(QIODevice::Append | QIODevice::Text)) |
| 297 | return; |
| 298 | |
| 299 | QTextStream out(&file); |
| 300 | out << str << "\n"; |
| 301 | file.close(); |
| 302 | } |