github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/proto/v2/client/main.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "bytes" 6 "encoding/binary" 7 "fmt" 8 "github.com/gogo/protobuf/proto" 9 "github.com/qiuhoude/go-web/proto/v2/models" 10 "io" 11 "io/ioutil" 12 "log" 13 "net" 14 "net/http" 15 "os" 16 "time" 17 ) 18 19 const ( 20 maxSize = 1048576 21 lengthSize = 4 22 ) 23 24 var Version = "No Version Provided ..." 25 26 func main() { 27 info := getLoginInfo("ax1", "000") 28 serverList(info.GetKeyId()) 29 //tcp(1, *info.KeyId, *info.Token) 30 } 31 32 func serverList(accountKey int64) { 33 basePb := &models.Base{ 34 Cmd: proto.Int32(models.E_ServerListRq_Ext.Field), 35 } 36 dataPb := &models.ServerListRq{ 37 AccountKey: proto.Int64(accountKey), 38 } 39 _ = proto.SetExtension(basePb, models.E_DoLoginRq_Ext, dataPb) 40 pbData, _ := proto.Marshal(basePb) 41 url := "http://192.168.1.151:9200/honor_account/account/account.do" 42 resp, err := http.Post(url, "application/octet-stream", bytes.NewReader(encode(pbData))) 43 if err != nil { 44 log.Fatal(err) 45 } 46 defer resp.Body.Close() 47 if resp.StatusCode == http.StatusOK { 48 body, err := ioutil.ReadAll(resp.Body) 49 if err != nil { 50 log.Fatal(err) 51 } 52 resBasePb := &models.Base{} 53 _ = proto.Unmarshal(decode(body), resBasePb) 54 fmt.Println(resBasePb) 55 extension, _ := proto.GetExtension(resBasePb, models.E_ServerListRs_Ext) 56 res := extension.(*models.ServerListRs) 57 fmt.Printf("%v\n", res) 58 } 59 60 } 61 62 func encode(data []byte) []byte { 63 buf := new(bytes.Buffer) 64 length := uint16(len(data)) 65 _ = binary.Write(buf, binary.BigEndian, length) 66 _ = binary.Write(buf, binary.BigEndian, data) 67 return buf.Bytes() 68 } 69 70 func decode(data []byte) []byte { 71 buf := bytes.NewBuffer(data) 72 var length uint16 73 _ = binary.Read(buf, binary.BigEndian, &length) 74 return buf.Bytes() 75 } 76 77 func getLoginInfo(account, pwd string) *models.DoLoginRs { 78 79 basePb := &models.Base{ 80 Cmd: proto.Int32(models.E_DoLoginRq_Ext.Field), 81 } 82 dataPb := &models.DoLoginRq{ 83 Sid: proto.String(fmt.Sprintf("%s_%s", account, pwd)), 84 BaseVersion: proto.String("1.4.0"), 85 Version: proto.String("1.0.0"), 86 DeviceNo: proto.String("00000000-2625-0b64-7b72-55e30033c587"), 87 Plat: proto.String("self"), 88 } 89 err := proto.SetExtension(basePb, models.E_DoLoginRq_Ext, dataPb) 90 91 pbData, _ := proto.Marshal(basePb) 92 url := "http://192.168.1.151:9200/honor_account/account/account.do" 93 resp, err := http.Post(url, "application/octet-stream", bytes.NewReader(encode(pbData))) 94 if err != nil { 95 log.Fatal(err) 96 } 97 defer resp.Body.Close() 98 if resp.StatusCode == http.StatusOK { 99 body, err := ioutil.ReadAll(resp.Body) 100 if err != nil { 101 log.Fatal(err) 102 } 103 resBasePb := &models.Base{} 104 _ = proto.Unmarshal(decode(body), resBasePb) 105 fmt.Println(resBasePb) 106 extension, _ := proto.GetExtension(resBasePb, models.E_DoLoginRs_Ext) 107 res := extension.(*models.DoLoginRs) 108 return res 109 } 110 log.Fatal("未获取到Token") 111 return nil 112 } 113 114 func tcp(serverId int32, keyId int64, token string) { 115 fmt.Println("Client Version is:", Version) 116 //go run -ldflags "-X main.Version=1.6.6" client.go 编译版本 117 strIP := "192.168.1.151:9201" 118 var conn net.Conn 119 var err error 120 addr, err := net.ResolveTCPAddr("tcp", strIP) 121 122 //连接服务器 123 for conn, err = net.DialTCP("tcp", nil, addr); err != nil; conn, err = net.Dial("tcp", strIP) { 124 fmt.Println("connect", strIP, "fail") 125 time.Sleep(time.Second) 126 fmt.Println("reconnect...") 127 } 128 fmt.Println("connect", strIP, "success") 129 defer conn.Close() 130 131 //writer := bufio.NewWriter(conn) 132 // 开个协程进行读数据 133 fc := warpConn(conn) 134 go recvMsg2(fc) 135 136 _ = fc.WriteFrame(begin(serverId, keyId, token)) 137 time.AfterFunc(3*time.Second, func() { 138 _ = fc.WriteFrame(login()) 139 }) 140 141 sender := bufio.NewScanner(os.Stdin) 142 for sender.Scan() { 143 _ = fc.WriteFrame(chatMsg(sender.Text())) 144 if sender.Text() == "stop" { 145 return 146 } 147 } 148 } 149 150 func chatMsg(msg string) []byte { 151 basePb := &models.Base{ 152 Cmd: proto.Int32(models.E_SendChatRq_Ext.Field), 153 } 154 dataPb := &models.SendChatRq{ 155 Channel: proto.Int32(1), 156 Content: []string{msg}, 157 } 158 _ = proto.SetExtension(basePb, models.E_SendChatRq_Ext, dataPb) 159 bdata, _ := proto.Marshal(basePb) 160 return bdata 161 } 162 163 func login() []byte { 164 basePb := &models.Base{ 165 Cmd: proto.Int32(models.E_RoleLoginRq_Ext.Field), 166 } 167 dataPb := &models.RoleLoginRq{} 168 _ = proto.SetExtension(basePb, models.E_RoleLoginRq_Ext, dataPb) 169 bdata, _ := proto.Marshal(basePb) 170 return bdata 171 } 172 173 func begin(serverId int32, keyId int64, token string) []byte { 174 basePb := &models.Base{ 175 Cmd: proto.Int32(models.E_BeginGameRq_Ext.Field), 176 } 177 beginRqPb := &models.BeginGameRq{ 178 ServerId: proto.Int32(serverId), 179 KeyId: proto.Int64(keyId), 180 Token: proto.String(token), 181 DeviceNo: proto.String(`00000000-2625-0b64-7b72-55e30033c587`), 182 CurVersion: proto.String(`1.0.0`), 183 } 184 _ = proto.SetExtension(basePb, models.E_BeginGameRq_Ext, beginRqPb) 185 beginData, _ := proto.Marshal(basePb) 186 return beginData 187 } 188 189 type wirter struct { 190 w *bufio.Writer 191 err error 192 } 193 194 func (w *wirter) write(v interface{}) { 195 if w.err == nil { 196 w.err = binary.Write(w.w, binary.BigEndian, v) 197 } 198 } 199 200 func sendMsg(writer *bufio.Writer, data []byte) error { 201 w := wirter{w: writer} 202 w.write(int32(len(data))) 203 w.write(data) 204 w.err = writer.Flush() 205 return w.err 206 } 207 208 func recvMsg(conn net.Conn) { 209 reader := bufio.NewReader(conn) 210 for { 211 conn.SetReadDeadline(time.Now().Add(3 * time.Minute)) 212 lengthByte, err := reader.Peek(lengthSize) 213 if err != nil { 214 if err == io.EOF { 215 fmt.Printf("远程链接:%s已经关闭!\n", conn.RemoteAddr().String()) 216 } 217 break 218 } 219 lengthBuff := bytes.NewBuffer(lengthByte) 220 var length int32 221 err = binary.Read(lengthBuff, binary.BigEndian, &length) 222 if err != nil { 223 break 224 } 225 if length > maxSize || length < 0 { //说明是错误数据调过1字节 226 reader.Discard(1) 227 continue 228 } 229 reader.Discard(lengthSize) 230 packBytes := make([]byte, length) 231 basePb := &models.Base{} 232 reader.Read(packBytes) 233 proto.Unmarshal(packBytes, basePb) 234 fmt.Println(basePb) 235 conn.SetReadDeadline(time.Time{}) 236 } 237 fmt.Println("读协程死掉了") 238 }