mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-29 06:35:07 +03:00
Refactor admin socket, export request/response structs, remove types package
This commit is contained in:
parent
dfca87ba80
commit
2d01386d6e
13 changed files with 290 additions and 218 deletions
|
@ -1,57 +1,31 @@
|
|||
package tuntap
|
||||
|
||||
import (
|
||||
//"encoding/hex"
|
||||
//"errors"
|
||||
//"fmt"
|
||||
//"net"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/admin"
|
||||
)
|
||||
|
||||
func (t *TunAdapter) SetupAdminHandlers(a *admin.AdminSocket) {
|
||||
a.AddHandler("getTunTap", []string{}, func(in admin.Info) (r admin.Info, e error) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
r = admin.Info{"none": admin.Info{}}
|
||||
e = nil
|
||||
}
|
||||
}()
|
||||
type GetTUNRequest struct{}
|
||||
type GetTUNResponse map[string]uint16
|
||||
|
||||
return admin.Info{
|
||||
t.Name(): admin.Info{
|
||||
"mtu": t.mtu,
|
||||
},
|
||||
}, nil
|
||||
})
|
||||
/*
|
||||
// TODO: rewrite this as I'm fairly sure it doesn't work right on many
|
||||
// platforms anyway, but it may require changes to Water
|
||||
a.AddHandler("setTunTap", []string{"name", "[tap_mode]", "[mtu]"}, func(in Info) (Info, error) {
|
||||
// Set sane defaults
|
||||
iftapmode := defaults.GetDefaults().DefaultIfTAPMode
|
||||
ifmtu := defaults.GetDefaults().DefaultIfMTU
|
||||
// Has TAP mode been specified?
|
||||
if tap, ok := in["tap_mode"]; ok {
|
||||
iftapmode = tap.(bool)
|
||||
}
|
||||
// Check we have enough params for MTU
|
||||
if mtu, ok := in["mtu"]; ok {
|
||||
if mtu.(float64) >= 1280 && ifmtu <= defaults.GetDefaults().MaximumIfMTU {
|
||||
ifmtu = int(in["mtu"].(float64))
|
||||
}
|
||||
}
|
||||
// Start the TUN adapter
|
||||
if err := a.startTunWithMTU(in["name"].(string), iftapmode, ifmtu); err != nil {
|
||||
return Info{}, errors.New("Failed to configure adapter")
|
||||
} else {
|
||||
return Info{
|
||||
a.core.router.tun.iface.Name(): Info{
|
||||
"tap_mode": a.core.router.tun.iface.IsTAP(),
|
||||
"mtu": ifmtu,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
})
|
||||
*/
|
||||
func (t *TunAdapter) getTUNHandler(req *GetTUNRequest, res *GetTUNResponse) error {
|
||||
res = &GetTUNResponse{
|
||||
t.Name(): t.MTU(),
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TunAdapter) SetupAdminHandlers(a *admin.AdminSocket) {
|
||||
_ = a.AddHandler("getTunTap", []string{}, func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetTUNRequest{}
|
||||
res := &GetTUNResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := t.getTUNHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
})
|
||||
}
|
||||
|
|
|
@ -23,11 +23,10 @@ import (
|
|||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/types"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
||||
)
|
||||
|
||||
type MTU = types.MTU
|
||||
type MTU uint16
|
||||
|
||||
// TunAdapter represents a running TUN interface and extends the
|
||||
// yggdrasil.Adapter type. In order to use the TUN adapter with Yggdrasil, you
|
||||
|
@ -40,7 +39,7 @@ type TunAdapter struct {
|
|||
log *log.Logger
|
||||
addr address.Address
|
||||
subnet address.Subnet
|
||||
mtu MTU
|
||||
mtu uint16
|
||||
iface tun.Device
|
||||
phony.Inbox // Currently only used for _handlePacket from the reader, TODO: all the stuff that currently needs a mutex below
|
||||
//mutex sync.RWMutex // Protects the below
|
||||
|
@ -56,7 +55,7 @@ func (tun *TunAdapter) SetSessionGatekeeper(gatekeeper func(pubkey ed25519.Publi
|
|||
|
||||
// Gets the maximum supported MTU for the platform based on the defaults in
|
||||
// defaults.GetDefaults().
|
||||
func getSupportedMTU(mtu MTU) MTU {
|
||||
func getSupportedMTU(mtu uint16) uint16 {
|
||||
if mtu < 1280 {
|
||||
return 1280
|
||||
}
|
||||
|
@ -78,7 +77,7 @@ func (tun *TunAdapter) Name() string {
|
|||
// MTU gets the adapter's MTU. This can range between 1280 and 65535, although
|
||||
// the maximum value is determined by your platform. The returned value will
|
||||
// never exceed that of MaximumMTU().
|
||||
func (tun *TunAdapter) MTU() MTU {
|
||||
func (tun *TunAdapter) MTU() uint16 {
|
||||
return getSupportedMTU(tun.mtu)
|
||||
}
|
||||
|
||||
|
@ -89,14 +88,14 @@ func DefaultName() string {
|
|||
|
||||
// DefaultMTU gets the default TUN interface MTU for your platform. This can
|
||||
// be as high as MaximumMTU(), depending on platform, but is never lower than 1280.
|
||||
func DefaultMTU() MTU {
|
||||
func DefaultMTU() uint16 {
|
||||
return defaults.GetDefaults().DefaultIfMTU
|
||||
}
|
||||
|
||||
// MaximumMTU returns the maximum supported TUN interface MTU for your
|
||||
// platform. This can be as high as 65535, depending on platform, but is never
|
||||
// lower than 1280.
|
||||
func MaximumMTU() MTU {
|
||||
func MaximumMTU() uint16 {
|
||||
return defaults.GetDefaults().MaximumIfMTU
|
||||
}
|
||||
|
||||
|
@ -151,7 +150,7 @@ func (tun *TunAdapter) _start() error {
|
|||
}
|
||||
mtu := current.IfMTU
|
||||
if tun.core.MTU() < uint64(mtu) {
|
||||
mtu = MTU(tun.core.MTU())
|
||||
mtu = uint16(tun.core.MTU())
|
||||
}
|
||||
if err := tun.setup(current.IfName, addr, mtu); err != nil {
|
||||
return err
|
||||
|
|
|
@ -16,7 +16,7 @@ import (
|
|||
)
|
||||
|
||||
// Configures the "utun" adapter with the correct IPv6 address and MTU.
|
||||
func (tun *TunAdapter) setup(ifname string, addr string, mtu MTU) error {
|
||||
func (tun *TunAdapter) setup(ifname string, addr string, mtu uint16) error {
|
||||
if ifname == "auto" {
|
||||
ifname = "utun"
|
||||
}
|
||||
|
@ -25,8 +25,8 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu MTU) error {
|
|||
panic(err)
|
||||
}
|
||||
tun.iface = iface
|
||||
if mtu, err := iface.MTU(); err == nil {
|
||||
tun.mtu = getSupportedMTU(MTU(mtu))
|
||||
if m, err := iface.MTU(); err == nil {
|
||||
tun.mtu = getSupportedMTU(uint16(m))
|
||||
} else {
|
||||
tun.mtu = 0
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue