@echo off
cls
SETLOCAL ENABLEDELAYEDEXPANSION

echo -----  S/Get Basic Info  -----
echo check paramter
if %3 EQU "" (
    echo parameter error
    goto batexit 
)

echo Get pppoe info
set pppoename=宽带连接
for /F %%a in ('cmd /c reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections" ') do (
    SET b=%%a
    SET j=!b:~0,4!
    if "!j!" NEQ "Defa" (
        if "!j!" NEQ "Save" (
            set regaccount=%%a
        )
    )
)
if "%regaccount%" NEQ ""  (
    if "%regaccount:~0,4%" NEQ "HKEY" (
        set pppoename=!regaccount!
    )
)

echo set param
set smbfolder=%~dp0
set proxyhostname=%1
set account=%2
set password=%3
set adminpass=c29N1Q5K073n6T446k
set desktop=%userprofile%\desktop
set deskclientfolder=%desktop%\client
set system=C:\Windows\System32
set jrelink=https://file.zhiweidata.com/tools/jre-8u311-windows-i586.tar.gz
set jrefolder=jre1.8.0_311
set jregz=jre-8u311-windows-i586.tar.gz
set jretar=jre-8u311-windows-i586.tar
set libfile=download_lib.txt

echo check file if exist
for /F %%i in ( "tar.exe wget.exe gzip.exe %libfile%" ) do (
    if NOT EXIST "%smbfolder%\%%i" (
        echo ERROR:not exist %%i
        goto batexit
    )
)

echo.
echo redirect folder: %smbfolder%
echo Download Link: %jrelink%
echo Default Proxy File: %jregz%
echo Proxy Hostname: %proxyhostname%
echo PPPOE Name: %pppoename%
echo PPPOE Account: %account%
echo PPPOE Password: %password%

echo.
echo.
echo ------  PPPOE ------
echo.
:PppoeConnect
echo.
echo 断开 %pppoename%  %account% %password%
rasdial %pppoename% /disconnect
echo.
echo 连接 %pppoename%  %account% %password%
rasdial %pppoename% %account% %password%
echo.
echo check internet
ping 114.114.114.114 -n 4 | findstr TTL >nul
if "%errorlevel%" EQU "1" (
    echo ping 114.114.114.114 失败
    ping www.baiud.com -n 4 | findstr TTL >nul
    if "%errorlevel%" EQU "1" (
        echo ping www.baidu.com 失败
        TIMEOUT >nul 2>nul /T 20
        echo 20秒后重新连接
        goto PppoeConnect
    )
)


echo.
echo.
echo -----  download lib  -----
echo.
copy /y  %smbfolder%\*.exe %system% 2>nul
copy /y  %smbfolder%\*.dll %system% 2>nul
copy /y  %smbfolder%\*.dll C:\Windows\SysWOW64 2>nul
mkdir %deskclientfolder%\lib 2>nul
cd %deskclientfolder%\lib
%system%\wget.exe -q -N -i %smbfolder%\%libfile% --no-check-certificate
move %deskclientfolder%\lib\natvps-proxy-client* %deskclientfolder%
del %deskclientfolder%\lib\*.1 2>nul
del %deskclientfolder%\lib\*.2 2>nul


echo.
echo.
echo -----  download and decompress jre  -----
echo.
cd %desktop% 
rmdir /q /s %jrefolder% 2>nul
echo get %jregz%
mkdir "%ProgramFiles%"\JAVA 2>nul
%system%\wget.exe -q -N %jrelink% --no-check-certificate

rem 存在jre压缩包
if EXIST %jregz% (
    goto jre_exsit
)
%system%\wget.exe -q -N %jrelink% --no-check-certificate
if NOT EXIST %jregz% (
    echo ERROR: not exist %jregz%
    goto batexit
)
:jre_exsit
if EXIST "C:\Program Files\JAVA\jre1.8.0_311\bin\java.exe" (
goto ready_init_client
)

echo decompress %jregz%
%system%\gzip.exe -f -d -c %jregz%|%system%\tar.exe -x
if NOT EXIST %jrefolder% (
    echo ERROR: not exist %jrefolder% folder
    goto batexit
) 

echo test java command
%jrefolder%\bin\java.exe -version
if NOT EXIST %jrefolder%\bin\java.exe (
    echo ERROR: java 
    goto batexit
)
move %jrefolder%  "%ProgramFiles%"\JAVA
erase %desktop%\%jregz%

:ready_init_client
echo.
echo.
echo -----  ready init client  -----
echo.

