Skip to content

Commit

Permalink
chore: add proxy protocol header to test tools
Browse files Browse the repository at this point in the history
  • Loading branch information
haveachin committed Nov 6, 2022
1 parent 87efc08 commit e3833e1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
38 changes: 36 additions & 2 deletions test/attacks/dos/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"crypto/rand"
"log"
"net"

"github.com/haveachin/infrared/internal/pkg/java/protocol"
"github.com/haveachin/infrared/internal/pkg/java/protocol/handshaking"
"github.com/haveachin/infrared/internal/pkg/java/protocol/login"
"github.com/pires/go-proxyproto"
)

var handshakePayload []byte
Expand All @@ -15,7 +17,7 @@ var loginStartPayload []byte
func init() {
handshake := handshaking.ServerBoundHandshake{
ProtocolVersion: 758,
ServerAddress: "localhost",
ServerAddress: "laptop.fritz.box",
ServerPort: 25565,
NextState: 2,
}
Expand Down Expand Up @@ -45,12 +47,44 @@ func main() {
log.Printf("%d requests sent\n", i)
}

c, err := net.Dial("tcp", "localhost:25565")
c, err := net.Dial("tcp", "192.168.178.20:25565")
if err != nil {
log.Fatal(err)
}

//writeProxyProtocolHeader(randomAddr(), c)
c.Write(handshakePayload)
c.Write(loginStartPayload)
}
}

func randomAddr() net.Addr {
addrBytes := make([]byte, 6)
rand.Read(addrBytes)

return &net.TCPAddr{
IP: net.IPv4(addrBytes[0], addrBytes[1], addrBytes[2], addrBytes[3]),
Port: int(addrBytes[4])*256 + int(addrBytes[5]),
}
}

func writeProxyProtocolHeader(addr net.Addr, rc net.Conn) error {
tp := proxyproto.TCPv4
addrTCP := addr.(*net.TCPAddr)
if addrTCP.IP.To4() == nil {
tp = proxyproto.TCPv6
}

header := &proxyproto.Header{
Version: 2,
Command: proxyproto.PROXY,
TransportProtocol: tp,
SourceAddr: addr,
DestinationAddr: rc.RemoteAddr(),
}

if _, err := header.WriteTo(rc); err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion test/attacks/maxpacketsize/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {

func main() {
for i := 0; i < 30; i++ {
c, err := net.Dial("tcp", "localhost:25565")
c, err := net.Dial("tcp", "192.168.178.20:25565")
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit e3833e1

Please sign in to comment.