2023-07-26 16:21:04 +02:00
|
|
|
//go:build !android
|
|
|
|
|
2023-07-26 14:00:47 +02:00
|
|
|
package wgproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/sirupsen/logrus"
|
2024-09-25 18:50:10 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/internal/wgproxy/ebpf"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/wgproxy/usp"
|
2023-07-26 14:00:47 +02:00
|
|
|
)
|
|
|
|
|
2024-09-25 18:50:10 +02:00
|
|
|
type Factory struct {
|
|
|
|
wgPort int
|
|
|
|
ebpfProxy *ebpf.WGEBPFProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFactory(userspace bool, wgPort int) *Factory {
|
2023-07-26 14:00:47 +02:00
|
|
|
f := &Factory{wgPort: wgPort}
|
|
|
|
|
2024-06-22 15:33:10 +02:00
|
|
|
if userspace {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2024-09-25 18:50:10 +02:00
|
|
|
ebpfProxy := ebpf.NewWGEBPFProxy(wgPort)
|
|
|
|
err := ebpfProxy.Listen()
|
2023-07-26 14:00:47 +02:00
|
|
|
if err != nil {
|
2023-08-03 18:24:23 +02:00
|
|
|
log.Warnf("failed to initialize ebpf proxy, fallback to user space proxy: %s", err)
|
2023-07-26 14:00:47 +02:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
f.ebpfProxy = ebpfProxy
|
|
|
|
return f
|
|
|
|
}
|
2024-09-25 18:50:10 +02:00
|
|
|
|
|
|
|
func (w *Factory) GetProxy() Proxy {
|
|
|
|
if w.ebpfProxy != nil {
|
|
|
|
p := &ebpf.ProxyWrapper{
|
|
|
|
WgeBPFProxy: w.ebpfProxy,
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
return usp.NewWGUserSpaceProxy(w.wgPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Factory) Free() error {
|
|
|
|
if w.ebpfProxy == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return w.ebpfProxy.Free()
|
|
|
|
}
|