一、注册

先访问https://dash.cloudflare.com/login,点“sign up”进行帐户注册

image-20240625094746792

输入邮箱,及cloudflare上的密码,注意不是邮箱的密码。确认不是“机器”注册后,点”sign up”,然后邮箱会收到激活邮件。

image-20240625094911087

打开邮箱,点击“激活链接”,激活帐户。

image-20240625095259769

打开cloudflare,进行登录

image-20240625095457508

成功登录后,在右上角,点“添加站点”。

image-20240625095656763

image-20240625095746510

选择免费

image-20240625095823419

系统会自动检测刚填写的域名,如果域名不在cloudflare解析,则系统会提示在dns注册商那里修改DNS权威解析地址到cloudflare。

image-20240625100120442

image-20240625100353358

然后在左边的“DNS”处,添加一个A记录,如docker.dockerpull.com。记录IP地址可以随意,如取8.8.8.8。

image-20240625100709853

创建一个“概述”

image-20240625100859421

image-20240625100933834

image-20240625101012262

编辑代码,删除掉现有的代码,粘贴以下代码,修改一处,把const workers_url 后边的域名,换成自己的。

'use strict'
const hub_host = 'registry-1.docker.io'
const auth_url = 'https://auth.docker.io'
const workers_url = 'https://dockerpull.dockerpull.com'
const PREFLIGHT_INIT = {
    status: 204,
    headers: new Headers({
        'access-control-allow-origin': '*',
        'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS',
        'access-control-max-age': '1728000',
    }),
}
function makeRes(body, status = 200, headers = {}) {
    headers['access-control-allow-origin'] = '*'
    return new Response(body, {status, headers})
}
function newUrl(urlStr) {
    try {
        return new URL(urlStr)
    } catch (err) {
        return null
    }
}
addEventListener('fetch', e => {
    const ret = fetchHandler(e)
        .catch(err => makeRes('cfworker error:n' + err.stack, 502))
    e.respondWith(ret)
})
async function fetchHandler(e) {
  const getReqHeader = (key) => e.request.headers.get(key);
  let url = new URL(e.request.url);
  if (url.pathname === '/token') {
      let token_parameter = {
        headers: {
        'Host': 'auth.docker.io',
        'User-Agent': getReqHeader("User-Agent"),
        'Accept': getReqHeader("Accept"),
        'Accept-Language': getReqHeader("Accept-Language"),
        'Accept-Encoding': getReqHeader("Accept-Encoding"),
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0'
        }
      };
      let token_url = auth_url + url.pathname + url.search
      return fetch(new Request(token_url, e.request), token_parameter)
  }
  url.hostname = hub_host;
  let parameter = {
    headers: {
      'Host': hub_host,
      'User-Agent': getReqHeader("User-Agent"),
      'Accept': getReqHeader("Accept"),
      'Accept-Language': getReqHeader("Accept-Language"),
      'Accept-Encoding': getReqHeader("Accept-Encoding"),
      'Connection': 'keep-alive',
      'Cache-Control': 'max-age=0'
    },
    cacheTtl: 3600
  };
  if (e.request.headers.has("Authorization")) {
    parameter.headers.Authorization = getReqHeader("Authorization");
  }
  let original_response = await fetch(new Request(url, e.request), parameter)
  let original_response_clone = original_response.clone();
  let original_text = original_response_clone.body;
  let response_headers = original_response.headers;
  let new_response_headers = new Headers(response_headers);
  let status = original_response.status;
  if (new_response_headers.get("Www-Authenticate")) {
    let auth = new_response_headers.get("Www-Authenticate");
    let re = new RegExp(auth_url, 'g');
    new_response_headers.set("Www-Authenticate", response_headers.get("Www-Authenticate").replace(re, workers_url));
  }
  if (new_response_headers.get("Location")) {
    return httpHandler(e.request, new_response_headers.get("Location"))
  }
  let response = new Response(original_text, {
            status,
            headers: new_response_headers
        })
  return response;
}
function httpHandler(req, pathname) {
    const reqHdrRaw = req.headers
    // preflight
    if (req.method === 'OPTIONS' &&
        reqHdrRaw.has('access-control-request-headers')
    ) {
        return new Response(null, PREFLIGHT_INIT)
    }
    let rawLen = ''
    const reqHdrNew = new Headers(reqHdrRaw)
    const refer = reqHdrNew.get('referer')
    let urlStr = pathname
    const urlObj = newUrl(urlStr)
    /** @type {RequestInit} */
    const reqInit = {
        method: req.method,
        headers: reqHdrNew,
        redirect: 'follow',
        body: req.body
    }
    return proxy(urlObj, reqInit, rawLen, 0)
}
async function proxy(urlObj, reqInit, rawLen) {
    const res = await fetch(urlObj.href, reqInit)
    const resHdrOld = res.headers
    const resHdrNew = new Headers(resHdrOld)
    // verify
    if (rawLen) {
        const newLen = resHdrOld.get('content-length') || ''
        const badLen = (rawLen !== newLen)
        if (badLen) {
            return makeRes(res.body, 400, {
                '--error': `bad len: ${newLen}, except: ${rawLen}`,
                'access-control-expose-headers': '--error',
            })
        }
    }
    const status = res.status
    resHdrNew.set('access-control-expose-headers', '*')
    resHdrNew.set('access-control-allow-origin', '*')
    resHdrNew.set('Cache-Control', 'max-age=1500')
    resHdrNew.delete('content-security-policy')
    resHdrNew.delete('content-security-policy-report-only')
    resHdrNew.delete('clear-site-data')
    return new Response(res.body, {
        status,
        headers: resHdrNew
    })
}

