package
com.example.deviceregister;
import
android.net.Uri;
import
android.util.Log;
import
com.bytedance.frameworks.encryptor.EncryptorUtil;
import
org.json.JSONObject;
import
java.io.BufferedReader;
import
java.io.ByteArrayOutputStream;
import
java.io.DataOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.net.HttpURLConnection;
import
java.net.URL;
import
java.util.UUID;
import
java.util.zip.GZIPOutputStream;
public
class
DeviceRegister {
static
String TAG =
"DeviceReg"
;
private
static
String bodyTemplate =
"{\"header\":{\"os\":\"Android\",\"os_version\":\"9\",\"os_api\":28,\"device_model\":\"Pixel\",\"device_brand\":\"google\",\"device_manufacturer\":\"Google\",\"cpu_abi\":\"arm64-v8a\",\"channel\":\"article_download_page\",\"not_request_sender\":1,\"aid\":13,\"release_build\":\"11111111\",\"custom\":{\"cold_start\":true},\"density_dpi\":420,\"display_density\":\"mdpi\",\"resolution\":\"1794x1080\",\"display_density_v2\":\"xxhdpi\",\"resolution_v2\":\"1920x1080\",\"language\":\"zh\",\"timezone\":8,\"region\":\"CN\",\"tz_name\":\"Asia\\/Shanghai\",\"tz_offset\":28800,\"access\":\"wifi\",\"package\":\"com.ss.android.article.news\",\"app_version\":\"9.5.4\",\"app_version_minor\":\"\",\"version_code\":954,\"update_version_code\":95407,\"manifest_version_code\":9540,\"app_name\":\"news_article\",\"tweaked_channel\":\"article_download_page\",\"display_name\":\"今日头条\",\"rom\":\"5670241\",\"rom_version\":\"PQ3A.190801.002\",\"sig_hash\":\"aea615ab910015038f73c47e45d21466\",\"clientudid\":\"11111111\",\"openudid\":\"11111111\",\"cdid\":\"11111111\",\"appkey\":\"4fd805175270154a3c000005\",\"mc\":\"11111111\",\"sim_serial_number\":[],\"oaid_may_support\":false,\"device_platform\":\"android\",\"git_hash\":\"8a70419\",\"sdk_version_code\":4000184,\"sdk_target_version\":30,\"req_id\":\"11111111\",\"sdk_version\":\"4.0.1-rc.34-toutiao\",\"guest_mode\":0,\"sdk_flavor\":\"cnInner\",\"pre_installed_channel\":\"\",\"apk_first_install_time\":11111111,\"is_system_app\":0},\"magic_tag\":\"ss_app_log\",\"_gen_time\":11111111}"
;
public
static
byte
[] gzipAndEnc(String str) {
if
(str ==
null
|| str.length() ==
0
) {
return
null
;
}
ByteArrayOutputStream out =
new
ByteArrayOutputStream();
GZIPOutputStream gzip;
try
{
gzip =
new
GZIPOutputStream(out);
gzip.write(str.getBytes(
"UTF-8"
));
gzip.close();
}
catch
(IOException e) {
Log.e(TAG,
"gzip error:"
+ e.getMessage());
}
byte
[] gzipout = out.toByteArray();
Log.e(TAG,
"gzip out-len:"
+ Integer.toHexString(gzipout.length));
byte
[] ret = EncryptorUtil.ttEncrypt(gzipout, gzipout.length);
Log.e(TAG,
"encrypt out-len:"
+ Integer.toHexString(ret.length));
return
ret;
}
public
static
byte
[] genBody(String req_id) {
try
{
JSONObject json =
new
JSONObject(bodyTemplate);
JSONObject header = json.getJSONObject(
"header"
);
header.put(
"release_build"
,
"11111"
);
header.put(
"clientudid"
, UUID.randomUUID().toString());
header.put(
"openudid"
,
"111111"
);
header.put(
"cdid"
, UUID.randomUUID().toString());
header.put(
"mc"
,
"02:00:00:00:00:00"
);
header.put(
"req_id"
, req_id);
header.put(
"apk_first_install_time"
, System.currentTimeMillis());
json.put(
"header"
, header);
json.put(
"_gen_time"
, System.currentTimeMillis());
Log.d(TAG, json.toString());
return
gzipAndEnc(json.toString());
}
catch
(Exception e) {
throw
new
RuntimeException(e);
}
}
public
static
String request() {
String req_id = UUID.randomUUID().toString();
String reqUri =
"https://log.snssdk.com/service/2/device_register/"
;
Uri uri = Uri.parse(reqUri);
Uri.Builder uriBuilder = uri.buildUpon();
uriBuilder.appendQueryParameter(
"req_id"
, req_id);
uriBuilder.appendQueryParameter(
"tt_data"
,
"a"
);
uriBuilder.appendQueryParameter(
"cronet_version"
,
"ff3f7153_2023-10-11"
);
uriBuilder.appendQueryParameter(
"ttnet_version"
,
"4.2.137.21-toutiao"
);
uriBuilder.appendQueryParameter(
"use_store_region_cookie"
,
"1"
);
String result =
""
;
try
{
URL url =
new
URL(uriBuilder.build().toString());
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod(
"POST"
);
connection.setRequestProperty(
"Content-Type"
,
"application/octet-stream;tt-data=a"
);
connection.setRequestProperty(
"Accept-Encoding"
,
"gzip, deflate"
);
connection.setRequestProperty(
"User-Agent"
,
"com.ss.android.article.news/9540 (Linux; U; Android 9; zh_CN_#Hans; Pixel; Build/PQ3A.190801.002; Cronet/TTNetVersion:ff3f7153 2023-10-11 QuicVersion:ec35dc6e 2023-10-11)"
);
connection.setRequestProperty(
"x-ss-req-ticket"
, String.valueOf(System.currentTimeMillis()));
connection.setDoInput(
true
);
connection.setDoOutput(
true
);
DataOutputStream outputStream =
new
DataOutputStream(connection.getOutputStream());
outputStream.write(genBody(req_id));
outputStream.flush();
outputStream.close();
InputStream inputStream = connection.getInputStream();
int
status = connection.getResponseCode();
Log.d(TAG,
"reponse-status: "
+ String.valueOf(status));
if
(inputStream !=
null
){
BufferedReader bufferedReader =
new
BufferedReader(
new
InputStreamReader(inputStream));
String line=
""
;
while
((line = bufferedReader.readLine()) !=
null
) {
Log.d(TAG,
"reponse-line: "
+ line);
result += (line+
"\n"
);
}
}
else
{
result =
"post ret null!"
;
}
}
catch
(Exception e) {
Log.d(TAG,
"post err: "
+ e.toString());
}
Log.d(TAG,
"post ret: "
+ result);
return
result;
}
}