-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add low level sockets to bypass nekobox/android vpn
- Loading branch information
uoosef
committed
Aug 10, 2023
1 parent
128d44e
commit 0b427e8
Showing
7 changed files
with
160 additions
and
39 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
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
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,27 @@ | ||
package protect | ||
|
||
import ( | ||
"github.com/daeuniverse/softwind/netproxy" | ||
"github.com/daeuniverse/softwind/protocol/direct" | ||
) | ||
|
||
var protectPath string | ||
|
||
type ClientDialer struct { | ||
netproxy.Dialer | ||
} | ||
|
||
func NewClientDialer() *ClientDialer { | ||
protectPath = "protect_path" | ||
return &ClientDialer{ | ||
direct.SymmetricDirect, | ||
} | ||
} | ||
|
||
func (c *ClientDialer) Dial(network string, addr string) (netproxy.Conn, error) { | ||
magicNetwork := netproxy.MagicNetwork{ | ||
Network: network, | ||
Mark: 114514, | ||
} | ||
return c.Dialer.Dial(magicNetwork.Encode(), addr) | ||
} |
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,73 @@ | ||
package protect | ||
|
||
import ( | ||
"fmt" | ||
"syscall" | ||
|
||
"github.com/daeuniverse/softwind/netproxy" | ||
) | ||
|
||
func init() { | ||
soMark := netproxy.SoMark | ||
netproxy.SoMark = func(fd, mark int) error { | ||
if protectPath == "" { | ||
return soMark(fd, mark) | ||
} | ||
if err := protect(fd, protectPath); err != nil { | ||
return fmt.Errorf("protect failed: %w", err) | ||
} | ||
return nil | ||
} | ||
soMarkControl := netproxy.SoMarkControl | ||
netproxy.SoMarkControl = func(c syscall.RawConn, mark int) error { | ||
if protectPath == "" { | ||
return soMarkControl(c, mark) | ||
} | ||
var sockOptErr error | ||
controlErr := c.Control(func(fd uintptr) { | ||
err := protect(int(fd), protectPath) | ||
if err != nil { | ||
sockOptErr = fmt.Errorf("error setting SO_MARK socket option: %w", err) | ||
} | ||
}) | ||
if controlErr != nil { | ||
return fmt.Errorf("error invoking socket control function: %w", controlErr) | ||
} | ||
return sockOptErr | ||
} | ||
} | ||
|
||
func protect(fd int, unixPath string) error { | ||
if fd <= 0 { | ||
return nil | ||
} | ||
|
||
socket, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) | ||
if err != nil { | ||
return err | ||
} | ||
defer syscall.Close(socket) | ||
|
||
_ = syscall.SetsockoptTimeval(socket, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &syscall.Timeval{Sec: 3}) | ||
_ = syscall.SetsockoptTimeval(socket, syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, &syscall.Timeval{Sec: 3}) | ||
|
||
err = syscall.Connect(socket, &syscall.SockaddrUnix{Name: unixPath}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = syscall.Sendmsg(socket, nil, syscall.UnixRights(fd), nil, 0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dummy := []byte{1} | ||
n, err := syscall.Read(socket, dummy) | ||
if err != nil { | ||
return err | ||
} | ||
if n != 1 { | ||
return fmt.Errorf("protect failed") | ||
} | ||
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