Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add private address into route list and add a choice on multi-network system. #34

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions chnroutes-win.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@cls
@chnroutes.py -p win
148 changes: 141 additions & 7 deletions chnroutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,100 @@ def generate_mac(metric):

def generate_win(metric):
results = fetch_ip_data()

upscript_header=textwrap.dedent("""@echo off
for /F "tokens=3" %%* in ('route print ^| findstr "\\<0.0.0.0\\>"') do set "gw=%%*"

""")
upscript_header=textwrap.dedent(""":BOF
@echo off && cls
SETLOCAL EnableDelayedExpansion

REM Comfirm Admin Access
set UAC=0
bcdedit>nul
if errorlevel 1 set UAC=1
if %UAC%==1 (
color CE
set exitconf=N
echo Admin access not detected!
set /p exitconf=Input y to ignore this message:
if /i "!exitconf!" NEQ "Y" (goto :EOF)
)
REM Admin Access Comfirmed

color 0A
REM Select gateway if in multi network
SET N=0
CALL :GeneGWL
IF !N! == 0 (
echo No adapter detected!
CALL :InputGW
) else (
IF !N! == 1 (
SET gw=!gwl[0]!
) else (
CALL :SetGW
)
)
goto :SetRouteConfirm

:GeneGWL
echo Generating Gateway List...
SET N=0
FOR /F "tokens=3" %%* in ('route print ^| findstr "\\<0.0.0.0\\>"') do (
SET gwl[!N!]=%%*
SET /a N+=1
)
GOTO :EOF

:SetGW
REM DISPLAY
echo ----------------------------
SET i=0
:DisplayLoopStart
IF !i! EQU !N! GOTO :DisplayLoopEnd
FOR /f "usebackq delims== tokens=1-3" %%i IN (`SET gwl[!i!]`) do (
echo !i!. %%j
)
SET /a i+=1
GOTO DisplayLoopStart
:DisplayLoopEnd
echo x. Input custom ip address
echo ----------------------------
REM DISPLAY END

REM SELECT
SET /p gwi=Please select your gateway:
SET /a i=0

if !gwi! EQU x (GOTO InputGW && GOTO :EOF)
if !gwi! EQU X (GOTO InputGW && GOTO :EOF)
:SelectLoopStart
IF "!i!" EQU "!N!" GOTO :SelectLoopEnd
FOR /f "usebackq delims== tokens=1-3" %%i IN (`SET gwl[!i!]`) do (
IF "!i!" EQU "!gwi!" (
SET gw=%%j
GOTO SelectLoopEnd
)
)
SET /a i+=1
GOTO SelectLoopStart
:SelectLoopEnd
REM SELECT END
GOTO :EOF

:InputGW
SET /p gw=Please input gateway ip address:
GOTO :EOF

:SetRouteConfirm
set confirm=D
set /p confirm=Confirm your gateway as !gw! or input your gateway now: (Y/N/IP):
if /i "!confirm!" EQU "Y" GOTO :SetRoute
if /i "!confirm!" EQU "N" GOTO :BOF
if /i "!confirm!" EQU "D" GOTO :SetRouteConfirm
set gw=!confirm!
GOTO SetRouteConfirm
:SetRoute
echo ------------------------------------------------------------------------------
""")

upfile=open('vpnup.bat','w')
downfile=open('vpndown.bat','w')
Expand All @@ -132,12 +221,21 @@ def generate_win(metric):
upfile.write('\n')
upfile.write('ipconfig /flushdns\n\n')

downfile.write("@echo off")
downfile.write("@echo off && color 0A")
downfile.write('\n')

l, i, p = len(results), 1, -1
for ip,mask,_ in results:
upfile.write('route add %s mask %s %s metric %d\n'%(ip,mask,"%gw%",metric))
# display percentage
cp = i * 100.0 / l
if cp - p >= 0.1:
p = cp
upfile.write('title %.01f%%%%...\n' % (p))
downfile.write('title %.01f%%%%...\n' % (p))
# write route rules
upfile.write('route add %s mask %s %s metric %d\n'%(ip,mask,"!gw!",metric))
downfile.write('route delete %s\n'%(ip))
i = i + 1

upfile.close()
downfile.close()
Expand Down Expand Up @@ -224,7 +322,43 @@ def fetch_ip_data():
mask2=32-int(math.log(num_ip,2))

results.append((starting_ip,mask,mask2))


# private network
results.append(("10.0.0.0","255.0.0.0",8))
results.append(("172.16.0.0","255.240.0.0",12))
results.append(("192.168.0.0","255.255.0.0",16))

# custom setting
custom_text = ''
custom_file = open('custom.txt')
customregex = re.compile(r'(\d+\.\d+\.\d+\.\d+)[\s/]+(\d+)')
try:
custom_text = custom_file.read()
finally:
custom_file.close()
customdata = customregex.findall(custom_text)

for item in customdata:
string_ip = item[0]
num_mask = int(item[1])

imask = 0x0
for i in xrange(1, num_mask + 1):
imask |= 1<<(32-i)
#convert to string
imask=hex(imask)[2:]
mask=[0]*4
mask[0]=imask[0:2]
mask[1]=imask[2:4]
mask[2]=imask[4:6]
mask[3]=imask[6:8]

#convert str to int
mask=[ int(i,16 ) for i in mask]
mask="%d.%d.%d.%d"%tuple(mask)

results.append((string_ip,mask,num_mask))

return results


Expand Down
40 changes: 40 additions & 0 deletions custom.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
121.14.64.29/32
121.14.64.76/32
121.14.64.155/32
121.14.64.30/32
121.14.28.171/32
121.14.28.152/32
125.39.136.3/32
121.14.64.162/32
121.14.64.19/32
121.14.64.97/32
121.14.64.3/32
121.14.28.88/32
121.14.64.5/32
121.14.64.153/32
121.14.64.59/32
121.14.64.224/32
121.14.64.14/32
125.39.62.233/32
121.14.64.214/32
121.14.64.53/32
121.14.64.120/32
125.39.62.224/32
121.14.64.84/32
125.39.136.32/32
125.39.136.28/32
121.14.64.78/32
125.39.136.12/32
121.14.28.82/32
125.39.136.19/32
125.39.136.29/32
121.14.64.72/32
125.39.136.101/32
121.14.28.79/32
121.14.64.145/32
121.14.28.85/32
121.14.28.66/32
125.39.136.33/32
121.14.64.92/32
121.14.64.23/32
10.20.18.21/32