21
21
*/
22
22
23
23
import android .content .Context ;
24
+
25
+ import androidx .annotation .NonNull ;
26
+
24
27
import com .hippo .ehviewer .EhApplication ;
25
28
import com .hippo .ehviewer .Hosts ;
26
29
import com .hippo .ehviewer .Settings ;
30
+
27
31
import java .net .InetAddress ;
28
32
import java .net .UnknownHostException ;
29
33
import java .util .Arrays ;
30
- import java .util .Collections ;
31
34
import java .util .HashMap ;
32
35
import java .util .List ;
33
36
import java .util .Map ;
37
+
34
38
import okhttp3 .Dns ;
35
39
import okhttp3 .HttpUrl ;
36
40
import okhttp3 .OkHttpClient ;
37
41
import okhttp3 .dnsoverhttps .DnsOverHttps ;
38
42
39
43
public class EhDns implements Dns {
40
44
41
- private static final Map <String , InetAddress > builtInHosts ;
45
+ private static final Map <String , List < InetAddress > > builtInHosts ;
42
46
43
47
static {
44
- Map <String , InetAddress > map = new HashMap <>();
45
- put (map , "e-hentai.org" , "104.20.26.25" );
46
- put (map , "exhentai.org" , "178.175.132.20" );
47
- put (map , "repo.e-hentai.org" , "94.100.29.73" );
48
+ Map <String , List <InetAddress >> map = new HashMap <>();
49
+ put (map , "e-hentai.org" , "104.20.26.25+104.20.27.25" );
50
+ put (map , "repo.e-hentai.org" , "94.100.28.57" );
48
51
put (map , "forums.e-hentai.org" , "94.100.18.243" );
49
- put (map , "ehgt.org" , "178.162.139.24" );
50
- put (map , "ul.ehgt.org" , "94.100.24.82" );
51
- put (map , "github.com" , "192.30.255.112" );
52
- put (map , "raw.githubusercontent.com" , "151.101.0.133" );
52
+ put (map , "ehgt.org" , "37.48.89.44+178.162.139.24+178.162.140.212+81.171.10.48" );
53
+ put (map , "ul.ehgt.org" , "94.100.24.82+94.100.24.72" );
53
54
builtInHosts = map ;
54
55
}
55
56
56
- private static void put (Map <String , InetAddress > map , String host , String ip ) {
57
- InetAddress address = Hosts .toInetAddress (host , ip );
58
- if (address != null ) {
59
- map .put (host , address );
60
- }
61
- }
62
-
63
57
private final Hosts hosts ;
64
58
private static DnsOverHttps dnsOverHttps ;
65
59
66
- // public EhDns(Context context) {
67
- // hosts = EhApplication.getHosts(context);
68
- // }
69
60
public EhDns (Context context ) {
70
61
hosts = EhApplication .getHosts (context );
71
62
DnsOverHttps .Builder builder = new DnsOverHttps .Builder ()
@@ -87,24 +78,31 @@ public EhDns(Context context) {
87
78
dnsOverHttps = builder .post (true ).build ();
88
79
}
89
80
90
- @ Override
91
- public List <InetAddress > lookup (String hostname ) throws UnknownHostException {
92
- if (hostname == null ) throw new UnknownHostException ("hostname == null" );
93
-
94
- InetAddress inetAddress = hosts .get (hostname );
95
- if (inetAddress != null ) {
96
- return Collections .singletonList (inetAddress );
81
+ private static void put (Map <String , List <InetAddress >> map , String host , String ip_s ) {
82
+ String [] ip_l = ip_s .split ("\\ +" );
83
+ InetAddress [] addr_l = new InetAddress [ip_l .length ];
84
+ for (int i = 0 ;i < ip_l .length ;i ++) {
85
+ addr_l [i ] = Hosts .toInetAddress (host , ip_l [i ]);
97
86
}
87
+ map .put (host , Arrays .asList (addr_l ));
88
+ }
98
89
90
+ @ NonNull
91
+ @ Override
92
+ public List <InetAddress > lookup (@ NonNull String hostname ) throws UnknownHostException {
93
+ hostname = hostname .replaceFirst ("h.github.io" , "hentai.org" ); // domain fronting
94
+ List <InetAddress > inetAddresses = (List <InetAddress >) hosts .get (hostname );
95
+ if (inetAddresses != null ) {
96
+ return inetAddresses ;
97
+ }
99
98
if (Settings .getBuiltInHosts ()) {
100
- inetAddress = builtInHosts .get (hostname );
101
- if (inetAddress != null ) {
102
- return Collections . singletonList ( inetAddress ) ;
99
+ inetAddresses = builtInHosts .get (hostname );
100
+ if (inetAddresses != null ) {
101
+ return inetAddresses ;
103
102
}
104
103
}
105
-
106
104
if (Settings .getDoH ()) {
107
- List < InetAddress > inetAddresses = dnsOverHttps .lookup (hostname );
105
+ inetAddresses = dnsOverHttps .lookup (hostname );
108
106
if (inetAddresses != null && inetAddresses .size () > 0 ) {
109
107
return inetAddresses ;
110
108
}
@@ -113,7 +111,7 @@ public List<InetAddress> lookup(String hostname) throws UnknownHostException {
113
111
return Arrays .asList (InetAddress .getAllByName (hostname ));
114
112
} catch (NullPointerException e ) {
115
113
UnknownHostException unknownHostException =
116
- new UnknownHostException ("Broken system behaviour for dns lookup of " + hostname );
114
+ new UnknownHostException ("Broken system behaviour for dns lookup of " + hostname );
117
115
unknownHostException .initCause (e );
118
116
throw unknownHostException ;
119
117
}
0 commit comments