mirror of
https://github.com/luscis/openlan.git
synced 2025-10-26 10:20:36 +08:00
fix: utun peer address for darwin
This commit is contained in:
@@ -10,8 +10,9 @@ import (
|
|||||||
type Access struct {
|
type Access struct {
|
||||||
MixAccess
|
MixAccess
|
||||||
// private
|
// private
|
||||||
brName string
|
brName string
|
||||||
addr string
|
addr string
|
||||||
|
gateway string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAccess(config *config.Access) *Access {
|
func NewAccess(config *config.Access) *Access {
|
||||||
@@ -32,46 +33,51 @@ func (p *Access) Initialize() {
|
|||||||
|
|
||||||
func (p *Access) routeAdd(prefix string) ([]byte, error) {
|
func (p *Access) routeAdd(prefix string) ([]byte, error) {
|
||||||
network.RouteDel("", prefix, "")
|
network.RouteDel("", prefix, "")
|
||||||
out, err := network.RouteAdd(p.IfName(), prefix, "")
|
out, err := network.RouteAdd("", prefix, p.gateway)
|
||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Access) AddAddr(ipStr string) error {
|
func (p *Access) AddAddr(addr, gateway string) error {
|
||||||
if ipStr == "" {
|
if addr == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// add Access-to-Access
|
// add Access-to-Access
|
||||||
ips := strings.SplitN(ipStr, "/", 2)
|
ips := strings.SplitN(addr, "/", 2)
|
||||||
out, err := network.AddrAdd(p.IfName(), ips[0], ips[0])
|
if gateway == "" {
|
||||||
|
gateway = ips[0]
|
||||||
|
}
|
||||||
|
out, err := network.AddrAdd(p.IfName(), ips[0], gateway)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.out.Warn("Access.AddAddr: %s, %s", err, out)
|
p.out.Warn("Access.AddAddr: %s, %s", err, out)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.out.Info("Access.AddAddr: %s", ipStr)
|
p.out.Info("Access.AddAddr: %s", addr)
|
||||||
|
|
||||||
|
p.addr = addr
|
||||||
|
p.gateway = gateway
|
||||||
|
|
||||||
// add directly route.
|
// add directly route.
|
||||||
out, err = p.routeAdd(ipStr)
|
out, err = p.routeAdd(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.out.Warn("Access.AddAddr: %s, %s", err, out)
|
p.out.Warn("Access.AddAddr: %s, %s", err, out)
|
||||||
}
|
}
|
||||||
p.AddRoute()
|
|
||||||
|
|
||||||
p.addr = ipStr
|
p.AddRoute()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Access) DelAddr(ipStr string) error {
|
func (p *Access) DelAddr(addr string) error {
|
||||||
// delete directly route.
|
// delete directly route.
|
||||||
out, err := network.RouteDel(p.IfName(), ipStr, "")
|
out, err := network.RouteDel(p.IfName(), addr, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.out.Warn("Access.DelAddr: %s, %s", err, out)
|
p.out.Warn("Access.DelAddr: %s, %s", err, out)
|
||||||
}
|
}
|
||||||
p.out.Info("Access.DelAddr: route %s via %s", ipStr, p.IfName())
|
p.out.Info("Access.DelAddr: route %s via %s", addr, p.IfName())
|
||||||
|
|
||||||
// delete Access-to-Access
|
// delete Access-to-Access
|
||||||
ip4 := strings.SplitN(ipStr, "/", 2)[0]
|
ip4 := strings.SplitN(addr, "/", 2)[0]
|
||||||
out, err = network.AddrDel(p.IfName(), ip4)
|
out, err = network.AddrDel(p.IfName(), ip4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.out.Warn("Access.DelAddr: %s, %s", err, out)
|
p.out.Warn("Access.DelAddr: %s, %s", err, out)
|
||||||
|
|||||||
@@ -42,30 +42,30 @@ func (p *Access) Initialize() {
|
|||||||
p.MixAccess.Initialize()
|
p.MixAccess.Initialize()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Access) DelAddr(ipStr string) error {
|
func (p *Access) DelAddr(addr string) error {
|
||||||
if p.link == nil || ipStr == "" {
|
if p.link == nil || addr == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ipAddr, err := netlink.ParseAddr(ipStr)
|
ipAddr, err := netlink.ParseAddr(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.out.Error("Access.AddAddr.ParseCIDR %s: %s", ipStr, err)
|
p.out.Error("Access.AddAddr.ParseCIDR %s: %s", addr, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := netlink.AddrDel(p.link, ipAddr); err != nil {
|
if err := netlink.AddrDel(p.link, ipAddr); err != nil {
|
||||||
p.out.Warn("Access.DelAddr.UnsetLinkIp: %s", err)
|
p.out.Warn("Access.DelAddr.UnsetLinkIp: %s", err)
|
||||||
}
|
}
|
||||||
p.out.Info("Access.DelAddr: %s", ipStr)
|
p.out.Info("Access.DelAddr: %s", addr)
|
||||||
p.addr = ""
|
p.addr = ""
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Access) AddAddr(ipStr string) error {
|
func (p *Access) AddAddr(addr, gateway string) error {
|
||||||
if ipStr == "" || p.link == nil {
|
if addr == "" || p.link == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ipAddr, err := netlink.ParseAddr(ipStr)
|
ipAddr, err := netlink.ParseAddr(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.out.Error("Access.AddAddr.ParseCIDR %s: %s", ipStr, err)
|
p.out.Error("Access.AddAddr.ParseCIDR %s: %s", addr, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := netlink.AddrAdd(p.link, ipAddr); err != nil {
|
if err := netlink.AddrAdd(p.link, ipAddr); err != nil {
|
||||||
@@ -73,11 +73,11 @@ func (p *Access) AddAddr(ipStr string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.out.Info("Access.AddAddr: %s", ipStr)
|
p.addr = addr
|
||||||
|
p.out.Info("Access.AddAddr: %s", addr)
|
||||||
|
|
||||||
p.AddRoute()
|
p.AddRoute()
|
||||||
|
|
||||||
p.addr = ipStr
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,17 +4,10 @@ package access
|
|||||||
|
|
||||||
import "github.com/luscis/openlan/pkg/config"
|
import "github.com/luscis/openlan/pkg/config"
|
||||||
|
|
||||||
type Point struct {
|
type Access struct {
|
||||||
|
MixAccess
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPoint(config *config.Point) *Point {
|
func NewAccess(config *config.Access) *Access {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Point) Initialize() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Point) Start() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ func (p *Access) Trim(out []byte) string {
|
|||||||
return strings.TrimSpace(string(out))
|
return strings.TrimSpace(string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Access) AddAddr(ipStr string) error {
|
func (p *Access) AddAddr(addr, gateway string) error {
|
||||||
if ipStr == "" {
|
if addr == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
addrExisted := network.AddrShow(p.IfName())
|
addrExisted := network.AddrShow(p.IfName())
|
||||||
@@ -59,13 +59,13 @@ func (p *Access) AddAddr(ipStr string) error {
|
|||||||
_, _ = network.AddrDel(p.IfName(), addr)
|
_, _ = network.AddrDel(p.IfName(), addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out, err := network.AddrAdd(p.IfName(), ipStr)
|
out, err := network.AddrAdd(p.IfName(), addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.out.Warn("Access.AddAddr: %s, %s", err, p.Trim(out))
|
p.out.Warn("Access.AddAddr: %s, %s", err, p.Trim(out))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.out.Info("Access.AddAddr: %s", ipStr)
|
p.out.Info("Access.AddAddr: %s", addr)
|
||||||
p.addr = ipStr
|
p.addr = addr
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ func NewEvent(newType, reason string) *WorkerEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type WorkerListener struct {
|
type WorkerListener struct {
|
||||||
AddAddr func(ipStr string) error
|
AddAddr func(ipStr, gateway string) error
|
||||||
DelAddr func(ipStr string) error
|
DelAddr func(ipStr string) error
|
||||||
OnTap func(w *TapWorker) error
|
OnTap func(w *TapWorker) error
|
||||||
AddRoutes func(routes []*models.Route) error
|
AddRoutes func(routes []*models.Route) error
|
||||||
@@ -337,7 +337,7 @@ func (w *Worker) OnIpAddr(s *SocketWorker, n *models.Network) error {
|
|||||||
ipStr := fmt.Sprintf("%s/%d", n.Address, prefix)
|
ipStr := fmt.Sprintf("%s/%d", n.Address, prefix)
|
||||||
w.tapWorker.OnIpAddr(ipStr)
|
w.tapWorker.OnIpAddr(ipStr)
|
||||||
if w.listener.AddAddr != nil {
|
if w.listener.AddAddr != nil {
|
||||||
_ = w.listener.AddAddr(ipStr)
|
_ = w.listener.AddAddr(ipStr, n.Gateway)
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.Gateway != "" && runtime.GOOS == "darwin" {
|
if n.Gateway != "" && runtime.GOOS == "darwin" {
|
||||||
@@ -375,7 +375,7 @@ func (w *Worker) OnSuccess(s *SocketWorker) error {
|
|||||||
if !w.cfg.RequestAddr {
|
if !w.cfg.RequestAddr {
|
||||||
w.out.Info("SocketWorker.AddAddr: notAllowed")
|
w.out.Info("SocketWorker.AddAddr: notAllowed")
|
||||||
} else if w.listener.AddAddr != nil {
|
} else if w.listener.AddAddr != nil {
|
||||||
_ = w.listener.AddAddr(w.ifAddr)
|
_ = w.listener.AddAddr(w.ifAddr, "")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user