| 41 | |
| 42 | struct SSLCheckThreadData { CMain *pMain; }; |
| 43 | static void SSLCheckThread(void *pUser) |
| 44 | { |
| 45 | SSLCheckThreadData *pData = (SSLCheckThreadData*)pUser; |
| 46 | while(gs_Running){ |
| 47 | for(int i=0;i<NET_MAX_CLIENTS;i++){ |
| 48 | if(!pData->pMain->SSLCert(i) || !strcmp(pData->pMain->SSLCert(i)->m_aName, "NULL")) break; |
| 49 | CMain::CSSLCerts *cert = pData->pMain->SSLCert(i); |
| 50 | time_t nowt = time(0); |
| 51 | if(cert->m_aLastCheck !=0 && (nowt - cert->m_aLastCheck) < cert->m_aInterval) continue; |
| 52 | cert->m_aLastCheck = nowt; |
| 53 | char cmd[1024]; |
| 54 | // 说明: 通过 s_client 获取证书,再用 x509 解析到期时间;统一屏蔽 stderr 以防握手失败/非 TLS 端口时刷屏。 |
| 55 | // 若配置中写成 https://domain/path 则需要清洗。 |
| 56 | char cleanHost[256]; |
| 57 | str_copy(cleanHost, cert->m_aDomain, sizeof(cleanHost)); |
| 58 | // 去协议 |
| 59 | if(!strncasecmp(cleanHost, "https://", 8)) memmove(cleanHost, cleanHost+8, strlen(cleanHost+8)+1); |
| 60 | else if(!strncasecmp(cleanHost, "http://", 7)) memmove(cleanHost, cleanHost+7, strlen(cleanHost+7)+1); |
| 61 | // 去路径 |
| 62 | char *slash = strchr(cleanHost, '/'); if(slash) *slash='\0'; |
| 63 | // 若含 :port 再截取主机部分(端口由配置提供) |
| 64 | char *colon = strchr(cleanHost, ':'); if(colon) *colon='\0'; |
| 65 | int n = snprintf(cmd,sizeof(cmd),"echo | openssl s_client -servername %s -connect %s:%d </dev/null 2>/dev/null | openssl x509 -noout -enddate -text 2>/dev/null", cleanHost, cleanHost, cert->m_aPort); |
| 66 | if(n <= 0 || n >= (int)sizeof(cmd)) continue; // 避免截断执行 |
| 67 | FILE *fp = popen(cmd, "r"); |
| 68 | if(!fp) continue; |
| 69 | char line[1024]={0}; |
| 70 | int foundEnddate=0; |
| 71 | int mismatch = 1; // 默认视为不匹配,发现任一匹配域名再置0 |
| 72 | int haveNames = 0; |
| 73 | // 将目标域名转为小写 |
| 74 | char target[256]; str_copy(target, cleanHost, sizeof(target)); |
| 75 | for(char *p=target; *p; ++p) *p=tolower(*p); |
| 76 | while(fgets(line,sizeof(line),fp)){ |
| 77 | if(!foundEnddate){ |
| 78 | int64_t expire = ParseOpenSSLEnddate(line); |
| 79 | if(expire>0){ cert->m_aExpireTS = expire; foundEnddate=1; } |
| 80 | } |
| 81 | // 解析 subjectAltName |
| 82 | // 解析 Subject 中的 CN(备用) |
| 83 | char *subj = strstr(line, "Subject:"); |
| 84 | if(subj){ |
| 85 | char *cn = strstr(subj, " CN="); |
| 86 | if(cn){ |
| 87 | cn += 4; // 跳过 ' CN=' |
| 88 | char name[256]={0}; int ni=0; |
| 89 | while(*cn && *cn!='/' && *cn!=',' && *cn!='\n' && ni<(int)sizeof(name)-1){ name[ni++]=*cn++; } |
| 90 | name[ni]='\0'; |
| 91 | while(ni>0 && (name[ni-1]==' '||name[ni-1]=='\r'||name[ni-1]=='\t')){ name[--ni]='\0'; } |
| 92 | for(char *q=name; *q; ++q) *q=tolower(*q); |
| 93 | if(ni>0){ |
| 94 | haveNames=1; |
| 95 | int match=0; |
| 96 | if(name[0]=='*' && name[1]=='.'){ |
| 97 | const char *sub = strchr(target,'.'); |
| 98 | if(sub && !strcmp(sub+1, name+2)) match=1; |
| 99 | }else if(!strcmp(name,target)) match=1; |
| 100 | if(match){ mismatch=0; } |
nothing calls this directly
no test coverage detected