mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-07-23 18:34:14 +00:00
70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package wireguard
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/netip"
|
|
"strings"
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/log"
|
|
"golang.zx2c4.com/wireguard/device"
|
|
)
|
|
|
|
var wgLogger = &device.Logger{
|
|
Verbosef: func(format string, args ...any) {
|
|
log.Record(&log.GeneralMessage{
|
|
Severity: log.Severity_Debug,
|
|
Content: fmt.Sprintf(format, args...),
|
|
})
|
|
},
|
|
Errorf: func(format string, args ...any) {
|
|
log.Record(&log.GeneralMessage{
|
|
Severity: log.Severity_Error,
|
|
Content: fmt.Sprintf(format, args...),
|
|
})
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
common.Must(common.RegisterConfig((*DeviceConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
deviceConfig := config.(*DeviceConfig)
|
|
return New(ctx, deviceConfig)
|
|
}))
|
|
}
|
|
|
|
// convert endpoint string to netip.Addr
|
|
func parseEndpoints(conf *DeviceConfig) ([]netip.Addr, bool, bool, error) {
|
|
var hasIPv4, hasIPv6 bool
|
|
|
|
endpoints := make([]netip.Addr, len(conf.Endpoint))
|
|
for i, str := range conf.Endpoint {
|
|
var addr netip.Addr
|
|
if strings.Contains(str, "/") {
|
|
prefix, err := netip.ParsePrefix(str)
|
|
if err != nil {
|
|
return nil, false, false, err
|
|
}
|
|
addr = prefix.Addr()
|
|
if prefix.Bits() != addr.BitLen() {
|
|
return nil, false, false, errors.New("interface address subnet should be /32 for IPv4 and /128 for IPv6")
|
|
}
|
|
} else {
|
|
var err error
|
|
addr, err = netip.ParseAddr(str)
|
|
if err != nil {
|
|
return nil, false, false, err
|
|
}
|
|
}
|
|
endpoints[i] = addr
|
|
|
|
if addr.Is4() {
|
|
hasIPv4 = true
|
|
} else if addr.Is6() {
|
|
hasIPv6 = true
|
|
}
|
|
}
|
|
|
|
return endpoints, hasIPv4, hasIPv6, nil
|
|
}
|