登录
首页 >  Golang >  Go问答

建立 https 代理时,遇到 SSL_ERROR_BAD_MAC_READ 错误

来源:stackoverflow

时间:2024-02-25 20:00:26 199浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《建立 https 代理时,遇到 SSL_ERROR_BAD_MAC_READ 错误》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

我正在尝试使用 golang 创建 http/https 代理,如下链接所示。 这是我的全部代码: 首先从浏览器获取命令。如果它是 connect 意味着 https 并创建简单的 tcp 套接字并让浏览器继续它。然后将每个连接连接在一起。

package main

import (
    "bufio"
    "fmt"
    "net"
    "strings"
)

func main() {
    fmt.Println("Start server...")

    ln, _ := net.Listen("tcp", ":8000")

    conn, _ := ln.Accept()
    handleSocket(conn)

}

func handleSocket(client_to_proxy net.Conn) {
    message, e := bufio.NewReader(client_to_proxy).ReadString('\n')
    message = strings.ReplaceAll(message, "\r\n", "")
    if e != nil {
        fmt.Println("ERROR1 ", e)
        return
    }

    splited := strings.Split(message, " ")
    host := strings.Split(splited[1], ":")
    if splited[0] == "CONNECT" {
        proxy_to_server, e := net.Dial("tcp", splited[1])
        if e != nil {
            fmt.Println("ERROR2 ", e)
            return
        }
        lenn, e := client_to_proxy.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
        if e != nil {
            fmt.Println("ERROR8 ", e)
            return
        }
        fmt.Println(lenn)

        readAll(client_to_proxy, proxy_to_server)

    } else if splited[0] == "GET" {
        remote_conn, e := net.Dial("tcp", strings.Replace(splited[1][:len(splited[1])-1], "http://", "", 2)+":80")
        if e != nil {
            fmt.Println("ERROR7 ", e)
            return
        }
        _, e = remote_conn.Write([]byte("GET / " + splited[2] + "\r\n" + "Host: " + host[0] + "\r\n\r\n"))
        if e != nil {
            fmt.Println("ERROR6 ", e)
            return
        }
        writeAll(client_to_proxy, remote_conn)
    }
}

func writeAll(client_to_proxy net.Conn, proxy_to_server net.Conn) {
    buffer := make([]byte, 32*1024)
    for {
        readLeng, err := proxy_to_server.Read(buffer)
        if err != nil {
            fmt.Println("ERROR9 ", err)
            return
        }
        if readLeng > 0 {
            _, err := client_to_proxy.Write(buffer)
            if err != nil {
                fmt.Println("ERR4 ", err)
                return
            }
        }
    }
}

func readAll(client_to_proxy net.Conn, proxy_to_server net.Conn) {
    buffer := make([]byte, 32*1024)
    go writeAll(client_to_proxy, proxy_to_server)

    for {
        read, err := client_to_proxy.Read(buffer)
        if err != nil {
            return
        }
        if read > 0 {
            _, err := proxy_to_server.Write(buffer)
            if err != nil {
                fmt.Println("ERR5 ", err)
                return
            }
        }
    }
}

使用 http 可以正常工作,但是在 firefox 中尝试使用 https 时,我收到此错误:

安全连接失败

连接 www.google.com 期间发生错误。 ssl 收到一条包含错误消息验证代码的记录。

错误代码:ssl_error_bad_mac_read


正确答案


非常感谢@steffen-ullrich,这是稍后提供的最终解决方案。适用于 http/https:

package main

import (
    "fmt"
    "net"
    "strings"
)

func main() {
    fmt.Println("Start server...")

    ln, _ := net.Listen("tcp", ":8000")

    for {
        conn, _ := ln.Accept()
        handleSocket(conn)
    }
}

func handleSocket(client_to_proxy net.Conn) {
    buffer := make([]byte, 32*1024)
    _, e := client_to_proxy.Read(buffer)
    if e != nil {
        fmt.Println("ERROR1 ", e)
        return
    }
    message := string(buffer)
    a := strings.Count(message, "\r\n")
    fmt.Println(message)
    fmt.Println(a)
    if e != nil {
        fmt.Println("ERROR1 ", e)
        return
    }

    splited := strings.Split(message, " ")
    //host := strings.Split(splited[1], ":")
    if splited[0] == "CONNECT" {
        //message = strings.Replace(message, "CONNECT", "GET", 1)
        proxy_to_server, e := net.Dial("tcp", splited[1])
        if e != nil {
            fmt.Println("ERROR2 ", e)
            return
        }
        //_, e = proxy_to_server.Write([]byte(message))
        //if e != nil {
        //  fmt.Println("ERROR2 ", e)
        //  return
        //}
        lenn, e := client_to_proxy.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
        if e != nil {
            fmt.Println("ERROR8 ", e)
            return
        }
        fmt.Println(lenn)

        read443(client_to_proxy, proxy_to_server)
    } else if splited[0] == "GET" {
        host1 := strings.Replace(splited[1], "http://", "", 1)
        host2 := host1[:len(host1)-1]
        var final_host string
        if strings.LastIndexAny(host2, "/") > 0 {
            final_host = host2[:strings.LastIndexAny(host2, "/")]
        } else {
            final_host = host2
        }
        proxy_to_server, e := net.Dial("tcp", final_host+":80")
        if e != nil {
            fmt.Println("ERROR7 ", e)
            return
        }
        _, e = proxy_to_server.Write([]byte(message))
        if e != nil {
            fmt.Println("ERROR6 ", e)
            return
        }

        write80(client_to_proxy, proxy_to_server)
    }
}

