@@ -17,18 +17,25 @@ type GEOIP struct {
17
17
country string
18
18
adapter string
19
19
noResolveIP bool
20
+ isSourceIP bool
20
21
geoIPMatcher * router.GeoIPMatcher
21
22
recodeSize int
22
23
}
23
24
24
25
var _ C.Rule = (* GEOIP )(nil )
25
26
26
27
func (g * GEOIP ) RuleType () C.RuleType {
28
+ if g .isSourceIP {
29
+ return C .SrcGEOIP
30
+ }
27
31
return C .GEOIP
28
32
}
29
33
30
34
func (g * GEOIP ) Match (metadata * C.Metadata ) (bool , string ) {
31
35
ip := metadata .DstIP
36
+ if g .isSourceIP {
37
+ ip = metadata .SrcIP
38
+ }
32
39
if ! ip .IsValid () {
33
40
return false , ""
34
41
}
@@ -49,6 +56,16 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
49
56
}
50
57
51
58
if ! C .GeodataMode {
59
+ if g .isSourceIP {
60
+ codes := mmdb .IPInstance ().LookupCode (ip .AsSlice ())
61
+ for _ , code := range codes {
62
+ if g .country == code {
63
+ return true , g .adapter
64
+ }
65
+ }
66
+ return false , g .adapter
67
+ }
68
+
52
69
if metadata .DstGeoIP != nil {
53
70
return false , g .adapter
54
71
}
@@ -62,7 +79,7 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
62
79
}
63
80
64
81
match := g .geoIPMatcher .Match (ip )
65
- if match {
82
+ if match && ! g . isSourceIP {
66
83
metadata .DstGeoIP = append (metadata .DstGeoIP , g .country )
67
84
}
68
85
return match , g .adapter
@@ -92,7 +109,7 @@ func (g *GEOIP) GetRecodeSize() int {
92
109
return g .recodeSize
93
110
}
94
111
95
- func NewGEOIP (country string , adapter string , noResolveIP bool ) (* GEOIP , error ) {
112
+ func NewGEOIP (country string , adapter string , isSrc , noResolveIP bool ) (* GEOIP , error ) {
96
113
if err := geodata .InitGeoIP (); err != nil {
97
114
log .Errorln ("can't initial GeoIP: %s" , err )
98
115
return nil , err
@@ -105,6 +122,7 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
105
122
country : country ,
106
123
adapter : adapter ,
107
124
noResolveIP : noResolveIP ,
125
+ isSourceIP : isSrc ,
108
126
}
109
127
return geoip , nil
110
128
}
@@ -120,6 +138,7 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
120
138
country : country ,
121
139
adapter : adapter ,
122
140
noResolveIP : noResolveIP ,
141
+ isSourceIP : isSrc ,
123
142
geoIPMatcher : geoIPMatcher ,
124
143
recodeSize : size ,
125
144
}
0 commit comments