You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
struct Configuration {
func buildConfigurationData(config: String) throws -> Data {
guard let inboundPortString = Util.loadFromUserDefaults(key: "sock5Port"),
let trafficPortString = Util.loadFromUserDefaults(key: "trafficPort"),
let inboundPort = NWEndpoint.Port(inboundPortString),
let trafficPort = NWEndpoint.Port(trafficPortString)
else {
throw NSError(
domain: "ConfigurationError",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "Unable to load port from UserDefaults or port format is incorrect"]
)
}
Using below code internet working fine but whats and other some application not working PacketTunnelProvider.swift (Only Internet working some apps not )
`import LibXray
import Network
import NetworkExtension
import os
struct RunXrayRequest: Codable {
var datDir: String?
var configPath: String?
var maxMemory: Int64?
}
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "PacketTunnelProvider")
class PacketTunnelProvider: NEPacketTunnelProvider {
Configuration.swift (Generate Json Config) (Working for internet only)
`import Foundation
import LibXray
import Network
struct Configuration {
func buildConfigurationData(config: String) throws -> Data {
guard let inboundPortString = Util.loadFromUserDefaults(key: "sock5Port"),
let trafficPortString = Util.loadFromUserDefaults(key: "trafficPort"),
let inboundPort = NWEndpoint.Port(inboundPortString),
let trafficPort = NWEndpoint.Port(trafficPortString)
else {
throw NSError(
domain: "ConfigurationError",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "Unable to load port from UserDefaults or port format is incorrect"]
)
}
var configuration = try buildOutInbound(config: config)
// Force all traffic through SOCKS5
configuration["inbounds"] = buildInbound(inboundPort: inboundPort, trafficPort: trafficPort)
configuration["dns"] = [
"servers": [
"https+local://1.1.1.1/dns-query",
"1.0.0.1"
],
"queryStrategy": "UseIPv4"
]
configuration["stats"] = [:]
// Apply Egypt DPI bypass enhancements
configuration = enhanceConfigForEgyptDPI(config: configuration)
configuration = removeNullValues(from: configuration)
configuration = removeSendThroughFromOutbounds(from: configuration)
return try JSONSerialization.data(withJSONObject: configuration, options: .prettyPrinted)
}
func removeSendThroughFromOutbounds(from configuration: [String: Any]) -> [String: Any] {
var updatedConfig = configuration
if var outbounds = configuration["outbounds"] as? [[String: Any]], !outbounds.isEmpty {
outbounds[0].removeValue(forKey: "sendThrough")
updatedConfig["outbounds"] = outbounds
}
return updatedConfig
}
func removeNullValues(from dictionary: [String: Any]) -> [String: Any] {
var updatedDictionary = dictionary
for (key, value) in dictionary {
if value is NSNull || "\(value)" == "<null>" {
updatedDictionary.removeValue(forKey: key)
} else if let nestedDictionary = value as? [String: Any] {
updatedDictionary[key] = removeNullValues(from: nestedDictionary)
} else if let nestedArray = value as? [[String: Any]] {
updatedDictionary[key] = nestedArray.map { removeNullValues(from: $0) }
}
}
return updatedDictionary
}
private func buildOutInbound(config: String) throws -> [String: Any] {
guard let configData = config.data(using: .utf8) else {
throw NSError(
domain: "InvalidConfig",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "无效的配置字符串"]
)
}
let base64EncodedConfig = configData.base64EncodedString()
let xrayJsonString = LibXrayConvertShareLinksToXrayJson(base64EncodedConfig)
guard
let decodedData = Data(base64Encoded: xrayJsonString),
let decodedString = String(data: decodedData, encoding: .utf8),
let jsonData = decodedString.data(using: .utf8),
let jsonDict = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any],
let success = jsonDict["success"] as? Bool, success,
var dataDict = jsonDict["data"] as? [String: Any],
var outboundsArray = dataDict["outbounds"] as? [[String: Any]]
else {
throw NSError(
domain: "InvalidXrayJson",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "解析 Xray JSON 失败"]
)
}
if var firstOutbound = outboundsArray.first {
firstOutbound["tag"] = "proxy"
outboundsArray[0] = firstOutbound
}
let newObject: [String: Any] = [
"protocol": "freedom",
"tag": "direct",
]
outboundsArray.append(newObject)
dataDict["outbounds"] = outboundsArray
return dataDict
}
private func buildInbound(inboundPort: NWEndpoint.Port = Constant.sock5Port,
trafficPort: NWEndpoint.Port = Constant.trafficPort) -> [[String: Any]]
{
let socksInbound: [String: Any] = [
"tag": "socks-in",
"port": 10808,
"listen": "127.0.0.1",
"protocol": "socks",
"settings": [
"auth": "noauth",
"udp": true,
"userLevel": 0,
"ip": "127.0.0.1",
"udpTimeout": 300
],
"sniffing": [
"enabled": true,
"destOverride": ["http", "tls"]
],
"streamSettings": [
"sockopt": [
"mark": 255,
"tcpFastOpen": true,
"tproxy": "redirect"
]
]
]
return [socksInbound]
}
private func buildMetrics() -> [String: Any] {
[
"tag": "metricsOut",
]
}
private func buildPolicy() -> [String: Any] {
[
"system": [
"statsInboundDownlink": true,
"statsInboundUplink": true,
"statsOutboundDownlink": true,
"statsOutboundUplink": true,
],
]
}
private func buildRoute() throws -> [String: Any] {
var route: [String: Any] = [
"domainStrategy": "IPIfNonMatch",
"domainMatcher": "mph",
"rules": [
[
"type": "field",
"outboundTag": "proxy",
"port": "0-65535" // Route all traffic through proxy
]
]
]
return route
}
// Add this method to your Configuration class
func enhanceConfigForEgyptDPI(config: [String: Any]) -> [String: Any] {
var updatedConfig = config
// Ensure SOCKS inbound has UDP enabled
if var inbounds = updatedConfig["inbounds"] as? [[String: Any]] {
for i in 0..<inbounds.count {
if var inbound = inbounds[i] as? [String: Any],
let protocolType = inbound["protocol"] as? String,
protocolType == "socks" {
var settings = inbound["settings"] as? [String: Any] ?? [:]
settings["udp"] = true
inbound["settings"] = settings
inbounds[i] = inbound
}
}
updatedConfig["inbounds"] = inbounds
}
// Add special sockopt settings for outbounds to evade DPI
if var outbounds = updatedConfig["outbounds"] as? [[String: Any]],
outbounds.count > 0,
var mainOutbound = outbounds[0] as? [String: Any] {
var streamSettings = mainOutbound["streamSettings"] as? [String: Any] ?? [:]
streamSettings["sockopt"] = [
"mark": 255,
"tcpFastOpen": true,
"tproxy": "redirect"
]
mainOutbound["streamSettings"] = streamSettings
outbounds[0] = mainOutbound
updatedConfig["outbounds"] = outbounds
}
return updatedConfig
}
}`
So according to last shared code it's working but half of the internet we can say where application using socket or any other communication which not via http in that case no internet. Please review and let me know where I have made wrong things.
struct Configuration { func buildConfigurationData(config: String) throws -> Data { guard let inboundPortString = Util.loadFromUserDefaults(key: "sock5Port"), let trafficPortString = Util.loadFromUserDefaults(key: "trafficPort"), let inboundPort = NWEndpoint.Port(inboundPortString), let trafficPort = NWEndpoint.Port(trafficPortString) else { throw NSError( domain: "ConfigurationError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Unable to load port from UserDefaults or port format is incorrect"] ) }
Using below code internet working fine but whats and other some application not working PacketTunnelProvider.swift (Only Internet working some apps not )
`import LibXray import Network import NetworkExtension import os
struct RunXrayRequest: Codable { var datDir: String? var configPath: String? var maxMemory: Int64? }
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "PacketTunnelProvider")
class PacketTunnelProvider: NEPacketTunnelProvider {
Configuration.swift (Generate Json Config) (Working for internet only) `import Foundation import LibXray import Network
struct Configuration { func buildConfigurationData(config: String) throws -> Data { guard let inboundPortString = Util.loadFromUserDefaults(key: "sock5Port"), let trafficPortString = Util.loadFromUserDefaults(key: "trafficPort"), let inboundPort = NWEndpoint.Port(inboundPortString), let trafficPort = NWEndpoint.Port(trafficPortString) else { throw NSError( domain: "ConfigurationError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Unable to load port from UserDefaults or port format is incorrect"] ) }
var configuration = try buildOutInbound(config: config)
// Force all traffic through SOCKS5
configuration["inbounds"] = buildInbound(inboundPort: inboundPort, trafficPort: trafficPort)
configuration["dns"] = [
"servers": [
"https+local://1.1.1.1/dns-query",
"1.0.0.1"
],
"queryStrategy": "UseIPv4"
]
configuration["stats"] = [:]
// Apply Egypt DPI bypass enhancements
configuration = enhanceConfigForEgyptDPI(config: configuration)
configuration = removeNullValues(from: configuration)
configuration = removeSendThroughFromOutbounds(from: configuration)
return try JSONSerialization.data(withJSONObject: configuration, options: .prettyPrinted)
}
func removeSendThroughFromOutbounds(from configuration: [String: Any]) -> [String: Any] {
var updatedConfig = configuration
if var outbounds = configuration["outbounds"] as? [[String: Any]], !outbounds.isEmpty {
outbounds[0].removeValue(forKey: "sendThrough")
updatedConfig["outbounds"] = outbounds
}
return updatedConfig
}
func removeNullValues(from dictionary: [String: Any]) -> [String: Any] {
var updatedDictionary = dictionary
for (key, value) in dictionary {
if value is NSNull || "\(value)" == "<null>" {
updatedDictionary.removeValue(forKey: key)
} else if let nestedDictionary = value as? [String: Any] {
updatedDictionary[key] = removeNullValues(from: nestedDictionary)
} else if let nestedArray = value as? [[String: Any]] {
updatedDictionary[key] = nestedArray.map { removeNullValues(from: $0) }
}
}
return updatedDictionary
}
private func buildOutInbound(config: String) throws -> [String: Any] {
guard let configData = config.data(using: .utf8) else {
throw NSError(
domain: "InvalidConfig",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "无效的配置字符串"]
)
}
let base64EncodedConfig = configData.base64EncodedString()
let xrayJsonString = LibXrayConvertShareLinksToXrayJson(base64EncodedConfig)
guard
let decodedData = Data(base64Encoded: xrayJsonString),
let decodedString = String(data: decodedData, encoding: .utf8),
let jsonData = decodedString.data(using: .utf8),
let jsonDict = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any],
let success = jsonDict["success"] as? Bool, success,
var dataDict = jsonDict["data"] as? [String: Any],
var outboundsArray = dataDict["outbounds"] as? [[String: Any]]
else {
throw NSError(
domain: "InvalidXrayJson",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "解析 Xray JSON 失败"]
)
}
if var firstOutbound = outboundsArray.first {
firstOutbound["tag"] = "proxy"
outboundsArray[0] = firstOutbound
}
let newObject: [String: Any] = [
"protocol": "freedom",
"tag": "direct",
]
outboundsArray.append(newObject)
dataDict["outbounds"] = outboundsArray
return dataDict
}
private func buildInbound(inboundPort: NWEndpoint.Port = Constant.sock5Port,
trafficPort: NWEndpoint.Port = Constant.trafficPort) -> [[String: Any]]
{
let socksInbound: [String: Any] = [
"tag": "socks-in",
"port": 10808,
"listen": "127.0.0.1",
"protocol": "socks",
"settings": [
"auth": "noauth",
"udp": true,
"userLevel": 0,
"ip": "127.0.0.1",
"udpTimeout": 300
],
"sniffing": [
"enabled": true,
"destOverride": ["http", "tls"]
],
"streamSettings": [
"sockopt": [
"mark": 255,
"tcpFastOpen": true,
"tproxy": "redirect"
]
]
]
return [socksInbound]
}
private func buildMetrics() -> [String: Any] {
[
"tag": "metricsOut",
]
}
private func buildPolicy() -> [String: Any] {
[
"system": [
"statsInboundDownlink": true,
"statsInboundUplink": true,
"statsOutboundDownlink": true,
"statsOutboundUplink": true,
],
]
}
private func buildRoute() throws -> [String: Any] {
var route: [String: Any] = [
"domainStrategy": "IPIfNonMatch",
"domainMatcher": "mph",
"rules": [
[
"type": "field",
"outboundTag": "proxy",
"port": "0-65535" // Route all traffic through proxy
]
]
]
return route
}
// Add this method to your Configuration class
func enhanceConfigForEgyptDPI(config: [String: Any]) -> [String: Any] {
var updatedConfig = config
// Ensure SOCKS inbound has UDP enabled
if var inbounds = updatedConfig["inbounds"] as? [[String: Any]] {
for i in 0..<inbounds.count {
if var inbound = inbounds[i] as? [String: Any],
let protocolType = inbound["protocol"] as? String,
protocolType == "socks" {
var settings = inbound["settings"] as? [String: Any] ?? [:]
settings["udp"] = true
inbound["settings"] = settings
inbounds[i] = inbound
}
}
updatedConfig["inbounds"] = inbounds
}
// Add special sockopt settings for outbounds to evade DPI
if var outbounds = updatedConfig["outbounds"] as? [[String: Any]],
outbounds.count > 0,
var mainOutbound = outbounds[0] as? [String: Any] {
var streamSettings = mainOutbound["streamSettings"] as? [String: Any] ?? [:]
streamSettings["sockopt"] = [
"mark": 255,
"tcpFastOpen": true,
"tproxy": "redirect"
]
mainOutbound["streamSettings"] = streamSettings
outbounds[0] = mainOutbound
updatedConfig["outbounds"] = outbounds
}
return updatedConfig
}
}`
So according to last shared code it's working but half of the internet we can say where application using socket or any other communication which not via http in that case no internet. Please review and let me know where I have made wrong things.
I’ve tried updating and removing the DNS, but the issue remains the same. The problem occurs specifically when someone located in Egypt uses the VPN—they still experience the issue.
Activity
yiguodev commentedon Jun 3, 2025
Try to use Wireshark to track the network traffic. I think it is NOT the problem of libXray.
codewithtamim commentedon Jun 3, 2025
Your setup is wrong im sure about that cause there's no reason that it will not work. Im using it on production apps and they are running smoothly
commencementtech commentedon Jun 6, 2025
@codewithtamim Please find attached code which I am using
PacketTunnelProvider.swift (Not working)
`import LibXray
import Network
import NetworkExtension
import os
import Tun2SocksKit
struct RunXrayRequest: Codable {
var datDir: String?
var configPath: String?
var maxMemory: Int64?
}
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "PacketTunnelProvider")
class PacketTunnelProvider: NEPacketTunnelProvider {
}`
Configuration.swift (Config Generation Code) (Not working)
`import Foundation
import LibXray
import Network
struct Configuration {
func buildConfigurationData(config: String) throws -> Data {
guard let inboundPortString = Util.loadFromUserDefaults(key: "sock5Port"),
let trafficPortString = Util.loadFromUserDefaults(key: "trafficPort"),
let inboundPort = NWEndpoint.Port(inboundPortString),
let trafficPort = NWEndpoint.Port(trafficPortString)
else {
throw NSError(
domain: "ConfigurationError",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "Unable to load port from UserDefaults or port format is incorrect"]
)
}
}`
Using above code having issue for no internet.
Using below code internet working fine but whats and other some application not working
PacketTunnelProvider.swift (Only Internet working some apps not )
`import LibXray
import Network
import NetworkExtension
import os
struct RunXrayRequest: Codable {
var datDir: String?
var configPath: String?
var maxMemory: Int64?
}
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "PacketTunnelProvider")
class PacketTunnelProvider: NEPacketTunnelProvider {
}`
Configuration.swift (Generate Json Config) (Working for internet only)
`import Foundation
import LibXray
import Network
struct Configuration {
func buildConfigurationData(config: String) throws -> Data {
guard let inboundPortString = Util.loadFromUserDefaults(key: "sock5Port"),
let trafficPortString = Util.loadFromUserDefaults(key: "trafficPort"),
let inboundPort = NWEndpoint.Port(inboundPortString),
let trafficPort = NWEndpoint.Port(trafficPortString)
else {
throw NSError(
domain: "ConfigurationError",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "Unable to load port from UserDefaults or port format is incorrect"]
)
}
}`
So according to last shared code it's working but half of the internet we can say where application using socket or any other communication which not via http in that case no internet. Please review and let me know where I have made wrong things.
In advance Thanks
codewithtamim commentedon Jun 6, 2025
DNS
commencementtech commentedon Jun 6, 2025
Sorry not getting your point @codewithtamim
codewithtamim commentedon Jun 6, 2025
Fix dns
commencementtech commentedon Jun 6, 2025
Okay let me check with updating it and update you
commencementtech commentedon Jun 7, 2025
I’ve tried updating and removing the DNS, but the issue remains the same. The problem occurs specifically when someone located in Egypt uses the VPN—they still experience the issue.