/** * Set online status, this is the last step of login * * @param err * @param lc */ static LwqqAsyncEvent* set_online_status(LwqqClient *lc,const char *status) { char msg[1024] ={0}; char *buf; LwqqHttpRequest *req = NULL; if (!status) { return NULL; } lc->clientid = generate_clientid(); if (!lc->clientid) { lwqq_log(LOG_ERROR, "Generate clientid error\n"); return NULL; } snprintf(msg, sizeof(msg), "{\"status\":\"%s\",\"ptwebqq\":\"%s\"," "\"passwd_sig\":""\"\",\"clientid\":\"%s\"" ", \"psessionid\":null}" ,status, lc->cookies->ptwebqq ,lc->clientid); buf = url_encode(msg); snprintf(msg, sizeof(msg), "r=%s", buf); s_free(buf); /* Create a POST request */ req = lwqq_http_create_default_request(lc,WEBQQ_D_HOST"/channel/login2", NULL); lwqq_puts("[set online status]\n"); /* Set header needed by server */ req->set_header(req, "Cookie2", "$Version=1"); req->set_header(req, "Referer", WEBQQ_D_REF_URL); req->set_header(req, "Content-type", "application/x-www-form-urlencoded"); /* Set http cookie */ req->set_header(req, "Cookie", lwqq_get_cookies(lc)); return req->do_request_async(req, 1, msg,_C_(p_i,set_online_status_back,req)); }
/** * Set online status, this is the last step of login * * @param err * @param lc */ static LwqqAsyncEvent* set_online_status(LwqqClient *lc,const char *status) { char msg[1024] ={0}; char* post = msg; LwqqHttpRequest *req = NULL; if (!status) { return NULL; } lc->clientid = generate_clientid(); if (!lc->clientid) { lwqq_log(LOG_ERROR, "Generate clientid error\n"); return NULL; } char* ptwebqq = lwqq_http_get_cookie(lwqq_get_http_handle(lc), "ptwebqq"); snprintf(msg, sizeof(msg), "r={\"status\":\"%s\",\"ptwebqq\":\"%s\"," "\"passwd_sig\":""\"\",\"clientid\":\"%s\"" ", \"psessionid\":null}" ,status, ptwebqq ,lc->clientid); urlencode(msg, 2); s_free(ptwebqq); /* Create a POST request */ char url[512] ={0}; snprintf(url,sizeof(url),"%s/channel/login2",WEBQQ_D_HOST); req = lwqq_http_create_default_request(lc,url, NULL); /* Set header needed by server */ //req->set_header(req, "Cookie2", "$Version=1"); req->set_header(req, "Referer", WEBQQ_D_REF_URL); req->set_header(req, "Content-type", "application/x-www-form-urlencoded"); return req->do_request_async(req, lwqq__has_post(),_C_(p_i,set_online_status_back,req)); }
/** * Set online status, this is the last step of login * * @param err * @param lc */ static void set_online_status(LwqqClient *lc, char *status, LwqqErrorCode *err) { char msg[1024] ={0}; char *buf; LwqqHttpRequest *req = NULL; char *response = NULL; char *cookies; int ret; json_t *json = NULL; char *value; if (!status || !err) { goto done ; } lc->clientid = generate_clientid(); if (!lc->clientid) { lwqq_log(LOG_ERROR, "Generate clientid error\n"); *err = LWQQ_EC_ERROR; goto done ; } snprintf(msg, sizeof(msg), "{\"status\":\"%s\",\"ptwebqq\":\"%s\"," "\"passwd_sig\":""\"\",\"clientid\":\"%s\"" ", \"psessionid\":null}" ,status, lc->cookies->ptwebqq ,lc->clientid); buf = url_encode(msg); snprintf(msg, sizeof(msg), "r=%s", buf); s_free(buf); /* Create a POST request */ req = lwqq_http_create_default_request(LWQQ_URL_SET_STATUS, err); if (!req) { goto done; } /* Set header needed by server */ req->set_header(req, "Cookie2", "$Version=1"); req->set_header(req, "Referer", "http://d.web2.qq.com/proxy.html?v=20101025002"); req->set_header(req, "Content-type", "application/x-www-form-urlencoded"); /* Set http cookie */ cookies = lwqq_get_cookies(lc); if (cookies) { req->set_header(req, "Cookie", cookies); s_free(cookies); } ret = req->do_request(req, 1, msg); if (ret) { *err = LWQQ_EC_NETWORK_ERROR; goto done; } if (req->http_code != 200) { *err = LWQQ_EC_HTTP_ERROR; goto done; } /** * Here, we got a json object like this: * {"retcode":0,"result":{"uin":1421032531,"cip":2013211875,"index":1060,"port":43415,"status":"online","vfwebqq":"e7ce7913336ad0d28de9cdb9b46a57e4a6127161e35b87d09486001870226ec1fca4c2ba31c025c7","psessionid":"8368046764001e636f6e6e7365727665725f77656271714031302e3133332e34312e32303200006b2900001544016e0400533cb3546d0000000a4046674d4652585136496d00000028e7ce7913336ad0d28de9cdb9b46a57e4a6127161e35b87d09486001870226ec1fca4c2ba31c025c7","user_state":0,"f":0}} * */ response = req->response; ret = json_parse_document(&json, response); if (ret != JSON_OK) { *err = LWQQ_EC_ERROR; goto done; } if (!(value = json_parse_simple_value(json, "retcode"))) { *err = LWQQ_EC_ERROR; goto done; } /** * Do we need parse "seskey? from kernelhcy's code, we need it, * but from the response we got like above, we dont need * */ if ((value = json_parse_simple_value(json, "seskey"))) { lc->seskey = s_strdup(value); } if ((value = json_parse_simple_value(json, "cip"))) { lc->cip = s_strdup(value); } if ((value = json_parse_simple_value(json, "index"))) { lc->index = s_strdup(value); } if ((value = json_parse_simple_value(json, "port"))) { lc->port = s_strdup(value); } if ((value = json_parse_simple_value(json, "status"))) { /* This really need? */ lc->status = s_strdup(value); } if ((value = json_parse_simple_value(json, "vfwebqq"))) { lc->vfwebqq = s_strdup(value); } if ((value = json_parse_simple_value(json, "psessionid"))) { lc->psessionid = s_strdup(value); } *err = LWQQ_EC_OK; done: if (json) json_free_value(&json); lwqq_http_request_free(req); }
/* * Get the psessionid. * * This function is the last step of loginning */ static int get_psessionid(QQInfo *info) { int ret = NO_ERR; gint res = 0; if(info -> ptwebqq == NULL || info -> ptwebqq -> len <= 0){ g_warning("Need ptwebqq!!(%s, %d)", __FILE__, __LINE__); return PARAMETER_ERR; } Request *req = request_new(); Response *rps = NULL; request_set_method(req, "POST"); request_set_version(req, "HTTP/1.1"); request_set_uri(req, PSIDPATH); request_set_default_headers(req); request_add_header(req, "Host", PSIDHOST); request_add_header(req, "Cookie2", "$Version=1"); request_add_header(req, "Referer" , "http://d.web2.qq.com/proxy.html?v=20101025002"); GString *clientid = generate_clientid(); info -> clientid = clientid; g_debug("clientid: %s", clientid -> str); gchar* msg = g_malloc(500); g_snprintf(msg, 500, "{\"status\":\"%s\",\"ptwebqq\":\"%s\"," "\"passwd_sig\":""\"\",\"clientid\":\"%s\"" ", \"psessionid\":null}" , info -> me -> status -> str, info -> ptwebqq -> str , clientid -> str); gchar *escape = g_uri_escape_string(msg, NULL, FALSE); g_snprintf(msg, 500, "r=%s", escape); g_free(escape); request_append_msg(req, msg, strlen(msg)); gchar cl[10]; g_sprintf(cl, "%u", (unsigned int)strlen(msg)); request_add_header(req, "Content-Length", cl); request_add_header(req, "Content-Type" , "application/x-www-form-urlencoded"); g_free(msg); gchar *cookie = g_malloc(2000); gint idx = 0; if(info -> ptvfsession != NULL){ idx += g_snprintf(cookie + idx, 2000 - idx, "ptvfsession=%s; " , info -> ptvfsession -> str); } idx += g_snprintf(cookie + idx, 2000 - idx, "%s" , info -> cookie -> str); request_add_header(req, "Cookie", cookie); g_free(cookie); Connection *con = connect_to_host(PSIDHOST, 80); if(con == NULL){ g_warning("Can NOT connect to server!(%s, %d)" , __FILE__, __LINE__); request_del(req); return NETWORK_ERR; } send_request(con, req); res = rcv_response(con, &rps); if (-1 == res || !rps) { g_warning("Null point access (%s, %d)\n", __FILE__, __LINE__); ret = -1; goto error; } const gchar *retstatus = rps -> status -> str; if(g_strstr_len(retstatus, -1, "200") == NULL){ g_warning("Server status %s (%s, %d)", retstatus , __FILE__, __LINE__); ret = NETWORK_ERR; goto error; } json_t *json = NULL; switch(json_parse_document(&json, rps -> msg -> str)) { case JSON_OK: break; default: g_warning("json_parser_document: syntax error. (%s, %d)" , __FILE__, __LINE__); ret = NETWORK_ERR; goto error; } json_t *val; val = json_find_first_label_all(json, "retcode"); if(val -> child -> text[0] != '0'){ g_warning("Server return code %s(%s, %d)", val -> child -> text , __FILE__, __LINE__); ret = NETWORK_ERR; goto error; } val = json_find_first_label_all(json, "seskey"); if(val != NULL){ g_debug("seskey: %s (%s, %d)", val -> child -> text , __FILE__, __LINE__); info -> seskey = g_string_new(val -> child -> text); } val = json_find_first_label_all(json, "cip"); if(val != NULL){ info -> cip = g_string_new(val -> child -> text); } val = json_find_first_label_all(json, "index"); if(val != NULL){ info -> index = g_string_new(val -> child -> text); } val = json_find_first_label_all(json, "port"); if(val != NULL){ info -> port = g_string_new(val -> child -> text); } val = json_find_first_label_all(json, "status"); { g_debug("status: %s (%s, %d)", val -> child -> text , __FILE__, __LINE__); } val = json_find_first_label_all(json, "vfwebqq"); if(val != NULL){ g_debug("vfwebqq: %s (%s, %d)", val -> child -> text , __FILE__, __LINE__); info -> vfwebqq = g_string_new(val -> child -> text); } val = json_find_first_label_all(json, "psessionid"); if(val != NULL){ g_debug("psessionid: %s (%s, %d)", val -> child -> text , __FILE__, __LINE__); info -> psessionid = g_string_new(val -> child -> text); }else{ g_debug("Can not find pesssionid!(%s, %d): %s" , __FILE__, __LINE__, rps -> msg -> str); } error: json_free_value(&json); close_con(con); connection_free(con); request_del(req); response_del(rps); return ret; }