Skip to content

Connection problem #24

@MMDSNIPER

Description

@MMDSNIPER

I am experiencing an issue where I am unable to connect to the configuration file. The connection attempts are initiated, but they are subsequently terminated without a successful connection.

config.ovpn file:

client
dev tun
remote [server ip] 1403 tcp
tun-mtu 1500
tls-client
nobind
persist-tun
persist-key
mute-replay-warnings
verb 3
cipher AES-256-GCM
redirect-gateway def1
auth SHA1
pull
auth-user-pass
connect-retry 1
reneg-sec 3600
remote-cert-tls server
[ca cert here]

appApp.swift:

import SwiftUI
import OpenVPNXor

@main
struct MyApp: App {
    @StateObject private var viewModel = AppViewModel()
    init() {
        OpenVPNManager.setup(openvpnPacketTunnelIdentifier: "com.eliteping.release.EliteAdaptor", appGroupIdentifier: "group.com.eliteping.release")
    }
    
    var body: some Scene {
        WindowGroup {
            VPNView()
                .environmentObject(viewModel)
                .preferredColorScheme(viewModel.selectedTheme)
        }
    }
}

Main Code:

import SwiftUI
import NetworkExtension
import OpenVPNXor
import os.log

class vm: ObservableObject {
    let configurationFileName = "config"
    let login = "userhere"
    let pass = "passwordhere"
    
    func conf() {
        if let fileURL = Bundle.main.url(forResource: configurationFileName, withExtension: "ovpn") {
            do {
                let configData = try Data(contentsOf: fileURL)
                OpenVPNManager.shared.appName = "Elite"
                OpenVPNManager.shared.configureVPN(openVPNConfiguration: configData, login: login, pass: pass) { success in
                    if success {
                        print("VPN profile saved successfully.")
                    } else {
                        print("Error saving VPN profile.")
                    }
                }
            } catch {
                print("Error loading file: \(error.localizedDescription)")
            }
        } else {
            print("File 'config.ovpn' not found.")
        }
    }
    
    func connect() {
        OpenVPNManager.shared.connectVPN { errorDescription in
            print(errorDescription ?? "No error description")
        }
    }
}

struct VPNView: View {
    @StateObject private var vpnVM = vm()
    @State private var vpnStatus: VPNStatus = OpenVPNManager.shared.vpnStatus ?? .disconnected
    @State private var logs: [String] = []
    @State private var networkStats: String = "No data yet"
    
    var body: some View {
        VStack(spacing: 20) {
            Text("VPN Status: \(vpnStatusDescription(vpnStatus))")
                .font(.headline)
                .padding()
            
            Button(action: {
                if vpnStatus == .connected {
                    OpenVPNManager.shared.disconnectVPN()
                    logs.append("🔴 VPN Disconnected.")
                } else {
                    vpnVM.conf()
                    vpnVM.connect()
                }
            }) {
                Text(vpnStatus == .connected ? "Disconnect" : "Connect")
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(vpnStatus == .connected ? Color.red : Color.green)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }
            .padding(.horizontal)
            
            Text("VPN Logs:")
                .font(.headline)
                .padding(.top)
            
            ScrollView {
                VStack(alignment: .leading) {
                    ForEach(logs, id: \ .self) { log in
                        Text(log)
                            .font(.caption)
                            .padding(2)
                            .frame(maxWidth: .infinity, alignment: .leading)
                    }
                }
            }
            .frame(height: 200)
            .border(Color.gray, width: 1)
            .padding()
            
            Text("Network Stats: \(networkStats)")
                .font(.subheadline)
                .padding()
            
            Spacer()
        }
        .onAppear {
            setupVPNListeners()
        }
    }
    
    private func setupVPNListeners() {
        OpenVPNManager.shared.onVPNStatusChange = { status in
            DispatchQueue.main.async {
                vpnStatus = status
                logs.append("🔔 VPN Status Changed: \(vpnStatusDescription(status))")
            }
        }
    }
    
    private func vpnStatusDescription(_ status: VPNStatus) -> String {
        switch status {
        case .invalid, .disconnected: return "Disconnected"
        case .connecting: return "Connecting..."
        case .connected: return "Connected"
        case .reasserting: return "Reconnecting..."
        case .disconnecting: return "Disconnecting..."
        }
    }
}

struct VPNView_Previews: PreviewProvider {
    static var previews: some View {
        VPNView()
    }
}

Screen shots:
Image
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions