2023-07-26 14:00:47 +02:00
|
|
|
package wgproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-04-03 11:11:46 +02:00
|
|
|
"net"
|
2023-07-26 14:00:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
portRangeStart = 3128
|
|
|
|
portRangeEnd = 3228
|
|
|
|
)
|
|
|
|
|
|
|
|
type portLookup struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl portLookup) searchFreePort() (int, error) {
|
|
|
|
for i := portRangeStart; i <= portRangeEnd; i++ {
|
|
|
|
if pl.tryToBind(i) == nil {
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("failed to bind free port for eBPF proxy")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl portLookup) tryToBind(port int) error {
|
2024-04-03 11:11:46 +02:00
|
|
|
l, err := net.ListenPacket("udp", fmt.Sprintf(":%d", port))
|
2023-07-26 14:00:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = l.Close()
|
|
|
|
return nil
|
|
|
|
}
|