func main() { conn, err := net.Dial("tcp", "192.168.1.107:3434") if err != nil { fmt.Println("dial failed", err) } else { fmt.Println("dial successfully", conn) } var recognize s2c.Packet recognize.SetType(100) recognize.SetData([]byte("IamBussnessLayer")) packetR := s2c.NewPacketReader(conn) packetR.AESKEY = []byte("AESKEYFORBUSINES") packetW := s2c.NewPacketWriter(conn) packetW.AESKEY = []byte("AESKEYFORBUSINES") fmt.Println(recognize) if err := packetW.WritePacket(&recognize, nil); err != nil { return } if err := packetW.Flush(); err != nil { fmt.Println("packetWrter flush error", err) return } }
func connectToServer() net.Conn { var RsaPublicKey []byte conn, err := net.Dial("tcp", "192.168.1.104:1212") if err != nil { fmt.Println("dial failed", err) } else { fmt.Println("dial successfully", conn) } packetR := s2c.NewPacketReader(conn) packetR.AESKEY = []byte("AESKEY7890123456") packetW := s2c.NewPacketWriter(conn) packetW.AESKEY = []byte("AESKEY7890123456") //--------------------------------------------------------------------------------------------- var publicPacket s2c.Packet //first step,dial and receive a public key for { //fmt.Println("------------------------------------", i) if publicKeyPacket, err := packetR.ReadPacket(); err == nil { if publicKeyPacket.GetType() == 0 { publicPacket = *publicKeyPacket break } } else { fmt.Println("read type 0 packet err", err) return nil } } //get public key from the publicpacket publicCMD, err := s2c.GetCommand(&publicPacket) if err != nil { fmt.Println("publicPacket getcommand err", err) } RsaPublicKey = []byte(publicCMD.Message.GetData()) //------------------------- //fmt.Println("packet data", publicPacket.GetData()) //I have receive rsa public key, send aeskey *ReturnPacket.Random = s2c.GenRandom() *ReturnPacket.Result = 1 *ReturnPacket.ResultDesciption = "AESKEY7890123456" returnPacket := s2c.MakePacket(ReturnPacket, 100) if err := packetW.WritePacket(returnPacket, RsaPublicKey); err != nil { return nil } if err := packetW.Flush(); err != nil { fmt.Println("packetWrter flush error", err) } return conn //传101-107号包 //等待各自的回馈包 }