mihomo/adapter/inbound/addition.go

66 lines
1.3 KiB
Go
Raw Permalink Normal View History

package inbound
import (
"net"
2023-11-03 21:01:45 +08:00
C "github.com/metacubex/mihomo/constant"
)
type Addition func(metadata *C.Metadata)
2023-10-11 22:54:19 +08:00
func ApplyAdditions(metadata *C.Metadata, additions ...Addition) {
for _, addition := range additions {
addition(metadata)
}
}
func WithInName(name string) Addition {
return func(metadata *C.Metadata) {
metadata.InName = name
}
}
func WithInUser(user string) Addition {
return func(metadata *C.Metadata) {
metadata.InUser = user
}
}
func WithSpecialRules(specialRules string) Addition {
return func(metadata *C.Metadata) {
metadata.SpecialRules = specialRules
}
}
func WithSpecialProxy(specialProxy string) Addition {
return func(metadata *C.Metadata) {
metadata.SpecialProxy = specialProxy
}
}
2023-10-11 22:54:19 +08:00
func WithDstAddr(addr net.Addr) Addition {
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{}
if err := m.SetRemoteAddr(addr);err ==nil{
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{}
if err := m.SetRemoteAddr(addr);err ==nil{
metadata.InIP = m.DstIP
metadata.InPort = m.DstPort
}
}
}