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

在 wsl 中使用指定的 Linux 发行版. #335

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion etc/smartdns/smartdns.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# dns server name, defaut is host name
# dns server name, default is host name
# server-name,
# example:
# server-name smartdns
Expand Down
18 changes: 16 additions & 2 deletions package/windows/install.bat
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
@echo off
set "CURR_PATH=%~dp0"
set "STARTUP_PATH=%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
set "StartMenuDir=%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs"
FOR /F %%i IN ('wsl pwd') DO @set DIR_IN_WSL=%%i

wsl sudo %DIR_IN_WSL%/../../install -i
bash "$(wslpath -u "%CURR_PATH%\script\init.sh")"
IF %ERRORLEVEL% == 0 (
wsl sudo %DIR_IN_WSL%/../../install -i
)^
ELSE IF %ERRORLEVEL% == 10 (
wsl -d ubuntu sudo %DIR_IN_WSL%/../../install -i
)^
ELSE echo error:%ERRORLEVEL%

IF NOT %ERRORLEVEL% == 0 (
echo Install smartdns failed.
pause
exit 1
)

copy %CURR_PATH%\wsl-run.vbs "%STARTUP_PATH%/"
md "%StartMenuDir%\Smartdns"
copy %CURR_PATH%\script\init.sh "%StartMenuDir%\smartdns\"

call %CURR_PATH%\script\startmenu.vbs

