Commit 6f2ba85e authored by Chunchi Che's avatar Chunchi Che Committed by GitHub

Merge pull request #3 from DarkNeos/dev

Dev
parents e4d56fc9 6368e27a
package darkneos
import (
"encoding/binary"
"errors"
"fmt"
"unicode/utf16"
"github.com/sktt1ryze/ygopro-proxy/DarkNeos/ygopropb"
"google.golang.org/protobuf/proto"
)
const FILLING_TOKEN uint16 = 0xcccc
const UTF16_BUFFER_MAX_LEN int = 20
const PACKET_MIN_LEN int = 3
const (
ProtobufToRawBuf = 1
RawBufToProtobuf = 2
)
const (
CtosProtoPlayerInfo = 16
CtosProtoJoinGame = 18
StocChat = 25
)
type YgoPacket struct {
PacketLen uint16
Proto uint8
Exdata []byte
}
func packetToBuffer(pkt YgoPacket) []byte {
buf := make([]byte, 0)
// packet len
buf = append(buf, byte(pkt.PacketLen), byte(pkt.PacketLen>>8))
// proto
buf = append(buf, byte(pkt.Proto))
// exdata
for _, v := range pkt.Exdata {
buf = append(buf, v)
}
return buf
}
func bufferToPacket(p []byte) (YgoPacket, error) {
if len(p) < PACKET_MIN_LEN {
return YgoPacket{}, errors.New(fmt.Sprintf("Packet len too short, len=%d", len(p)))
}
// todo: impl Reader/Writer for buffer
packet_len := binary.LittleEndian.Uint16(p)
proto := p[2]
exdata := p[3 : packet_len+2]
return YgoPacket{
PacketLen: packet_len,
Proto: proto,
Exdata: exdata,
}, nil
}
func Transform(src []byte, tranformType int) ([]byte, error) {
if tranformType == ProtobufToRawBuf {
message := &ygopropb.YgoCtosMsg{}
err := proto.Unmarshal(src, message)
if err != nil {
return nil, err
}
var packet YgoPacket
switch message.Msg.(type) {
case *(ygopropb.YgoCtosMsg_CtosPlayerInfo):
packet = transformPlayerInfo(message.GetCtosPlayerInfo())
case *(ygopropb.YgoCtosMsg_CtosJoinGame):
packet = transformJoinGame(message.GetCtosJoinGame())
default:
return nil, errors.New("Unhandled YgoCtosMsg type")
}
return packetToBuffer(packet), nil
} else if tranformType == RawBufToProtobuf {
packet, err := bufferToPacket(src)
if err != nil {
return nil, err
}
var pb ygopropb.YgoStocMsg
switch packet.Proto {
case StocChat:
msg := transformChat(packet)
pb = ygopropb.YgoStocMsg{
Msg: &msg,
}
default:
return nil, errors.New("Unhandled YgoStocMsg type")
}
return proto.Marshal(&pb)
} else {
return nil, errors.New("Unknown tranformType")
}
}
// todo: use interface
// +++++ Client To Server +++++
// @Name: [20]uint16
func transformPlayerInfo(pb *ygopropb.CtosPlayerInfo) YgoPacket {
buf := strToUtf16Buffer(pb.Name)
exdata := uint16BufToByteBuf(buf)
return YgoPacket{
PacketLen: uint16(len(exdata)) + 1,
Proto: CtosProtoPlayerInfo,
Exdata: exdata,
}
}
// @Version: uint16
// @Gameid: uint32
// @Passwd: [20]uint16
func transformJoinGame(pb *ygopropb.CtosJoinGame) YgoPacket {
exdata := make([]byte, 0)
version := uint16(pb.Version)
exdata = append(exdata, byte(version), byte(version>>8), 0, 0)
exdata = append(exdata, byte(pb.Gameid), byte(pb.Gameid>>8), byte(pb.Gameid>>16), byte(pb.Gameid>>24))
for _, v := range uint16BufToByteBuf(strToUtf16Buffer(pb.Passwd)) {
exdata = append(exdata, v)
}
return YgoPacket{
PacketLen: uint16(len(exdata)) + 1,
Proto: CtosProtoJoinGame,
Exdata: exdata,
}
}
// +++++ Server To Client +++++
// @player: uint16
// @message: []uint16
func transformChat(pkt YgoPacket) ygopropb.YgoStocMsg_StocChat {
player := int32(binary.LittleEndian.Uint16(pkt.Exdata))
message := utf16BufferToStr(pkt.Exdata[2:])
return ygopropb.YgoStocMsg_StocChat{
StocChat: &ygopropb.StocChat{
Player: player,
Msg: message,
},
}
}
// +++++ Util Functions +++++
func strToUtf16Buffer(s string) []uint16 {
b := make([]uint16, UTF16_BUFFER_MAX_LEN, UTF16_BUFFER_MAX_LEN)
for i := range b {
b[i] = FILLING_TOKEN
}
s_utf16 := utf16.Encode([]rune(s))
// todo: optimize
for i, v := range s_utf16 {
if i < UTF16_BUFFER_MAX_LEN {
b[i] = v
if i == len(s_utf16)-1 && i < len(b)-1 {
b[i+1] = 0
}
} else {
break
}
}
return b
}
func utf16BufferToStr(p []byte) string {
v := chunkBytesToUint16s(p)
return string(utf16.Decode(v))
}
func uint16BufToByteBuf(u16_b []uint16) []byte {
b := make([]byte, 0, len(u16_b)*2)
for _, v := range u16_b {
// little endian
b = append(b, byte(v), byte(v>>8))
}
return b
}
func chunkBytesToUint16s(items []byte) []uint16 {
const chunkSize = 2
var chunks []uint16
for chunkSize < len(items) {
items, chunks = items[chunkSize:], append(chunks, binary.LittleEndian.Uint16(items))
}
return chunks
}
package ygoprobuffer
const PLAYER_NAME_MAX_LEN int = 20
type CtosPlayerInfo struct {
Name [PLAYER_NAME_MAX_LEN]uint16
}
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc v3.21.2
// protoc v3.21.5
// source: ygopro.proto
package ygopropb
......@@ -25,15 +25,10 @@ type YgoCtosMsg struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Proto int32 `protobuf:"varint,1,opt,name=proto,proto3" json:"proto,omitempty"`
// Types that are assignable to Msg:
// *YgoCtosMsg_CtosPlayerInfo
// *YgoCtosMsg_CtosJoinGame
// *YgoCtosMsg_CtosUpdateDeck
// *YgoCtosMsg_StocJoinGame
// *YgoCtosMsg_StocChat
// *YgoCtosMsg_StocHsPlayerEnter
// *YgoCtosMsg_StocTypeChange
Msg isYgoCtosMsg_Msg `protobuf_oneof:"msg"`
}
......@@ -69,13 +64,6 @@ func (*YgoCtosMsg) Descriptor() ([]byte, []int) {
return file_ygopro_proto_rawDescGZIP(), []int{0}
}
func (x *YgoCtosMsg) GetProto() int32 {
if x != nil {
return x.Proto
}
return 0
}
func (m *YgoCtosMsg) GetMsg() isYgoCtosMsg_Msg {
if m != nil {
return m.Msg
......@@ -104,79 +92,135 @@ func (x *YgoCtosMsg) GetCtosUpdateDeck() *CtosUpdateDeck {
return nil
}
func (x *YgoCtosMsg) GetStocJoinGame() *StocJoinGame {
if x, ok := x.GetMsg().(*YgoCtosMsg_StocJoinGame); ok {
return x.StocJoinGame
type isYgoCtosMsg_Msg interface {
isYgoCtosMsg_Msg()
}
type YgoCtosMsg_CtosPlayerInfo struct {
CtosPlayerInfo *CtosPlayerInfo `protobuf:"bytes,1,opt,name=ctos_player_info,json=ctosPlayerInfo,proto3,oneof"`
}
type YgoCtosMsg_CtosJoinGame struct {
CtosJoinGame *CtosJoinGame `protobuf:"bytes,2,opt,name=ctos_join_game,json=ctosJoinGame,proto3,oneof"`
}
type YgoCtosMsg_CtosUpdateDeck struct {
CtosUpdateDeck *CtosUpdateDeck `protobuf:"bytes,3,opt,name=ctos_update_deck,json=ctosUpdateDeck,proto3,oneof"`
}
func (*YgoCtosMsg_CtosPlayerInfo) isYgoCtosMsg_Msg() {}
func (*YgoCtosMsg_CtosJoinGame) isYgoCtosMsg_Msg() {}
func (*YgoCtosMsg_CtosUpdateDeck) isYgoCtosMsg_Msg() {}
type YgoStocMsg struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Types that are assignable to Msg:
// *YgoStocMsg_StocJoinGame
// *YgoStocMsg_StocChat
// *YgoStocMsg_StocHsPlayerEnter
// *YgoStocMsg_StocTypeChange
Msg isYgoStocMsg_Msg `protobuf_oneof:"msg"`
}
func (x *YgoStocMsg) Reset() {
*x = YgoStocMsg{}
if protoimpl.UnsafeEnabled {
mi := &file_ygopro_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
return nil
}
func (x *YgoCtosMsg) GetStocChat() *StocChat {
if x, ok := x.GetMsg().(*YgoCtosMsg_StocChat); ok {
return x.StocChat
func (x *YgoStocMsg) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*YgoStocMsg) ProtoMessage() {}
func (x *YgoStocMsg) ProtoReflect() protoreflect.Message {
mi := &file_ygopro_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return nil
return ms
}
return mi.MessageOf(x)
}
func (x *YgoCtosMsg) GetStocHsPlayerEnter() *StocHsPlayerEnter {
if x, ok := x.GetMsg().(*YgoCtosMsg_StocHsPlayerEnter); ok {
return x.StocHsPlayerEnter
// Deprecated: Use YgoStocMsg.ProtoReflect.Descriptor instead.
func (*YgoStocMsg) Descriptor() ([]byte, []int) {
return file_ygopro_proto_rawDescGZIP(), []int{1}
}
func (m *YgoStocMsg) GetMsg() isYgoStocMsg_Msg {
if m != nil {
return m.Msg
}
return nil
}
func (x *YgoCtosMsg) GetStocTypeChange() *StocTypeChange {
if x, ok := x.GetMsg().(*YgoCtosMsg_StocTypeChange); ok {
return x.StocTypeChange
func (x *YgoStocMsg) GetStocJoinGame() *StocJoinGame {
if x, ok := x.GetMsg().(*YgoStocMsg_StocJoinGame); ok {
return x.StocJoinGame
}
return nil
}
type isYgoCtosMsg_Msg interface {
isYgoCtosMsg_Msg()
func (x *YgoStocMsg) GetStocChat() *StocChat {
if x, ok := x.GetMsg().(*YgoStocMsg_StocChat); ok {
return x.StocChat
}
return nil
}
type YgoCtosMsg_CtosPlayerInfo struct {
CtosPlayerInfo *CtosPlayerInfo `protobuf:"bytes,2,opt,name=ctos_player_info,json=ctosPlayerInfo,proto3,oneof"`
func (x *YgoStocMsg) GetStocHsPlayerEnter() *StocHsPlayerEnter {
if x, ok := x.GetMsg().(*YgoStocMsg_StocHsPlayerEnter); ok {
return x.StocHsPlayerEnter
}
return nil
}
type YgoCtosMsg_CtosJoinGame struct {
CtosJoinGame *CtosJoinGame `protobuf:"bytes,3,opt,name=ctos_join_game,json=ctosJoinGame,proto3,oneof"`
func (x *YgoStocMsg) GetStocTypeChange() *StocTypeChange {
if x, ok := x.GetMsg().(*YgoStocMsg_StocTypeChange); ok {
return x.StocTypeChange
}
return nil
}
type YgoCtosMsg_CtosUpdateDeck struct {
CtosUpdateDeck *CtosUpdateDeck `protobuf:"bytes,4,opt,name=ctos_update_deck,json=ctosUpdateDeck,proto3,oneof"`
type isYgoStocMsg_Msg interface {
isYgoStocMsg_Msg()
}
type YgoCtosMsg_StocJoinGame struct {
StocJoinGame *StocJoinGame `protobuf:"bytes,101,opt,name=stoc_join_game,json=stocJoinGame,proto3,oneof"`
type YgoStocMsg_StocJoinGame struct {
StocJoinGame *StocJoinGame `protobuf:"bytes,1,opt,name=stoc_join_game,json=stocJoinGame,proto3,oneof"`
}
type YgoCtosMsg_StocChat struct {
StocChat *StocChat `protobuf:"bytes,102,opt,name=stoc_chat,json=stocChat,proto3,oneof"`
type YgoStocMsg_StocChat struct {
StocChat *StocChat `protobuf:"bytes,2,opt,name=stoc_chat,json=stocChat,proto3,oneof"`
}
type YgoCtosMsg_StocHsPlayerEnter struct {
StocHsPlayerEnter *StocHsPlayerEnter `protobuf:"bytes,103,opt,name=stoc_hs_player_enter,json=stocHsPlayerEnter,proto3,oneof"`
type YgoStocMsg_StocHsPlayerEnter struct {
StocHsPlayerEnter *StocHsPlayerEnter `protobuf:"bytes,3,opt,name=stoc_hs_player_enter,json=stocHsPlayerEnter,proto3,oneof"`
}
type YgoCtosMsg_StocTypeChange struct {
StocTypeChange *StocTypeChange `protobuf:"bytes,104,opt,name=stoc_type_change,json=stocTypeChange,proto3,oneof"`
type YgoStocMsg_StocTypeChange struct {
StocTypeChange *StocTypeChange `protobuf:"bytes,4,opt,name=stoc_type_change,json=stocTypeChange,proto3,oneof"`
}
func (*YgoCtosMsg_CtosPlayerInfo) isYgoCtosMsg_Msg() {}
func (*YgoStocMsg_StocJoinGame) isYgoStocMsg_Msg() {}
func (*YgoCtosMsg_CtosJoinGame) isYgoCtosMsg_Msg() {}
func (*YgoStocMsg_StocChat) isYgoStocMsg_Msg() {}
func (*YgoCtosMsg_CtosUpdateDeck) isYgoCtosMsg_Msg() {}
func (*YgoStocMsg_StocHsPlayerEnter) isYgoStocMsg_Msg() {}
func (*YgoCtosMsg_StocJoinGame) isYgoCtosMsg_Msg() {}
func (*YgoCtosMsg_StocChat) isYgoCtosMsg_Msg() {}
func (*YgoCtosMsg_StocHsPlayerEnter) isYgoCtosMsg_Msg() {}
func (*YgoCtosMsg_StocTypeChange) isYgoCtosMsg_Msg() {}
func (*YgoStocMsg_StocTypeChange) isYgoStocMsg_Msg() {}
type CtosPlayerInfo struct {
state protoimpl.MessageState
......@@ -189,7 +233,7 @@ type CtosPlayerInfo struct {
func (x *CtosPlayerInfo) Reset() {
*x = CtosPlayerInfo{}
if protoimpl.UnsafeEnabled {
mi := &file_ygopro_proto_msgTypes[1]
mi := &file_ygopro_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
......@@ -202,7 +246,7 @@ func (x *CtosPlayerInfo) String() string {
func (*CtosPlayerInfo) ProtoMessage() {}
func (x *CtosPlayerInfo) ProtoReflect() protoreflect.Message {
mi := &file_ygopro_proto_msgTypes[1]
mi := &file_ygopro_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
......@@ -215,7 +259,7 @@ func (x *CtosPlayerInfo) ProtoReflect() protoreflect.Message {
// Deprecated: Use CtosPlayerInfo.ProtoReflect.Descriptor instead.
func (*CtosPlayerInfo) Descriptor() ([]byte, []int) {
return file_ygopro_proto_rawDescGZIP(), []int{1}
return file_ygopro_proto_rawDescGZIP(), []int{2}
}
func (x *CtosPlayerInfo) GetName() string {
......@@ -238,7 +282,7 @@ type CtosJoinGame struct {
func (x *CtosJoinGame) Reset() {
*x = CtosJoinGame{}
if protoimpl.UnsafeEnabled {
mi := &file_ygopro_proto_msgTypes[2]
mi := &file_ygopro_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
......@@ -251,7 +295,7 @@ func (x *CtosJoinGame) String() string {
func (*CtosJoinGame) ProtoMessage() {}
func (x *CtosJoinGame) ProtoReflect() protoreflect.Message {
mi := &file_ygopro_proto_msgTypes[2]
mi := &file_ygopro_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
......@@ -264,7 +308,7 @@ func (x *CtosJoinGame) ProtoReflect() protoreflect.Message {
// Deprecated: Use CtosJoinGame.ProtoReflect.Descriptor instead.
func (*CtosJoinGame) Descriptor() ([]byte, []int) {
return file_ygopro_proto_rawDescGZIP(), []int{2}
return file_ygopro_proto_rawDescGZIP(), []int{3}
}
func (x *CtosJoinGame) GetVersion() int32 {
......@@ -301,7 +345,7 @@ type CtosUpdateDeck struct {
func (x *CtosUpdateDeck) Reset() {
*x = CtosUpdateDeck{}
if protoimpl.UnsafeEnabled {
mi := &file_ygopro_proto_msgTypes[3]
mi := &file_ygopro_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
......@@ -314,7 +358,7 @@ func (x *CtosUpdateDeck) String() string {
func (*CtosUpdateDeck) ProtoMessage() {}
func (x *CtosUpdateDeck) ProtoReflect() protoreflect.Message {
mi := &file_ygopro_proto_msgTypes[3]
mi := &file_ygopro_proto_msgTypes[4]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
......@@ -327,7 +371,7 @@ func (x *CtosUpdateDeck) ProtoReflect() protoreflect.Message {
// Deprecated: Use CtosUpdateDeck.ProtoReflect.Descriptor instead.
func (*CtosUpdateDeck) Descriptor() ([]byte, []int) {
return file_ygopro_proto_rawDescGZIP(), []int{3}
return file_ygopro_proto_rawDescGZIP(), []int{4}
}
func (x *CtosUpdateDeck) GetMain() []int32 {
......@@ -371,7 +415,7 @@ type StocJoinGame struct {
func (x *StocJoinGame) Reset() {
*x = StocJoinGame{}
if protoimpl.UnsafeEnabled {
mi := &file_ygopro_proto_msgTypes[4]
mi := &file_ygopro_proto_msgTypes[5]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
......@@ -384,7 +428,7 @@ func (x *StocJoinGame) String() string {
func (*StocJoinGame) ProtoMessage() {}
func (x *StocJoinGame) ProtoReflect() protoreflect.Message {
mi := &file_ygopro_proto_msgTypes[4]
mi := &file_ygopro_proto_msgTypes[5]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
......@@ -397,7 +441,7 @@ func (x *StocJoinGame) ProtoReflect() protoreflect.Message {
// Deprecated: Use StocJoinGame.ProtoReflect.Descriptor instead.
func (*StocJoinGame) Descriptor() ([]byte, []int) {
return file_ygopro_proto_rawDescGZIP(), []int{4}
return file_ygopro_proto_rawDescGZIP(), []int{5}
}
func (x *StocJoinGame) GetLflist() int32 {
......@@ -482,7 +526,7 @@ type StocChat struct {
func (x *StocChat) Reset() {
*x = StocChat{}
if protoimpl.UnsafeEnabled {
mi := &file_ygopro_proto_msgTypes[5]
mi := &file_ygopro_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
......@@ -495,7 +539,7 @@ func (x *StocChat) String() string {
func (*StocChat) ProtoMessage() {}
func (x *StocChat) ProtoReflect() protoreflect.Message {
mi := &file_ygopro_proto_msgTypes[5]
mi := &file_ygopro_proto_msgTypes[6]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
......@@ -508,7 +552,7 @@ func (x *StocChat) ProtoReflect() protoreflect.Message {
// Deprecated: Use StocChat.ProtoReflect.Descriptor instead.
func (*StocChat) Descriptor() ([]byte, []int) {
return file_ygopro_proto_rawDescGZIP(), []int{5}
return file_ygopro_proto_rawDescGZIP(), []int{6}
}
func (x *StocChat) GetPlayer() int32 {
......@@ -537,7 +581,7 @@ type StocHsPlayerEnter struct {
func (x *StocHsPlayerEnter) Reset() {
*x = StocHsPlayerEnter{}
if protoimpl.UnsafeEnabled {
mi := &file_ygopro_proto_msgTypes[6]
mi := &file_ygopro_proto_msgTypes[7]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
......@@ -550,7 +594,7 @@ func (x *StocHsPlayerEnter) String() string {
func (*StocHsPlayerEnter) ProtoMessage() {}
func (x *StocHsPlayerEnter) ProtoReflect() protoreflect.Message {
mi := &file_ygopro_proto_msgTypes[6]
mi := &file_ygopro_proto_msgTypes[7]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
......@@ -563,7 +607,7 @@ func (x *StocHsPlayerEnter) ProtoReflect() protoreflect.Message {
// Deprecated: Use StocHsPlayerEnter.ProtoReflect.Descriptor instead.
func (*StocHsPlayerEnter) Descriptor() ([]byte, []int) {
return file_ygopro_proto_rawDescGZIP(), []int{6}
return file_ygopro_proto_rawDescGZIP(), []int{7}
}
func (x *StocHsPlayerEnter) GetName() string {
......@@ -591,7 +635,7 @@ type StocTypeChange struct {
func (x *StocTypeChange) Reset() {
*x = StocTypeChange{}
if protoimpl.UnsafeEnabled {
mi := &file_ygopro_proto_msgTypes[7]
mi := &file_ygopro_proto_msgTypes[8]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
......@@ -604,7 +648,7 @@ func (x *StocTypeChange) String() string {
func (*StocTypeChange) ProtoMessage() {}
func (x *StocTypeChange) ProtoReflect() protoreflect.Message {
mi := &file_ygopro_proto_msgTypes[7]
mi := &file_ygopro_proto_msgTypes[8]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
......@@ -617,7 +661,7 @@ func (x *StocTypeChange) ProtoReflect() protoreflect.Message {
// Deprecated: Use StocTypeChange.ProtoReflect.Descriptor instead.
func (*StocTypeChange) Descriptor() ([]byte, []int) {
return file_ygopro_proto_rawDescGZIP(), []int{7}
return file_ygopro_proto_rawDescGZIP(), []int{8}
}
func (x *StocTypeChange) GetType() int32 {
......@@ -631,35 +675,35 @@ var File_ygopro_proto protoreflect.FileDescriptor
var file_ygopro_proto_rawDesc = []byte{
0x0a, 0x0c, 0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06,
0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x22, 0xf0, 0x03, 0x0a, 0x0a, 0x59, 0x67, 0x6f, 0x43, 0x74,
0x6f, 0x73, 0x4d, 0x73, 0x67, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x18, 0x01,
0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x42, 0x0a, 0x10, 0x63,
0x74, 0x6f, 0x73, 0x5f, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x6e, 0x66, 0x6f, 0x18,
0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x2e, 0x43,
0x74, 0x6f, 0x73, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x6e, 0x66, 0x6f, 0x48, 0x00, 0x52,
0x0e, 0x63, 0x74, 0x6f, 0x73, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x6e, 0x66, 0x6f, 0x12,
0x3c, 0x0a, 0x0e, 0x63, 0x74, 0x6f, 0x73, 0x5f, 0x6a, 0x6f, 0x69, 0x6e, 0x5f, 0x67, 0x61, 0x6d,
0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f,
0x2e, 0x43, 0x74, 0x6f, 0x73, 0x4a, 0x6f, 0x69, 0x6e, 0x47, 0x61, 0x6d, 0x65, 0x48, 0x00, 0x52,
0x0c, 0x63, 0x74, 0x6f, 0x73, 0x4a, 0x6f, 0x69, 0x6e, 0x47, 0x61, 0x6d, 0x65, 0x12, 0x42, 0x0a,
0x10, 0x63, 0x74, 0x6f, 0x73, 0x5f, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x5f, 0x64, 0x65, 0x63,
0x6b, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f,
0x2e, 0x43, 0x74, 0x6f, 0x73, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x44, 0x65, 0x63, 0x6b, 0x48,
0x00, 0x52, 0x0e, 0x63, 0x74, 0x6f, 0x73, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x44, 0x65, 0x63,
0x6b, 0x12, 0x3c, 0x0a, 0x0e, 0x73, 0x74, 0x6f, 0x63, 0x5f, 0x6a, 0x6f, 0x69, 0x6e, 0x5f, 0x67,
0x61, 0x6d, 0x65, 0x18, 0x65, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x79, 0x67, 0x6f, 0x70,
0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x22, 0xd9, 0x01, 0x0a, 0x0a, 0x59, 0x67, 0x6f, 0x43, 0x74,
0x6f, 0x73, 0x4d, 0x73, 0x67, 0x12, 0x42, 0x0a, 0x10, 0x63, 0x74, 0x6f, 0x73, 0x5f, 0x70, 0x6c,
0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32,
0x16, 0x2e, 0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x2e, 0x43, 0x74, 0x6f, 0x73, 0x50, 0x6c, 0x61,
0x79, 0x65, 0x72, 0x49, 0x6e, 0x66, 0x6f, 0x48, 0x00, 0x52, 0x0e, 0x63, 0x74, 0x6f, 0x73, 0x50,
0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x3c, 0x0a, 0x0e, 0x63, 0x74, 0x6f,
0x73, 0x5f, 0x6a, 0x6f, 0x69, 0x6e, 0x5f, 0x67, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x14, 0x2e, 0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x2e, 0x43, 0x74, 0x6f, 0x73, 0x4a,
0x6f, 0x69, 0x6e, 0x47, 0x61, 0x6d, 0x65, 0x48, 0x00, 0x52, 0x0c, 0x63, 0x74, 0x6f, 0x73, 0x4a,
0x6f, 0x69, 0x6e, 0x47, 0x61, 0x6d, 0x65, 0x12, 0x42, 0x0a, 0x10, 0x63, 0x74, 0x6f, 0x73, 0x5f,
0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x5f, 0x64, 0x65, 0x63, 0x6b, 0x18, 0x03, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x16, 0x2e, 0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x2e, 0x43, 0x74, 0x6f, 0x73, 0x55,
0x70, 0x64, 0x61, 0x74, 0x65, 0x44, 0x65, 0x63, 0x6b, 0x48, 0x00, 0x52, 0x0e, 0x63, 0x74, 0x6f,
0x73, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x44, 0x65, 0x63, 0x6b, 0x42, 0x05, 0x0a, 0x03, 0x6d,
0x73, 0x67, 0x22, 0x94, 0x02, 0x0a, 0x0a, 0x59, 0x67, 0x6f, 0x53, 0x74, 0x6f, 0x63, 0x4d, 0x73,
0x67, 0x12, 0x3c, 0x0a, 0x0e, 0x73, 0x74, 0x6f, 0x63, 0x5f, 0x6a, 0x6f, 0x69, 0x6e, 0x5f, 0x67,
0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x79, 0x67, 0x6f, 0x70,
0x72, 0x6f, 0x2e, 0x53, 0x74, 0x6f, 0x63, 0x4a, 0x6f, 0x69, 0x6e, 0x47, 0x61, 0x6d, 0x65, 0x48,
0x00, 0x52, 0x0c, 0x73, 0x74, 0x6f, 0x63, 0x4a, 0x6f, 0x69, 0x6e, 0x47, 0x61, 0x6d, 0x65, 0x12,
0x2f, 0x0a, 0x09, 0x73, 0x74, 0x6f, 0x63, 0x5f, 0x63, 0x68, 0x61, 0x74, 0x18, 0x66, 0x20, 0x01,
0x2f, 0x0a, 0x09, 0x73, 0x74, 0x6f, 0x63, 0x5f, 0x63, 0x68, 0x61, 0x74, 0x18, 0x02, 0x20, 0x01,
0x28, 0x0b, 0x32, 0x10, 0x2e, 0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x2e, 0x53, 0x74, 0x6f, 0x63,
0x43, 0x68, 0x61, 0x74, 0x48, 0x00, 0x52, 0x08, 0x73, 0x74, 0x6f, 0x63, 0x43, 0x68, 0x61, 0x74,
0x12, 0x4c, 0x0a, 0x14, 0x73, 0x74, 0x6f, 0x63, 0x5f, 0x68, 0x73, 0x5f, 0x70, 0x6c, 0x61, 0x79,
0x65, 0x72, 0x5f, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x18, 0x67, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19,
0x65, 0x72, 0x5f, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19,
0x2e, 0x79, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x2e, 0x53, 0x74, 0x6f, 0x63, 0x48, 0x73, 0x50, 0x6c,
0x61, 0x79, 0x65, 0x72, 0x45, 0x6e, 0x74, 0x65, 0x72, 0x48, 0x00, 0x52, 0x11, 0x73, 0x74, 0x6f,
0x63, 0x48, 0x73, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x45, 0x6e, 0x74, 0x65, 0x72, 0x12, 0x42,
0x0a, 0x10, 0x73, 0x74, 0x6f, 0x63, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x5f, 0x63, 0x68, 0x61, 0x6e,
0x67, 0x65, 0x18, 0x68, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x79, 0x67, 0x6f, 0x70, 0x72,
0x67, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x79, 0x67, 0x6f, 0x70, 0x72,
0x6f, 0x2e, 0x53, 0x74, 0x6f, 0x63, 0x54, 0x79, 0x70, 0x65, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65,
0x48, 0x00, 0x52, 0x0e, 0x73, 0x74, 0x6f, 0x63, 0x54, 0x79, 0x70, 0x65, 0x43, 0x68, 0x61, 0x6e,
0x67, 0x65, 0x42, 0x05, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x22, 0x24, 0x0a, 0x0e, 0x43, 0x74, 0x6f,
......@@ -720,25 +764,26 @@ func file_ygopro_proto_rawDescGZIP() []byte {
return file_ygopro_proto_rawDescData
}
var file_ygopro_proto_msgTypes = make([]protoimpl.MessageInfo, 8)
var file_ygopro_proto_msgTypes = make([]protoimpl.MessageInfo, 9)
var file_ygopro_proto_goTypes = []interface{}{
(*YgoCtosMsg)(nil), // 0: ygopro.YgoCtosMsg
(*CtosPlayerInfo)(nil), // 1: ygopro.CtosPlayerInfo
(*CtosJoinGame)(nil), // 2: ygopro.CtosJoinGame
(*CtosUpdateDeck)(nil), // 3: ygopro.CtosUpdateDeck
(*StocJoinGame)(nil), // 4: ygopro.StocJoinGame
(*StocChat)(nil), // 5: ygopro.StocChat
(*StocHsPlayerEnter)(nil), // 6: ygopro.StocHsPlayerEnter
(*StocTypeChange)(nil), // 7: ygopro.StocTypeChange
(*YgoStocMsg)(nil), // 1: ygopro.YgoStocMsg
(*CtosPlayerInfo)(nil), // 2: ygopro.CtosPlayerInfo
(*CtosJoinGame)(nil), // 3: ygopro.CtosJoinGame
(*CtosUpdateDeck)(nil), // 4: ygopro.CtosUpdateDeck
(*StocJoinGame)(nil), // 5: ygopro.StocJoinGame
(*StocChat)(nil), // 6: ygopro.StocChat
(*StocHsPlayerEnter)(nil), // 7: ygopro.StocHsPlayerEnter
(*StocTypeChange)(nil), // 8: ygopro.StocTypeChange
}
var file_ygopro_proto_depIdxs = []int32{
1, // 0: ygopro.YgoCtosMsg.ctos_player_info:type_name -> ygopro.CtosPlayerInfo
2, // 1: ygopro.YgoCtosMsg.ctos_join_game:type_name -> ygopro.CtosJoinGame
3, // 2: ygopro.YgoCtosMsg.ctos_update_deck:type_name -> ygopro.CtosUpdateDeck
4, // 3: ygopro.YgoCtosMsg.stoc_join_game:type_name -> ygopro.StocJoinGame
5, // 4: ygopro.YgoCtosMsg.stoc_chat:type_name -> ygopro.StocChat
6, // 5: ygopro.YgoCtosMsg.stoc_hs_player_enter:type_name -> ygopro.StocHsPlayerEnter
7, // 6: ygopro.YgoCtosMsg.stoc_type_change:type_name -> ygopro.StocTypeChange
2, // 0: ygopro.YgoCtosMsg.ctos_player_info:type_name -> ygopro.CtosPlayerInfo
3, // 1: ygopro.YgoCtosMsg.ctos_join_game:type_name -> ygopro.CtosJoinGame
4, // 2: ygopro.YgoCtosMsg.ctos_update_deck:type_name -> ygopro.CtosUpdateDeck
5, // 3: ygopro.YgoStocMsg.stoc_join_game:type_name -> ygopro.StocJoinGame
6, // 4: ygopro.YgoStocMsg.stoc_chat:type_name -> ygopro.StocChat
7, // 5: ygopro.YgoStocMsg.stoc_hs_player_enter:type_name -> ygopro.StocHsPlayerEnter
8, // 6: ygopro.YgoStocMsg.stoc_type_change:type_name -> ygopro.StocTypeChange
7, // [7:7] is the sub-list for method output_type
7, // [7:7] is the sub-list for method input_type
7, // [7:7] is the sub-list for extension type_name
......@@ -765,7 +810,7 @@ func file_ygopro_proto_init() {
}
}
file_ygopro_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*CtosPlayerInfo); i {
switch v := v.(*YgoStocMsg); i {
case 0:
return &v.state
case 1:
......@@ -777,7 +822,7 @@ func file_ygopro_proto_init() {
}
}
file_ygopro_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*CtosJoinGame); i {
switch v := v.(*CtosPlayerInfo); i {
case 0:
return &v.state
case 1:
......@@ -789,7 +834,7 @@ func file_ygopro_proto_init() {
}
}
file_ygopro_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*CtosUpdateDeck); i {
switch v := v.(*CtosJoinGame); i {
case 0:
return &v.state
case 1:
......@@ -801,7 +846,7 @@ func file_ygopro_proto_init() {
}
}
file_ygopro_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*StocJoinGame); i {
switch v := v.(*CtosUpdateDeck); i {
case 0:
return &v.state
case 1:
......@@ -813,7 +858,7 @@ func file_ygopro_proto_init() {
}
}
file_ygopro_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*StocChat); i {
switch v := v.(*StocJoinGame); i {
case 0:
return &v.state
case 1:
......@@ -825,7 +870,7 @@ func file_ygopro_proto_init() {
}
}
file_ygopro_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*StocHsPlayerEnter); i {
switch v := v.(*StocChat); i {
case 0:
return &v.state
case 1:
......@@ -837,6 +882,18 @@ func file_ygopro_proto_init() {
}
}
file_ygopro_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*StocHsPlayerEnter); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_ygopro_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*StocTypeChange); i {
case 0:
return &v.state
......@@ -853,10 +910,12 @@ func file_ygopro_proto_init() {
(*YgoCtosMsg_CtosPlayerInfo)(nil),
(*YgoCtosMsg_CtosJoinGame)(nil),
(*YgoCtosMsg_CtosUpdateDeck)(nil),
(*YgoCtosMsg_StocJoinGame)(nil),
(*YgoCtosMsg_StocChat)(nil),
(*YgoCtosMsg_StocHsPlayerEnter)(nil),
(*YgoCtosMsg_StocTypeChange)(nil),
}
file_ygopro_proto_msgTypes[1].OneofWrappers = []interface{}{
(*YgoStocMsg_StocJoinGame)(nil),
(*YgoStocMsg_StocChat)(nil),
(*YgoStocMsg_StocHsPlayerEnter)(nil),
(*YgoStocMsg_StocTypeChange)(nil),
}
type x struct{}
out := protoimpl.TypeBuilder{
......@@ -864,7 +923,7 @@ func file_ygopro_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_ygopro_proto_rawDesc,
NumEnums: 0,
NumMessages: 8,
NumMessages: 9,
NumExtensions: 0,
NumServices: 0,
},
......
......@@ -2,4 +2,8 @@ module github.com/sktt1ryze/ygopro-proxy
go 1.17
require github.com/gorilla/websocket v1.5.0 // indirect
require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
......@@ -9,6 +9,7 @@ import (
"sync"
"github.com/gorilla/websocket"
darkneos "github.com/sktt1ryze/ygopro-proxy/DarkNeos"
)
const TARGET_PORT = ":8000"
......@@ -50,10 +51,12 @@ func ygoEndpoint(w http.ResponseWriter, r *http.Request) {
}
func wsProxy(ws *websocket.Conn, tcp *net.Conn, wg *sync.WaitGroup) {
writer := bufio.NewWriter(*tcp)
for {
messageType, buf, err := ws.ReadMessage()
messageType, buffer, err := ws.ReadMessage()
if err != nil {
log.Fatal("websocket read message error: ", err)
log.Println("websocket read message error: ", err)
break
}
......@@ -62,10 +65,11 @@ func wsProxy(ws *websocket.Conn, tcp *net.Conn, wg *sync.WaitGroup) {
break
}
log.Println("websocket to tcp: " + string(buf))
writer := bufio.NewWriter(*tcp)
buffer := make([]byte, BUFFER_SIZE)
buffer, err = darkneos.Transform(buffer, darkneos.ProtobufToRawBuf)
if err != nil {
log.Fatal(err)
break
}
_, err = writer.Write(buffer)
if err != nil {
......@@ -78,23 +82,27 @@ func wsProxy(ws *websocket.Conn, tcp *net.Conn, wg *sync.WaitGroup) {
}
func tcpProxy(tcp *net.Conn, ws *websocket.Conn, wg *sync.WaitGroup) {
for {
reader := bufio.NewReader(*tcp)
buffer := make([]byte, BUFFER_SIZE)
for {
_, err := reader.Read(buffer)
if err != nil {
if err == io.EOF {
continue
}
log.Fatal("tcp read message error: ", err)
log.Println("tcp read message error: ", err)
break
}
log.Println("tcp to websocket: " + string(buffer))
buffer, err = darkneos.Transform(buffer, darkneos.RawBufToProtobuf)
if err != nil {
log.Fatal(err)
break
}
err = ws.WriteMessage(websocket.TextMessage, buffer) // temporary TextMessage, should be BinaryMessage in ygopro
err = ws.WriteMessage(websocket.BinaryMessage, buffer)
if err != nil {
log.Fatal("tcp send message error: ", err)
break
......
......@@ -4,16 +4,19 @@ package ygopro;
option go_package = "DarkNeos/ygopropb";
message YgoCtosMsg {
int32 proto = 1;
oneof msg {
CtosPlayerInfo ctos_player_info = 2;
CtosJoinGame ctos_join_game = 3;
CtosUpdateDeck ctos_update_deck = 4;
CtosPlayerInfo ctos_player_info = 1;
CtosJoinGame ctos_join_game = 2;
CtosUpdateDeck ctos_update_deck = 3;
}
}
StocJoinGame stoc_join_game = 101;
StocChat stoc_chat = 102;
StocHsPlayerEnter stoc_hs_player_enter = 103;
StocTypeChange stoc_type_change = 104;
message YgoStocMsg {
oneof msg {
StocJoinGame stoc_join_game = 1;
StocChat stoc_chat = 2;
StocHsPlayerEnter stoc_hs_player_enter = 3;
StocTypeChange stoc_type_change = 4;
}
}
......
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