Commit 6a6ce870 authored by Chunchi Che's avatar Chunchi Che

add util/context.go

parent ace8c9c1
......@@ -10,6 +10,7 @@ import (
"unicode/utf16"
"github.com/sktt1ryze/ygopro-proxy/DarkNeos/ygopropb"
util "github.com/sktt1ryze/ygopro-proxy/util"
"google.golang.org/protobuf/proto"
)
......@@ -65,7 +66,7 @@ func packetToBuffer(pkt YgoPacket) []byte {
return buf
}
func bufferToPacket(p []byte) (YgoPacket, error) {
func bufferToPacket(p []byte, ctx *util.Context) (YgoPacket, error) {
if len(p) < PACKET_MIN_LEN {
return YgoPacket{}, errors.New(fmt.Sprintf("Packet len too short, len=%d", len(p)))
}
......@@ -76,9 +77,9 @@ func bufferToPacket(p []byte) (YgoPacket, error) {
if len(p) < int(packet_len+2) {
log.Printf(COMPONENT+
`Unmatched packet size, proto=%d, buffer length=%d, packet_len=%d,
`Unmatched packet size, proto=%d, buffer_length=%d, packet_len=%d, infa_read_len=%d
Use the buffer length.\n`,
proto, len(p), packet_len,
proto, len(p), packet_len, ctx.InfaReadBufferLen,
)
packet_len = uint16(len(p) - 2)
......@@ -93,7 +94,7 @@ Use the buffer length.\n`,
}, nil
}
func Transform(src []byte, tranformType int) ([]byte, error) {
func Transform(src []byte, tranformType int, ctx *util.Context) ([]byte, error) {
if tranformType == ProtobufToRawBuf {
message := &ygopropb.YgoCtosMsg{}
err := proto.Unmarshal(src, message)
......@@ -114,7 +115,7 @@ func Transform(src []byte, tranformType int) ([]byte, error) {
return packetToBuffer(packet), nil
} else if tranformType == RawBufToProtobuf {
packet, err := bufferToPacket(src)
packet, err := bufferToPacket(src, ctx)
if err != nil {
return nil, err
}
......
......@@ -10,6 +10,7 @@ import (
"github.com/gorilla/websocket"
darkneos "github.com/sktt1ryze/ygopro-proxy/DarkNeos"
util "github.com/sktt1ryze/ygopro-proxy/util"
)
const TARGET_PORT = ":8000"
......@@ -24,15 +25,20 @@ var upgrader = websocket.Upgrader{
WriteBufferSize: 0x1000,
}
// todo: create Context
func ygoEndpoint(w http.ResponseWriter, r *http.Request) {
defer log.Println(COMPONENT + "ygoEndpoint finished")
ctx := util.NewContext()
upgrader.CheckOrigin = wsChecker
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
ctx.WsConnected = true
log.Println(COMPONENT + "Connection to ws://localhost" + TARGET_PORT + " [websocket] succeeded!")
......@@ -40,6 +46,7 @@ func ygoEndpoint(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Fatal(err)
}
ctx.TcpConnected = true
log.Println(COMPONENT + "Connection to " + "12.0.0.1" + PROXY_PORT + " [tcp] succeeded!")
......@@ -56,8 +63,8 @@ func ygoEndpoint(w http.ResponseWriter, r *http.Request) {
close(tcpStopCh)
}()
go wsProxy(ws, wsCh, wsStopCh)
go tcpProxy(tcp, tcpCh, tcpStopCh)
go wsProxy(ws, wsCh, wsStopCh, ctx)
go tcpProxy(tcp, tcpCh, tcpStopCh, ctx)
for {
select {
......@@ -83,8 +90,8 @@ func ygoEndpoint(w http.ResponseWriter, r *http.Request) {
}
}
// todo: generic
func wsProxy(ws *websocket.Conn, Ch chan<- []byte, stopCh <-chan bool) {
// todo: use interface
func wsProxy(ws *websocket.Conn, Ch chan<- []byte, stopCh <-chan bool, ctx util.Context) {
defer ws.Close()
defer close(Ch)
......@@ -108,13 +115,14 @@ func wsProxy(ws *websocket.Conn, Ch chan<- []byte, stopCh <-chan bool) {
log.Println(err)
return
}
ctx.InfaReadBufferLen = len(buffer)
if messageType == websocket.CloseMessage {
log.Println(COMPONENT + "Websocket closed")
return
}
buffer, err = darkneos.Transform(buffer, darkneos.ProtobufToRawBuf)
buffer, err = darkneos.Transform(buffer, darkneos.ProtobufToRawBuf, &ctx)
if err != nil {
log.Println(err)
return
......@@ -125,7 +133,7 @@ func wsProxy(ws *websocket.Conn, Ch chan<- []byte, stopCh <-chan bool) {
}
}
func tcpProxy(tcp net.Conn, Ch chan<- []byte, stopCh <-chan bool) {
func tcpProxy(tcp net.Conn, Ch chan<- []byte, stopCh <-chan bool, ctx util.Context) {
defer tcp.Close()
defer close(Ch)
......@@ -143,7 +151,7 @@ func tcpProxy(tcp net.Conn, Ch chan<- []byte, stopCh <-chan bool) {
return
}
_, err := reader.Read(buffer)
n, err := reader.Read(buffer)
if err != nil {
if err == io.EOF {
continue
......@@ -156,8 +164,9 @@ func tcpProxy(tcp net.Conn, Ch chan<- []byte, stopCh <-chan bool) {
log.Println(err)
return
}
ctx.InfaReadBufferLen = n
buffer, err = darkneos.Transform(buffer, darkneos.RawBufToProtobuf)
buffer, err = darkneos.Transform(buffer, darkneos.RawBufToProtobuf, &ctx)
if err != nil {
log.Println(err)
return
......
package util
type Context struct {
ContextId int64
WsConnected bool
TcpConnected bool
InfaReadBufferLen int
}
func NewContext() Context {
return Context{
ContextId: 0, // todo
WsConnected: false,
TcpConnected: false,
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment