tuntap: add routing gateway options
This commit is contained in:
parent
267885f1b4
commit
4d818442d7
@ -437,18 +437,20 @@ func (r *route) GenRouters() ([]router, error) {
|
|||||||
handler = gost.SNIHandler()
|
handler = gost.SNIHandler()
|
||||||
case "tun":
|
case "tun":
|
||||||
cfg := gost.TunConfig{
|
cfg := gost.TunConfig{
|
||||||
Name: node.Get("name"),
|
Name: node.Get("name"),
|
||||||
Addr: node.Get("net"),
|
Addr: node.Get("net"),
|
||||||
MTU: node.GetInt("mtu"),
|
MTU: node.GetInt("mtu"),
|
||||||
Routes: strings.Split(node.Get("route"), ","),
|
Routes: strings.Split(node.Get("route"), ","),
|
||||||
|
Gateway: node.Get("gw"),
|
||||||
}
|
}
|
||||||
handler = gost.TunHandler(node.Remote, gost.TunConfigHandlerOption(cfg))
|
handler = gost.TunHandler(node.Remote, gost.TunConfigHandlerOption(cfg))
|
||||||
case "tap":
|
case "tap":
|
||||||
cfg := gost.TapConfig{
|
cfg := gost.TapConfig{
|
||||||
Name: node.Get("name"),
|
Name: node.Get("name"),
|
||||||
Addr: node.Get("net"),
|
Addr: node.Get("net"),
|
||||||
MTU: node.GetInt("mtu"),
|
MTU: node.GetInt("mtu"),
|
||||||
Routes: strings.Split(node.Get("route"), ","),
|
Routes: strings.Split(node.Get("route"), ","),
|
||||||
|
Gateway: node.Get("gw"),
|
||||||
}
|
}
|
||||||
handler = gost.TapHandler(node.Remote, gost.TapConfigHandlerOption(cfg))
|
handler = gost.TapHandler(node.Remote, gost.TapConfigHandlerOption(cfg))
|
||||||
default:
|
default:
|
||||||
|
20
tuntap.go
20
tuntap.go
@ -38,10 +38,11 @@ func ipProtocol(p waterutil.IPProtocol) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TunConfig struct {
|
type TunConfig struct {
|
||||||
Name string
|
Name string
|
||||||
Addr string
|
Addr string
|
||||||
MTU int
|
MTU int
|
||||||
Routes []string
|
Routes []string
|
||||||
|
Gateway string
|
||||||
}
|
}
|
||||||
|
|
||||||
type tunRouteKey [16]byte
|
type tunRouteKey [16]byte
|
||||||
@ -355,14 +356,15 @@ func etherType(et waterutil.Ethertype) string {
|
|||||||
if s, ok := mEtherTypes[et]; ok {
|
if s, ok := mEtherTypes[et]; ok {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
return "unknown"
|
return fmt.Sprintf("unknown(%v)", et)
|
||||||
}
|
}
|
||||||
|
|
||||||
type TapConfig struct {
|
type TapConfig struct {
|
||||||
Name string
|
Name string
|
||||||
Addr string
|
Addr string
|
||||||
MTU int
|
MTU int
|
||||||
Routes []string
|
Routes []string
|
||||||
|
Gateway string
|
||||||
}
|
}
|
||||||
|
|
||||||
type tapRouteKey [6]byte
|
type tapRouteKey [6]byte
|
||||||
|
@ -57,7 +57,7 @@ func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = addRoutes("tun", ifce.Name(), cfg.Routes...); err != nil {
|
if err = addTunRoutes(ifce.Name(), cfg.Routes...); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = addRoutes("tap", ifce.Name(), cfg.Routes...); err != nil {
|
if err = addTapRoutes(ifce.Name(), cfg.Gateway, cfg.Routes...); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,16 +136,30 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func addRoutes(ifType, ifName string, routes ...string) error {
|
func addTunRoutes(ifName string, routes ...string) error {
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
if route == "" {
|
if route == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
cmd := fmt.Sprintf("ip route add %s dev %s", route, ifName)
|
cmd := fmt.Sprintf("ip route add %s dev %s", route, ifName)
|
||||||
log.Logf("[%s] %s", ifType, cmd)
|
log.Logf("[tun] %s", cmd)
|
||||||
if err := netlink.AddRoute(route, "", "", ifName); err != nil {
|
if err := netlink.AddRoute(route, "", "", ifName); err != nil {
|
||||||
return fmt.Errorf("%s: %v", cmd, err)
|
return fmt.Errorf("%s: %v", cmd, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addTapRoutes(ifName string, gw string, routes ...string) error {
|
||||||
|
for _, route := range routes {
|
||||||
|
if route == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cmd := fmt.Sprintf("ip route add %s via %s dev %s", route, gw, ifName)
|
||||||
|
log.Logf("[tap] %s", cmd)
|
||||||
|
if err := netlink.AddRoute(route, "", gw, ifName); err != nil {
|
||||||
|
return fmt.Errorf("%s: %v", cmd, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -38,7 +38,7 @@ func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = addRoutes("tun", ifce.Name(), cfg.Routes...); err != nil {
|
if err = addTunRoutes(ifce.Name(), cfg.Routes...); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = addRoutes("tap", ifce.Name(), cfg.Routes...); err != nil {
|
if err = addTapRoutes(ifce.Name(), cfg.Gateway, cfg.Routes...); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,13 +96,31 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func addRoutes(ifType, ifName string, routes ...string) error {
|
func addTunRoutes(ifName string, routes ...string) error {
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
if route == "" {
|
if route == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
cmd := fmt.Sprintf("route add -net %s -interface %s", route, ifName)
|
cmd := fmt.Sprintf("route add -net %s -interface %s", route, ifName)
|
||||||
log.Logf("[%s] %s", ifType, cmd)
|
log.Logf("[tun] %s", cmd)
|
||||||
|
args := strings.Split(cmd, " ")
|
||||||
|
if er := exec.Command(args[0], args[1:]...).Run(); er != nil {
|
||||||
|
return fmt.Errorf("%s: %v", cmd, er)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addTapRoutes(ifName string, gw string, routes ...string) error {
|
||||||
|
for _, route := range routes {
|
||||||
|
if route == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cmd := fmt.Sprintf("route add -net %s dev %s", route, ifName)
|
||||||
|
if gw != "" {
|
||||||
|
cmd += " gw " + gw
|
||||||
|
}
|
||||||
|
log.Logf("[tap] %s", 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 {
|
||||||
return fmt.Errorf("%s: %v", cmd, er)
|
return fmt.Errorf("%s: %v", cmd, er)
|
||||||
|
@ -38,7 +38,7 @@ func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = addRoutes("tun", ifce.Name(), cfg.Routes...); err != nil {
|
if err = addTunRoutes(ifce.Name(), cfg.Gateway, cfg.Routes...); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = addRoutes("tap", ifce.Name(), cfg.Routes...); err != nil {
|
if err = addTapRoutes(ifce.Name(), cfg.Gateway, cfg.Routes...); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func addRoutes(ifType, ifName string, routes ...string) error {
|
func addTunRoutes(ifName string, gw string, routes ...string) error {
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
if route == "" {
|
if route == "" {
|
||||||
continue
|
continue
|
||||||
@ -108,7 +108,32 @@ func addRoutes(ifType, ifName string, routes ...string) error {
|
|||||||
|
|
||||||
cmd := fmt.Sprintf("netsh interface ip add route prefix=%s interface=%s store=active",
|
cmd := fmt.Sprintf("netsh interface ip add route prefix=%s interface=%s store=active",
|
||||||
route, ifName)
|
route, ifName)
|
||||||
log.Logf("[%s] %s", ifType, cmd)
|
if gw != "" {
|
||||||
|
cmd += " nexthop=" + gw
|
||||||
|
}
|
||||||
|
log.Logf("[tun] %s", cmd)
|
||||||
|
args := strings.Split(cmd, " ")
|
||||||
|
if er := exec.Command(args[0], args[1:]...).Run(); er != nil {
|
||||||
|
return fmt.Errorf("%s: %v", cmd, er)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addTapRoutes(ifName string, gw string, routes ...string) error {
|
||||||
|
for _, route := range routes {
|
||||||
|
if route == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteRoute(ifName, route)
|
||||||
|
|
||||||
|
cmd := fmt.Sprintf("netsh interface ip add route prefix=%s interface=%s store=active",
|
||||||
|
route, ifName)
|
||||||
|
if gw != "" {
|
||||||
|
cmd += " nexthop=" + gw
|
||||||
|
}
|
||||||
|
log.Logf("[tap] %s", 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 {
|
||||||
return fmt.Errorf("%s: %v", cmd, er)
|
return fmt.Errorf("%s: %v", cmd, er)
|
||||||
|
Loading…
Reference in New Issue
Block a user