Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
923c68a2ea |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
yuchat-proxy-server
|
yuchat-proxy-server
|
||||||
|
go.sum
|
||||||
*.exe
|
*.exe
|
||||||
11
Dockerfile
11
Dockerfile
@ -1,13 +1,20 @@
|
|||||||
from docker.yuchat.top/golang:1.21-alpine AS builder
|
from docker.yuchat.top/golang:1.21-alpine AS builder
|
||||||
workdir /app
|
workdir /app
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN go mod tidy && go build -o yuchat-proxy-server server/main.go
|
RUN echo "nameserver 114.114.114.114" > /etc/resolv.conf
|
||||||
|
RUN apk add --no-cache \
|
||||||
|
libpcap-dev \
|
||||||
|
build-base
|
||||||
|
|
||||||
|
RUN go mod tidy
|
||||||
|
RUN go build -o yuchat-proxy-server server/main.go
|
||||||
|
|
||||||
from docker.yuchat.top/alpine:3.11
|
from docker.yuchat.top/alpine:3.11
|
||||||
|
|
||||||
workdir /app
|
workdir /app
|
||||||
expose 8080
|
expose 8080
|
||||||
|
RUN echo "nameserver 114.114.114.114" > /etc/resolv.conf
|
||||||
|
RUN apk add --no-cache libpcap
|
||||||
copy --from=builder /app/yuchat-proxy-server /app/yuchat-proxy-server
|
copy --from=builder /app/yuchat-proxy-server /app/yuchat-proxy-server
|
||||||
|
|
||||||
entrypoint ["./yuchat-proxy-server"]
|
entrypoint ["./yuchat-proxy-server"]
|
||||||
54
eth/eth.go
Normal file
54
eth/eth.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package eth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/google/gopacket"
|
||||||
|
"github.com/google/gopacket/pcap"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
interfaceName = "eth0"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Eth struct {
|
||||||
|
Handle *pcap.Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
func Init() *Eth {
|
||||||
|
handle, err := pcap.OpenLive(interfaceName, 1600, true, pcap.BlockForever)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("打开驱动器失败: ", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &Eth{Handle: handle}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 抓取网卡的数据,并写入WebSocket
|
||||||
|
*/
|
||||||
|
func (eth *Eth) EthernetPacketsToWs(wsConn *websocket.Conn) error {
|
||||||
|
|
||||||
|
packetSource := gopacket.NewPacketSource(eth.Handle, eth.Handle.LinkType())
|
||||||
|
for packet := range packetSource.Packets() {
|
||||||
|
wsConn.WriteMessage(websocket.BinaryMessage, packet.Data())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将ws的数据写入网卡
|
||||||
|
*/
|
||||||
|
func (eth *Eth) WsToEthernet(wsConn *websocket.Conn) {
|
||||||
|
for {
|
||||||
|
_, p, err := wsConn.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = eth.Handle.WritePacketData(p)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
go.mod
5
go.mod
@ -3,3 +3,8 @@ module yuchat-proxy
|
|||||||
go 1.21.0
|
go 1.21.0
|
||||||
|
|
||||||
require github.com/gorilla/websocket v1.5.3
|
require github.com/gorilla/websocket v1.5.3
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/google/gopacket v1.1.19
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
|
||||||
|
)
|
||||||
|
|||||||
2
go.sum
2
go.sum
@ -1,2 +0,0 @@
|
|||||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
|
||||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
# 1. 打包客户端
|
|
||||||
|
|
||||||
本地安装golang的环境
|
|
||||||
执行 `./yuchat-build.bat` 即可打包出exe
|
|
||||||
|
|
||||||
# 2. 服务端打包
|
|
||||||
|
|
||||||
使用docker,拉取代码到linux,直接执行 `docker build -t yuchat-proxy-server:0.0.1` 即可打包出docker镜像
|
|
||||||
@ -4,17 +4,18 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"yuchat-proxy/common"
|
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
|
"yuchat-proxy/eth"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
tcpAddress = "172.20.0.1:1080"
|
tcpAddress = "172.20.0.1:1080"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var e = eth.Init()
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
return true // 允许来自任何来源的连接
|
return true // 允许来自任何来源的连接
|
||||||
@ -28,25 +29,17 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Println("websocket连接失败:", err)
|
fmt.Println("websocket连接失败:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
remoteConn, err := net.Dial("tcp", tcpAddress)
|
|
||||||
if err != nil {
|
// remoteConn, err := net.Dial("tcp", tcpAddress)
|
||||||
log.Printf("本地服务连接失败 %s: %v", tcpAddress, err)
|
// if err != nil {
|
||||||
return
|
// log.Printf("本地服务连接失败 %s: %v", tcpAddress, err)
|
||||||
}
|
// return
|
||||||
|
// }
|
||||||
fmt.Println("本地服务启动成功", tcpAddress)
|
fmt.Println("本地服务启动成功", tcpAddress)
|
||||||
|
go e.WsToEthernet(conn)
|
||||||
go common.WsToTcpHandler(remoteConn, conn)
|
go e.EthernetPacketsToWs(conn)
|
||||||
go common.TcpToWsHandler(remoteConn, conn)
|
// 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() {
|
func main() {
|
||||||
@ -57,10 +50,8 @@ func main() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
http.HandleFunc("/ws", wsHandler)
|
http.HandleFunc("/ws", wsHandler)
|
||||||
http.HandleFunc("/hello", helloHandler)
|
|
||||||
fmt.Println("WebSocket服务已启动,地址:ws://localhost:8080/ws")
|
fmt.Println("WebSocket服务已启动,地址:ws://localhost:8080/ws")
|
||||||
if err := server.ListenAndServe(); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
log.Fatalf("Server failed: %s", err)
|
log.Fatalf("Server failed: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user