小工具,检测域名SSL证书到期日期

@Ta 2023-01-04 3749点击

源码:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#include <time.h>

int timestamp_conversion(int n, char *temp)
{
    char str[100];
    struct tm *time;
    uint16_t year, yday;
    uint8_t month, day, week, hour, minute, second;
    time_t timestamp = n;       /*北京时间2020-06-24 01:16:51 */

    /* 北京时间补偿 */
    //timestamp += 8*60*60;

    /* 调用系统函数 */
    time = localtime(&timestamp);

    year = time->tm_year;       /* 自1900年算起 */
    month = time->tm_mon;       /* 从1月算起,范围0-11 */
    week = time->tm_wday;       /* 从周末算起,范围0-6 */
    yday = time->tm_yday;       /* 从1月1日算起,范围0-365 */
    day = time->tm_mday;        /* 日: 1-31 */
    hour = time->tm_hour;       /* 小时:0-23点,UTC+0时间 */
    minute = time->tm_min;      /* 分钟:0-59 */
    second = time->tm_sec;      /* 0-60,偶尔出现的闰秒 */

    /* 时间校正 */
    year += 1900;
    month += 1;
    week += 1;

    //printf("UNIX时间戳:%ld\r\n", timestamp);
    //printf("日期:%d%d%d 第%d天 星期%d 时间:%d:%d:%d\r\n", year, month, day, yday, week, hour, minute, second);

    /* 格式化时间字符串 */
    strftime(str, 100, "%F %T", time); /* 2020-07-01 02:16:51 */

    //printf("%s\r\n", str);

    sprintf(temp, "日期:%d%d%d 第%d天 星期%d 时间:%d:%d:%d\n", year, month, day, yday, week, hour, minute, second);

    return 0;

}

int main(int argc, char *argv[])
{
    CURL *curl;                 //定义CURL类型的指针
    CURLcode res;               //定义CURLcode类型的变量,保存返回状态码

    struct tm *tmp_time;
    time_t t;
    char temp[1024], domain[1024];
    char *p1, *p2;

    FILE *file;
    long file_size;
    char *buff;

    FILE *redirect;
    if ((redirect = fopen("/tmp/libcurl.log", "wt+")) == NULL) {
        return 1;
    }

    FILE *outstream;
    if ((outstream = fopen("/tmp/libcurl-out.log", "wt+")) == NULL) {
        return 1;
    }

    if (argv[1] == NULL) {
        printf("%s \"domain\"\n", argv[0]);
        exit(0);
    }

    p1 = strstr(argv[1], "https");
    if (p1) {
        strcpy(domain, argv[1]);
    } else {
        strcpy(domain, "https://");
        strcat(domain, argv[1]);
    }
    //printf("%s\n", domain);

    //初始化SSL and Win32运行环境
    curl_global_init(CURL_GLOBAL_DEFAULT);

    curl = curl_easy_init();    //初始化一个CURL类型的指针
    if (curl != NULL) {
        //设置curl选项. 其中CURLOPT_URL是让用户指 定url. argv[1]中存放的命令行传进来的网址
        curl_easy_setopt(curl, CURLOPT_URL, domain);

        //用第三方机构颁发的CA证书解密服务器端返回的证书并验证合法性,默认1L开启检查,0L忽略检查
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);

        //验证返回的证书是否与请求的域名相同,避免被篡改证书文件,默认值2L,0L忽略检查
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);

        curl_easy_setopt(curl, CURLOPT_CAPATH, "./"); //ca证书路径
        //curl_easy_setopt(curl,CURLOPT_CAINFO,"cacert.pem");//curl官网下载的根证书

        //设置curl选项,其中CURLOPT_VERBOSE是显示执行过程中的调试信息 输出到控制台
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        //不显示进度,如果设置为0L,则显示处理的进度条
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);

        //将stderr重定向到另一个流
        curl_easy_setopt(curl, CURLOPT_STDERR, redirect);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, outstream);

        //调用curl_easy_perform 执行我们的设置.并进行相关的操作. 在这里只在屏幕上显示出来.
        res = curl_easy_perform(curl);

        //检查错误信息输出到控制台
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perfrom() failed:%s\n", curl_easy_strerror(res));
        }
        //清除curl操作.
        curl_easy_cleanup(curl);
    }

    curl_global_cleanup();
    fclose(redirect);
    fclose(outstream);

    file = fopen("/tmp/libcurl.log", "r");
    if (file == NULL) {
        perror("cannot open config file.");
        exit(-1);
    }
    fseek(file, 0, SEEK_END);
    file_size = ftell(file);
    buff = (char *)alloca(file_size + 1);
    if (buff == NULL)
        perror("out of memory.");
    rewind(file);
    if (fread(buff, file_size, 1, file) < 1) {
        perror("fread");
    }
    fclose(file);
    buff[file_size] = '\0';

    p1 = strstr(buff, "expire date: ");
    p2 = strstr(p1, "\n");

    memset(temp, 0, 1024);
    memcpy(temp, p1 + 13, (p2 - p1) - 13);
    //printf("%s\n", temp);

    //setenv("TZ", "GMT-8", 1);     //时区设置为东8区
    //setenv("TZ", "GMT+0", 1);     //将当前时区设置成标准区
    tmp_time = (struct tm *)malloc(sizeof(struct tm));
    memset(tmp_time, 0, sizeof(struct tm));

    // Dec 19 02:11:06 2023 GMT
    strptime(temp, "%b %d %H:%M:%S %Y", tmp_time);
    t = mktime(tmp_time);
    //printf("%ld\n", t);

    memset(temp, 0, 1024);
    timestamp_conversion(t, temp);
    printf("%s %s", domain, temp);
    
    
    remove("/tmp/libcurl.log");
    remove("/tmp/libcurl-out.log");

    free(tmp_time);
    return 0;
}






编译:
gcc -Wall -g -Os -D_GNU_SOURCE -D__USE_XOPEN 1.c -lcurl

示列:
1672797082677.png(20.43 KB)

回复列表(0|隐藏机器人聊天)
帖子没有回复
添加新回复
回复需要登录