雾雾的百宝箱

【反代】反代imgur&反代谷歌翻译&反代dockerhub

反代imgur

事因是巴哈姆特的某个博客用了一大堆的imgur的图,然后我不管怎么挂代理都是404 ,imgur的域名隔三岔五挂那个啥都上不去 (试了一下,就是代理自己的问题),所以就得查查有没有什么人搞反代了。

前面一部分基本上照抄自转载地址博文的nginx配置,下面是纯搬运。

不过首先有个准备工作,我是用的宝塔面板。

先把域名解析到VPS的ip上,然后新建反代用二级域名的网站,设为“静态”,然后申请好SSL证书。再修改nginx配置文件的相关内容。(后面两个相同)

# 作者(Author):Mashiro
# 链接(URL):https://2heng.xin/2018/06/06/javascript-upload-images-with-imgur-api/
# 来源(Source):樱花庄的白猫

server {
    ......
    # 图片文件镜像
    location ^~ /imgur/ {
        proxy_pass https://i.imgur.com/;
        proxy_buffering off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 取消下面的注释可以启用 Nginx 缓存
        #proxy_cache            STATIC;
        #proxy_cache_key $uri;  
        #proxy_cache_valid      200  30d;
        #proxy_cache_use_stale  error timeout invalid_header updating
        #                       http_500 http_502 http_503 http_504;
        #add_header X-Nginx-Cache $upstream_cache_status;
    }
 
    # API 镜像
    location ^~ /imgur-api/ {
        # 跨域,注意不要和前面的跨域策略冲突/重复
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 
                   'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
 
        proxy_pass https://api.imgur.com/;
        proxy_buffering off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    ......
}

后面是问的Kimi帮我写了个油猴脚本,批量替换网页中imgur图片:

// ==UserScript==
// @name         Imgur Image Proxy for Dynamic Images
// @namespace   http://tampermonkey.net/
// @version      1.4
// @description  Automatically proxy imgur images in data-src or data-srcset attributes
// @author       YourName
// @match        http://*/*
// @match        https://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 定义一个函数来添加代理前缀
    function addProxyToSrc(imgSrc) {
        // 匹配imgur的URL
        const imgurPattern = /^https?:\/\/i\.imgur\.com\//i;
        if (imgurPattern.test(imgSrc)) {
            // 构建代理URL,保留原始URL的查询参数
            const url = new URL(imgSrc);
            return 'https://images.weserv.nl/?url=' + encodeURIComponent(url.origin + url.pathname + url.search);
        }
        return imgSrc;
    }

    // 获取页面中所有的img标签
    const images = document.querySelectorAll('img');

    // 遍历所有的img标签
    images.forEach(img => {
        // 检查data-src属性
        if (img.getAttribute('data-src')) {
            img.setAttribute('data-src', addProxyToSrc(img.getAttribute('data-src')));
        }
        // 检查data-srcset属性
        if (img.getAttribute('data-srcset')) {
            img.setAttribute('data-srcset', addProxyToSrc(img.getAttribute('data-srcset')));
        }
    });

    // 观察img元素的data-src和data-srcset属性变动
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.type === 'attributes' && (mutation.attributeName === 'data-src' || mutation.attributeName === 'data-srcset')) {
                const proxySrc = addProxyToSrc(mutation.target.getAttribute(mutation.attributeName));
                mutation.target.setAttribute(mutation.attributeName, proxySrc);
            }
        });
    });

    // 配置观察者,观察所有img元素的data-src和data-srcset属性变动
    observer.observe(document.body, {
        attributes: true,
        childList: false,
        subtree: true,
        attributeFilter: ['data-src', 'data-srcset']
    });
})();

使用前:

使用后:

上面脚本中使用的是另外一个imgur的图片镜像,可以直接用,也省得各位到处翻了。

类似的图片镜像还可以参考这篇文章

[tips]上面提到的反代方法可以通过markdown等形式显示图片,但是不能通过直链访问,也不能在memos上直接保存。绕路的话一个是可以通过剪贴板复制图片(微博,qq一类的都可以复制),另外一个也可以用local image一类的obsidian插件来辅助进行保存。[/tips]

反代谷歌api

某个翻译插件的issues下面看到的,正好我的沉浸式翻译以及copytranslator也需要这个,就顺带弄一个吧。

server {
    listen 443 ssl http2;
    server_name fy.example.com;
    access_log off;

    ssl_certificate /etc/nginx/cert/example.com/fullchain.pem;
    ssl_certificate_key /etc/nginx/cert/example.com/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:bw:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";

    location / {
        proxy_pass https://translate.google.com.hk;
        proxy_set_header Host 'translate.google.com.hk';
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Accept-Encoding "";
        sub_filter_types *;
        sub_filter_once off;
        sub_filter 'translate.google.com.hk' $host;
    }
}

和前面一样就在宝塔面板下面,插入反代部分的代码就行。

假设fy.example.com是自己的域名,要配置好SSL证书,以上配置只需替换自己域名和证书路径即可,其他配置项不用动。

宝塔面板的话就是先建站——解析域名过来——申请SSL——配置文件修改就行。(上面的imgur是完全一样的)

反代docker hub

参考地址:https://mp.weixin.qq.com/s/cAqI37a0YxzRRCI6G3UVnA

下面的部分我们用https://dockermirror.example.com代替使用的反代网址。

    location / {
        proxy_pass https://registry-1.docker.io;  
        proxy_set_header Host registry-1.docker.io;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;               
        proxy_buffering off;
        proxy_set_header Authorization $http_authorization;
        proxy_pass_header  Authorization;
        proxy_intercept_errors on;
        recursive_error_pages on;
        error_page 301 302 307 = @handle_redirect;
    }

    location @handle_redirect {
        resolver 1.1.1.1;
        set $saved_redirect_location '$upstream_http_location';
        proxy_pass $saved_redirect_location;
    }

镜像拉取需要修改相关域名:

sudo tee /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": ["https://dockermirror.example.com"]
}
EOF

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »