V2ray-Android icon indicating copy to clipboard operation
V2ray-Android copied to clipboard

ADD WIREGUARD

Open mhdi-khosravi opened this issue 1 year ago • 0 comments

Just replace

public static boolean refillV2rayConfig(String remark, String config, final ArrayList<String> blockedApplications)

with this attached function in Utilites.java to add wireguard to this library.

public static boolean refillV2rayConfig(String remark, String config, final ArrayList<String> blockedApplications) { currentConfig.remark = remark; currentConfig.blockedApplications = blockedApplications; try { JSONObject config_json = new JSONObject(normalizeV2rayFullConfig(config)); Log.e("JSON FILE" , String.valueOf(config_json)); try { JSONArray inbounds = config_json.getJSONArray("inbounds"); for (int i = 0; i < inbounds.length(); i++) { try { if (inbounds.getJSONObject(i).getString("protocol").equals("socks")) { currentConfig.localSocksPort = inbounds.getJSONObject(i).getInt("port"); } } catch (Exception e) { //ignore } try { if (inbounds.getJSONObject(i).getString("protocol").equals("http")) { currentConfig.localHttpPort = inbounds.getJSONObject(i).getInt("port"); } } catch (Exception e) { //ignore } } } catch (Exception e) { Log.w(Utilities.class.getSimpleName(), "startCore warn => can`t find inbound port of socks5 or http."); return false; } String protocol = config_json.getJSONArray("outbounds").getJSONObject(0).getString("protocol"); if (protocol.equals("wireguard")) { currentConfig.currentServerAddress = config_json.getJSONArray("outbounds").getJSONObject(0).getJSONObject("settings").getJSONArray("peers").getJSONObject(0).getString("endpoint"); if (currentConfig.currentServerAddress.contains(":")) { String[] parts = currentConfig.currentServerAddress.split(":"); currentConfig.currentServerAddress = parts[0]; currentConfig.currentServerPort = Integer.parseInt(parts[1]); } else { throw new IllegalArgumentException("Endpoint format is invalid. Expected format: ADDRESS:PORT"); } } else { try { currentConfig.currentServerAddress = config_json.getJSONArray("outbounds").getJSONObject(0).getJSONObject("settings").getJSONArray("vnext").getJSONObject(0).getString("address"); currentConfig.currentServerPort = config_json.getJSONArray("outbounds").getJSONObject(0).getJSONObject("settings").getJSONArray("vnext").getJSONObject(0).getInt("port"); } catch (Exception e) { currentConfig.currentServerAddress = config_json.getJSONArray("outbounds").getJSONObject(0).getJSONObject("settings").getJSONArray("servers").getJSONObject(0).getString("address"); currentConfig.currentServerPort = config_json.getJSONArray("outbounds").getJSONObject(0).getJSONObject("settings").getJSONArray("servers").getJSONObject(0).getInt("port"); } } try { if (config_json.has("policy")) { config_json.remove("policy"); } if (config_json.has("stats")) { config_json.remove("stats"); } } catch (Exception ignore_error) { //ignore } if (currentConfig.enableTrafficStatics) { try { JSONObject policy = new JSONObject(); JSONObject levels = new JSONObject(); levels.put("8", new JSONObject().put("connIdle", 300).put("downlinkOnly", 1).put("handshake", 4).put("uplinkOnly", 1)); JSONObject system = new JSONObject().put("statsOutboundUplink", true).put("statsOutboundDownlink", true); policy.put("levels", levels); policy.put("system", system); config_json.put("policy", policy); config_json.put("stats", new JSONObject()); } catch (Exception e) { Log.e("log is here",e.toString()); currentConfig.enableTrafficStatics = false; //ignore } } currentConfig.fullJsonConfig = config_json.toString(); return true; } catch (Exception e) { Log.e(Utilities.class.getSimpleName(), "parseV2rayJsonFile failed => ", e); return false; } }

mhdi-khosravi avatar Jan 13 '25 00:01 mhdi-khosravi