mirror of
https://github.com/net-byte/vtun
synced 2024-03-14 10:50:03 +08:00
fix ipv6
This commit is contained in:
parent
0da8a5d486
commit
f2a06dd324
@ -52,6 +52,7 @@ func (app *App) InitConfig() {
|
||||
app.Config.LocalGateway = netutil.DiscoverGateway(false)
|
||||
}
|
||||
}
|
||||
app.Config.BufferSize = 64 * 1024
|
||||
cipher.SetKey(app.Config.Key)
|
||||
app.Iface = tun.CreateTun(*app.Config)
|
||||
log.Printf("initialized config: %+v", app.Config)
|
||||
|
@ -24,4 +24,5 @@ type Config struct {
|
||||
TLSCertificateKeyFilePath string
|
||||
TLSSni string
|
||||
TLSInsecureSkipVerify bool
|
||||
BufferSize int
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ func StartClient(iface *water.Interface, config config.Config) {
|
||||
|
||||
// tunToGrpc sends packets from tun to grpc
|
||||
func tunToGrpc(config config.Config, iface *water.Interface) {
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, config.BufferSize)
|
||||
for {
|
||||
n, err := iface.Read(packet)
|
||||
if err != nil || n == 0 {
|
||||
|
@ -54,7 +54,7 @@ func StartServer(iface *water.Interface, config config.Config) {
|
||||
|
||||
// toClient sends packets from tun to grpc
|
||||
func toClient(config config.Config, iface *water.Interface) {
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, config.BufferSize)
|
||||
for {
|
||||
n, err := iface.Read(packet)
|
||||
if err != nil || n == 0 {
|
||||
|
@ -39,7 +39,7 @@ func StartClient(iface *water.Interface, config config.Config) {
|
||||
|
||||
// tunToTLS sends packets from tun to tls
|
||||
func tunToTLS(config config.Config, iface *water.Interface) {
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, config.BufferSize)
|
||||
for {
|
||||
n, err := iface.Read(packet)
|
||||
if err != nil || n == 0 {
|
||||
@ -67,7 +67,7 @@ func tunToTLS(config config.Config, iface *water.Interface) {
|
||||
// tlsToTun sends packets from tls to tun
|
||||
func tlsToTun(config config.Config, tlsconn net.Conn, iface *water.Interface) {
|
||||
defer tlsconn.Close()
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, config.BufferSize)
|
||||
for {
|
||||
tlsconn.SetReadDeadline(time.Now().Add(time.Duration(config.Timeout) * time.Second))
|
||||
n, err := tlsconn.Read(packet)
|
||||
|
@ -53,7 +53,7 @@ func StartServer(iface *water.Interface, config config.Config) {
|
||||
|
||||
// toClient sends packets from iface to tlsconn
|
||||
func toClient(config config.Config, iface *water.Interface) {
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, config.BufferSize)
|
||||
for {
|
||||
n, err := iface.Read(packet)
|
||||
if err != nil || err == io.EOF || n == 0 {
|
||||
@ -78,7 +78,7 @@ func toClient(config config.Config, iface *water.Interface) {
|
||||
// toServer sends packets from tlsconn to iface
|
||||
func toServer(config config.Config, tlsconn net.Conn, iface *water.Interface) {
|
||||
defer tlsconn.Close()
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, config.BufferSize)
|
||||
for {
|
||||
tlsconn.SetReadDeadline(time.Now().Add(time.Duration(config.Timeout) * time.Second))
|
||||
n, err := tlsconn.Read(packet)
|
||||
|
26
tun/tun.go
26
tun/tun.go
@ -14,15 +14,19 @@ import (
|
||||
// CreateTun creates a tun interface
|
||||
func CreateTun(config config.Config) (iface *water.Interface) {
|
||||
c := water.Config{DeviceType: water.TUN}
|
||||
network := config.CIDR
|
||||
serverAddrIP := netutil.LookupServerAddrIP(config.ServerAddr)
|
||||
if serverAddrIP.To4() == nil {
|
||||
network = config.CIDRv6
|
||||
}
|
||||
c.PlatformSpecificParams = water.PlatformSpecificParams{}
|
||||
os := runtime.GOOS
|
||||
if os == "windows" {
|
||||
c.PlatformSpecificParams.Name = "vtun"
|
||||
c.PlatformSpecificParams.Network = network
|
||||
}
|
||||
if config.DeviceName != "" {
|
||||
c.PlatformSpecificParams = water.PlatformSpecificParams{Name: config.DeviceName, Network: config.CIDR}
|
||||
} else {
|
||||
os := runtime.GOOS
|
||||
if os == "windows" {
|
||||
c.PlatformSpecificParams = water.PlatformSpecificParams{Name: "vtun", Network: config.CIDR}
|
||||
} else {
|
||||
c.PlatformSpecificParams = water.PlatformSpecificParams{Network: config.CIDR}
|
||||
}
|
||||
c.PlatformSpecificParams.Name = config.DeviceName
|
||||
}
|
||||
iface, err := water.New(c)
|
||||
if err != nil {
|
||||
@ -56,7 +60,7 @@ func configTun(config config.Config, iface *water.Interface) {
|
||||
if serverAddrIP.To4() != nil {
|
||||
netutil.ExecCmd("/sbin/ip", "route", "add", serverAddrIP.To4().String()+"/32", "via", config.LocalGateway, "dev", physicalIface)
|
||||
} else {
|
||||
netutil.ExecCmd("/sbin/ip", "-6", "route", "add", serverAddrIP.To16().String()+"/64", "via", config.LocalGateway, "dev", physicalIface)
|
||||
netutil.ExecCmd("/sbin/ip", "-6", "route", "add", serverAddrIP.To16().String()+"/128", "via", config.LocalGateway, "dev", physicalIface)
|
||||
}
|
||||
netutil.ExecCmd("/sbin/ip", "route", "add", "0.0.0.0/1", "dev", iface.Name())
|
||||
netutil.ExecCmd("/sbin/ip", "-6", "route", "add", "::/1", "dev", iface.Name())
|
||||
@ -94,11 +98,11 @@ func configTun(config config.Config, iface *water.Interface) {
|
||||
if serverAddrIP.To4() != nil {
|
||||
netutil.ExecCmd("cmd", "/C", "route", "delete", "0.0.0.0", "mask", "0.0.0.0")
|
||||
netutil.ExecCmd("cmd", "/C", "route", "add", "0.0.0.0", "mask", "0.0.0.0", config.ServerIP, "metric", "6")
|
||||
netutil.ExecCmd("cmd", "/C", "route", "add", serverAddrIP.To4().String(), config.LocalGateway, "metric", "5")
|
||||
netutil.ExecCmd("cmd", "/C", "route", "add", serverAddrIP.To4().String()+"/32", config.LocalGateway, "metric", "5")
|
||||
} else {
|
||||
netutil.ExecCmd("cmd", "/C", "route", "-6", "delete", "::/0", "mask", "::/0")
|
||||
netutil.ExecCmd("cmd", "/C", "route", "-6", "add", "::/0", "mask", "::/0", config.ServerIPv6, "metric", "6")
|
||||
netutil.ExecCmd("cmd", "/C", "route", "-6", "add", serverAddrIP.To16().String(), config.LocalGateway, "metric", "5")
|
||||
netutil.ExecCmd("cmd", "/C", "route", "-6", "add", serverAddrIP.To16().String()+"/128", config.LocalGateway, "metric", "5")
|
||||
}
|
||||
netutil.ExecCmd("cmd", "/C", "route", "add", config.DNSIP, config.LocalGateway, "metric", "5")
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ type Client struct {
|
||||
|
||||
// udpToTun sends packets from udp to tun
|
||||
func (c *Client) udpToTun() {
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, c.config.BufferSize)
|
||||
for {
|
||||
n, _, err := c.localConn.ReadFromUDP(packet)
|
||||
if err != nil || n == 0 {
|
||||
@ -65,7 +65,7 @@ func (c *Client) udpToTun() {
|
||||
|
||||
// tunToUdp sends packets from tun to udp
|
||||
func (c *Client) tunToUdp() {
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, c.config.BufferSize)
|
||||
for {
|
||||
n, err := c.iface.Read(packet)
|
||||
if err != nil || n == 0 {
|
||||
|
@ -41,7 +41,7 @@ type Server struct {
|
||||
|
||||
// tunToUdp sends packets from tun to udp
|
||||
func (s *Server) tunToUdp() {
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, s.config.BufferSize)
|
||||
for {
|
||||
n, err := s.iface.Read(packet)
|
||||
if err != nil || n == 0 {
|
||||
@ -65,7 +65,7 @@ func (s *Server) tunToUdp() {
|
||||
|
||||
// udpToTun sends packets from udp to tun
|
||||
func (s *Server) udpToTun() {
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, s.config.BufferSize)
|
||||
for {
|
||||
n, cliAddr, err := s.localConn.ReadFromUDP(packet)
|
||||
if err != nil || n == 0 {
|
||||
|
@ -56,7 +56,7 @@ func wsToTun(config config.Config, wsconn net.Conn, iface *water.Interface) {
|
||||
|
||||
// tunToWs sends packets from tun to ws
|
||||
func tunToWs(config config.Config, iface *water.Interface) {
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, config.BufferSize)
|
||||
for {
|
||||
n, err := iface.Read(packet)
|
||||
if err != nil || n == 0 {
|
||||
|
@ -145,7 +145,7 @@ func checkPermission(w http.ResponseWriter, req *http.Request, config config.Con
|
||||
|
||||
// toClient sends data to client
|
||||
func toClient(config config.Config, iface *water.Interface) {
|
||||
packet := make([]byte, 4096)
|
||||
packet := make([]byte, config.BufferSize)
|
||||
for {
|
||||
n, err := iface.Read(packet)
|
||||
if err != nil || err == io.EOF || n == 0 {
|
||||
|
Loading…
Reference in New Issue
Block a user