|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "net" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +var localPort = ":10805" // 本机监听端口 |
| 13 | +var vpsServerAddr = "10.10.10.10:10888" // 远程vps 对应的地址和端口 |
| 14 | +var vpsServerName = "test.xyz" // 证书对应的域名 |
| 15 | + |
| 16 | +func sendVps(message []byte, localConn net.Conn) { |
| 17 | + |
| 18 | + config := &tls.Config{ |
| 19 | + InsecureSkipVerify: false, // 跳过证书验证(在生产环境中不建议这样做) |
| 20 | + ServerName: vpsServerName, // 证书对应的域名 |
| 21 | + } |
| 22 | + remoteConn, err := tls.Dial("tcp", vpsServerAddr, config) |
| 23 | + if err != nil { |
| 24 | + log.Println("连接到服务器时出错:", err) |
| 25 | + return |
| 26 | + } |
| 27 | + _, err = remoteConn.Write([]byte(message)) |
| 28 | + if err != nil { |
| 29 | + log.Println("发送消息时出错:", err) |
| 30 | + return |
| 31 | + } |
| 32 | + log.Println("收到服务器响应了, 快点告诉客户端 200") |
| 33 | + // 发送 HTTP 200 响应给客户端(代表连接成功可以转发数据了) |
| 34 | + localConn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")) |
| 35 | + // 启动 goroutine 开始互转 |
| 36 | + go io.Copy(remoteConn, localConn) // 客户端数据 ==> 目标服务器 |
| 37 | + go io.Copy(localConn, remoteConn) // 目标服务器 ==> 客户端数据 |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +func main() { |
| 42 | + // 监听本地 10805 |
| 43 | + localListener, err := net.Listen("tcp", localPort) |
| 44 | + if err != nil { |
| 45 | + log.Fatalf("Failed to listen on local port 10901: %v", err) |
| 46 | + } |
| 47 | + fmt.Println("开始监听" + localPort) |
| 48 | + for { |
| 49 | + // 接受本地连接 |
| 50 | + localConn, err := localListener.Accept() |
| 51 | + if err != nil { |
| 52 | + log.Printf("Failed to accept local connection: %v", err) |
| 53 | + continue |
| 54 | + } |
| 55 | + // 读取本地请求中的目标地址(C 服务器地址) |
| 56 | + buf := make([]byte, 1024) |
| 57 | + n, err := localConn.Read(buf) |
| 58 | + if err != nil { |
| 59 | + log.Printf("Failed to read target address from local connection: %v", err) |
| 60 | + return |
| 61 | + } |
| 62 | + // 获取域名地址, 根据请求的域名进行区分 |
| 63 | + addr := descAddr(string(buf[:n])) |
| 64 | + |
| 65 | + if strings.Contains(addr, "baidu") { |
| 66 | + fmt.Println("本地直连==>", addr) |
| 67 | + handleConnectRequest(addr, localConn) |
| 68 | + } |
| 69 | + if strings.Contains(addr, "google") { |
| 70 | + fmt.Println("发送代理==>", addr) |
| 71 | + sendVps(buf, localConn) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// 解析请求获取目标地址 |
| 77 | +func descAddr(request string) string { |
| 78 | + lines := strings.Split(request, "\n") |
| 79 | + firstLine := strings.Split(lines[0], " ") |
| 80 | + if len(firstLine) != 3 { |
| 81 | + log.Println("无效的请求行") |
| 82 | + return "" |
| 83 | + } |
| 84 | + return firstLine[1] |
| 85 | +} |
| 86 | + |
| 87 | +func handleConnectRequest(targetAddr string, localConn net.Conn) { |
| 88 | + // 连接到目标服务器 |
| 89 | + remoteConn, err := net.Dial("tcp", targetAddr) |
| 90 | + if err != nil { |
| 91 | + log.Printf("连接到目标服务器 %s 时出错: %v", targetAddr, err) |
| 92 | + return |
| 93 | + } |
| 94 | + // 发送 HTTP 200 响应给客户端(代表连接成功可以转发数据了) |
| 95 | + localConn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")) |
| 96 | + |
| 97 | + // 启动 goroutine 开始互转 |
| 98 | + go io.Copy(remoteConn, localConn) // 客户端数据 ==> 目标服务器 |
| 99 | + go io.Copy(localConn, remoteConn) // 目标服务器 ==> 客户端数据 |
| 100 | +} |
0 commit comments