2023-02-08 19:54:28 +07:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2024-01-04 16:28:16 +07:00
|
|
|
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
|
2023-02-08 19:54:28 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
package control
|
|
|
|
|
2023-02-08 20:50:47 +07:00
|
|
|
import (
|
2023-04-23 12:27:29 +07:00
|
|
|
"strconv"
|
|
|
|
|
2023-02-08 20:50:47 +07:00
|
|
|
"github.com/cilium/ebpf"
|
2023-03-14 14:01:55 +07:00
|
|
|
"github.com/daeuniverse/dae/component/outbound/dialer"
|
2023-03-23 14:34:56 +07:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-02-08 21:43:28 +07:00
|
|
|
"golang.org/x/sys/unix"
|
2023-02-08 20:50:47 +07:00
|
|
|
)
|
2023-02-08 19:54:28 +07:00
|
|
|
|
2023-02-08 21:43:28 +07:00
|
|
|
func FormatL4Proto(l4proto uint8) string {
|
|
|
|
if l4proto == unix.IPPROTO_TCP {
|
|
|
|
return "tcp"
|
|
|
|
}
|
|
|
|
if l4proto == unix.IPPROTO_UDP {
|
|
|
|
return "udp"
|
|
|
|
}
|
|
|
|
return strconv.Itoa(int(l4proto))
|
|
|
|
}
|
|
|
|
|
2023-06-29 21:30:33 +07:00
|
|
|
func (c *controlPlaneCore) outboundAliveChangeCallback(outbound uint8, dryrun bool) func(alive bool, networkType *dialer.NetworkType, isInit bool) {
|
2023-03-02 20:28:30 +07:00
|
|
|
return func(alive bool, networkType *dialer.NetworkType, isInit bool) {
|
2023-03-23 14:34:56 +07:00
|
|
|
select {
|
|
|
|
case <-c.closed.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2023-06-29 21:30:33 +07:00
|
|
|
if !isInit && dryrun {
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 14:34:56 +07:00
|
|
|
if !isInit || c.log.IsLevelEnabled(logrus.TraceLevel) {
|
2023-03-30 00:55:56 +07:00
|
|
|
strAlive := "NOT ALIVE"
|
|
|
|
if alive {
|
|
|
|
strAlive = "ALIVE"
|
|
|
|
}
|
2023-03-02 20:28:30 +07:00
|
|
|
c.log.WithFields(logrus.Fields{
|
2023-03-23 14:34:56 +07:00
|
|
|
"outboundId": outbound,
|
2024-01-01 16:19:18 +07:00
|
|
|
}).Tracef("Outbound <%v> %v -> %v, notify the kernel program.", c.outboundId2Name[outbound], networkType.StringWithoutDns(), strAlive)
|
2023-03-02 20:28:30 +07:00
|
|
|
}
|
2023-02-08 20:50:47 +07:00
|
|
|
|
2023-02-08 19:54:28 +07:00
|
|
|
value := uint32(0)
|
|
|
|
if alive {
|
|
|
|
value = 1
|
|
|
|
}
|
2023-03-23 14:34:56 +07:00
|
|
|
if err := c.bpf.OutboundConnectivityMap.Update(bpfOutboundConnectivityQuery{
|
2023-02-08 19:54:28 +07:00
|
|
|
Outbound: outbound,
|
2023-02-12 14:39:00 +07:00
|
|
|
L4proto: networkType.L4Proto.ToL4Proto(),
|
|
|
|
Ipversion: networkType.IpVersion.ToIpVersion(),
|
2023-03-23 14:34:56 +07:00
|
|
|
}, value, ebpf.UpdateAny); err != nil {
|
|
|
|
c.log.WithFields(logrus.Fields{
|
|
|
|
"alive": alive,
|
|
|
|
"network": networkType.StringWithoutDns(),
|
|
|
|
"outbound": c.outboundId2Name[outbound],
|
|
|
|
}).Warnf("Failed to notify the kernel program: %v", err)
|
|
|
|
}
|
2023-02-08 19:54:28 +07:00
|
|
|
}
|
|
|
|
}
|