add vpn v2

This commit is contained in:
JaiPandey425 2025-12-11 14:05:33 +05:30
parent 882f4acad8
commit 94840a93dd
6 changed files with 65 additions and 23 deletions

View File

@ -6,5 +6,5 @@ DNS = 1.1.1.1
[Peer]
PublicKey = jG+7H5thEiNjxY1XqI1R9oyMAMTIegLwE3tZkS1Rj2U=
Endpoint = 35.95.209.161:51820
AllowedIPs = 10.0.0.0/32 0.0.0.0/0
AllowedIPs = 10.0.0.0/32
PersistentKeepalive = 25

View File

@ -4,15 +4,22 @@ import { getToken } from '../utils/tokenHelper.js';
import chalk from "chalk";
import { sendTokenExpired } from '../ipc/ipcHandler.js';
import https from 'https';
import { getBaseURL } from './baseURL.js';
const TOKEN_KEY = 'ACCESS_TOKEN';
const Base_URL = await getBaseURL()
const httpsAgent = new https.Agent({
rejectUnauthorized: false
});
console.log('Base_URL$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$' ,Base_URL)
const api: AxiosInstance = axios.create({
baseURL: 'https://quality-friendly-goldfish.ngrok-free.app/api/v1/va', // 🔁 your backend
// baseURL: 'https://quality-friendly-goldfish.ngrok-free.app/api/v1/va', // 🔁 your backend
baseURL: `${Base_URL}/api/v1/va`,
httpsAgent
// timeout: 10000,
});
@ -29,7 +36,7 @@ api.interceptors.request.use(
if (token) {
config.headers['token'] = token;
config.headers['ngrok-skip-browser-warning'] = 'true';
config.headers['ngrok-skip-browser-warning'] = 'true';
}
return config;
@ -54,7 +61,7 @@ api.interceptors.response.use(
(response: AxiosResponse) => response.data,
async (error) => {
console.log(error , "Line 46")
console.log(error, "Line 46")
const status = error?.response?.status;
console.error('[Axios Error]', status);

View File

@ -1,4 +1,5 @@
let CURRENT_BASE_URL = "https://quality-friendly-goldfish.ngrok-free.app/api/v1/va";
// let CURRENT_BASE_URL = "https://quality-friendly-goldfish.ngrok-free.app/api/v1/va";
let CURRENT_BASE_URL = "https://quality-friendly-goldfish.ngrok-free.app";
export const getBaseURL = () => CURRENT_BASE_URL;
export const setBaseURL = (url: string) => {

View File

@ -71,7 +71,7 @@ export const handleLogin = async (credentials: { email: string, password: string
try {
console.log("➡️ Calling API...");
const response = await axios.post(
`https://quality-friendly-goldfish.ngrok-free.app/api/v1/va/login`,
`${baseURL}/api/v1/va/login`,
{ userDetails: { ...credentials } },
{ headers: { "ngrok-skip-browser-warning": 69420 }, httpsAgent }
);

View File

@ -123,9 +123,9 @@ const App: React.FC = () => {
path="/login"
element={
<PublicRoute user={user}>
{/* <VpnGuard> */}
<VpnGuard>
<Login />
{/* </VpnGuard> */}
</VpnGuard>
</PublicRoute>
}
/>

View File

@ -1,5 +1,5 @@
import React, { useEffect, useState } from "react";
import { Shield, ShieldAlert, RefreshCw, Wifi } from "lucide-react";
import { Shield, ShieldAlert, RefreshCw, Wifi, Power } from "lucide-react";
interface Props {
children: React.ReactNode;
@ -8,6 +8,7 @@ interface Props {
const VpnGuard: React.FC<Props> = ({ children }) => {
const VPN_API = "http://10.0.0.2:3000";
const DEFAULT_API = "https://quality-friendly-goldfish.ngrok-free.app"
const [checking, setChecking] = useState(true);
const [vpnConnected, setVpnConnected] = useState(true);
@ -19,8 +20,10 @@ const VpnGuard: React.FC<Props> = ({ children }) => {
const res = await window.electron?.checkRegion();
console.log("VpnGuard", res);
setVpnConnected(false);
return
setVpnConnected(res?.success === true);
// setVpnConnected(res?.success === true);
} catch (error) {
console.error("VPN check failed:", error);
setVpnConnected(false);
@ -61,6 +64,27 @@ const VpnGuard: React.FC<Props> = ({ children }) => {
};
const disconnectVPN = async () => {
setChecking(true);
try {
const res = await window.electron.disconnectVPN();;
console.log("disconnectVPN →", res);
// Restore original base URL
const baseRes = await window.electron.updateBaseURL(DEFAULT_API);
console.log("baseRes disconnect →", baseRes);
setVpnConnected(true);
// await checkVPN();
} catch (error) {
console.log("Disconnect Error:", error);
} finally {
setChecking(false);
}
};
// Checking state
if (checking && !isRetrying) {
return (
@ -93,7 +117,8 @@ const VpnGuard: React.FC<Props> = ({ children }) => {
<div className="max-w-md w-full">
<div className="card bg-base-100 shadow-2xl">
<div className="card-body items-center text-center">
{/* Icon with animation */}
{/* Icon */}
<div className="relative mb-4">
<div className="absolute inset-0 flex items-center justify-center">
<div className="w-20 h-20 border-4 border-error opacity-20 rounded-full animate-pulse"></div>
@ -103,21 +128,16 @@ const VpnGuard: React.FC<Props> = ({ children }) => {
</div>
</div>
{/* Title */}
<h2 className="card-title text-3xl mb-3">
VPN Required
</h2>
{/* Description */}
<h2 className="card-title text-3xl mb-3">VPN Required</h2>
<p className="mb-6 opacity-80">
Please connect to your company VPN to access this application.
</p>
{/* Retry button */}
{/* Connect VPN */}
<button
className="btn btn-primary btn-wide gap-2"
onClick={connectVPN}
disabled={isRetrying}
disabled={isRetrying || vpnConnected} // Disable if retrying OR already connected
>
{isRetrying ? (
<>
@ -132,12 +152,21 @@ const VpnGuard: React.FC<Props> = ({ children }) => {
)}
</button>
{/* Help text */}
{/* Disconnect VPN (Disabled when NOT connected) */}
<button
className="btn btn-error btn-wide gap-2 mt-3"
onClick={disconnectVPN}
// disabled={!vpnConnected || checking} // Disable if not connected
>
<Power className="w-4 h-4" />
Disconnect VPN
</button>
<div className="divider"></div>
<p className="text-xs opacity-60">
Need help? Contact IT support at{" "}
<a href="mailto:support@company.com" className="link link-primary">
support@company.com
<a href="mailto:support@docva.com" className="link link-primary">
support@docva.com
</a>
</p>
</div>
@ -147,7 +176,12 @@ const VpnGuard: React.FC<Props> = ({ children }) => {
);
}
return <>{children}</>;
return (
<>
{children}
</>
);
};