func write80(client_to_proxy net.Conn, proxy_to_server net.Conn) {
    buffer := make([]byte, 64*1024)

    readLeng, err := proxy_to_server.Read(buffer)
    if err != nil {
        fmt.Println("ERROR9 ", err)
        return
    }
    fmt.Println("WRIIIIIIIIIIIIIIIIIIIIIIT from server:")
    fmt.Println(string(buffer[:readLeng]))
    if readLeng > 0 {
        _, err := client_to_proxy.Write(buffer[:readLeng])
        if err != nil {
            fmt.Println("ERR4 ", err)
            return
        }
    }

    go read80(client_to_proxy, proxy_to_server)
    for {
        readLeng, err := proxy_to_server.Read(buffer)
        if err != nil {
            fmt.Println("ERROR10 ", err)
            return
        }
        fmt.Println("WRIIIIIIIIIIIIIIIIIIIIIIT from server:")
        fmt.Println(string(buffer[:readLeng]))
        if readLeng > 0 {
            _, err := client_to_proxy.Write(buffer[:readLeng])
            if err != nil {
                fmt.Println("ERR4 ", err)
                return
            }
        }
    }
}

func read80(client_to_proxy net.Conn, proxy_to_server net.Conn) {
    buffer := make([]byte, 32*1024)

    for {
        readLeng, err := client_to_proxy.Read(buffer)
        if err != nil {
            return
        }
        fmt.Println("REEEEEEEEEEEEEEEEEEEEEEED from client:")
        fmt.Println(string(buffer[:readLeng]))
        if readLeng > 0 {
            _, err := proxy_to_server.Write(buffer[:readLeng])
            if err != nil {
                fmt.Println("ERR5 ", err)
                return
            }
        }
    }
}

func write443(client_to_proxy net.Conn, proxy_to_server net.Conn) {
    buffer := make([]byte, 32*1024)
    for {
        readLeng, err := proxy_to_server.Read(buffer)
        if err != nil {
            fmt.Println("ERROR10 ", err)
            return
        }
        fmt.Println("WRIIIIIIIIIIIIIIIIIIIIIIT from server:")
        fmt.Println(string(buffer[:readLeng]))
        if readLeng > 0 {
            _, err := client_to_proxy.Write(buffer[:readLeng])
            if err != nil {
                fmt.Println("ERR4 ", err)
                return
            }
        }
    }
}

func read443(client_to_proxy net.Conn, proxy_to_server net.Conn) {
    buffer := make([]byte, 32*1024)

    readLeng, err := client_to_proxy.Read(buffer)
    if err != nil {
        return
    }
    fmt.Println("REEEEEEEEEEEEEEEEEEEEEEED from client:")
    fmt.Println(string(buffer[:readLeng]))
    if readLeng > 0 {
        _, err := proxy_to_server.Write(buffer[:readLeng])
        if err != nil {
            fmt.Println("ERR5 ", err)
            return
        }
    }

    go write443(client_to_proxy, proxy_to_server)

    for {
        readLeng, err := client_to_proxy.Read(buffer)
        if err != nil {
            return
        }
        fmt.Println("REEEEEEEEEEEEEEEEEEEEEEED from client:")
        fmt.Println(string(buffer[:readLeng]))
        if readLeng > 0 {
            _, err := proxy_to_server.Write(buffer[:readLeng])
            if err != nil {
                fmt.Println("ERR5 ", err)
                return
            }
        }
    }
}

与所有 http 请求一样,connect 请求以多行请求标头开始,并以仅由 \r\n 组成的行结束。但你只读了它的第一行:

请求的其余部分将发送到服务器,这可能会返回一些错误,因为这不是 tls 握手的预期开始。 tls 握手仅在请求标头之后开始。服务器返回的错误随后被转发到客户端并解释为 tls 消息 - 这被视为损坏的消息,因此是“坏的 mac”。

您需要首先读取完整的请求标头,然后才在客户端和服务器之间转发所有内容,而不是将所有剩余数据(即除请求的第一行之外的所有内容)转发到服务器。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《建立 https 代理时,遇到 SSL_ERROR_BAD_MAC_READ 错误》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>