分享

使用 Golang 为 Docker 编写一个简单的 HTTP 代理

xioaxu790 发表于 2014-5-17 18:26:56 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 1 24388

问题导读:

1.什么Golang语言?
2.如何使用 Golang 编写一个简单的 HTTP 代理?




Golang语言简介
官网地址
Go语言是由Google开发的一个开源项目,具体语言特征就不细说了,可以查看一下文档。 学习使用了几天,想起了一句广告语: 简约而不简单。

一、前言:
使用  Golang 编写一个简单的 HTTP 代理goproxy 是一个轻量级的 HTTP 代理库,可以很容易的编写一个仅供 Docker 使用的代理程序 。 packagemain import ( "github.com/elazarl/goproxy" "net/http""regexp" ) func main() { proxy := goproxy.NewProxyHttpServer()proxy.OnRequest().DoFunc( func(r *http.Request, ctx *goproxy.ProxyCtx)(*http.Request, *http.Response) { match, _ :=regexp.MatchString("^*.docker.io$", r.URL.Host


二、使用 Golang 编写一个简单的 HTTP 代理

goproxy 是一个轻量级的 HTTP 代理库,可以很容易的编写一个仅供 Docker 使用的代理程序 。
  1. package main
  2. import (  
  3.   "github.com/elazarl/goproxy"
  4.   "net/http"
  5.   "regexp"
  6. )
  7. func main() {  
  8.   proxy := goproxy.NewProxyHttpServer()
  9.   proxy.OnRequest().DoFunc(
  10.     func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
  11.       match, _ := regexp.MatchString("^*.docker.io[        DISCUZ_CODE_0        ]quot;, r.URL.Host)
  12.       if match == false {
  13.         return r, goproxy.NewResponse(r, goproxy.ContentTypeText, http.StatusForbidden,
  14.           "The proxy is used exclusively to download docker image, please don't abuse it for any purpose.")
  15.       } else {
  16.         return r, nil
  17.       }
  18.     })
  19.   proxy.Verbose = false
  20.   http.ListenAndServe(":8384", proxy)
  21. }
复制代码

程序在每次 HTTP 的请求过程中都检测目标地址的 URL,如果不是 docker.io 或子站会返回一个错误信息。这为了避免使用代理访问其它的网站,致使服务器被墙。



三、编写 Ubuntu init.d 的启动脚本
为了保证服务在后台运行,编写 Ubuntu init.d 的启动脚本管理 Proxy 服务。
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:           dockboard.org
  4. # Required-Start:     $syslog $remote_fs
  5. # Required-Stop:      $syslog $remote_fs
  6. # Default-Start:      2 3 4 5
  7. # Default-Stop:       0 1 6
  8. # Short-Description:  Create lightweight http proxy for Docker daemon.
  9. # Description:
  10. #       Author: Meaglith Ma
  11. #         Email: genedna@gmail.com
  12. #       Website: http://www.dockboard.org
  13. ### END INIT INFO
  14. BASE=$(basename $0)
  15. DOCKER_PROXY=/usr/bin/$BASE  
  16. DOCKER_PROXY_PIDFILE=/var/run/$BASE.pid  
  17. DOCKER_PROXY_OPTS=
  18. PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
  19. # Check docker is present
  20. if [ ! -x $DOCKER_PROXY ]; then  
  21.     exit 1
  22. fi
  23. fail_unless_root() {  
  24.     if [ "$(id -u)" != '0' ]; then
  25.         exit 1
  26.     fi
  27. }
  28. case "$1" in  
  29.     start)
  30.         fail_unless_root
  31.         start-stop-daemon --start --background \
  32.             --exec "$DOCKER_PROXY" \
  33.             --pidfile "$DOCKER_PROXY_PIDFILE" \
  34.             -- -d -p "$DOCKER_PROXY_PIDFILE" \
  35.             $DOCKER_PROXY_OPTS
  36.         ;;
  37.     stop)
  38.         fail_unless_root
  39.         start-stop-daemon --stop \
  40.             --pidfile "$DOCKER_PROXY_PIDFILE"
  41.         log_end_msg $?
  42.         ;;
  43.     restart)
  44.         fail_unless_root
  45.         docker_pid=`cat "$DOCKER_PROXY_PIDFILE" 2>/dev/null`
  46.         [ -n "$docker_pid" ] \
  47.             && ps -p $docker_pid > /dev/null 2>&1 \
  48.             && $0 stop
  49.         $0 start
  50.         ;;
  51.     force-reload)
  52.         fail_unless_root
  53.         $0 restart
  54.         ;;
  55.     status)
  56.         status_of_proc -p "$DOCKER_PROXY_PIDFILE" "$DOCKER" docker
  57.         ;;
  58.     *)
  59.         echo "Usage: $0 {start|stop|restart|status}"
  60.         exit 1
  61.         ;;
  62. esac
  63. exit 0
复制代码


四、Ubuntu 中修改 Docker 的配置文件使用 HTTPProxy
修改 /etc/init/docker.conf 加入 http proxy 的环境变量。
  1. description "Docker daemon"
  2. start on filesystem and started lxc-net  
  3. stop on runlevel [!2345]
  4. respawn
  5. env HTTP_PROXY="http://192.241.209.203:8384"
  6. script  
  7.   DOCKER=/usr/bin/$UPSTART_JOB
  8.   DOCKER_OPTS=
  9.   if [ -f /etc/default/$UPSTART_JOB ]; then
  10.     . /etc/default/$UPSTART_JOB
  11.   fi
  12.   "$DOCKER" -d $DOCKER_OPTS
  13. end script  
复制代码

或者是修改 /etc/default/docker 文件,取消注释 http_proxy 的部分。
  1. # If you need Docker to use an HTTP proxy, it can also be specified here.
  2. export http_proxy=http://192.241.209.203:8384/  
复制代码

重启 Docker 的服务后下载 images 就会通过 192.241.209.203:8384 这个代理进行访问。

五、可用的代理地址如下:
http://192.241.209.203:8384  

目前 dockboard 为了方便国内用户使用和学习 docker 技术,使用 DigitalOcean 的 VPS 架设了一个代理。

六、更多内容:

1、《学习 go语言》 中文版
2、《go 编程导读》
3、golang windows 上安装
4、读者,还可以参考这里:Docker 中文社区






已有(1)人评论

跳转到指定楼层
穆紫 发表于 2016-10-27 13:16:43
折腾好久,终于找到有效的~用上面方法四修改配置文件,成功。多谢作者
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条