copy %CURR_PATH%\wsl-run.vbs "%STARTUP_PATH%\"
IF NOT %ERRORLEVEL% == 0 (
echo Install startupt script failed.
pause
Expand Down
17 changes: 10 additions & 7 deletions package/windows/reload.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ set "CURR_PATH=%~dp0"
set "STARTUP_PATH=%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
FOR /F %%i IN ('wsl pwd') DO @set DIR_IN_WSL=%%i

wsl sudo cp -avf %DIR_IN_WSL%/../../etc/smartdns/* /etc/smartdns/
IF NOT %ERRORLEVEL% == 0 (
echo copy smartdns configuration file failed.
pause
exit 1
)
bash "$(wslpath -u "%CURR_PATH%\script\init.sh")"
IF %ERRORLEVEL% == 0 (
wsl sudo cp -avf %DIR_IN_WSL%/../../etc/smartdns/* /etc/smartdns/
wsl sudo /etc/init.d/smartdns restart
)^
ELSE IF %ERRORLEVEL% == 10 (
wsl -d ubuntu sudo cp -avf %DIR_IN_WSL%/../../etc/smartdns/* /etc/smartdns/
wsl -d ubuntu sudo /etc/init.d/smartdns restart
)^
ELSE echo error:%ERRORLEVEL%

wsl sudo /etc/init.d/smartdns restart
IF NOT %ERRORLEVEL% == 0 (
echo reload smartdns failed.
pause
Expand Down
19 changes: 19 additions & 0 deletions package/windows/script/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

source /etc/os-release
case $ID in
debian | ubuntu)
echo default wsl distro is debian or ubuntu.
echo
echo using default distro.
echo
exit 0
;;
*)
echo default wsl distro is not debian or ubuntu.
echo
echo try to use ubuntu now.
echo
exit 10
;;
esac
10 changes: 10 additions & 0 deletions package/windows/script/restart.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@echo off
bash "$(wslpath -u "%~dp0\init.sh")"
IF %ERRORLEVEL% == 0 (
wsl sudo /etc/init.d/smartdns restart
)^
ELSE IF %ERRORLEVEL% == 10 (
wsl -d ubuntu sudo /etc/init.d/smartdns restart
)^
ELSE echo error:%ERRORLEVEL%
pause
27 changes: 27 additions & 0 deletions package/windows/script/startmenu.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Set ws = WScript.CreateObject("WScript.Shell")
Set fs = WScript.CreateObject("Scripting.FileSystemObject")

CURR_PATH =fs.GetParentFolderName(WScript.ScriptFullName)
USERPROFILE = ws.ExpandEnvironmentStrings("%USERPROFILE%")
SmartdnsDir = USERPROFILE + "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\smartdns"

Set lnk = ws.CreateShortcut(SmartdnsDir & "\restart.lnk")
lnk.TargetPath = CURR_PATH +"\restart.bat"
lnk.WindowStyle = "1"
lnk.WorkingDirectory = CURR_PATH
lnk.Save
Set lnk = Nothing

Set lnk = ws.CreateShortcut(SmartdnsDir & "\stop.lnk")
lnk.TargetPath = CURR_PATH +"\stop.bat"
lnk.WindowStyle = "1"
lnk.WorkingDirectory = CURR_PATH
lnk.Save
Set lnk = Nothing

Set lnk = ws.CreateShortcut(SmartdnsDir & "\test.lnk")
lnk.TargetPath = CURR_PATH +"\test.bat"
lnk.WindowStyle = "1"
lnk.WorkingDirectory = CURR_PATH
lnk.Save
Set lnk = Nothing
10 changes: 10 additions & 0 deletions package/windows/script/stop.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@echo off
bash "$(wslpath -u "%~dp0\init.sh")"
IF %ERRORLEVEL% == 0 (
wsl sudo /etc/init.d/smartdns stop
)^
ELSE IF %ERRORLEVEL% == 10 (
wsl -d ubuntu sudo /etc/init.d/smartdns stop
)^
ELSE echo error:%ERRORLEVEL%
pause
2 changes: 2 additions & 0 deletions package/windows/script/test.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nslookup -querytype=ptr 127.0.0.1
nslookup -querytype=ptr 0.0.0.0
17 changes: 15 additions & 2 deletions package/windows/uninstall.bat
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
@echo off
set "CURR_PATH=%~dp0"
set "STARTUP_PATH=%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
set "StartMenuDir=%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs"
FOR /F %%i IN ('wsl pwd') DO @set DIR_IN_WSL=%%i

wsl sudo %DIR_IN_WSL%/../../install -u
bash "$(wslpath -u "%CURR_PATH%\script\init.sh")"
IF %ERRORLEVEL% == 0 (
wsl sudo %DIR_IN_WSL%/../../install -u
)^
ELSE IF %ERRORLEVEL% == 10 (
wsl -d ubuntu sudo %DIR_IN_WSL%/../../install -u
)^
ELSE echo error:%ERRORLEVEL%

IF NOT %ERRORLEVEL% == 0 (
echo Uninstall smartdns failed.
pause
exit 1
)

del "%StartMenuDir%\smartdns\init.sh"
del "%StartMenuDir%\smartdns\restart.lnk"
del "%StartMenuDir%\smartdns\stop.lnk"
del "%StartMenuDir%\smartdns\test.lnk"

del "%STARTUP_PATH%\wsl-run.vbs"
IF NOT %ERRORLEVEL% == 0 (
echo Uninstall startup script failed.
Expand All @@ -18,5 +32,4 @@ IF NOT %ERRORLEVEL% == 0 (
)

echo uninstall success

pause
18 changes: 17 additions & 1 deletion package/windows/wsl-run.vbs
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
Set ws = WScript.CreateObject("WScript.Shell")
ws.run "wsl sudo /etc/init.d/smartdns restart", vbhide

USERPROFILE = ws.ExpandEnvironmentStrings("%USERPROFILE%")
initScriptPath = Chr(34) + USERPROFILE + "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\smartdns\init.sh" + Chr(34)
tmp = replace(initScriptPath ,"\", "/")
unixPath = replace(tmp ,"C:", "/mnt/c")

command = "bash " + unixPath

returnCode = ws.run(command,0,True)

if returnCode = 0 Then
ws.run "wsl sudo /etc/init.d/smartdns restart", vbhide
elseif returnCode = 10 Then
ws.run "wsl -d ubuntu sudo /etc/init.d/smartdns restart", vbhide
else
WScript.Echo "Start smartdns failed"
End If