自建跨域代理解决Gitalk无法登录的问题

Gitalk 一个基于 Github issuesPreact 开发的评论插件,在 Hexo NexT 主题中可以很方便的开启,并且简洁、轻量。

刚开始使用的时候挺稳的,但是近期出现登录不上的问题。通过查看浏览器的「网络面板」,发现 cors-anywhere.herokuapp.com 这个地址报错 403。这是一个实现跨域请求转发的服务,而 Gitalk 正是依赖它与 Github 进行通信。为什么会 403 呢,最后找到官方说明,大体意思就是:这个地址只是用来演示的,但是发现流量越来越大,所以要进行限流。

解决这个问题最好的办法是:自建

今天查看 NexT 官网,发现有人已经提供了解决方案参见这里

本文主要就是介绍如何自建这个跨域代理,实测可用的完整过程如下下文:

1. 注册 heroku

打开官网地址(需要科学上网):https://signup.heroku.com

2. 安装 heroku-cli

安装说明:https://devcenter.heroku.com/articles/heroku-cli

  • Windows

  • Ubuntu

    $ sudo apt-get install snap
    $ sudo snap install --classic heroku
  • macOS

    $ brew tap heroku/brew && brew install heroku

3. 在终端下登录 heroku

$ heroku login -i
› Warning: heroku update available from 7.52.0 to 7.53.0.
heroku: Enter your login credentials
Email: xxxxx@gmail.com
Password: *********
Logged in as xxxxx@gmail.com

4. 创建步骤

$ git clone https://github.com/Rob--W/cors-anywhere.git
$ cd cors-anywhere/
$ npm install
$ heroku create
$ git push heroku master
remote: -----> Compressing...
remote: Done: 32M
remote: -----> Launching...
remote: Released v3
remote: https://xxxxxxxxxx.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/xxxxxxxxxx.git
* [new branch] master -> master

5. 修改 Gitalk 设置

gitalk:
proxy: https://xxxxxxxxxx.herokuapp.com/https://github.com/login/oauth/access_token

6. 重新部署 Hexo

$ hexo clean && hexo g -d