Skip to content

Commit

Permalink
refactor: removed conn package and placed its code inside proxy package
Browse files Browse the repository at this point in the history
  • Loading branch information
realDragonium committed Jul 7, 2021
1 parent ae5816b commit b6fc05a
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 216 deletions.
4 changes: 3 additions & 1 deletion conn/listener.go → proxy/listener.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package conn
package proxy

import (
"bufio"
Expand All @@ -18,6 +18,7 @@ const (
DISCONNECT
SEND_STATUS
CLOSE
ERROR
)

const (
Expand Down Expand Up @@ -117,6 +118,7 @@ func ReadConnection(c net.Conn, reqCh chan ConnRequest) {
case SEND_STATUS:
// Notchian servers will wait for ping packet before sending response...?
// source: https://wiki.vg/Server_List_Ping#Response (first line -> second sentence)
// (which is different from how we do it rn)
conn.ReadPacket()
conn.WritePacket(ans.StatusPk)
pingPk, _ := conn.ReadPacket()
Expand Down
116 changes: 58 additions & 58 deletions conn/listener_test.go → proxy/listener_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package conn_test
package proxy_test

import (
"bytes"
Expand All @@ -8,8 +8,8 @@ import (
"testing"
"time"

"github.com/realDragonium/Ultraviolet/conn"
"github.com/realDragonium/Ultraviolet/mc"
"github.com/realDragonium/Ultraviolet/proxy"
)

var defaultChTimeout time.Duration = 10 * time.Millisecond
Expand Down Expand Up @@ -96,12 +96,12 @@ func samePK(expected, received mc.Packet) bool {

func TestListener(t *testing.T) {
runSimpleListener := func(newConnCh <-chan net.Conn) {
reqCh := make(chan conn.ConnRequest)
reqCh := make(chan proxy.ConnRequest)
mockListener := &testListener{
newConnCh: newConnCh,
}
go func() {
conn.Serve(mockListener, reqCh)
proxy.Serve(mockListener, reqCh)
}()
}

Expand Down Expand Up @@ -139,8 +139,8 @@ func TestListener(t *testing.T) {
func TestReadConnection(t *testing.T) {
t.Run("Can read handshake packet", func(t *testing.T) {
client, server := net.Pipe()
reqCh := make(chan conn.ConnRequest)
go conn.ReadConnection(server, reqCh)
reqCh := make(chan proxy.ConnRequest)
go proxy.ReadConnection(server, reqCh)

finishedWritingCh := make(chan struct{})
go func() {
Expand All @@ -160,8 +160,8 @@ func TestReadConnection(t *testing.T) {

t.Run("will close connection by invalid packet size", func(t *testing.T) {
client, server := net.Pipe()
reqCh := make(chan conn.ConnRequest)
go conn.ReadConnection(server, reqCh)
reqCh := make(chan proxy.ConnRequest)
go proxy.ReadConnection(server, reqCh)

finishedWritingCh := make(chan struct{})
go func() {
Expand All @@ -187,12 +187,12 @@ func TestReadConnection(t *testing.T) {

t.Run("Can read start login packet", func(t *testing.T) {
clientConn, proxyFrontend := net.Pipe()
reqCh := make(chan conn.ConnRequest)
go conn.ReadConnection(proxyFrontend, reqCh)
reqCh := make(chan proxy.ConnRequest)
go proxy.ReadConnection(proxyFrontend, reqCh)

finishedWritingCh := make(chan struct{})
go func() {
client := conn.NewMcConn(clientConn)
client := proxy.NewMcConn(clientConn)
hsPk := loginHandshakePacket()
client.WritePacket(hsPk)
loginPk := basicLoginStartPacket()
Expand All @@ -215,11 +215,11 @@ func TestReadConnection(t *testing.T) {
conn: proxyFrontend,
remoteAddr: &clientAddr,
}
reqCh := make(chan conn.ConnRequest)
go conn.ReadConnection(&mockClientconn, reqCh)
reqCh := make(chan proxy.ConnRequest)
go proxy.ReadConnection(&mockClientconn, reqCh)

go func() {
client := conn.NewMcConn(clientConn)
client := proxy.NewMcConn(clientConn)
hsPk := loginHandshakePacket()
client.WritePacket(hsPk)
loginPk := basicLoginStartPacket()
Expand All @@ -245,44 +245,44 @@ func TestReadConnection(t *testing.T) {
})

t.Run("Close response closes channel", func(t *testing.T) {
reqCh := make(chan conn.ConnRequest)
reqCh := make(chan proxy.ConnRequest)
clientConn, proxyFrontend := net.Pipe()
go conn.ReadConnection(proxyFrontend, reqCh)
go proxy.ReadConnection(proxyFrontend, reqCh)

go func() {
client := conn.NewMcConn(clientConn)
client := proxy.NewMcConn(clientConn)
hsPk := statusHandshakePacket()
client.WritePacket(hsPk)
}()

request := <-reqCh

request.Ch <- conn.ConnAnswer{
Action: conn.CLOSE,
request.Ch <- proxy.ConnAnswer{
Action: proxy.CLOSE,
}
testConnectedClosed(t, clientConn)
})

t.Run("can proxy login connection", func(t *testing.T) {
reqCh := make(chan conn.ConnRequest)
reqCh := make(chan proxy.ConnRequest)
clientConn, proxyFrontend := net.Pipe()
proxyBackend, serverConn := net.Pipe()
go conn.ReadConnection(proxyFrontend, reqCh)
go proxy.ReadConnection(proxyFrontend, reqCh)

hsPk := loginHandshakePacket()
loginPk := basicLoginStartPacket()

client := conn.NewMcConn(clientConn)
client := proxy.NewMcConn(clientConn)
client.WritePacket(hsPk)
client.WritePacket(loginPk)
request := <-reqCh

request.Ch <- conn.ConnAnswer{
Action: conn.PROXY,
ServerConn: conn.NewMcConn(proxyBackend),
request.Ch <- proxy.ConnAnswer{
Action: proxy.PROXY,
ServerConn: proxy.NewMcConn(proxyBackend),
}

server := conn.NewMcConn(serverConn)
server := proxy.NewMcConn(serverConn)
receivedHsPk, _ := server.ReadPacket()
if !samePK(hsPk, receivedHsPk) {
t.Errorf("expected: %v, \ngot: %v", hsPk, receivedHsPk)
Expand All @@ -296,11 +296,11 @@ func TestReadConnection(t *testing.T) {
})

t.Run("can send disconnect packet", func(t *testing.T) {
reqCh := make(chan conn.ConnRequest)
reqCh := make(chan proxy.ConnRequest)
clientConn, proxyFrontend := net.Pipe()
go conn.ReadConnection(proxyFrontend, reqCh)
go proxy.ReadConnection(proxyFrontend, reqCh)

client := conn.NewMcConn(clientConn)
client := proxy.NewMcConn(clientConn)
hsPk := loginHandshakePacket()
client.WritePacket(hsPk)
loginPk := basicLoginStartPacket()
Expand All @@ -312,8 +312,8 @@ func TestReadConnection(t *testing.T) {
Reason: mc.Chat(disconMessage),
}.Marshal()

request.Ch <- conn.ConnAnswer{
Action: conn.DISCONNECT,
request.Ch <- proxy.ConnAnswer{
Action: proxy.DISCONNECT,
DisconMessage: disconPk,
}

Expand All @@ -328,18 +328,18 @@ func TestReadConnection(t *testing.T) {

t.Run("expect connection to be closed after disconnect", func(t *testing.T) {
clientConn, proxyFrontend := net.Pipe()
reqCh := make(chan conn.ConnRequest)
go conn.ReadConnection(proxyFrontend, reqCh)
reqCh := make(chan proxy.ConnRequest)
go proxy.ReadConnection(proxyFrontend, reqCh)

client := conn.NewMcConn(clientConn)
client := proxy.NewMcConn(clientConn)
hsPk := loginHandshakePacket()
client.WritePacket(hsPk)
loginPk := basicLoginStartPacket()
client.WritePacket(loginPk)

request := <-reqCh
request.Ch <- conn.ConnAnswer{
Action: conn.DISCONNECT,
request.Ch <- proxy.ConnAnswer{
Action: proxy.DISCONNECT,
DisconMessage: mc.ClientBoundDisconnect{}.Marshal(),
}
client.ReadPacket()
Expand All @@ -348,11 +348,11 @@ func TestReadConnection(t *testing.T) {
})

t.Run("send status request through channel", func(t *testing.T) {
reqCh := make(chan conn.ConnRequest)
reqCh := make(chan proxy.ConnRequest)
clientConn, proxyFrontend := net.Pipe()
go conn.ReadConnection(proxyFrontend, reqCh)
go proxy.ReadConnection(proxyFrontend, reqCh)

client := conn.NewMcConn(clientConn)
client := proxy.NewMcConn(clientConn)
hsPk := statusHandshakePacket()
client.WritePacket(hsPk)

Expand All @@ -366,24 +366,24 @@ func TestReadConnection(t *testing.T) {
t.Run("can proxy connection to server", func(t *testing.T) {
clientConn, proxyFrontend := net.Pipe()
proxyBackend, serverConn := net.Pipe()
reqCh := make(chan conn.ConnRequest)
go conn.ReadConnection(proxyFrontend, reqCh)
reqCh := make(chan proxy.ConnRequest)
go proxy.ReadConnection(proxyFrontend, reqCh)

hsPk := statusHandshakePacket()

go func() {
client := conn.NewMcConn(clientConn)
client := proxy.NewMcConn(clientConn)
client.WritePacket(hsPk)
request := <-reqCh

proxyConn := conn.NewMcConn(proxyBackend)
request.Ch <- conn.ConnAnswer{
Action: conn.PROXY,
proxyConn := proxy.NewMcConn(proxyBackend)
request.Ch <- proxy.ConnAnswer{
Action: proxy.PROXY,
ServerConn: proxyConn,
}
}()

server := conn.NewMcConn(serverConn)
server := proxy.NewMcConn(serverConn)
receivedHsPk, _ := server.ReadPacket()
if !samePK(hsPk, receivedHsPk) {
t.Errorf("expected:\t %v, \ngot:\t %v", hsPk, receivedHsPk)
Expand All @@ -394,10 +394,10 @@ func TestReadConnection(t *testing.T) {

t.Run("can reply to status", func(t *testing.T) {
clientConn, proxyFrontend := net.Pipe()
reqCh := make(chan conn.ConnRequest)
go conn.ReadConnection(proxyFrontend, reqCh)
reqCh := make(chan proxy.ConnRequest)
go proxy.ReadConnection(proxyFrontend, reqCh)

client := conn.NewMcConn(clientConn)
client := proxy.NewMcConn(clientConn)
hsPk := statusHandshakePacket()
client.WritePacket(hsPk)

Expand All @@ -407,8 +407,8 @@ func TestReadConnection(t *testing.T) {
Protocol: 751,
Description: "Some broken proxy",
}.Marshal()
statusAnswer := conn.ConnAnswer{
Action: conn.SEND_STATUS,
statusAnswer := proxy.ConnAnswer{
Action: proxy.SEND_STATUS,
StatusPk: statusPk,
}
request.Ch <- statusAnswer
Expand All @@ -428,10 +428,10 @@ func TestReadConnection(t *testing.T) {

t.Run("close connection after non proxied status response", func(t *testing.T) {
clientConn, proxyFrontend := net.Pipe()
reqCh := make(chan conn.ConnRequest)
go conn.ReadConnection(proxyFrontend, reqCh)
reqCh := make(chan proxy.ConnRequest)
go proxy.ReadConnection(proxyFrontend, reqCh)

client := conn.NewMcConn(clientConn)
client := proxy.NewMcConn(clientConn)
hsPk := statusHandshakePacket()
client.WritePacket(hsPk)

Expand All @@ -441,8 +441,8 @@ func TestReadConnection(t *testing.T) {
Protocol: 751,
Description: "Some broken proxy",
}.Marshal()
statusAnswer := conn.ConnAnswer{
Action: conn.SEND_STATUS,
statusAnswer := proxy.ConnAnswer{
Action: proxy.SEND_STATUS,
StatusPk: statusPk,
}
request.Ch <- statusAnswer
Expand Down Expand Up @@ -498,7 +498,7 @@ func TestProxyConnection(t *testing.T) {
t.Run("Client writes to Server", func(t *testing.T) {
client, server := net.Pipe()
closedCh := make(chan struct{})
go conn.ProxyConnections(client, server, closedCh)
go proxy.ProxyConnections(client, server, closedCh)
readBuffer := make([]byte, 10)
couldReachCh := make(chan struct{})
go func() {
Expand All @@ -518,7 +518,7 @@ func TestProxyConnection(t *testing.T) {
t.Run("Server writes to Client", func(t *testing.T) {
client, server := net.Pipe()
closedCh := make(chan struct{})
go conn.ProxyConnections(client, server, closedCh)
go proxy.ProxyConnections(client, server, closedCh)
readBuffer := make([]byte, 10)
couldReachCh := make(chan struct{})
go func() {
Expand Down
23 changes: 11 additions & 12 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"net"
"sync"

"github.com/realDragonium/Ultraviolet/conn"
"github.com/realDragonium/Ultraviolet/mc"
)

func NewProxy(reqCh chan conn.ConnRequest) Proxy {
func NewProxy(reqCh chan ConnRequest) Proxy {
return Proxy{
reqCh: reqCh,
NotifyCh: make(chan struct{}),
Expand All @@ -22,7 +21,7 @@ func NewProxy(reqCh chan conn.ConnRequest) Proxy {
}

type Proxy struct {
reqCh chan conn.ConnRequest
reqCh chan ConnRequest
NotifyCh chan struct{}

ShouldNotifyCh chan struct{}
Expand All @@ -41,31 +40,31 @@ func (p *Proxy) backend() {
for {
request := <-p.reqCh
switch request.Type {
case conn.LOGIN:
case LOGIN:
somethingElse(request)
serverConn, err := net.Dial("tcp", "192.168.1.15:25560")
if err != nil {
log.Printf("Error while connection to server: %v", err)
request.Ch <- conn.ConnAnswer{
Action: conn.CLOSE,
request.Ch <- ConnAnswer{
Action: CLOSE,
}
return
}
request.Ch <- conn.ConnAnswer{
Action: conn.PROXY,
ServerConn: conn.NewMcConn(serverConn),
request.Ch <- ConnAnswer{
Action: PROXY,
ServerConn: NewMcConn(serverConn),
NotifyClosed: p.closedProxy,
}
p.openedProxy <- struct{}{}
case conn.STATUS:
case STATUS:
somethingElse(request)
statusPk := mc.AnotherStatusResponse{
Name: "Ultraviolet",
Protocol: 751,
Description: "Some broken proxy",
}.Marshal()
request.Ch <- conn.ConnAnswer{
Action: conn.SEND_STATUS,
request.Ch <- ConnAnswer{
Action: SEND_STATUS,
StatusPk: statusPk,
NotifyClosed: p.closedProxy,
}
Expand Down
Loading

0 comments on commit b6fc05a

Please sign in to comment.