mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-12 17:28:29 +03:00
https://github.com/XTLS/Xray-core/pull/6771#issuecomment-5752154561 https://github.com/XTLS/Xray-core/pull/6795#issuecomment-6095740152 https://github.com/XTLS/Xray-core/pull/7130#issuecomment-6116258188
94 lines
2.0 KiB
Go
94 lines
2.0 KiB
Go
//go:build android
|
|
|
|
package tun
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"strconv"
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/platform"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
type AndroidTun struct {
|
|
*fdTun
|
|
options *Config
|
|
}
|
|
|
|
// DefaultTun implements Tun
|
|
var _ Tun = (*AndroidTun)(nil)
|
|
|
|
// NewTun builds new tun interface handler
|
|
func NewTun(options *Config) (Tun, error) {
|
|
fd, err := strconv.Atoi(platform.NewEnvFlag(platform.TunFdKey).GetValue(func() string { return "0" }))
|
|
errors.LogInfo(context.Background(), "read Android Tun Fd ", fd, err)
|
|
|
|
err = unix.SetNonblock(fd, true)
|
|
if err != nil {
|
|
_ = unix.Close(fd)
|
|
return nil, err
|
|
}
|
|
|
|
// the descriptor stays with the application, Close ends the reading of a duplicate
|
|
fd, err = unix.FcntlInt(uintptr(fd), unix.F_DUPFD_CLOEXEC, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &AndroidTun{
|
|
fdTun: newFdTun(fd, 0),
|
|
options: options,
|
|
}, nil
|
|
}
|
|
|
|
func (t *AndroidTun) Start() error {
|
|
return nil
|
|
}
|
|
|
|
func (t *AndroidTun) Close() error {
|
|
return t.closeFile()
|
|
}
|
|
|
|
func (t *AndroidTun) Name() (string, error) {
|
|
ifr, err := unix.NewIfreq("")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err = unix.IoctlIfreq(t.fd, unix.TUNGETIFF, ifr); err != nil {
|
|
return "", err
|
|
}
|
|
return ifr.Name(), nil
|
|
}
|
|
|
|
func (t *AndroidTun) Index() (int, error) {
|
|
name, err := t.Name()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
iface, err := net.InterfaceByName(name)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return iface.Index, nil
|
|
}
|
|
|
|
func setinterface(network, address string, fd uintptr, iface *net.Interface) error {
|
|
return unix.BindToDevice(int(fd), iface.Name)
|
|
}
|
|
|
|
func findOutboundInterface(tunIndex int, fixedName string) (*net.Interface, error) {
|
|
if fixedName == "" {
|
|
return nil, errors.New("automatic outbound interface selection is not supported on this platform")
|
|
}
|
|
iface, err := net.InterfaceByName(fixedName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if iface.Index == tunIndex {
|
|
return nil, errors.New("outbound interface cannot be the TUN interface")
|
|
}
|
|
return iface, nil
|
|
}
|