forked from eycorsican/go-tun2socks
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to wireguard driver for a better windows support
- Loading branch information
Showing
9 changed files
with
142 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package tun | ||
|
||
import ( | ||
"io" | ||
"runtime" | ||
|
||
"golang.zx2c4.com/wireguard/tun" | ||
) | ||
|
||
type tunnel struct { | ||
tun.Device | ||
} | ||
|
||
func (t *tunnel) Read(b []byte) (int, error) { | ||
if runtime.GOOS == "windows" { | ||
return t.Device.Read(b, 0) | ||
} | ||
// unix.IFF_NO_PI is not set, therefore we receive packet information | ||
n, err := t.Device.File().Read(b) | ||
if n < 4 { | ||
return 0, err | ||
} | ||
// shift slice to the left | ||
return copy(b[:n-4], b[4:n]), nil | ||
} | ||
|
||
func (t *tunnel) Write(b []byte) (int, error) { | ||
if runtime.GOOS == "windows" { | ||
return t.Device.Write(b, 0) | ||
} | ||
return t.Device.Write(append(make([]byte, 4), b...), 4) | ||
} | ||
|
||
func (t *tunnel) Close() error { | ||
return t.Device.Close() | ||
} | ||
|
||
func OpenTunDevice(name, addr, gw, mask string, dnsServers []string, persist bool) (io.ReadWriteCloser, error) { | ||
tunDev, err := tun.CreateTUN(name, 1500) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
getName, err := tunDev.Name() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &tunnel{Device: tunDev}, setInterface(getName, addr, gw, mask, tunDev.(*tun.NativeTun)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,31 @@ | ||
package tun | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/songgao/water" | ||
"github.com/eycorsican/go-tun2socks/routes" | ||
"golang.zx2c4.com/wireguard/tun" | ||
) | ||
|
||
func isIPv4(ip net.IP) bool { | ||
if ip.To4() != nil { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func isIPv6(ip net.IP) bool { | ||
// To16() also valid for ipv4, ensure it's not an ipv4 address | ||
if ip.To4() != nil { | ||
return false | ||
} | ||
if ip.To16() != nil { | ||
return true | ||
func setInterface(name, addr, gw, mask string, tun *tun.NativeTun) error { | ||
addrs, err := routes.ParseAddresses(addr, gw, mask) | ||
if err != nil { | ||
return err | ||
} | ||
return false | ||
} | ||
|
||
func OpenTunDevice(name, addr, gw, mask string, dnsServers []string, persist bool) (io.ReadWriteCloser, error) { | ||
tunDev, err := water.New(water.Config{ | ||
DeviceType: water.TUN, | ||
}) | ||
v, err := exec.Command("ifconfig", name, "mtu", "1500").Output() | ||
if err != nil { | ||
return nil, err | ||
return fmt.Errorf("failed to set MTU: %s: %s", v, err) | ||
} | ||
name = tunDev.Name() | ||
ip := net.ParseIP(addr) | ||
if ip == nil { | ||
return nil, errors.New("invalid IP address") | ||
} | ||
|
||
var params string | ||
if isIPv4(ip) { | ||
params = fmt.Sprintf("%s inet %s netmask %s %s", name, addr, mask, gw) | ||
} else if isIPv6(ip) { | ||
prefixlen, err := strconv.Atoi(mask) | ||
if err != nil { | ||
return nil, errors.New(fmt.Sprintf("parse IPv6 prefixlen failed: %v", err)) | ||
} | ||
params = fmt.Sprintf("%s inet6 %s/%d", name, addr, prefixlen) | ||
} else { | ||
return nil, errors.New("invalid IP address") | ||
v, err = exec.Command("ifconfig", name, "inet", addrs[0].String(), addrs[1].String()).Output() | ||
if err != nil { | ||
return fmt.Errorf("failed to set ip addr: %s: %s", v, err) | ||
} | ||
|
||
out, err := exec.Command("ifconfig", strings.Split(params, " ")...).Output() | ||
v, err = exec.Command("ifconfig", name, "up").Output() | ||
if err != nil { | ||
if len(out) != 0 { | ||
return nil, errors.New(fmt.Sprintf("%v, output: %s", err, out)) | ||
} | ||
return nil, err | ||
return fmt.Errorf("failed to bring up interface: %s: %s", v, err) | ||
} | ||
return tunDev, nil | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.