// 密钥交换 func P_get_seed_req(sess *Session, reader *packet.Packet) []byte { tbl, _ := PKT_seed_info(reader) // KEY1 X1, E1 := dh.DHExchange() KEY1 := dh.DHKey(X1, big.NewInt(int64(tbl.F_client_send_seed))) // KEY2 X2, E2 := dh.DHExchange() KEY2 := dh.DHKey(X2, big.NewInt(int64(tbl.F_client_receive_seed))) ret := seed_info{int32(E1.Int64()), int32(E2.Int64())} // 服务器加密种子是客户端解密种子 encoder, err := rc4.NewCipher([]byte(fmt.Sprintf("%v%v", SALT, KEY2))) if err != nil { log.Critical(err) return nil } decoder, err := rc4.NewCipher([]byte(fmt.Sprintf("%v%v", SALT, KEY1))) if err != nil { log.Critical(err) return nil } sess.Encoder = encoder sess.Decoder = decoder sess.Flag |= SESS_KEYEXCG return packet.Pack(Code["get_seed_ack"], ret, nil) }
func TestAgent(t *testing.T) { host := DEFAULT_AGENT_HOST if env := os.Getenv("AGENT_HOST"); env != "" { host = env } addr, err := net.ResolveTCPAddr("tcp", host) if err != nil { log.Println(err) os.Exit(-1) } conn, err := net.DialTCP("tcp", nil, addr) if err != nil { log.Println(err) os.Exit(-1) } defer conn.Close() //get_seed_req S1, M1 := dh.DHExchange() S2, M2 := dh.DHExchange() p2 := seed_info{ int32(M1.Int64()), int32(M2.Int64()), } rst := send_proto(conn, Code["get_seed_req"], p2) r1, _ := PKT_seed_info(rst) K1 := dh.DHKey(S1, big.NewInt(int64(r1.F_client_send_seed))) K2 := dh.DHKey(S2, big.NewInt(int64(r1.F_client_receive_seed))) encoder, err = rc4.NewCipher([]byte(fmt.Sprintf("%v%v", SALT, K1))) if err != nil { log.Println(err) return } decoder, err = rc4.NewCipher([]byte(fmt.Sprintf("%v%v", SALT, K2))) if err != nil { log.Println(err) return } KEY_EXCHANGE = true // heart_beat_req p1 := auto_id{F_id: rand.Int31()} log.Printf("send: %#v", p1) rst = send_proto(conn, Code["heart_beat_req"], p1) r2, _ := PKT_auto_id(rst) log.Printf("response: %#v", r2) }
// 密钥交换 func P_get_pike_seed_req(sess *Session, reader *packet.Packet) []byte { tbl, _ := PKT_pike_seed_info(reader) // KEY1 X1, E1 := dh.DHExchange() KEY1 := dh.DHKey(X1, big.NewInt(int64(tbl.F_client_send_seed))) // KEY2 X2, E2 := dh.DHExchange() KEY2 := dh.DHKey(X2, big.NewInt(int64(tbl.F_client_receive_seed))) ret := pike_seed_info{int32(E1.Int64()), int32(E2.Int64())} // 服务器加密种子是客户端解密种子 sess.Encoder = pike.NewCtx(uint32(KEY2.Int64())) sess.Decoder = pike.NewCtx(uint32(KEY1.Int64())) sess.Flag |= SESS_KEYEXCG return packet.Pack(Code["get_pike_seed_ack"], ret, nil) }
func main() { host := DEFAULT_AGENT_HOST if env := os.Getenv("AGENT_HOST"); env != "" { host = env } addr, err := net.ResolveTCPAddr("tcp", host) if err != nil { log.Println(err) os.Exit(-1) } conn, err := net.DialTCP("tcp", nil, addr) if err != nil { log.Println(err) os.Exit(-1) } defer conn.Close() //get_seed_req S1, M1 := dh.DHExchange() S2, M2 := dh.DHExchange() p2 := seed_info{ int32(M1.Int64()), int32(M2.Int64()), } rst := send_proto(conn, Code["get_seed_req"], p2) r1, _ := PKT_seed_info(rst) log.Printf("result: %#v", r1) K1 := dh.DHKey(S1, big.NewInt(int64(r1.F_client_send_seed))) K2 := dh.DHKey(S2, big.NewInt(int64(r1.F_client_receive_seed))) encoder, err = rc4.NewCipher([]byte(fmt.Sprintf("%v%v", SALT, K1))) if err != nil { log.Println(err) return } decoder, err = rc4.NewCipher([]byte(fmt.Sprintf("%v%v", SALT, K2))) if err != nil { log.Println(err) return } KEY_EXCHANGE = true //user_login_req p3 := user_login_info{ F_login_way: 0, F_open_udid: "udid", F_client_certificate: "qwertyuiopasdfgh", F_client_version: 1, F_user_lang: "en", F_app_id: "com.yrhd.lovegame", F_os_version: "android4.4", F_device_name: "simulate", F_device_id: "device_id", F_device_id_type: 1, F_login_ip: "127.0.0.1", } send_proto(conn, Code["user_login_req"], p3) //heart_beat_req p4 := auto_id{ F_id: rand.Int31(), } send_proto(conn, Code["heart_beat_req"], p4) //proto_ping_req p5 := auto_id{ F_id: rand.Int31(), } send_proto(conn, Code["proto_ping_req"], p5) }