Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
ygopro-proxy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
ygopro-proxy
Commits
6a6ce870
Commit
6a6ce870
authored
Oct 02, 2022
by
Chunchi Che
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add util/context.go
parent
ace8c9c1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
13 deletions
+39
-13
DarkNeos/transform.go
DarkNeos/transform.go
+6
-5
proxy.go
proxy.go
+17
-8
util/context.go
util/context.go
+16
-0
No files found.
DarkNeos/transform.go
View file @
6a6ce870
...
...
@@ -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
}
...
...
proxy.go
View file @
6a6ce870
...
...
@@ -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
...
...
util/context.go
0 → 100644
View file @
6a6ce870
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
,
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment