给Windows中执行的wget设置证书包文件

在Windows中执行时,wget会在四个地方查找wgetrc配置文件,按照优先级高低它们分别是:

  • 环境变量WGETRC指向的文件。
  • 如果设置了环境变量HOME,则配置文件为%HOME%\.wgetrc。
  • wget.exe所在目录的.wgetrc。
  • wget.exe所在目录的wget.ini。

配置实例

现在,使用https的下载站点越来越多。如果不在wget配置文件里设置好ca-certificate参数,每次用wget从https网站下载时都必须在命令中加入--ca-certificate参数或者--no-check-certificate参数,比较繁琐。最简单的而且全局有效的配置方法就是把ca-certificate参数写入wget.exe所在目录的配置文件,即wget.ini或.wgetrc;其中wget.ini后缀符合Windows的规范,打开编辑更方便一些。若wget.exe保存在C:\Tools目录,则只须在C:\Tools目录创建以下两个文件:

1. 配置文件C:\Tools\wget.ini

ca-certificate=C:\Tools\ca-certificates.crt

2. 证书包文件C:\Tools\ca-certificates.crt

CA证书包文件ca-certificates.crt可以从Linux系统中复制过来,也可以从https://github.com/bagder/ca-bundle下载。Debian/Ubuntu系统中CA证书包文件是/etc/ssl/certs/ca-certificates.crt;RHEL/CentOS系统中CA证书包文件是/etc/pki/tls/certs/ca-bundle.crt。

参考

  1. GNU Wget主页
  2. 一个维护得不错的用MinGW编译的wget.exe: https://eternallybored.org/misc/wget/
  3. wget源代码wget-1.16.3/src/init.c中的wgetrc_file_name()函数:
  4. /* Return the path to the user's .wgetrc.  This is either the value of
       `WGETRC' environment variable, or `$HOME/.wgetrc'.
    
       Additionally, for windows, look in the directory where wget.exe
       resides.  */
    char *
    wgetrc_file_name (void)
    {
      char *file = wgetrc_env_file_name ();
      if (file && *file)
        return file;
    
      file = wgetrc_user_file_name ();
    
    #ifdef WINDOWS
      /* Under Windows, if we still haven't found .wgetrc, look for the file
         `wget.ini' in the directory where `wget.exe' resides; we do this for
         backward compatibility with previous versions of Wget.
         SYSTEM_WGETRC should not be defined under WINDOWS.  */
      if (!file)
        {
          char *home = home_dir ();
          xfree (file);
          home = ws_mypath ();
          if (home)
            {
              file = aprintf ("%s/wget.ini", home);
              if (!file_exists_p (file))
                {
                  xfree (file);
                }
              xfree (home);
            }
        }
    #endif /* WINDOWS */
    
      return file;
    }
    

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.