change tun from water to wireguard
This commit is contained in:
@ -2,15 +2,15 @@ package tun
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
"github.com/songgao/water"
|
||||
)
|
||||
|
||||
type conn struct {
|
||||
ifce *water.Interface
|
||||
ifce io.ReadWriteCloser
|
||||
laddr net.Addr
|
||||
raddr net.Addr
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ func (l *tunListener) Init(md mdata.Metadata) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
ifce, ip, err := l.createTun()
|
||||
ifce, name, ip, err := l.createTun()
|
||||
if err != nil {
|
||||
if ifce != nil {
|
||||
ifce.Close()
|
||||
@ -58,7 +58,7 @@ func (l *tunListener) Init(md mdata.Metadata) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
itf, err := net.InterfaceByName(ifce.Name())
|
||||
itf, err := net.InterfaceByName(name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
57
listener/tun/tun.go
Normal file
57
listener/tun/tun.go
Normal file
@ -0,0 +1,57 @@
|
||||
package tun
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/go-gost/core/common/bufpool"
|
||||
"golang.zx2c4.com/wireguard/tun"
|
||||
)
|
||||
|
||||
const (
|
||||
tunOffsetBytes = 4
|
||||
)
|
||||
|
||||
type tunDevice struct {
|
||||
dev tun.Device
|
||||
}
|
||||
|
||||
func (d *tunDevice) Read(p []byte) (n int, err error) {
|
||||
b := bufpool.Get(tunOffsetBytes + 65535)
|
||||
defer bufpool.Put(b)
|
||||
|
||||
n, err = d.dev.Read(*b, tunOffsetBytes)
|
||||
if n <= tunOffsetBytes || err != nil {
|
||||
d.dev.Flush()
|
||||
if n <= tunOffsetBytes {
|
||||
err = io.EOF
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
n = copy(p, (*b)[tunOffsetBytes:tunOffsetBytes+n])
|
||||
return
|
||||
}
|
||||
|
||||
func (d *tunDevice) Write(p []byte) (n int, err error) {
|
||||
b := bufpool.Get(tunOffsetBytes + len(p))
|
||||
defer bufpool.Put(b)
|
||||
|
||||
copy((*b)[tunOffsetBytes:], p)
|
||||
return d.dev.Write(*b, tunOffsetBytes)
|
||||
}
|
||||
|
||||
func (d *tunDevice) Close() error {
|
||||
return d.dev.Close()
|
||||
}
|
||||
|
||||
func (l *tunListener) createTunDevice() (dev io.ReadWriteCloser, name string, err error) {
|
||||
ifce, err := tun.CreateTUN(l.md.config.Name, l.md.config.MTU)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
dev = &tunDevice{dev: ifce}
|
||||
name, err = ifce.Name()
|
||||
|
||||
return
|
||||
}
|
@ -2,23 +2,21 @@ package tun
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
tun_util "github.com/go-gost/x/internal/util/tun"
|
||||
"github.com/songgao/water"
|
||||
)
|
||||
|
||||
func (l *tunListener) createTun() (ifce *water.Interface, ip net.IP, err error) {
|
||||
func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.IP, err error) {
|
||||
ip, _, err = net.ParseCIDR(l.md.config.Net)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ifce, err = water.New(water.Config{
|
||||
DeviceType: water.TUN,
|
||||
})
|
||||
ifce, name, err = l.createTunDevice()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -27,9 +25,8 @@ func (l *tunListener) createTun() (ifce *water.Interface, ip net.IP, err error)
|
||||
if peer == "" {
|
||||
peer = ip.String()
|
||||
}
|
||||
|
||||
cmd := fmt.Sprintf("ifconfig %s inet %s %s mtu %d up",
|
||||
ifce.Name(), l.md.config.Net, l.md.config.Peer, l.md.config.MTU)
|
||||
name, l.md.config.Net, l.md.config.Peer, l.md.config.MTU)
|
||||
l.logger.Debug(cmd)
|
||||
|
||||
args := strings.Split(cmd, " ")
|
||||
@ -37,7 +34,7 @@ func (l *tunListener) createTun() (ifce *water.Interface, ip net.IP, err error)
|
||||
return
|
||||
}
|
||||
|
||||
if err = l.addRoutes(ifce.Name(), l.md.config.Routes...); err != nil {
|
||||
if err = l.addRoutes(name, l.md.config.Routes...); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -2,38 +2,40 @@ package tun
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
tun_util "github.com/go-gost/x/internal/util/tun"
|
||||
"github.com/songgao/water"
|
||||
)
|
||||
|
||||
func (l *tunListener) createTun() (ifce *water.Interface, ip net.IP, err error) {
|
||||
ifce, err = water.New(water.Config{
|
||||
DeviceType: water.TUN,
|
||||
PlatformSpecificParams: water.PlatformSpecificParams{
|
||||
Name: l.md.config.Name,
|
||||
},
|
||||
})
|
||||
func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.IP, err error) {
|
||||
ip, _, err = net.ParseCIDR(l.md.config.Net)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = l.exeCmd(fmt.Sprintf("ip link set dev %s mtu %d", ifce.Name(), l.md.config.MTU)); err != nil {
|
||||
ifce, name, err = l.createTunDevice()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
if err = l.exeCmd(fmt.Sprintf("ip link set dev %s mtu %d", name, l.md.config.MTU)); err != nil {
|
||||
l.logger.Warn(err)
|
||||
}
|
||||
*/
|
||||
|
||||
if err = l.exeCmd(fmt.Sprintf("ip address add %s dev %s", l.md.config.Net, name)); err != nil {
|
||||
l.logger.Warn(err)
|
||||
}
|
||||
|
||||
if err = l.exeCmd(fmt.Sprintf("ip address add %s dev %s", l.md.config.Net, ifce.Name())); err != nil {
|
||||
if err = l.exeCmd(fmt.Sprintf("ip link set dev %s up", name)); err != nil {
|
||||
l.logger.Warn(err)
|
||||
}
|
||||
|
||||
if err = l.exeCmd(fmt.Sprintf("ip link set dev %s up", ifce.Name())); err != nil {
|
||||
l.logger.Warn(err)
|
||||
}
|
||||
|
||||
if err = l.addRoutes(ifce.Name(), l.md.config.Routes...); err != nil {
|
||||
if err = l.addRoutes(name, l.md.config.Routes...); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -4,29 +4,27 @@ package tun
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
tun_util "github.com/go-gost/x/internal/util/tun"
|
||||
"github.com/songgao/water"
|
||||
)
|
||||
|
||||
func (l *tunListener) createTun() (ifce *water.Interface, ip net.IP, err error) {
|
||||
func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.IP, err error) {
|
||||
ip, _, err = net.ParseCIDR(l.md.config.Net)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ifce, err = water.New(water.Config{
|
||||
DeviceType: water.TUN,
|
||||
})
|
||||
ifce, name, err = l.createTunDevice()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
cmd := fmt.Sprintf("ifconfig %s inet %s mtu %d up",
|
||||
ifce.Name(), l.md.config.Net, l.md.config.MTU)
|
||||
name, l.md.config.Net, l.md.config.MTU)
|
||||
l.logger.Debug(cmd)
|
||||
|
||||
args := strings.Split(cmd, " ")
|
||||
@ -35,7 +33,7 @@ func (l *tunListener) createTun() (ifce *water.Interface, ip net.IP, err error)
|
||||
return
|
||||
}
|
||||
|
||||
if err = l.addRoutes(ifce.Name(), l.md.config.Routes...); err != nil {
|
||||
if err = l.addRoutes(name, l.md.config.Routes...); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -2,35 +2,28 @@ package tun
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
tun_util "github.com/go-gost/x/internal/util/tun"
|
||||
"github.com/songgao/water"
|
||||
)
|
||||
|
||||
func (l *tunListener) createTun() (ifce *water.Interface, ip net.IP, err error) {
|
||||
func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.IP, err error) {
|
||||
ip, ipNet, err := net.ParseCIDR(l.md.config.Net)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ifce, err = water.New(water.Config{
|
||||
DeviceType: water.TUN,
|
||||
PlatformSpecificParams: water.PlatformSpecificParams{
|
||||
ComponentID: "tap0901",
|
||||
InterfaceName: l.md.config.Name,
|
||||
Network: l.md.config.Net,
|
||||
},
|
||||
})
|
||||
ifce, name, err = l.createTunDevice()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
|
||||
"source=static addr=%s mask=%s gateway=none",
|
||||
ifce.Name(), ip.String(), ipMask(ipNet.Mask))
|
||||
name, ip.String(), ipMask(ipNet.Mask))
|
||||
l.logger.Debug(cmd)
|
||||
|
||||
args := strings.Split(cmd, " ")
|
||||
@ -39,7 +32,7 @@ func (l *tunListener) createTun() (ifce *water.Interface, ip net.IP, err error)
|
||||
return
|
||||
}
|
||||
|
||||
if err = l.addRoutes(ifce.Name(), l.md.config.Gateway, l.md.config.Routes...); err != nil {
|
||||
if err = l.addRoutes(name, l.md.config.Gateway, l.md.config.Routes...); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user