code cleaning, remove dns

This commit is contained in:
olebeck 2023-04-10 19:55:08 +02:00
parent c20e017f0d
commit aa73c86d11
14 changed files with 59 additions and 83 deletions

View File

@ -35,7 +35,6 @@ func (c *CLI) Init() bool {
func (c *CLI) Start(ctx context.Context, cancel context.CancelFunc) error {
flag.Parse()
utils.InitDNS()
subcommands.Execute(ctx)
time.Sleep(50 * time.Millisecond)
cancel()

View File

@ -4,7 +4,6 @@ import (
"bytes"
"encoding/binary"
"fmt"
"io"
"net"
"os"
"sync"
@ -14,23 +13,22 @@ import (
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)
var dumpLock sync.Mutex
func dumpPacket(f io.WriteCloser, toServer bool, payload []byte) {
dumpLock.Lock()
defer dumpLock.Unlock()
f.Write([]byte{0xAA, 0xAA, 0xAA, 0xAA})
func (p *packetCapturer) dumpPacket(toServer bool, payload []byte) {
p.dumpLock.Lock()
defer p.dumpLock.Unlock()
p.fio.Write([]byte{0xAA, 0xAA, 0xAA, 0xAA})
packetSize := uint32(len(payload))
binary.Write(f, binary.LittleEndian, packetSize)
binary.Write(f, binary.LittleEndian, toServer)
binary.Write(f, binary.LittleEndian, time.Now().UnixMilli())
f.Write(payload)
f.Write([]byte{0xBB, 0xBB, 0xBB, 0xBB})
binary.Write(p.fio, binary.LittleEndian, packetSize)
binary.Write(p.fio, binary.LittleEndian, toServer)
binary.Write(p.fio, binary.LittleEndian, time.Now().UnixMilli())
p.fio.Write(payload)
p.fio.Write([]byte{0xBB, 0xBB, 0xBB, 0xBB})
}
type packetCapturer struct {
proxy *utils.ProxyContext
fio *os.File
proxy *utils.ProxyContext
fio *os.File
dumpLock sync.Mutex
}
func (p *packetCapturer) AddressAndName(address, hostname string) error {
@ -48,7 +46,7 @@ func (p *packetCapturer) PacketFunc(header packet.Header, payload []byte, src, d
buf := bytes.NewBuffer(nil)
header.Write(buf)
buf.Write(payload)
dumpPacket(p.fio, p.proxy.IsClient(src), buf.Bytes())
p.dumpPacket(p.proxy.IsClient(src), buf.Bytes())
}
func NewPacketCapturer() *utils.ProxyHandler {
@ -61,8 +59,8 @@ func NewPacketCapturer() *utils.ProxyHandler {
AddressAndName: p.AddressAndName,
PacketFunc: p.PacketFunc,
OnEnd: func() {
dumpLock.Lock()
defer dumpLock.Unlock()
p.dumpLock.Lock()
defer p.dumpLock.Unlock()
p.fio.Close()
},
}

View File

@ -56,13 +56,13 @@ func (s *SkinSaver) AddSkin(playerName string, playerID uuid.UUID, playerSkin *p
}
}
if !strings.HasPrefix(playerName, s.PlayerNameFilter) {
return "", nil, false
return playerName, nil, false
}
s.playerNames[playerID] = playerName
skin := &utils.Skin{Skin: playerSkin}
if s.OnlyIfHasGeometry && !skin.HaveGeometry() {
return "", nil, false
return playerName, nil, false
}
wasAdded := s.AddPlayerSkin(playerID, playerName, skin)

View File

@ -21,8 +21,7 @@ func (w *worldsHandler) processChangeDimension(pk *packet.ChangeDimension) {
if w.serverState.ispre118 && dimensionID == 0 {
dimensionID += 10
}
d, _ := world.DimensionByID(int(dimensionID))
w.worldState.dimension = d
w.worldState.dimension, _ = world.DimensionByID(int(dimensionID))
}
func (w *worldsHandler) processLevelChunk(pk *packet.LevelChunk) {

View File

@ -14,7 +14,7 @@ import (
func Test(t *testing.T) {
data, _ := os.ReadFile("chunk.bin")
ch, _, _ := chunk.NetworkDecode(33, data, 6, cube.Range{0, 255}, true, false)
i := utils.Chunk2Img(ch, true)
i := utils.Chunk2Img(ch)
f, _ := os.Create("chunk.png")
png.Encode(f, i)
f.Close()
@ -47,7 +47,7 @@ func Benchmark_render_chunk(b *testing.B) {
b.Error(err)
}
for i := 0; i < b.N; i++ {
utils.Chunk2Img(ch, true)
utils.Chunk2Img(ch)
}
pprof.StopCPUProfile()
}

View File

@ -112,10 +112,6 @@ func (w *worldsHandler) processItemPacketsServer(pk packet.Packet) packet.Packet
case *packet.ItemComponent:
w.bp.ApplyComponentEntries(pk.Items)
case *packet.MobArmourEquipment:
if pk.EntityRuntimeID == w.proxy.Server.GameData().EntityRuntimeID {
}
}
return pk
}

View File

@ -43,31 +43,37 @@ var MapItemPacket packet.InventoryContent = packet.InventoryContent{
},
}
func imin(a, b int32) int32 {
if a < b {
return a
}
return b
}
func imax(a, b int32) int32 {
if a > b {
return a
}
return b
}
func (m *MapUI) GetBounds() (min, max protocol.ChunkPos) {
// get the chunk coord bounds
i := 0
for _ch := range m.renderedChunks {
if _ch.X() < min.X() || i == 0 {
min[0] = _ch.X()
}
if _ch.Z() < min.Z() || i == 0 {
min[1] = _ch.Z()
}
if _ch.X() > max.X() || i == 0 {
max[0] = _ch.X()
}
if _ch.Z() > max.Z() || i == 0 {
max[1] = _ch.Z()
}
i++
if len(m.renderedChunks) == 0 {
return
}
min = protocol.ChunkPos{math.MaxInt32, math.MaxInt32}
for chunk := range m.renderedChunks {
min[0] = imin(min[0], chunk[0])
min[1] = imin(min[1], chunk[1])
max[0] = imax(max[0], chunk[0])
max[1] = imax(max[1], chunk[1])
}
return
}
type RenderElem struct {
pos protocol.ChunkPos
ch *chunk.Chunk
complete bool
pos protocol.ChunkPos
ch *chunk.Chunk
}
type MapUI struct {
@ -190,9 +196,7 @@ func (m *MapUI) Redraw() {
break
}
if r.ch != nil {
m.renderedChunks[r.pos] = utils.Chunk2Img(r.ch, !r.complete)
} else {
m.renderedChunks[r.pos] = black16x16
m.renderedChunks[r.pos] = utils.Chunk2Img(r.ch)
}
updatedChunks = append(updatedChunks, r.pos)
}
@ -262,7 +266,7 @@ func (m *MapUI) ToImage() *image.RGBA {
}
func (m *MapUI) SetChunk(pos protocol.ChunkPos, ch *chunk.Chunk, complete bool) {
m.renderQueue.Enqueue(&RenderElem{pos, ch, complete})
m.renderQueue.Enqueue(&RenderElem{pos, ch})
m.SchedRedraw()
}

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"image"
"image/png"
"math/rand"
"os"
@ -83,28 +82,16 @@ type worldsHandler struct {
settings WorldSettings
}
var black16x16 = image.NewRGBA(image.Rect(0, 0, 16, 16))
func init() {
for i := 3; i < len(black16x16.Pix); i += 4 {
black16x16.Pix[i] = 255
}
}
func NewWorldsHandler(ctx context.Context, ui utils.UI, settings WorldSettings) *utils.ProxyHandler {
w := &worldsHandler{
ctx: ctx,
mapUI: nil,
gui: ui,
bp: nil,
ctx: ctx,
gui: ui,
serverState: serverState{
ispre118: false,
worldCounter: 0,
ChunkRadius: 0,
playerInventory: nil,
PlayerPos: TPlayerPos{},
PlayerPos: TPlayerPos{},
},
settings: settings,

View File

@ -101,7 +101,6 @@ func (p *Page) Layout(gtx C, th *material.Theme) D {
p.Router.Wg.Add(1)
go func() {
defer p.Router.Wg.Done()
utils.InitDNS()
err := cmd.Execute(p.Router.Ctx, utils.CurrentUI)
if err != nil {

View File

@ -97,7 +97,7 @@ func chunkGetColorAt(c *chunk.Chunk, x uint8, y int16, z uint8) color.RGBA {
return blockColor
}
func Chunk2Img(c *chunk.Chunk, warn bool) *image.RGBA {
func Chunk2Img(c *chunk.Chunk) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, 16, 16))
hm := c.HeightMapWithWater()

View File

@ -1,3 +1,5 @@
//go:build false
package utils
import (

View File

@ -5,15 +5,11 @@ import (
"image/color"
"image/png"
"os"
"reflect"
"unsafe"
)
func Img2rgba(img *image.RGBA) []color.RGBA {
header := *(*reflect.SliceHeader)(unsafe.Pointer(&img.Pix))
header.Len /= 4
header.Cap /= 4
return *(*[]color.RGBA)(unsafe.Pointer(&header))
return unsafe.Slice((*color.RGBA)(unsafe.Pointer(unsafe.SliceData(img.Pix))), len(img.Pix)/4)
}
func loadPng(path string) (*image.RGBA, error) {

View File

@ -72,9 +72,6 @@ func (c *InteractiveCLI) Start(ctx context.Context, cancel context.CancelFunc) e
os.Args = append(os.Args, _cmd...)
}
flag.Parse()
InitDNS()
subcommands.Execute(ctx)
if Options.IsInteractive {

View File

@ -95,6 +95,7 @@ type ProxyContext struct {
func NewProxy() (*ProxyContext, error) {
p := &ProxyContext{
commands: make(map[string]IngameCommand),
AlwaysGetPacks: false,
WithClient: true,
IgnoreDisconnect: false,
}
@ -286,6 +287,10 @@ func (p *ProxyContext) Run(ctx context.Context, serverAddress, name string) (err
if Options.Debug || Options.ExtraDebug {
p.AddHandler(NewDebugLogger(Options.ExtraDebug))
}
p.AddHandler(&ProxyHandler{
Name: "Commands",
PacketCB: p.CommandHandlerPacketCB,
})
for _, handler := range p.handlers {
if handler.AddressAndName != nil {
@ -409,12 +414,6 @@ func (p *ProxyContext) Run(ctx context.Context, serverAddress, name string) (err
}
}
// append self to handlers for commands
p.handlers = append(p.handlers, &ProxyHandler{
Name: "Commands",
PacketCB: p.CommandHandlerPacketCB,
})
wg := sync.WaitGroup{}
// server to client
wg.Add(1)