github.com/metacubex/mihomo@v1.18.5/adapter/inbound/addition.go (about) 1 package inbound 2 3 import ( 4 "net" 5 6 C "github.com/metacubex/mihomo/constant" 7 ) 8 9 type Addition func(metadata *C.Metadata) 10 11 func ApplyAdditions(metadata *C.Metadata, additions ...Addition) { 12 for _, addition := range additions { 13 addition(metadata) 14 } 15 } 16 17 func WithInName(name string) Addition { 18 return func(metadata *C.Metadata) { 19 metadata.InName = name 20 } 21 } 22 23 func WithInUser(user string) Addition { 24 return func(metadata *C.Metadata) { 25 metadata.InUser = user 26 } 27 } 28 29 func WithSpecialRules(specialRules string) Addition { 30 return func(metadata *C.Metadata) { 31 metadata.SpecialRules = specialRules 32 } 33 } 34 35 func WithSpecialProxy(specialProxy string) Addition { 36 return func(metadata *C.Metadata) { 37 metadata.SpecialProxy = specialProxy 38 } 39 } 40 41 func WithDstAddr(addr net.Addr) Addition { 42 return func(metadata *C.Metadata) { 43 _ = metadata.SetRemoteAddr(addr) 44 } 45 } 46 47 func WithSrcAddr(addr net.Addr) Addition { 48 return func(metadata *C.Metadata) { 49 m := C.Metadata{} 50 if err := m.SetRemoteAddr(addr); err == nil { 51 metadata.SrcIP = m.DstIP 52 metadata.SrcPort = m.DstPort 53 } 54 } 55 } 56 57 func WithInAddr(addr net.Addr) Addition { 58 return func(metadata *C.Metadata) { 59 m := C.Metadata{} 60 if err := m.SetRemoteAddr(addr); err == nil { 61 metadata.InIP = m.DstIP 62 metadata.InPort = m.DstPort 63 } 64 } 65 } 66 67 func WithDSCP(dscp uint8) Addition { 68 return func(metadata *C.Metadata) { 69 metadata.DSCP = dscp 70 } 71 }