com.qiniu.android.dns.NetworkInfo Java Examples

The following examples show how to use com.qiniu.android.dns.NetworkInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Hosts.java    From happy-dns-android with MIT License 6 votes vote down vote up
private LinkedList<Value> filter(LinkedList<Value> origin, NetworkInfo info) {
    LinkedList<Value> normal = new LinkedList<>();
    LinkedList<Value> special = new LinkedList<>();
    for (Value v : origin) {
        if (v.provider == NetworkInfo.ISP_GENERAL) {
            normal.add(v);
        }
        if (info.provider != NetworkInfo.ISP_GENERAL
                && v.provider == info.provider) {
            special.add(v);
        }
    }
    if (special.size() != 0) {
        return special;
    }
    return normal;
}
 
Example #2
Source File: Hosts.java    From happy-dns-android with MIT License 5 votes vote down vote up
public synchronized String[] query(Domain domain, NetworkInfo info) {
    LinkedList<Value> values = hosts.get(domain.domain);
    if (values == null || values.isEmpty()) {
        return null;
    }
    if (values.size() > 1) {
        Value first = values.get(0);
        values.remove(0);
        values.add(first);
    }
    values = filter(values, info);
    return toIps(values);
}
 
Example #3
Source File: Resolver.java    From happy-dns-android with MIT License 5 votes vote down vote up
@Override
public Record[] resolve(Domain domain, NetworkInfo info) throws IOException {
    int id;
    synchronized (random) {
        id = random.nextInt() & 0XFF;
    }
    byte[] query = DnsMessage.buildQuery(domain.domain, id);
    byte[] answer = udpCommunicate(query);
    if (answer == null) {
        throw new DnsException(domain.domain, "cant get answer");
    }

    return DnsMessage.parseResponse(answer, id, domain.domain);
}
 
Example #4
Source File: Hosts.java    From happy-dns-android with MIT License 4 votes vote down vote up
public Value(String ip) {
    this(ip, NetworkInfo.ISP_GENERAL);
}
 
Example #5
Source File: QiniuDns.java    From happy-dns-android with MIT License 4 votes vote down vote up
@Override
public Record[] resolve(Domain domain, NetworkInfo info) throws IOException {
    if (mAccountId == null || mEncryptKey == null) {
        throw new DnsException(domain.domain, "Invalid account id or encrypt key");
    }
    HttpURLConnection connection = (HttpURLConnection) new URL(mIsHttps ? ENDPOINT_SSL : ENDPOINT + mAccountId
            + "/d?dn=" + (mIsEncrypted ? DES.encrypt(domain.domain, mEncryptKey) : domain.domain)
            + "&e=" + Integer.toString(mExpireTimeSecond)
            + "&s=" + MD5.encrypt(domain.domain + "-" + mEncryptKey + "-" + mExpireTimeSecond)
            + "&ttl=1" + "&echo=1").openConnection();
    connection.setConnectTimeout(3000);
    connection.setReadTimeout(10000);
    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line);
    }
    try {
        JSONArray result = mIsEncrypted ?
                new JSONArray(DES.decrypt(new JSONObject(sb.toString()).optString("data"),
                        mEncryptKey)).optJSONArray(0) :
                new JSONObject(sb.toString()).optJSONArray("data").optJSONArray(0);
        if (result.length() <= 0) {
            return null;
        }
        Record[] records = new Record[result.length()];
        for (int i = 0; i < records.length;++i) {
            JSONObject item = result.optJSONObject(i);
            records[i] = new Record(item.optString("data"), Record.TYPE_A,
                    item.optInt("TTL"), System.currentTimeMillis() / 1000);
        }
        return records;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #6
Source File: DnspodFree.java    From happy-dns-android with MIT License 4 votes vote down vote up
@Override
public Record[] resolve(Domain domain, NetworkInfo info) throws IOException {
    URL url = new URL("http://" + ip + "/d?ttl=1&dn=" + domain.domain);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setConnectTimeout(3000);
    httpConn.setReadTimeout(timeout * 1000);
    int responseCode = httpConn.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_OK) {
        return null;
    }

    int length = httpConn.getContentLength();
    if (length <= 0 || length > 1024) {
        return null;
    }
    InputStream is = httpConn.getInputStream();
    byte[] data = new byte[length];
    int read = is.read(data);
    is.close();
    if (read <= 0) {
        return null;
    }
    String response = new String(data, 0, read);
    String[] r1 = response.split(",");
    if (r1.length != 2) {
        return null;
    }
    int ttl;
    try {
        ttl = Integer.parseInt(r1[1]);
    } catch (Exception e) {
        return null;
    }
    String[] ips = r1[0].split(";");
    if (ips.length == 0) {
        return null;
    }
    Record[] records = new Record[ips.length];
    long time = System.currentTimeMillis() / 1000;
    for (int i = 0; i < ips.length; i++) {
        records[i] = new Record(ips[i], Record.TYPE_A, ttl, time);
    }
    return records;
}
 
Example #7
Source File: DnspodEnterprise.java    From happy-dns-android with MIT License 4 votes vote down vote up
@Override
public Record[] resolve(Domain domain, NetworkInfo info) throws IOException {
    URL url = new URL("http://" + ip + "/d?ttl=1&dn=" + encrypt(domain.domain)
            + "&id=" + id);

    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setConnectTimeout(3000);
    httpConn.setReadTimeout(timeout * 1000);
    int responseCode = httpConn.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_OK) {
        return null;
    }

    int length = httpConn.getContentLength();
    if (length <= 0 || length > 1024) {
        return null;
    }
    InputStream is = httpConn.getInputStream();
    byte[] data = new byte[length];
    int read = is.read(data);
    is.close();
    if (read <= 0) {
        return null;
    }
    String response = new String(data, 0, read);
    String result = decrypt(response);
    String[] r1 = result.split(",");
    if (r1.length != 2) {
        return null;
    }
    int ttl;
    try {
        ttl = Integer.parseInt(r1[1]);
    } catch (Exception e) {
        return null;
    }
    String[] ips = r1[0].split(";");
    if (ips.length == 0) {
        return null;
    }
    Record[] records = new Record[ips.length];
    long time = System.currentTimeMillis() / 1000;
    for (int i = 0; i < ips.length; i++) {
        records[i] = new Record(ips[i], Record.TYPE_A, ttl, time);
    }
    return records;
}