Skip to content

Commit 062512f

Browse files
committed
u: url
1 parent d6927c6 commit 062512f

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
local.properties
1515
app/debug
1616
app/release
17+
*TestDemo.*
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.github.jsbxyyx.xbook.common;
2+
3+
public class Ba {
4+
5+
public static String atob(byte[] a) {
6+
return atob(a, false);
7+
}
8+
9+
public static String atoab(byte[] a) {
10+
return atob(a, true);
11+
}
12+
13+
private static String atob(byte[] a, boolean alternate) {
14+
int aLen = a.length;
15+
int numFullGroups = aLen / 3;
16+
int numBytesInPartialGroup = aLen - 3 * numFullGroups;
17+
int resultLen = 4 * ((aLen + 2) / 3);
18+
StringBuilder result = new StringBuilder(resultLen);
19+
char[] intToAlpha = (alternate ? itoab : itob);
20+
21+
int inCursor = 0;
22+
for (int i = 0; i < numFullGroups; i++) {
23+
int byte0 = a[inCursor++] & 0xff;
24+
int byte1 = a[inCursor++] & 0xff;
25+
int byte2 = a[inCursor++] & 0xff;
26+
result.append(intToAlpha[byte0 >> 2]);
27+
result.append(intToAlpha[(byte0 << 4) & 0x3f | (byte1 >> 4)]);
28+
result.append(intToAlpha[(byte1 << 2) & 0x3f | (byte2 >> 6)]);
29+
result.append(intToAlpha[byte2 & 0x3f]);
30+
}
31+
32+
if (numBytesInPartialGroup != 0) {
33+
int byte0 = a[inCursor++] & 0xff;
34+
result.append(intToAlpha[byte0 >> 2]);
35+
if (numBytesInPartialGroup == 1) {
36+
result.append(intToAlpha[(byte0 << 4) & 0x3f]);
37+
result.append("==");
38+
} else {
39+
int byte1 = a[inCursor++] & 0xff;
40+
result.append(intToAlpha[(byte0 << 4) & 0x3f | (byte1 >> 4)]);
41+
result.append(intToAlpha[(byte1 << 2) & 0x3f]);
42+
result.append('=');
43+
}
44+
}
45+
return result.toString();
46+
}
47+
48+
private static final char[] itob = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
49+
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
50+
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2',
51+
'3', '4', '5', '6', '7', '8', '9', '+', '/'};
52+
53+
private static final char[] itoab = {'!', '"', '#', '$', '%', '&', '\'', '(', ')', ',', '-', '.', ':',
54+
';', '<', '>', '@', '[', ']', '^', '`', '_', '{', '|', '}', '~', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
55+
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2',
56+
'3', '4', '5', '6', '7', '8', '9', '+', '?'};
57+
58+
public static byte[] btoa(String s) {
59+
return btoa(s, false);
60+
}
61+
62+
public static byte[] abtoa(String s) {
63+
return btoa(s, true);
64+
}
65+
66+
private static byte[] btoa(String s, boolean alternate) {
67+
byte[] alphaToInt = (alternate ? abtoi : btoi);
68+
int sLen = s.length();
69+
int numGroups = sLen / 4;
70+
if (4 * numGroups != sLen) {
71+
throw new IllegalArgumentException("String length must be a multiple of four.");
72+
}
73+
int missingBytesInLastGroup = 0;
74+
int numFullGroups = numGroups;
75+
if (sLen != 0) {
76+
if (s.charAt(sLen - 1) == '=') {
77+
missingBytesInLastGroup++;
78+
numFullGroups--;
79+
}
80+
if (s.charAt(sLen - 2) == '=') {
81+
missingBytesInLastGroup++;
82+
}
83+
}
84+
byte[] result = new byte[3 * numGroups - missingBytesInLastGroup];
85+
86+
int inCursor = 0, outCursor = 0;
87+
for (int i = 0; i < numFullGroups; i++) {
88+
int ch0 = btoi(s.charAt(inCursor++), alphaToInt);
89+
int ch1 = btoi(s.charAt(inCursor++), alphaToInt);
90+
int ch2 = btoi(s.charAt(inCursor++), alphaToInt);
91+
int ch3 = btoi(s.charAt(inCursor++), alphaToInt);
92+
result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
93+
result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
94+
result[outCursor++] = (byte) ((ch2 << 6) | ch3);
95+
}
96+
97+
if (missingBytesInLastGroup != 0) {
98+
int ch0 = btoi(s.charAt(inCursor++), alphaToInt);
99+
int ch1 = btoi(s.charAt(inCursor++), alphaToInt);
100+
result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
101+
102+
if (missingBytesInLastGroup == 1) {
103+
int ch2 = btoi(s.charAt(inCursor++), alphaToInt);
104+
result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
105+
}
106+
}
107+
return result;
108+
}
109+
110+
private static int btoi(char c, byte[] alphaToInt) {
111+
int result = alphaToInt[c];
112+
if (result < 0) {
113+
throw new IllegalArgumentException("Illegal character " + c);
114+
}
115+
return result;
116+
}
117+
118+
private static final byte[] btoi = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
119+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62,
120+
-1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7,
121+
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
122+
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
123+
124+
private static final byte[] abtoi = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
125+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10,
126+
11, -1, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1, -1, -1, -1, -1, -1, -1,
127+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, -1, 18, 19, 21, 20, 26, 27, 28,
128+
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 22, 23, 24, 25};
129+
130+
}

