package main

import (
    "bytes"
    "crypto/tls"
    "flag"
    "fmt"
    "io"
    "io/ioutil"
    "math/rand"
    "net"
    "net/http"
    "net/http/httputil"
    "runtime"
    "time"
)

// Console flags
//参数解析
var (
    listen                = flag.String("l", ":8888", "port to accept requests")  //接收请求端口 默认渡口是8888
    targetProduction      = flag.String("a", "localhost:8080", "where production traffic goes. http://localhost:8080/production")  //a代表产品机器  默认端口是8080
    altTarget             = flag.String("b", "localhost:8081", "where testing traffic goes. response are skipped. http://localhost:8081/test")  //b 测试机器 端口是8081 
    debug                 = flag.Bool("debug", false, "more logging, showing ignored output")  //日志开关
    productionTimeout     = flag.Int("a.timeout", 3, "timeout in seconds for production traffic")// 生产机器请求超时时间
    alternateTimeout      = flag.Int("b.timeout", 1, "timeout in seconds for alternate site traffic")//测试机器清酒超时时间
    productionHostRewrite = flag.Bool("a.rewrite", false, "rewrite the host header when proxying production traffic") //生产机器是重定向开关  
    alternateHostRewrite  = flag.Bool("b.rewrite", false, "rewrite the host header when proxying alternate site traffic")//测试机器是否重定向开关
    percent               = flag.Float64("p", 100.0, "float64 percentage of traffic to send to testing")// 生产数据发给测试机器数据的百分比  流量分割
    tlsPrivateKey         = flag.String("key.file", "", "path to the TLS private key file") //TSL 私钥证书
    tlsCertificate        = flag.String("cert.file", "", "path to the TLS certificate file")//Tsl 龚玥证书
)

// handler contains the address of the main Target and the one for the Alternative target
//handler 包含连个地址  其中一个是生产服务器  另个一是测试服务器
type handler struct {
    Target      string
    Alternative string
    Randomizer  rand.Rand
}

// ServeHTTP duplicates the incoming request (req) and does the request to the Target and the Alternate target discading the Alternate response
//sereHttp 复制获取到的req 并且发送到生产服务器和测试服务器   测试服务器丢弃响应结果
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    var productionRequest, alternativeRequest *http.Request
    if *percent == 100.0 || h.Randomizer.Float64()*100 < *percent {
        alternativeRequest, productionRequest = DuplicateRequest(req) //复制数据到生产和测试请求中
        go func() {
            defer func() {
                if r := recover(); r != nil && *debug {
                    fmt.Println("Recovered in f", r)
                }
            }()
            // Open new TCP connection to the server
                      //获取客户端连接 带有超时时间   
            clientTcpConn, err := net.DialTimeout("tcp", h.Alternative, time.Duration(time.Duration(*alternateTimeout)*time.Second))
            if err != nil {
                if *debug {
                    fmt.Printf("Failed to connect to %s\n", h.Alternative)
                }
                return
            }
            clientHttpConn := httputil.NewClientConn(clientTcpConn, nil) // Start a new HTTP connection on it
            defer clientHttpConn.Close()                                 // Close the connection to the server
            if *alternateHostRewrite {
                alternativeRequest.Host = h.Alternative
            }
            err = clientHttpConn.Write(alternativeRequest) // Pass on the request
            if err != nil {
                if *debug {
                    fmt.Printf("Failed to send to %s: %v\n", h.Alternative, err)
                }
                return
            }
            _, err = clientHttpConn.Read(alternativeRequest) // Read back the reply
            if err != nil {
                if *debug {
                    fmt.Printf("Failed to receive from %s: %v\n", h.Alternative, err)
                }
                return
            }
        }()
    } else {
        productionRequest = req
    }
    defer func() {
        if r := recover(); r != nil && *debug {
            fmt.Println("Recovered in f", r)
        }
    }()

    // Open new TCP connection to the server
       //生产服务器
    clientTcpConn, err := net.DialTimeout("tcp", h.Target, time.Duration(time.Duration(*productionTimeout)*time.Second))
    if err != nil {
        fmt.Printf("Failed to connect to %s\n", h.Target)
        return
    }
    clientHttpConn := httputil.NewClientConn(clientTcpConn, nil) // Start a new HTTP connection on it
    defer clientHttpConn.Close()                                 // Close the connection to the server
    if *productionHostRewrite {
        productionRequest.Host = h.Target
    }
    err = clientHttpConn.Write(productionRequest) // Pass on the request
    if err != nil {
        fmt.Printf("Failed to send to %s: %v\n", h.Target, err)
        return
    }
    resp, err := clientHttpConn.Read(productionRequest) // Read back the reply
    if err != nil {
        fmt.Printf("Failed to receive from %s: %v\n", h.Target, err)
        return
    }
    defer resp.Body.Close()
    for k, v := range resp.Header {
        w.Header()[k] = v
    }
    w.WriteHeader(resp.StatusCode)
    body, _ := ioutil.ReadAll(resp.Body)
    w.Write(body)
}

