parent
5775c0e730
commit
4f919c5024
@ -0,0 +1,155 @@ |
||||
package com.github.tvbox.osc.util; |
||||
|
||||
import android.annotation.SuppressLint; |
||||
import android.content.Context; |
||||
import android.net.ConnectivityManager; |
||||
import android.net.NetworkInfo; |
||||
import android.net.wifi.WifiInfo; |
||||
import android.net.wifi.WifiManager; |
||||
|
||||
import java.net.Inet4Address; |
||||
import java.net.InetAddress; |
||||
import java.net.NetworkInterface; |
||||
import java.net.SocketException; |
||||
import java.util.Enumeration; |
||||
import java.util.regex.Pattern; |
||||
|
||||
|
||||
/** |
||||
* 作者:By hdy |
||||
* 日期:On 2018/11/1 |
||||
* 时间:At 19:17 |
||||
*/ |
||||
public class LocalIPAddress { |
||||
@SuppressLint("DefaultLocale") |
||||
public static String getLocalIPAddress(Context context) { |
||||
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); |
||||
int ipAddress = wifiManager.getConnectionInfo().getIpAddress(); |
||||
if (ipAddress == 0) { |
||||
try { |
||||
Enumeration<NetworkInterface> enumerationNi = NetworkInterface.getNetworkInterfaces(); |
||||
while (enumerationNi.hasMoreElements()) { |
||||
NetworkInterface networkInterface = enumerationNi.nextElement(); |
||||
String interfaceName = networkInterface.getDisplayName(); |
||||
if (interfaceName.equals("eth0") || interfaceName.equals("wlan0")) { |
||||
Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); |
||||
while (enumIpAddr.hasMoreElements()) { |
||||
InetAddress inetAddress = enumIpAddr.nextElement(); |
||||
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { |
||||
return inetAddress.getHostAddress(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} catch (SocketException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} else { |
||||
return String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)); |
||||
} |
||||
return "127.0.0.1"; |
||||
} |
||||
public static String getIP(Context context) { |
||||
try { |
||||
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); |
||||
//判断wifi是否开启
|
||||
WifiInfo wifiInfo = null; |
||||
if (wifiManager != null) { |
||||
wifiInfo = wifiManager.getConnectionInfo(); |
||||
} |
||||
int ipAddress = 0; |
||||
if (wifiInfo != null) { |
||||
ipAddress = wifiInfo.getIpAddress(); |
||||
} |
||||
return intToIp(ipAddress); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
try { |
||||
return getLocalIPAddress(); |
||||
} catch (Exception e1) { |
||||
e1.printStackTrace(); |
||||
} |
||||
} |
||||
return "127.0.0.1"; |
||||
} |
||||
|
||||
public static String getLocalIPAddress() { |
||||
try { |
||||
for (Enumeration<NetworkInterface> mEnumeration = NetworkInterface |
||||
.getNetworkInterfaces(); mEnumeration.hasMoreElements(); ) { |
||||
NetworkInterface intf = mEnumeration.nextElement(); |
||||
for (Enumeration<InetAddress> enumIPAddr = intf |
||||
.getInetAddresses(); enumIPAddr.hasMoreElements(); ) { |
||||
InetAddress inetAddress = enumIPAddr.nextElement(); |
||||
// 如果不是回环地址
|
||||
if (!inetAddress.isLoopbackAddress()) { |
||||
// 直接返回本地IP地址
|
||||
return inetAddress.getHostAddress(); |
||||
} |
||||
} |
||||
} |
||||
} catch (SocketException ex) { |
||||
System.err.print("error"); |
||||
} |
||||
return "127.0.0.1"; |
||||
} |
||||
|
||||
|
||||
private static String intToIp(int i) { |
||||
return (i & 0xFF) + "." + |
||||
((i >> 8) & 0xFF) + "." + |
||||
((i >> 16) & 0xFF) + "." + |
||||
(i >> 24 & 0xFF); |
||||
} |
||||
|
||||
/** |
||||
* Ipv4 address check. |
||||
*/ |
||||
private static final Pattern IPV4_PATTERN = Pattern.compile( |
||||
"^(" + "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}" + |
||||
"([0-9]|[1-9][0-9]|1[0-9]{2}|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}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$"); |
||||
|
||||
/** |
||||
* Check if valid IPV4 address. |
||||
* |
||||
* @param input the address string to check for validity. |
||||
* @return True if the input parameter is a valid IPv4 address. |
||||
*/ |
||||
public static boolean isIPv4Address(String input) { |
||||
return IPV4_PATTERN.matcher(input).matches(); |
||||
} |
||||
|
||||
public static boolean isIPv6Address(String str) { |
||||
return str != null && IPV6_PATTERN.matcher(str).matches(); |
||||
} |
||||
|
||||
public static boolean isIPAddress(String str) { |
||||
return isIPv4Address(str) || isIPv6Address(str); |
||||
} |
||||
|
||||
public static boolean isNetworkAvailable(final Context context) { |
||||
boolean hasWifoCon = false; |
||||
boolean hasMobileCon = false; |
||||
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE); |
||||
NetworkInfo[] netInfos = cm.getAllNetworkInfo(); |
||||
for (NetworkInfo net : netInfos) { |
||||
|
||||
String type = net.getTypeName(); |
||||
if (type.equalsIgnoreCase("WIFI")) { |
||||
if (net.isConnected()) { |
||||
hasWifoCon = true; |
||||
} |
||||
} |
||||
|
||||
if (type.equalsIgnoreCase("MOBILE")) { |
||||
if (net.isConnected()) { |
||||
hasMobileCon = true; |
||||
} |
||||
} |
||||
} |
||||
return hasWifoCon || hasMobileCon; |
||||
|
||||
} |
||||
} |
||||
@ -0,0 +1,53 @@ |
||||
package com.github.tvbox.osc.util.thunder; |
||||
|
||||
import android.net.Uri; |
||||
import android.text.TextUtils; |
||||
|
||||
import com.github.tvbox.osc.base.App; |
||||
import com.github.tvbox.osc.util.LocalIPAddress; |
||||
import com.p2p.P2PClass; |
||||
|
||||
import java.io.UnsupportedEncodingException; |
||||
import java.net.URLDecoder; |
||||
import java.net.URLEncoder; |
||||
|
||||
|
||||
public class Jianpian { |
||||
|
||||
public static String JPUrlDec(String url) { |
||||
if (App.getp2p() != null) { |
||||
try { |
||||
String decode = URLDecoder.decode(url, "UTF-8"); |
||||
String[] split = decode.split("\\|"); |
||||
String replace = split[0].replace("xg://", "ftp://"); |
||||
if (replace.contains("xgplay://")) { |
||||
replace = split[0].replace("xgplay://", "ftp://"); |
||||
} |
||||
if (!TextUtils.isEmpty(App.burl)) { |
||||
App.getp2p().P2Pdoxpause(App.burl.getBytes("GBK")); |
||||
App.getp2p().P2Pdoxdel(App.burl.getBytes("GBK")); |
||||
} |
||||
App.burl = replace; |
||||
App.getp2p().P2Pdoxstart(replace.getBytes("GBK")); |
||||
App.getp2p().P2Pdoxadd(replace.getBytes("GBK")); |
||||
return "http://" + LocalIPAddress.getIP(App.getInstance()) + ":" + P2PClass.port + "/" + URLEncoder.encode(Uri.parse(replace).getLastPathSegment(), "GBK"); |
||||
} catch (Exception e) { |
||||
return e.getLocalizedMessage(); |
||||
} |
||||
} else { |
||||
return ""; |
||||
} |
||||
} |
||||
|
||||
public static void finish() { |
||||
if (!TextUtils.isEmpty(App.burl) && App.getp2p() != null) { |
||||
try { |
||||
App.getp2p().P2Pdoxpause(App.burl.getBytes("GBK")); |
||||
App.getp2p().P2Pdoxdel(App.burl.getBytes("GBK")); |
||||
App.burl = ""; |
||||
} catch (UnsupportedEncodingException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,204 @@ |
||||
package com.p2p; |
||||
|
||||
import java.io.File; |
||||
import java.util.concurrent.Executors; |
||||
|
||||
|
||||
public class P2PClass { |
||||
private static final String TAG = "P2PClass"; |
||||
|
||||
public static int port = 8087; |
||||
|
||||
public String path = null; |
||||
|
||||
class init extends Thread { |
||||
|
||||
final String CardPath; |
||||
|
||||
init(String str) { |
||||
this.CardPath = str; |
||||
} |
||||
|
||||
public void run() { |
||||
P2PClass p2PClass = P2PClass.this; |
||||
p2PClass.path = this.CardPath + "/jpali"; |
||||
File file = new File(P2PClass.this.path); |
||||
if (!file.exists()) { |
||||
file.mkdirs(); |
||||
} |
||||
P2PClass.port = P2PClass.this.doxstarthttpd("TEST3E63BAAECDAA79BEAA91853490A69F08".getBytes(), this.CardPath.getBytes()); |
||||
} |
||||
} |
||||
|
||||
static { |
||||
System.loadLibrary("p2p"); |
||||
} |
||||
|
||||
public P2PClass(String str) { |
||||
path = str + "/jpali"; |
||||
File file = new File(path); |
||||
if (!file.exists()) { |
||||
file.mkdirs(); |
||||
} |
||||
port = doxstarthttpd("TEST3E63BAAECDAA79BEAA91853490A69F08".getBytes(), str.getBytes()); |
||||
//Executors.newCachedThreadPool().execute(new init(str));
|
||||
} |
||||
|
||||
|
||||
private final native void XGFilmCloseFile(long j); |
||||
|
||||
private final native long XGFilmOpenFile(byte[] bArr); |
||||
|
||||
private final native int XGFilmReadFile(long j, long j2, int i, byte[] bArr); |
||||
|
||||
private final native int dosetupload(int i); |
||||
|
||||
private final native void doxSetP2PPauseUpdate(int i); |
||||
|
||||
private final native int doxadd(byte[] bArr); |
||||
|
||||
private final native int doxcheck(byte[] bArr); |
||||
|
||||
private final native int doxdel(byte[] bArr); |
||||
|
||||
private final native int doxdelall(); |
||||
|
||||
private final native int doxdownload(byte[] bArr); |
||||
|
||||
private final native int doxendhttpd(); |
||||
|
||||
private final native String doxgetVersion(); |
||||
|
||||
private final native String doxgethostbynamehook(String str); |
||||
|
||||
private final native String doxgetlocalAddress(); |
||||
|
||||
private final native String doxgettaskstat(int i); |
||||
|
||||
private final native int doxpause(byte[] bArr); |
||||
|
||||
private final native int doxsave(); |
||||
|
||||
private final native int doxsetduration(int i); |
||||
|
||||
private final native int doxstart(byte[] bArr); |
||||
|
||||
private final native int doxstarthttpd(byte[] bArr, byte[] bArr2); |
||||
|
||||
private final native int doxterminate(); |
||||
|
||||
private final native long getdownsize(int i); |
||||
|
||||
private final native long getfilesize(int i); |
||||
|
||||
private final native long getlocalfilesize(byte[] bArr); |
||||
|
||||
private final native int getpercent(); |
||||
|
||||
private final native long getspeed(int i); |
||||
|
||||
|
||||
public int P2Pdoxstart(byte[] bArr) { |
||||
return doxstart(bArr); |
||||
} |
||||
|
||||
public int P2Pdoxdownload(byte[] bArr) { |
||||
return doxdownload(bArr); |
||||
} |
||||
|
||||
public int P2Pdoxterminate() { |
||||
return doxterminate(); |
||||
} |
||||
|
||||
public int P2Pdosetupload(int i) { |
||||
return dosetupload(i); |
||||
} |
||||
|
||||
public int P2Pdoxcheck(byte[] bArr) { |
||||
return doxcheck(bArr); |
||||
} |
||||
|
||||
public int P2Pdoxadd(byte[] bArr) { |
||||
return doxadd(bArr); |
||||
} |
||||
|
||||
public int P2Pdoxpause(byte[] bArr) { |
||||
return doxpause(bArr); |
||||
} |
||||
|
||||
public int P2Pdoxdel(byte[] bArr) { |
||||
return doxdel(bArr); |
||||
} |
||||
|
||||
public int P2PdoxdelAll() { |
||||
return doxdelall(); |
||||
} |
||||
|
||||
public long P2Pgetspeed(int i) { |
||||
return getspeed(i); |
||||
} |
||||
|
||||
public long P2Pgetdownsize(int i) { |
||||
return getdownsize(i); |
||||
} |
||||
|
||||
public long P2Pgetfilesize(int i) { |
||||
return getfilesize(i); |
||||
} |
||||
|
||||
public int P2Pgetpercent() { |
||||
return getpercent(); |
||||
} |
||||
|
||||
public long P2Pgetlocalfilesize(byte[] bArr) { |
||||
return getlocalfilesize(bArr); |
||||
} |
||||
|
||||
public long P2Pdosetduration(int i) { |
||||
return doxsetduration(i); |
||||
} |
||||
|
||||
public String getServiceAddress() { |
||||
return doxgethostbynamehook("xx0.github.com"); |
||||
} |
||||
|
||||
public int P2Pdoxstarthttpd(byte[] bArr, byte[] bArr2) { |
||||
return doxstarthttpd(bArr, bArr2); |
||||
} |
||||
|
||||
public int P2Pdoxsave() { |
||||
return doxsave(); |
||||
} |
||||
|
||||
public int P2Pdoxendhttpd() { |
||||
return doxendhttpd(); |
||||
} |
||||
|
||||
public String getVersion() { |
||||
return doxgetVersion(); |
||||
} |
||||
|
||||
public long xGFilmOpenFile(byte[] bArr) { |
||||
return XGFilmOpenFile(bArr); |
||||
} |
||||
|
||||
public void xGFilmCloseFile(long j) { |
||||
XGFilmCloseFile(j); |
||||
} |
||||
|
||||
public int xGFilmReadFile(long j, long j2, int i, byte[] bArr) { |
||||
return XGFilmReadFile(j, j2, i, bArr); |
||||
} |
||||
|
||||
public void setP2PPauseUpdate(int i) { |
||||
doxSetP2PPauseUpdate(i); |
||||
} |
||||
|
||||
public String getTouPingUrl() { |
||||
return doxgetlocalAddress(); |
||||
} |
||||
|
||||
public String P2Pdoxgettaskstat(int i) { |
||||
return doxgettaskstat(i); |
||||
} |
||||
} |
||||
Binary file not shown.
Loading…
Reference in new issue