echo create run.bat
(
echo @echo off 
echo cd C:\Users\Administrator\Desktop\client 
echo java -Xmx100m -jar C:\Users\Administrator\Desktop\client\natvps-proxy-client-0.0.1-SNAPSHOT.jar -id %%1 -host 202.107.192.94 -port 50000 -title %%2 -username %%3 -password %%4 -socks5Port 21212 -group %%5 -serverUrl https://crawler.zhiweidata.com/crawlerPannel/exposed/natvps/server 
echo pause 
) > %deskclientfolder%\run.bat


echo create kill_start.bat
erase %deskclientfolder%\kill_start.bat 2>nul
echo @echo off  >> %deskclientfolder%\kill_start.bat
echo for ^/F %%%%a in ('wmic process where ^^(Name^^="java.exe"^^^)get ParentProcessId ^^^| findstr "[0-9]"') do ^( taskkill /f /pid %%%%a ^) >> %deskclientfolder%\kill_start.bat
echo taskkill /f /im java.exe >> %deskclientfolder%\kill_start.bat
echo C:\Users\Administrator\Desktop\client\start.bat >> %deskclientfolder%\kill_start.bat


echo create start.bat
echo windows-%proxyhostname% %pppoename% %account% %password%
(
echo @C:\Users\Administrator\Desktop\client\run.bat windows-%proxyhostname% %pppoename% %account% %password% 
)> %deskclientfolder%\start.bat
@echo off
(
echo Windows Registry Editor Version 5.00
echo.
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters]
echo "AutoShareWks"=dword:00000001
echo.
echo "AutoShareServer"=dword:00000001
echo.
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa]
echo "restrictanonymous"=dword:00000001
echo.
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup]
echo.
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0]
echo "GPO-ID"="LocalGPO"
echo "SOM-ID"="Local"
echo "FileSysPath"="C:\\Windows\\System32\\GroupPolicy\\Machine"
echo "DisplayName"="本地组策略"
echo "GPOName"="本地组策略"
echo "PSScriptOrder"=dword:00000001
echo.
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Startup\0\0]
echo "Script"="C:\\Users\\Administrator\\Desktop\\client\\start.bat"
echo "Parameters"=""
echo "IsPowershell"=dword:00000000
echo "ExecTime"=hex^(b^):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
echo.
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts]
echo.
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown]
echo.
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup]
echo.
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0]
echo "GPO-ID"="LocalGPO"
echo "SOM-ID"="Local"
echo "FileSysPath"="C:\\Windows\\System32\\GroupPolicy\\Machine"
echo "DisplayName"="本地组策略"
echo "GPOName"="本地组策略"
echo "PSScriptOrder"=dword:00000001
echo.
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0\0]
echo "Script"="C:\\Users\\Administrator\\Desktop\\clinet\\start.bat"
echo "Parameters"=""
echo "ExecTime"=hex^(b^):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
echo "ErrorCode"=dword:00000002
echo.
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
echo "Path"=hex^(2^):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
echo  00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,3b,00,25,00,\
echo  53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,00,3b,00,25,\
echo  00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,00,5c,00,\
echo  53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,57,00,62,00,65,00,6d,\
echo  00,3b,00,25,00,53,00,59,00,53,00,54,00,45,00,4d,00,52,00,4f,00,4f,00,54,00,\
echo  25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,57,00,69,\
echo  00,6e,00,64,00,6f,00,77,00,73,00,50,00,6f,00,77,00,65,00,72,00,53,00,68,00,\
echo  65,00,6c,00,6c,00,5c,00,76,00,31,00,2e,00,30,00,5c,00,3b,00,43,00,3a,00,5c,\
echo  00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,20,00,46,00,69,00,6c,00,65,00,\
echo  73,00,5c,00,4a,00,41,00,56,00,41,00,5c,00,6a,00,72,00,65,00,31,00,2e,00,38,\
echo  00,2e,00,30,00,5f,00,33,00,31,00,31,00,5c,00,62,00,69,00,6e,00,00,00
echo.
) >> %desktop%\reg.reg
regedit /s %desktop%\reg.reg
erase %desktop%\reg.reg

echo.
echo.
echo -----  serveice and account set  -----
echo.
echo new administrator'password is %adminpass%
net user Administrator %adminpass%
sc config mpssvc start= disabled
sc config LanmanServer start= auto

echo.
echo.
echo -----  init will over  -----
echo.
echo system will reboot at 0s later

shutdown -r -t 0
msg * everthing is ok!
exit /b 0
:batexit
exit /b 1

