Skip to content

Commit

Permalink
start working on more separated worker responsibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
realDragonium committed Jul 7, 2021
1 parent b6fc05a commit 7751053
Show file tree
Hide file tree
Showing 8 changed files with 706 additions and 116 deletions.
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/cloudflare/tableflip"
"github.com/realDragonium/Ultraviolet/config"
"github.com/realDragonium/Ultraviolet/conn"
"github.com/realDragonium/Ultraviolet/proxy"
)

Expand Down Expand Up @@ -54,8 +53,8 @@ func main() {
}
defer ln.Close()

reqCh := make(chan conn.ConnRequest)
go conn.Serve(ln, reqCh)
reqCh := make(chan proxy.McRequest)
go proxy.Serve(ln, reqCh)

p := proxy.NewProxy(reqCh)
p.Serve()
Expand Down
26 changes: 13 additions & 13 deletions proxy/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ import (
"github.com/realDragonium/Ultraviolet/mc"
)

type ConnAction byte
type ConnType byte
type McAction byte
type McType byte

const (
PROXY ConnAction = iota
PROXY McAction = iota
DISCONNECT
SEND_STATUS
CLOSE
ERROR
)

const (
STATUS ConnType = iota + 1
STATUS McType = iota + 1
LOGIN
)

type ConnRequest struct {
Type ConnType
type McRequest struct {
Type McType
ServerAddr string
Username string
Addr net.Addr
Ch chan ConnAnswer
Ch chan McAnswer
}

type ConnAnswer struct {
type McAnswer struct {
ServerConn McConn
DisconMessage mc.Packet
Action ConnAction
Action McAction
StatusPk mc.Packet
NotifyClosed chan struct{}
}

func Serve(listener net.Listener, reqCh chan ConnRequest) {
func Serve(listener net.Listener, reqCh chan McRequest) {
for {
conn, err := listener.Accept()
if err != nil {
Expand All @@ -57,7 +57,7 @@ func Serve(listener net.Listener, reqCh chan ConnRequest) {
}
}

func ReadConnection(c net.Conn, reqCh chan ConnRequest) {
func ReadConnection(c net.Conn, reqCh chan McRequest) {
// Rewrite connection code?
conn := NewMcConn(c)
// Add better error handling
Expand All @@ -80,8 +80,8 @@ func ReadConnection(c net.Conn, reqCh chan ConnRequest) {
isLoginReq = true
}

ansCh := make(chan ConnAnswer)
req := ConnRequest{
ansCh := make(chan McAnswer)
req := McRequest{
Ch: ansCh,
Addr: c.RemoteAddr(),
ServerAddr: string(handshake.ServerAddress),
Expand Down
40 changes: 20 additions & 20 deletions proxy/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func samePK(expected, received mc.Packet) bool {

func TestListener(t *testing.T) {
runSimpleListener := func(newConnCh <-chan net.Conn) {
reqCh := make(chan proxy.ConnRequest)
reqCh := make(chan proxy.McRequest)
mockListener := &testListener{
newConnCh: newConnCh,
}
Expand Down Expand Up @@ -139,7 +139,7 @@ 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 proxy.ConnRequest)
reqCh := make(chan proxy.McRequest)
go proxy.ReadConnection(server, reqCh)

finishedWritingCh := make(chan struct{})
Expand All @@ -160,7 +160,7 @@ 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 proxy.ConnRequest)
reqCh := make(chan proxy.McRequest)
go proxy.ReadConnection(server, reqCh)

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

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

finishedWritingCh := make(chan struct{})
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestReadConnection(t *testing.T) {
conn: proxyFrontend,
remoteAddr: &clientAddr,
}
reqCh := make(chan proxy.ConnRequest)
reqCh := make(chan proxy.McRequest)
go proxy.ReadConnection(&mockClientconn, reqCh)

go func() {
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestReadConnection(t *testing.T) {
})

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

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

request := <-reqCh

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

t.Run("can proxy login connection", func(t *testing.T) {
reqCh := make(chan proxy.ConnRequest)
reqCh := make(chan proxy.McRequest)
clientConn, proxyFrontend := net.Pipe()
proxyBackend, serverConn := net.Pipe()
go proxy.ReadConnection(proxyFrontend, reqCh)
Expand All @@ -277,7 +277,7 @@ func TestReadConnection(t *testing.T) {
client.WritePacket(loginPk)
request := <-reqCh

request.Ch <- proxy.ConnAnswer{
request.Ch <- proxy.McAnswer{
Action: proxy.PROXY,
ServerConn: proxy.NewMcConn(proxyBackend),
}
Expand All @@ -296,7 +296,7 @@ func TestReadConnection(t *testing.T) {
})

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

Expand All @@ -312,7 +312,7 @@ func TestReadConnection(t *testing.T) {
Reason: mc.Chat(disconMessage),
}.Marshal()

request.Ch <- proxy.ConnAnswer{
request.Ch <- proxy.McAnswer{
Action: proxy.DISCONNECT,
DisconMessage: disconPk,
}
Expand All @@ -328,7 +328,7 @@ 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 proxy.ConnRequest)
reqCh := make(chan proxy.McRequest)
go proxy.ReadConnection(proxyFrontend, reqCh)

client := proxy.NewMcConn(clientConn)
Expand All @@ -338,7 +338,7 @@ func TestReadConnection(t *testing.T) {
client.WritePacket(loginPk)

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

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

Expand All @@ -366,7 +366,7 @@ 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 proxy.ConnRequest)
reqCh := make(chan proxy.McRequest)
go proxy.ReadConnection(proxyFrontend, reqCh)

hsPk := statusHandshakePacket()
Expand All @@ -377,7 +377,7 @@ func TestReadConnection(t *testing.T) {
request := <-reqCh

proxyConn := proxy.NewMcConn(proxyBackend)
request.Ch <- proxy.ConnAnswer{
request.Ch <- proxy.McAnswer{
Action: proxy.PROXY,
ServerConn: proxyConn,
}
Expand All @@ -394,7 +394,7 @@ func TestReadConnection(t *testing.T) {

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

client := proxy.NewMcConn(clientConn)
Expand All @@ -407,7 +407,7 @@ func TestReadConnection(t *testing.T) {
Protocol: 751,
Description: "Some broken proxy",
}.Marshal()
statusAnswer := proxy.ConnAnswer{
statusAnswer := proxy.McAnswer{
Action: proxy.SEND_STATUS,
StatusPk: statusPk,
}
Expand All @@ -428,7 +428,7 @@ 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 proxy.ConnRequest)
reqCh := make(chan proxy.McRequest)
go proxy.ReadConnection(proxyFrontend, reqCh)

client := proxy.NewMcConn(clientConn)
Expand All @@ -441,7 +441,7 @@ func TestReadConnection(t *testing.T) {
Protocol: 751,
Description: "Some broken proxy",
}.Marshal()
statusAnswer := proxy.ConnAnswer{
statusAnswer := proxy.McAnswer{
Action: proxy.SEND_STATUS,
StatusPk: statusPk,
}
Expand Down
10 changes: 5 additions & 5 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/realDragonium/Ultraviolet/mc"
)

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

type Proxy struct {
reqCh chan ConnRequest
reqCh chan McRequest
NotifyCh chan struct{}

ShouldNotifyCh chan struct{}
Expand All @@ -45,12 +45,12 @@ func (p *Proxy) backend() {
serverConn, err := net.Dial("tcp", "192.168.1.15:25560")
if err != nil {
log.Printf("Error while connection to server: %v", err)
request.Ch <- ConnAnswer{
request.Ch <- McAnswer{
Action: CLOSE,
}
return
}
request.Ch <- ConnAnswer{
request.Ch <- McAnswer{
Action: PROXY,
ServerConn: NewMcConn(serverConn),
NotifyClosed: p.closedProxy,
Expand All @@ -63,7 +63,7 @@ func (p *Proxy) backend() {
Protocol: 751,
Description: "Some broken proxy",
}.Marshal()
request.Ch <- ConnAnswer{
request.Ch <- McAnswer{
Action: SEND_STATUS,
StatusPk: statusPk,
NotifyClosed: p.closedProxy,
Expand Down
Loading

0 comments on commit 7751053

Please sign in to comment.