修改完后,点“部署”

image-20240625101700516

添加路由

image-20240625101812165

image-20240625101854705

拉到images

#正常加速
[root@zabbix7-238 ~]# docker pull mysql:latest
latest: Pulling from library/mysql
7af76bb36546: Retrying in 1 second 
24d40f69285f: Waiting 
94e5412f594e: Waiting 
e00a64de64e9: Waiting 
e3dd3d47ce6c: Waiting 
18af3efb629d: Waiting 
ba3db9dfd86e: Waiting 
787130cbc394: Waiting 
d458a2361496: Waiting 
d48f1878172c: Waiting 
error pulling image configuration: download failed after attempts=6: dial tcp 199.16.156.40:443: i/o timeout

#加速,未加上组织名称library,会报错
[root@zabbix7-238 ~]# docker pull dockerpull.hnops.com/mysql:latest
Error response from daemon: pull access denied for dockerpull.hnops.com/mysql, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

#正常加速
[root@zabbix7-238 ~]# docker pull dockerpull.hnops.com/library/mysql:latest


#以下从lscr.io拉到smokeping很慢
[root@zabbix7-238 ~]# docker pull lscr.io/linuxserver/smokeping:latest
latest: Pulling from linuxserver/smokeping
b4432782efac: Pull complete 
1a264b4f2a58: Pull complete 
012868fab03e: Pull complete 
a1c5247eab90: Pull complete 
6df5a1fe177e: Pull complete 
a9294c712ef2: Pull complete 
ef835904d5c7: Downloading [====>                                              ]  10.18MB/109.8MB


#加速,未去掉lscr.io就报错
[root@zabbix7-238 ~]# docker pull dockerpull.hnops.com/lscr.io/linuxserver/smokeping:latest
Error response from daemon: pull access denied for dockerpull.hnops.com/lscr.io/linuxserver/smokeping, repository does not exist or may require 'docker login': denied: requested access to the resource is denied


#正常加速
docker pull dockerpull.hnops.com/linuxserver/smokeping:latest

有时cloudflare的CDN在国内的加速节点少,有时会很慢,可以通过以下小程序,测试各节点的延迟,然后去绑hosts文件。

程序项目官网:
https://github.com/XIU2/CloudflareSpeedTest/releases

文件下载地址:
https://pan.lanpw.com/b0742hkxe
声明:欢迎大家光临本站,学习IT运维技术,转载本站内容,请注明内容出处”来源刘国华教育“。如若本站内容侵犯了原著者的合法权益,请联系我们进行处理。