forked from eycorsican/go-tun2socks
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// +build !windows | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func checkPermissions() error { | ||
if uid := os.Getuid(); uid != 0 { | ||
return fmt.Errorf("tun2socks needs to run as root") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// +build windows | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
const ( | ||
winTun = "wintun.dll" | ||
winTunSite = "https://www.wintun.net/" | ||
) | ||
|
||
func checkPermissions() error { | ||
// https://github.com/golang/go/issues/28804#issuecomment-505326268 | ||
var sid *windows.SID | ||
|
||
// https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-checktokenmembership | ||
err := windows.AllocateAndInitializeSid( | ||
&windows.SECURITY_NT_AUTHORITY, | ||
2, | ||
windows.SECURITY_BUILTIN_DOMAIN_RID, | ||
windows.DOMAIN_ALIAS_RID_ADMINS, | ||
0, 0, 0, 0, 0, 0, | ||
&sid) | ||
if err != nil { | ||
return fmt.Errorf("error while checking for elevated permissions: %s", err) | ||
} | ||
|
||
// We must free the sid to prevent security token leaks | ||
defer windows.FreeSid(sid) | ||
token := windows.Token(0) | ||
|
||
member, err := token.IsMember(sid) | ||
if err != nil { | ||
return fmt.Errorf("error while checking for elevated permissions: %s", err) | ||
} | ||
if !member { | ||
return fmt.Errorf("tun2socks needs to run with administrator permissions") | ||
} | ||
|
||
err = windows.NewLazyDLL(winTun).Load() | ||
if err != nil { | ||
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) | ||
if err != nil { | ||
dir = "tun2socks" | ||
} | ||
return fmt.Errorf("the %s was not found, you can download it from %s and place it into the %q directory", winTun, winTunSite, dir) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> | ||
<assemblyIdentity | ||
version="6.0.0.0" | ||
name="org.eycorsican.tun2socks" | ||
type="win32" | ||
/> | ||
<description>tun2socks requires Administrator privileges</description> | ||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> | ||
<security> | ||
<requestedPrivileges> | ||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> | ||
</requestedPrivileges> | ||
</security> | ||
</trustInfo> | ||
</assembly> |
Binary file not shown.