feat(vpn服务器): VPN 服务器的简单实现
WebSocket的数据写入网卡,将网卡读取的数据写入WebSocket
This commit is contained in:
@@ -4,17 +4,18 @@ import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"yuchat-proxy/common"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"yuchat-proxy/eth"
|
||||
)
|
||||
|
||||
const (
|
||||
tcpAddress = "172.20.0.1:1080"
|
||||
)
|
||||
|
||||
var e = eth.Init()
|
||||
var upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true // 允许来自任何来源的连接
|
||||
@@ -28,25 +29,17 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("websocket连接失败:", err)
|
||||
return
|
||||
}
|
||||
remoteConn, err := net.Dial("tcp", tcpAddress)
|
||||
if err != nil {
|
||||
log.Printf("本地服务连接失败 %s: %v", tcpAddress, 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)
|
||||
}
|
||||
go e.WsToEthernet(conn)
|
||||
go e.EthernetPacketsToWs(conn)
|
||||
// go common.WsToTcpHandler(remoteConn, conn)
|
||||
// go common.TcpToWsHandler(remoteConn, conn)
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -57,10 +50,8 @@ func main() {
|
||||
},
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user