xjasonlyu
4 years ago
9 changed files with 16 additions and 141 deletions
@ -0,0 +1,5 @@ |
|||
// +build darwin freebsd openbsd
|
|||
|
|||
package tun |
|||
|
|||
const offset = 4 /* 4 bytes TUN_PI */ |
@ -0,0 +1,3 @@ |
|||
package tun |
|||
|
|||
const offset = 0 |
@ -1,13 +0,0 @@ |
|||
package tun |
|||
|
|||
func WithComponentID(componentID string) Option { |
|||
return func(t *TUN) { |
|||
t.componentID = componentID |
|||
} |
|||
} |
|||
|
|||
func WithNetwork(network string) Option { |
|||
return func(t *TUN) { |
|||
t.network = network |
|||
} |
|||
} |
@ -1,65 +0,0 @@ |
|||
package tun |
|||
|
|||
import ( |
|||
"fmt" |
|||
|
|||
"github.com/xjasonlyu/tun2socks/device" |
|||
"github.com/xjasonlyu/tun2socks/device/rwbased" |
|||
|
|||
"github.com/songgao/water" |
|||
) |
|||
|
|||
const defaultMTU = 1500 |
|||
|
|||
type TUN struct { |
|||
*rwbased.Endpoint |
|||
|
|||
iface *water.Interface |
|||
mtu uint32 |
|||
name string |
|||
|
|||
// windows only
|
|||
componentID string |
|||
network string |
|||
} |
|||
|
|||
func Open(opts ...Option) (device.Device, error) { |
|||
t := &TUN{} |
|||
|
|||
for _, opt := range opts { |
|||
opt(t) |
|||
} |
|||
|
|||
iface, err := water.New(water.Config{ |
|||
DeviceType: water.TUN, |
|||
PlatformSpecificParams: water.PlatformSpecificParams{ |
|||
ComponentID: t.componentID, |
|||
InterfaceName: t.name, |
|||
Network: t.network, |
|||
}, |
|||
}) |
|||
if err != nil { |
|||
return nil, fmt.Errorf("create tun: %w", err) |
|||
} |
|||
t.iface = iface |
|||
|
|||
if t.mtu == 0 { |
|||
t.mtu = defaultMTU |
|||
} |
|||
|
|||
ep, err := rwbased.New(iface, t.mtu) |
|||
if err != nil { |
|||
return nil, fmt.Errorf("create endpoint: %w", err) |
|||
} |
|||
t.Endpoint = ep |
|||
|
|||
return t, nil |
|||
} |
|||
|
|||
func (t *TUN) Name() string { |
|||
return t.name |
|||
} |
|||
|
|||
func (t *TUN) Close() error { |
|||
return t.iface.Close() |
|||
} |
@ -1,15 +0,0 @@ |
|||
// +build !windows
|
|||
|
|||
package engine |
|||
|
|||
import ( |
|||
"net/url" |
|||
|
|||
"github.com/xjasonlyu/tun2socks/device" |
|||
"github.com/xjasonlyu/tun2socks/device/tun" |
|||
) |
|||
|
|||
func openTUN(u *url.URL, mtu uint32) (device.Device, error) { |
|||
name := u.Host |
|||
return tun.Open(tun.WithName(name), tun.WithMTU(mtu)) |
|||
} |
@ -1,33 +0,0 @@ |
|||
package engine |
|||
|
|||
import ( |
|||
"net/url" |
|||
|
|||
"github.com/xjasonlyu/tun2socks/device" |
|||
"github.com/xjasonlyu/tun2socks/device/tun" |
|||
) |
|||
|
|||
func openTUN(u *url.URL, mtu uint32) (device.Device, error) { |
|||
/* |
|||
e.g. tun://TUN0/?id=tap0901&network=10.10.10.10/24
|
|||
*/ |
|||
|
|||
name := u.Host |
|||
|
|||
componentID := u.Query().Get("id") |
|||
network := u.Query().Get("network") |
|||
|
|||
if componentID == "" { |
|||
componentID = "tap0901" /* default */ |
|||
} |
|||
if network == "" { |
|||
network = "10.10.10.10/24" /* default */ |
|||
} |
|||
|
|||
return tun.Open( |
|||
tun.WithName(name), |
|||
tun.WithMTU(mtu), |
|||
tun.WithComponentID(componentID), |
|||
tun.WithNetwork(network), |
|||
) |
|||
} |
Loading…
Reference in new issue