tap: make net parameter optional (#472)

This commit is contained in:
ginuerzh 2020-01-22 10:39:23 +08:00
parent 08d2f7c516
commit 990c7b56c3
3 changed files with 32 additions and 25 deletions

View File

@ -74,10 +74,14 @@ func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
} }
func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) { func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
ip, ipNet, err := net.ParseCIDR(cfg.Addr) var ip net.IP
var ipNet *net.IPNet
if cfg.Addr != "" {
ip, ipNet, err = net.ParseCIDR(cfg.Addr)
if err != nil { if err != nil {
return return
} }
}
ifce, err := water.New(water.Config{ ifce, err := water.New(water.Config{
DeviceType: water.TAP, DeviceType: water.TAP,
@ -106,12 +110,14 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
return return
} }
if cfg.Addr != "" {
cmd = fmt.Sprintf("ip address add %s dev %s", cfg.Addr, ifce.Name()) cmd = fmt.Sprintf("ip address add %s dev %s", cfg.Addr, ifce.Name())
log.Log("[tap]", cmd) log.Log("[tap]", cmd)
if er := link.SetLinkIp(ip, ipNet); er != nil { if er := link.SetLinkIp(ip, ipNet); er != nil {
err = fmt.Errorf("%s: %v", cmd, er) err = fmt.Errorf("%s: %v", cmd, er)
return return
} }
}
cmd = fmt.Sprintf("ip link set dev %s up", ifce.Name()) cmd = fmt.Sprintf("ip link set dev %s up", ifce.Name())
log.Log("[tap]", cmd) log.Log("[tap]", cmd)

View File

@ -55,10 +55,7 @@ func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
} }
func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) { func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
ip, _, err := net.ParseCIDR(cfg.Addr) ip, _, _ := net.ParseCIDR(cfg.Addr)
if err != nil {
return
}
ifce, err := water.New(water.Config{ ifce, err := water.New(water.Config{
DeviceType: water.TAP, DeviceType: water.TAP,
@ -72,7 +69,12 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
mtu = DefaultMTU mtu = DefaultMTU
} }
cmd := fmt.Sprintf("ifconfig %s inet %s mtu %d up", ifce.Name(), cfg.Addr, mtu) var cmd string
if cfg.Addr != "" {
cmd = fmt.Sprintf("ifconfig %s inet %s mtu %d up", ifce.Name(), cfg.Addr, mtu)
} else {
cmd = fmt.Sprintf("ifconfig %s mtu %d up", ifce.Name(), mtu)
}
log.Log("[tap]", cmd) log.Log("[tap]", cmd)
args := strings.Split(cmd, " ") args := strings.Split(cmd, " ")
if er := exec.Command(args[0], args[1:]...).Run(); er != nil { if er := exec.Command(args[0], args[1:]...).Run(); er != nil {

View File

@ -55,10 +55,7 @@ func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
} }
func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) { func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
ip, ipNet, err := net.ParseCIDR(cfg.Addr) ip, ipNet, _ := net.ParseCIDR(cfg.Addr)
if err != nil {
return
}
ifce, err := water.New(water.Config{ ifce, err := water.New(water.Config{
DeviceType: water.TAP, DeviceType: water.TAP,
@ -72,6 +69,7 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
return return
} }
if ip != nil && ipNet != nil {
cmd := fmt.Sprintf("netsh interface ip set address name=%s "+ cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
"source=static addr=%s mask=%s gateway=none", "source=static addr=%s mask=%s gateway=none",
ifce.Name(), ip.String(), ipMask(ipNet.Mask)) ifce.Name(), ip.String(), ipMask(ipNet.Mask))
@ -81,6 +79,7 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
err = fmt.Errorf("%s: %v", cmd, er) err = fmt.Errorf("%s: %v", cmd, er)
return return
} }
}
if err = addTapRoutes(ifce.Name(), cfg.Gateway, cfg.Routes...); err != nil { if err = addTapRoutes(ifce.Name(), cfg.Gateway, cfg.Routes...); err != nil {
return return