Skip to content

Commit

Permalink
opt: routingRules
Browse files Browse the repository at this point in the history
  • Loading branch information
ircfspace committed Apr 26, 2024
1 parent 06070b9 commit d800d51
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
7 changes: 7 additions & 0 deletions assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ body:before {
direction: ltr;
text-align: left;
}
.dirLeft[dir="rtl"] {
direction: rtl;
}
.dirRight {
direction: rtl;
text-align: right;
Expand Down Expand Up @@ -370,6 +373,10 @@ nav i.backBtn:hover {
}
.settings .item .key {
float: right;
width: 40%;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
font-size: 22px;
font-weight: 300;
color: #303030;
Expand Down
18 changes: 12 additions & 6 deletions src/renderer/components/Modal/RoutingRules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@ export default function RoutingRulesModal({
if (!isOpen) return null;
const [routingRulesInput, setRoutingRulesInput] = useState(routingRules);

const validateRules = (textareaContent:string) => {
const validateRules = (textareaContent: string) => {
if (textareaContent === "") {
return "";
}
const lines = textareaContent.split('\n');
const validEntriesSet = new Set(); // Use Set to store unique entries
const validEntriesSet = new Set();
const entryRegex = /^(geoip|regexp|domain|geosite):(.+)$/;
const ipRegex = /^([0-9]{1,3}\.){3}[0-9]{1,3}$/;
const ipRangeRegex = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}$/;
for (const line of lines) {
const trimmedLine = line.trim();
if (!trimmedLine) {
continue;
}
const lineWithoutQuotes = trimmedLine.replace(/"/g, '');
const lineWithoutQuotes = trimmedLine.replace(/['"]/g, '');
const entry = lineWithoutQuotes.endsWith(',') ? lineWithoutQuotes.slice(0, -1) : lineWithoutQuotes;
const cleanedEntry = entry.replace(/,+$/, '');
const match = cleanedEntry.match(entryRegex);
if (match) {
const ipMatch = cleanedEntry.match(ipRegex);
const ipRangeMatch = cleanedEntry.match(ipRangeRegex);
if (match || ipMatch || ipRangeMatch) {
validEntriesSet.add(cleanedEntry);
} else {
return false;
}
}
const validEntries = Array.from(validEntriesSet);
Expand All @@ -50,6 +52,10 @@ export default function RoutingRulesModal({
setRoutingRules(checkRules);
settings.set('routingRules', checkRules);
}
else {
setRoutingRules("");
settings.set('routingRules', "");
}
onClose();
};

Expand Down
15 changes: 15 additions & 0 deletions src/renderer/lib/toPersianNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const farsiDigits = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];

export const toPersianNumber = (
n: string | number | undefined,
doNotConvertFloat = false
): any => {
if (typeof n === "undefined") {
return "";
}
n = n.toString();
if (doNotConvertFloat && n.includes(".")) {
return n;
}
return n.replace(/\d/g, (x: any) => farsiDigits[x]);
};
17 changes: 13 additions & 4 deletions src/renderer/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { settings } from '../lib/settings';
import { defaultSettings } from '../../defaultSettings';
import Lottie from 'lottie-react';
import LottieFile from '../../../assets/json/1713988096625.json';
import { toPersianNumber } from '../lib/toPersianNumber';

export default function Settings() {
const [endpoint, setEndpoint] = useState();
Expand Down Expand Up @@ -61,6 +62,14 @@ export default function Settings() {
});
}, []);

const countRoutingRules = (value:any) => {
if (value === "") {
return 'غیرفعال';
}
const lines = value.split('\n');
return lines?.length > 0 ? toPersianNumber(lines.length)+' قانون' : 'غیرفعال';
};

if (
typeof psiphon === 'undefined' ||
typeof location === 'undefined' ||
Expand Down Expand Up @@ -119,7 +128,7 @@ export default function Settings() {
routingRules,
setRoutingRules,
}}
title='مسیریابی'
title='قوانین مسیریابی'
isOpen={showRoutingRulesModal}
onClose={() => {
setShowRoutingRulesModal(false);
Expand Down Expand Up @@ -259,11 +268,11 @@ export default function Settings() {
setShowRoutingRulesModal(true);
}}
>
<label className='key'>مسیریابی</label>
<label className='key'>لیست سیاه</label>
<div className='value'>
<span className='dirLeft'>{routingRules !== '' ? 'فعال' : 'غیرفعال'}</span>
<span className='dirLeft' dir="rtl">{countRoutingRules(routingRules)}</span>
</div>
<div className='info'>قوانین مسیریابی برای ترافیک مستقیم</div>
<div className='info'>جلوگیری از عبور ترافیک از وارپ</div>
</div>
</div>
<div className='moreSettings'>
Expand Down

0 comments on commit d800d51

Please sign in to comment.