app/src/main/java/com/github/jsbxyyx/xbook/common/Common.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.OutputStream;
1414
import java.io.UnsupportedEncodingException;
1515
import java.net.URLEncoder;
16+
import java.nio.charset.StandardCharsets;
1617
import java.util.ArrayList;
1718
import java.util.HashMap;
1819
import java.util.List;
@@ -27,7 +28,7 @@ public class Common {
2728

2829
private static final List<Ip> IPS = new ArrayList<>();
2930

30-
public static final String host = "http2.idingdang.org";
31+
public static final String host = new String(Ba.abtoa("a([0c$)u:j!w:$!w:$!x.nh5eg=="), StandardCharsets.UTF_8);
3132
private static final String xurl = "https://" + host + "/xbook";
3233
private static final String xburl = "https://" + host + "/xbookb";
3334
public static final String zurl = "";

app/src/main/java/com/github/jsbxyyx/xbook/data/IpNetHelper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import androidx.annotation.NonNull;
44

55
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.github.jsbxyyx.xbook.common.Ba;
67
import com.github.jsbxyyx.xbook.common.Common;
78
import com.github.jsbxyyx.xbook.common.DataCallback;
89
import com.github.jsbxyyx.xbook.common.HttpStatusException;
@@ -11,6 +12,7 @@
1112
import com.github.jsbxyyx.xbook.data.bean.Ip;
1213

1314
import java.io.IOException;
15+
import java.nio.charset.StandardCharsets;
1416
import java.util.ArrayList;
1517
import java.util.List;
1618

@@ -25,8 +27,9 @@ public class IpNetHelper {
2527

2628
public void fetchIP(DataCallback<List<Ip>> dataCallback) {
2729
final String reqUrl = "/ip";
30+
final String h = new String(Ba.abtoa("a([0c$%u:j!w:$!w:$!x.nh5eg=="), StandardCharsets.UTF_8);
2831
Request.Builder builder = new Request.Builder()
29-
.url("https://http1.idingdang.org" + reqUrl)
32+
.url("https://" + h + reqUrl)
3033
.get();
3134
LogUtil.d(TAG, "fetch ip request: %s", reqUrl);
3235
HttpHelper.getDnsClient().newCall(builder.build()).enqueue(new Callback() {

0 commit comments

Comments
 (0)