tun device private
This commit is contained in:
2 files changed
+10
-10
No files matched your search
@@ -11,13 +11,13 @@ import (
|
||||
"github.com/Dreamacro/clash/log"
|
||||
)
|
||||
|
||||
type TunDevice struct {
|
||||
type tunDevice struct {
|
||||
mtu int
|
||||
fd int
|
||||
linkCache *stack.LinkEndpoint
|
||||
}
|
||||
|
||||
func OpenTunDevice(fd int, mtu int) (*TunDevice, error) {
|
||||
func openTunDevice(fd int, mtu int) (*tunDevice, error) {
|
||||
var ifr struct {
|
||||
name [16]byte
|
||||
flags uint16
|
||||
@@ -35,13 +35,13 @@ func OpenTunDevice(fd int, mtu int) (*TunDevice, error) {
|
||||
|
||||
log.Infoln("Open tun fd = %d mtu = %d", fd, mtu)
|
||||
|
||||
return &TunDevice{
|
||||
return &tunDevice{
|
||||
fd: fd,
|
||||
mtu: mtu,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t TunDevice) AsLinkEndpoint() (result stack.LinkEndpoint, err error) {
|
||||
func (t tunDevice) asLinkEndpoint() (result stack.LinkEndpoint, err error) {
|
||||
if t.linkCache != nil {
|
||||
return *t.linkCache, nil
|
||||
}
|
||||
@@ -61,6 +61,6 @@ func (t TunDevice) AsLinkEndpoint() (result stack.LinkEndpoint, err error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (t TunDevice) Close() {
|
||||
func (t tunDevice) close() {
|
||||
syscall.Close(t.fd)
|
||||
}
|
||||
@@ -25,7 +25,7 @@ import (
|
||||
)
|
||||
|
||||
type tun struct {
|
||||
device *TunDevice
|
||||
device *tunDevice
|
||||
ipstack *stack.Stack
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func StopTunProxy() {
|
||||
}
|
||||
|
||||
func newTunProxy(fd int, mtu int) (*tun, error) {
|
||||
tundev, err := OpenTunDevice(fd, mtu)
|
||||
tundev, err := openTunDevice(fd, mtu)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Can't open tun: %v", err)
|
||||
}
|
||||
@@ -66,7 +66,7 @@ func newTunProxy(fd int, mtu int) (*tun, error) {
|
||||
TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol(), udp.NewProtocol()},
|
||||
})
|
||||
|
||||
linkEP, err := tundev.AsLinkEndpoint()
|
||||
linkEP, err := tundev.asLinkEndpoint()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to create virtual endpoint: %v", err)
|
||||
}
|
||||
@@ -120,14 +120,14 @@ func newTunProxy(fd int, mtu int) (*tun, error) {
|
||||
ipstack: ipstack,
|
||||
}
|
||||
|
||||
log.Infoln("Tun device opened")
|
||||
log.Infoln("Tun Adapter ready", fd, mtu)
|
||||
|
||||
return tl, nil
|
||||
}
|
||||
|
||||
// Close close the TunAdapter
|
||||
func (t *tun) close() {
|
||||
t.device.Close()
|
||||
t.device.close()
|
||||
t.ipstack.Close()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user