2022-12-05 00:20:50 +08:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
2023-09-28 18:59:31 +08:00
|
|
|
"net"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2022-12-05 00:20:50 +08:00
|
|
|
)
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
type Addition func(metadata *C.Metadata)
|
2022-12-05 00:20:50 +08:00
|
|
|
|
2023-10-11 22:54:19 +08:00
|
|
|
func ApplyAdditions(metadata *C.Metadata, additions ...Addition) {
|
|
|
|
for _, addition := range additions {
|
|
|
|
addition(metadata)
|
|
|
|
}
|
2022-12-05 10:12:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func WithInName(name string) Addition {
|
|
|
|
return func(metadata *C.Metadata) {
|
|
|
|
metadata.InName = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 17:19:57 +08:00
|
|
|
func WithInUser(user string) Addition {
|
|
|
|
return func(metadata *C.Metadata) {
|
|
|
|
metadata.InUser = user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
func WithSpecialRules(specialRules string) Addition {
|
|
|
|
return func(metadata *C.Metadata) {
|
|
|
|
metadata.SpecialRules = specialRules
|
|
|
|
}
|
2022-12-05 00:20:50 +08:00
|
|
|
}
|
2022-12-05 17:53:54 +08:00
|
|
|
|
|
|
|
func WithSpecialProxy(specialProxy string) Addition {
|
|
|
|
return func(metadata *C.Metadata) {
|
|
|
|
metadata.SpecialProxy = specialProxy
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 18:59:31 +08:00
|
|
|
|
2023-10-11 22:54:19 +08:00
|
|
|
func WithDstAddr(addr net.Addr) Addition {
|
2023-09-28 18:59:31 +08:00
|
|
|
return func(metadata *C.Metadata) {
|
2023-10-11 22:54:19 +08:00
|
|
|
_ = metadata.SetRemoteAddr(addr)
|
2023-10-10 19:43:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 22:54:19 +08:00
|
|
|
func WithSrcAddr(addr net.Addr) Addition {
|
2023-10-10 19:43:26 +08:00
|
|
|
return func(metadata *C.Metadata) {
|
2023-10-11 22:54:19 +08:00
|
|
|
m := C.Metadata{}
|
2024-04-04 21:25:27 +08:00
|
|
|
if err := m.SetRemoteAddr(addr); err == nil {
|
2023-10-11 22:54:19 +08:00
|
|
|
metadata.SrcIP = m.DstIP
|
|
|
|
metadata.SrcPort = m.DstPort
|
|
|
|
}
|
2023-10-10 19:43:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithInAddr(addr net.Addr) Addition {
|
|
|
|
return func(metadata *C.Metadata) {
|
2023-10-11 22:54:19 +08:00
|
|
|
m := C.Metadata{}
|
2024-04-04 21:25:27 +08:00
|
|
|
if err := m.SetRemoteAddr(addr); err == nil {
|
2023-10-11 22:54:19 +08:00
|
|
|
metadata.InIP = m.DstIP
|
|
|
|
metadata.InPort = m.DstPort
|
|
|
|
}
|
2023-09-28 18:59:31 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-20 10:19:42 +08:00
|
|
|
|
|
|
|
func WithDSCP(dscp uint8) Addition {
|
|
|
|
return func(metadata *C.Metadata) {
|
|
|
|
metadata.DSCP = dscp
|
|
|
|
}
|
|
|
|
}
|
2024-07-25 19:49:56 +08:00
|
|
|
|
|
|
|
func Placeholder(metadata *C.Metadata) {}
|