https://www.fastssh.com/page/create-ssh-cdn-websocket/server/
这其实不是标准的websocket报文(服务器响应报文无Sec-Websocket-Accept字段),所以无法使用github.com/gorilla/websocket包:
GET / HTTP/1.1
Host: hostname:8080
User-Agent: Go-http-client/1.1
Connection: Upgrade
Origin: http://hostname:8080
Sec-WebSocket-Key: t+M8AiisFSInx6hLi1YBOg==
Sec-WebSocket-Version: 13
Upgrade: websocket
HTTP/1.1 101 Switching Protocol
Server: nginx/1.24.0
Date: Sat, 04 May 2024 08:37:56 GMT
Content-Length: 1048576000000
Connection: upgrade
首先申请账户:
使用golang1.20 编写转发程序:
package main
import (
"bufio"
"fmt"
"net"
"io"
"net/http"
_ "strings"
)
var upgradeHeaders = []string{
"GET / HTTP/1.1",
"Host: sg-public1.sshws.net:80",
"User-Agent: Go-http-client/1.1",
"Connection: Upgrade",
"Origin: http://sg-public1.sshws.net:80",
"Sec-WebSocket-Key: t+M8AiisFSInx6hLi1YBOg==",
"Sec-WebSocket-Version: 13",
"Upgrade: websocket",
"",
"",
}
func main() {
listener, err := net.Listen("tcp", ":6022")
if err != nil {
fmt.Println("Error listening on port 6022: ", err)
return
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting connection: ", err)
continue
}
go handleClient(conn)
}
}
func handleClient(conn net.Conn) {
defer conn.Close()
// Connect to the WebSocket server
serverConn, err := net.Dial("tcp", "sg-public1.sshws.net:80")
if err != nil {
fmt.Println("Error connecting to WebSocket server: ", err)
return
}
defer serverConn.Close()
// Send upgrade headers
for _, header := range upgradeHeaders {
if _, err := fmt.Fprint(serverConn, header+"\r\n"); err != nil {
fmt.Println("Error sending header: ", err)
return
}
}
// Read server response
reader := bufio.NewReader(serverConn)
response, err := http.ReadResponse(reader, nil)
if err != nil {
fmt.Println("Error reading server response: ", err)
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusSwitchingProtocols {
fmt.Println("Server did not accept WebSocket upgrade: ", response.Status)
return
}
// Forward data between TCP client and WebSocket server
buffer := make([]byte, 1024)
go io.CopyBuffer(serverConn, conn, buffer)
io.CopyBuffer(conn, serverConn, buffer)
}
编译后运行:
mkdir -pv /tmp/Ay;
cd /tmp/Ay;
go build -ldflags="-s -w" -o ws2.elf ws2.go ;
./ws2.elf
验证并使用:
/tmp/busybox netstat -anl|grep 6022;
ssh -o StrictHostKeyChecking=no -o HostKeyAlgorithms=+ssh-dss -o ServerAliveInterval=10 -o ServerAliveCountMax=3 -D 3080 -Nf -v fastssh.com-username@127.0.0.22 -p 6022;
/tmp/busybox netstat -anl|grep 3080;
curl -v -4 -x socks5h://127.0.0.1:3080 cip.cc;