feat(项目): 项目初始化
This commit is contained in:
commit
947718c5f6
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
yuchat-proxy-server
|
||||
yuchat-proxy-client.exe
|
||||
8
Dockerfile
Normal file
8
Dockerfile
Normal file
@ -0,0 +1,8 @@
|
||||
from docker.yuchat.top/alpine:3.11
|
||||
|
||||
workdir /app
|
||||
expose 8080
|
||||
|
||||
add yuchat-proxy-server /app
|
||||
|
||||
entrypoint ["./yuchat-proxy-server"]
|
||||
45
client/main.go
Normal file
45
client/main.go
Normal file
@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"test-proxy-go/common"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
const (
|
||||
localPort = ":1080"
|
||||
websocketAddress = "ws://proxy.yuchat.top/ws"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 启动TCP监听
|
||||
listener, err := net.Listen("tcp", localPort)
|
||||
if err != nil {
|
||||
log.Fatal("启动服务失败,错误信息: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("监听TCP端口成功,端口", localPort)
|
||||
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
|
||||
log.Println("有新的连接:", conn.RemoteAddr())
|
||||
if err != nil {
|
||||
log.Println("Error accepting connection:", err)
|
||||
continue
|
||||
}
|
||||
wsConn, _, err := websocket.DefaultDialer.Dial(websocketAddress, nil)
|
||||
|
||||
if err != nil {
|
||||
log.Println("Websocket连接失败:", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("Websocket连接成功,端口", websocketAddress)
|
||||
go common.WsToTcpHandler(conn, wsConn)
|
||||
go common.TcpToWsHandler(conn, wsConn)
|
||||
}
|
||||
}
|
||||
30
common/common.go
Normal file
30
common/common.go
Normal file
@ -0,0 +1,30 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
func WsToTcpHandler(conn net.Conn, wsConn *websocket.Conn) {
|
||||
for {
|
||||
_, p, err := wsConn.ReadMessage()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
conn.Write(p)
|
||||
}
|
||||
}
|
||||
|
||||
func TcpToWsHandler(conn net.Conn, wsConn *websocket.Conn) {
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
wsConn.WriteMessage(websocket.BinaryMessage, buf[:n])
|
||||
}
|
||||
|
||||
}
|
||||
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module test-proxy-go
|
||||
|
||||
go 1.21.0
|
||||
|
||||
require github.com/gorilla/websocket v1.5.3 // indirect
|
||||
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
66
server/main.go
Normal file
66
server/main.go
Normal file
@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"test-proxy-go/common"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
const (
|
||||
tcpAddress = "172.20.0.1:1080"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true // 允许来自任何来源的连接
|
||||
},
|
||||
}
|
||||
|
||||
func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("有新的客户端连接了")
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
fmt.Println("websocket连接失败:", err)
|
||||
return
|
||||
}
|
||||
remoteConn, err := net.Dial("tcp", tcpAddress)
|
||||
if err != nil {
|
||||
log.Printf("本地服务连接失败 %s: %v", tcpAddress, err)
|
||||
return
|
||||
}
|
||||
fmt.Println("本地服务启动成功", tcpAddress)
|
||||
|
||||
go common.WsToTcpHandler(remoteConn, conn)
|
||||
go common.TcpToWsHandler(remoteConn, conn)
|
||||
}
|
||||
|
||||
// HTTP GET 接口,返回 "Hello, World!"
|
||||
func helloHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK) // 设置状态码为 200 OK
|
||||
w.Header().Set("Content-Type", "text/plain") // 设置响应内容类型
|
||||
_, err := w.Write([]byte("Hello, World!"))
|
||||
if err != nil {
|
||||
log.Println("Write failed:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
server := &http.Server{
|
||||
Addr: ":8080",
|
||||
TLSConfig: &tls.Config{
|
||||
InsecureSkipVerify: true, // 可选
|
||||
},
|
||||
}
|
||||
http.HandleFunc("/ws", wsHandler)
|
||||
http.HandleFunc("/hello", helloHandler)
|
||||
fmt.Println("WebSocket服务已启动,地址:ws://localhost:8080/ws")
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
log.Fatalf("Server failed: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user