我正在嘗試像這個鏈接一樣使用 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
uj5u.com熱心網友回復:
與所有 HTTP 請求一樣,CONNECT 請求以多行請求標頭開始,以僅包含 \r\n 的行結束。但是您只閱讀了它的第一行:
message, e := bufio.NewReader(client_to_proxy).ReadString('\n')
請求的其余部分被發送到服務器,這可能會回傳一些錯誤,因為這不是 TLS 握手的預期開始。TLS 握手的開始僅出現在請求標頭之后。然后服務器回傳的錯誤被轉發到客戶端并被解釋為 TLS 訊息 - 這被視為損壞的訊息,因此是“bad mac”。
readAll(client_to_proxy, proxy_to_server)
無需將所有剩余資料(即除了請求的第一行之外的所有資料)轉發到服務器,您需要首先讀取完整的請求標頭,然后才在客戶端和服務器之間轉發所有資料。
uj5u.com熱心網友回復:
非常感謝@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
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/520334.html
