Ejemplo n.º 1
0
func readRaw(reader io.Reader, raw *packet.RawPacket, buf []byte) (err error) {
	var sz uint32

	err = binary.Read(reader, binary.LittleEndian, &raw.Id)
	if err != nil {
		return
	}

	err = binary.Read(reader, binary.LittleEndian, &sz)
	if err != nil {
		return
	}

	err = binary.Read(reader, binary.LittleEndian, &raw.Seq)
	if err != nil {
		return
	}

	if sz > uint32(len(buf)) {
		return errors.New("packet size too large")
	}

	n, err := io.ReadFull(reader, buf[:sz])
	if err != nil {
		return
	}
	if n != int(sz) {
		err = errors.New("read get less data than needed")
		return
	}

	raw.Data = buf[:sz]
	return nil
}
Ejemplo n.º 2
0
func hajackLoginServer(client net.Conn, notice chan<- struct{}) {
	server, err := net.Dial("tcp", trueLoginServer)
	if err != nil {
		panic(err)
	}

	go func() {
		var raw packet.RawPacket
		var buf [400]byte
		for {
			err := readRaw(client, &raw, buf[:])
			if err != nil {
				if err == io.EOF {
					fmt.Println("CL客户端关了,没法读取了")
					return
				} else {
					panic(err)
				}
			}

			fmt.Println("[C->L]", len(raw.Data), raw)

			err = writeRaw(server, &raw)
			if err != nil {
				if err == io.EOF {
					fmt.Println("LC服务端关了,没法把客户端数据写过去")
					return
				} else {
					panic(err)
				}
			}

		}
	}()

	var raw packet.RawPacket
	var buf [400]byte
	for {
		err := readRaw(server, &raw, buf[:])
		if err != nil {
			if err == io.EOF {
				fmt.Println("LC服务器那边关了,没法读取了")
				return
			} else {
				panic(err)
			}
		}

		fmt.Println("[L->C]", len(raw.Data), raw)

		// if raw.Id == packet.PACKET_LC_PC_LIST {
		//     fmt.Println("run here...")
		//     pkt := &packet.LCPCListPacket{}

		//     raw.Data, _ = pkt.MarshalBinary(0)
		//     fmt.Println("[实际发送L->C]", len(raw.Data), raw)
		// }

		// 劫持修改最后的服务器包
		if raw.Id == packet.PACKET_LC_RECONNECT {
			pkt, err := raw.Unmarshal(0)
			if err != nil {
				panic(err)
			}
			reconn := pkt.(*packet.LCReconnectPacket)
			reconn.Ip = "192.168.10.102"
			reconn.Port = 9998

			raw.Data, err = reconn.MarshalBinary(0)
			err = writeRaw(client, &raw)
			close(notice)
			client.Close()
			return
		}

		err = writeRaw(client, &raw)
		if err != nil {
			if err == io.EOF {
				fmt.Println("CL客户端关了,写不到客户端了")
			} else {
				panic(err)
			}
		}
	}
}