func main() {
    flag.Parse()

    runtime.GOMAXPROCS(runtime.NumCPU())

    var err error

    var listener net.Listener

    if len(*tlsPrivateKey) > 0 {
        cer, err := tls.LoadX509KeyPair(*tlsCertificate, *tlsPrivateKey)
        if err != nil {
            fmt.Printf("Failed to load certficate: %s and private key: %s", *tlsCertificate, *tlsPrivateKey)
            return
        }

        config := &tls.Config{Certificates: []tls.Certificate{cer}}
        listener, err = tls.Listen("tcp", *listen, config)
        if err != nil {
            fmt.Printf("Failed to listen to %s: %s\n", *listen, err)
            return
        }
    } else {
        listener, err = net.Listen("tcp", *listen)
        if err != nil {
            fmt.Printf("Failed to listen to %s: %s\n", *listen, err)
            return
        }
    }

    h := handler{
        Target:      *targetProduction,
        Alternative: *altTarget,
        Randomizer:  *rand.New(rand.NewSource(time.Now().UnixNano())),
    }
    http.Serve(listener, h)
}

type nopCloser struct {
    io.Reader
}

func (nopCloser) Close() error { return nil }
//复制req到生茶服务器和测试服务器
func DuplicateRequest(request *http.Request) (request1 *http.Request, request2 *http.Request) {
    b1 := new(bytes.Buffer)
    b2 := new(bytes.Buffer)
    w := io.MultiWriter(b1, b2)  //同时向多个对象中写入数据
    io.Copy(w, request.Body) //复制数据到  w中
    defer request.Body.Close()
    request1 = &http.Request{
        Method:        request.Method,
        URL:           request.URL,
        Proto:         request.Proto,
        ProtoMajor:    request.ProtoMajor,
        ProtoMinor:    request.ProtoMinor,
        Header:        request.Header,
        Body:          nopCloser{b1},
        Host:          request.Host,
        ContentLength: request.ContentLength,
    }
    request2 = &http.Request{
        Method:        request.Method,
        URL:           request.URL,
        Proto:         request.Proto,
        ProtoMajor:    request.ProtoMajor,
        ProtoMinor:    request.ProtoMinor,
        Header:        request.Header,
        Body:          nopCloser{b2},
        Host:          request.Host,
        ContentLength: request.ContentLength,
    }
    return
}

teeporxy.go的更多相关文章

随机推荐

  1. https认证

    HTTPS认证 说明 1. HTTPS协议的站点信息更加安全,同时可降低网站被劫持的风险,如网站同时存在HTTP和HTTPS站点,可使用本工具进行认证,便于百度搜索识别网站HTTP与HTTPS之间的对 ...

  2. SpringBoot2.0之一 新建项目helloWorld

    SpringBoot 以简单快速很快获得了广大开发者的青睐,本套SpringBoot系列以最新的SpringBoot 2.0为基础,同时会提及不同版本下SpringBoot的修改和变化,如有理解不当的 ...

  3. 《MySQL必知必会》读书笔记_2

    通配符:(尾空格可能会干扰通配符匹配) % 匹配任意字符 _ 匹配任意单个字符 正则表达式:REGEXP 用法就是替换掉LIKE的位置,后面配合正则表达式. 默认不区分大小写,如果区分的话添加关键字B ...

  4. JAVA代码设置selector不同状态下的背景颜色

    代码实现Shape 代码实现Selector StateListDrawable与GradientDrawable 的运用 在Android开发中,我们时常会用到自定义drawable样式,在draw ...

  5. 【读书笔记】C++Primer---第三章

    1.由于为了与C语言兼容,字符串字面值与标准库string类型不是同一种类型: 2.以下代码中,cin有几点需要注意:a.读取并忽略开头所有的空白字符(如空格.换行符.制表符):b.读取字符直至再次遇 ...

  6. ACCA AI来袭会议笔记

    ACCA AI来袭会议笔记 Technology in Accounting 调研报告: http://cn.accaglobal.com/news/professional_report.html ...

  7. HDU-5738

    Eureka Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  8. Hbase出现ERROR: Can't get master address from ZooKeeper; znode data == null正确找到解决思路

    第一次配置时出现这样的错误,也很懵的,到处上网找博客看资料,都试了个遍,但是问题还是存在,以下这些博客写的或许是解决一类问题的方式. https://blog.csdn.net/whbo111/art ...

  9. 分布式任务调度平台XXL-JOB

    <分布式任务调度平台XXL-JOB>       一.简介 1.1 概述 XXL-JOB是一个轻量级分布式任务调度框架,其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源代码并 ...

  10. 如何查看chrome浏览器已保存的密码

    该方法是针对在chrome中已经存储了登陆密码的情况. chrome版本是 66.0.3359.139(正式版本) (64 位),不知道哪天会改了这个bug. 一般来说,我们登陆chrome浏览器已经 ...