@@ -25,7 +25,7 @@ const (
25
25
const NatChain = "1PANEL"
26
26
27
27
var (
28
- natListRegex = regexp .MustCompile (`^(\d+)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)(?:\s+(.+?) .+?:(\d{1,5}(?::\d+)?).+?[ :](.+-.+|(?:.+:)?\d{1,5}(?:-\d{1,5})?))?$` )
28
+ natListRegex = regexp .MustCompile (`^(\d+)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+?) (?:\s+(.+?) .+?:(\d{1,5}(?::\d+)?).+?[ :](.+-.+|(?:.+:)?\d{1,5}(?:-\d{1,5})?))?$` )
29
29
)
30
30
31
31
type Iptables struct {
@@ -92,7 +92,7 @@ func (iptables *Iptables) NatList(chain ...string) ([]IptablesNatInfo, error) {
92
92
if len (chain ) == 0 {
93
93
chain = append (chain , PreRoutingChain )
94
94
}
95
- stdout , err := iptables .outf (NatTab , "-nL %s --line" , chain [0 ])
95
+ stdout , err := iptables .outf (NatTab , "-nvL %s --line-numbers " , chain [0 ])
96
96
if err != nil {
97
97
return nil , err
98
98
}
@@ -104,35 +104,35 @@ func (iptables *Iptables) NatList(chain ...string) ([]IptablesNatInfo, error) {
104
104
})
105
105
if natListRegex .MatchString (line ) {
106
106
match := natListRegex .FindStringSubmatch (line )
107
- if ! strings .Contains (match [9 ], ":" ) {
108
- match [9 ] = fmt .Sprintf (":%s" , match [9 ])
107
+ if ! strings .Contains (match [13 ], ":" ) {
108
+ match [13 ] = fmt .Sprintf (":%s" , match [13 ])
109
109
}
110
110
forwardList = append (forwardList , IptablesNatInfo {
111
111
Num : match [1 ],
112
- Target : match [2 ],
113
- Protocol : match [7 ],
114
- Opt : match [4 ],
115
- Source : match [5 ],
116
- Destination : match [6 ],
117
- SrcPort : match [8 ],
118
- DestPort : match [9 ],
112
+ Target : match [4 ],
113
+ Protocol : match [11 ],
114
+ InIface : match [7 ],
115
+ OutIface : match [8 ],
116
+ Opt : match [6 ],
117
+ Source : match [9 ],
118
+ Destination : match [10 ],
119
+ SrcPort : match [12 ],
120
+ DestPort : match [13 ],
119
121
})
120
122
}
121
123
}
122
124
123
125
return forwardList , nil
124
126
}
125
127
126
- func (iptables * Iptables ) NatAdd (protocol , srcPort , dest , destPort string , save bool ) error {
128
+ func (iptables * Iptables ) NatAdd (protocol , srcPort , dest , destPort , iface string , save bool ) error {
127
129
if dest != "" && dest != "127.0.0.1" && dest != "localhost" {
128
- if err := iptables .runf (NatTab , fmt .Sprintf (
129
- "-A %s -p %s --dport %s -j DNAT --to-destination %s:%s" ,
130
- PreRoutingChain ,
131
- protocol ,
132
- srcPort ,
133
- dest ,
134
- destPort ,
135
- )); err != nil {
130
+ iptablesArg := fmt .Sprintf ("-A %s" , PreRoutingChain )
131
+ if iface != "" {
132
+ iptablesArg += fmt .Sprintf (" -i %s" , iface )
133
+ }
134
+ iptablesArg += fmt .Sprintf (" -p %s --dport %s -j DNAT --to-destination %s:%s" , protocol , srcPort , dest , destPort )
135
+ if err := iptables .runf (NatTab , iptablesArg ); err != nil {
136
136
return err
137
137
}
138
138
@@ -166,13 +166,12 @@ func (iptables *Iptables) NatAdd(protocol, srcPort, dest, destPort string, save
166
166
return err
167
167
}
168
168
} else {
169
- if err := iptables .runf (NatTab , fmt .Sprintf (
170
- "-A %s -p %s --dport %s -j REDIRECT --to-port %s" ,
171
- PreRoutingChain ,
172
- protocol ,
173
- srcPort ,
174
- destPort ,
175
- )); err != nil {
169
+ iptablesArg := fmt .Sprintf ("-A %s" , PreRoutingChain )
170
+ if iface != "" {
171
+ iptablesArg += fmt .Sprintf (" -i %s" , iface )
172
+ }
173
+ iptablesArg += fmt .Sprintf (" -p %s --dport %s -j REDIRECT --to-port %s" , protocol , srcPort , destPort )
174
+ if err := iptables .runf (NatTab , iptablesArg ); err != nil {
176
175
return err
177
176
}
178
177
}
@@ -183,12 +182,13 @@ func (iptables *Iptables) NatAdd(protocol, srcPort, dest, destPort string, save
183
182
Port : srcPort ,
184
183
TargetIP : dest ,
185
184
TargetPort : destPort ,
185
+ Interface : iface ,
186
186
}).Error
187
187
}
188
188
return nil
189
189
}
190
190
191
- func (iptables * Iptables ) NatRemove (num string , protocol , srcPort , dest , destPort string ) error {
191
+ func (iptables * Iptables ) NatRemove (num string , protocol , srcPort , dest , destPort , iface string ) error {
192
192
if err := iptables .runf (NatTab , "-D %s %s" , PreRoutingChain , num ); err != nil {
193
193
return err
194
194
}
@@ -226,11 +226,13 @@ func (iptables *Iptables) NatRemove(num string, protocol, srcPort, dest, destPor
226
226
}
227
227
228
228
global .DB .Where (
229
- "protocol = ? AND port = ? AND target_ip = ? AND target_port = ?" ,
229
+ "protocol = ? AND port = ? AND target_ip = ? AND target_port = ? AND (interface = ? OR (interface IS NULL AND ? = '')) " ,
230
230
protocol ,
231
231
srcPort ,
232
232
dest ,
233
233
destPort ,
234
+ iface ,
235
+ iface ,
234
236
).Delete (& model.Forward {})
235
237
return nil
236
238
}
@@ -249,7 +251,7 @@ func (iptables *Iptables) Reload() error {
249
251
var rules []model.Forward
250
252
global .DB .Find (& rules )
251
253
for _ , forward := range rules {
252
- if err := iptables .NatAdd (forward .Protocol , forward .Port , forward .TargetIP , forward .TargetPort , false ); err != nil {
254
+ if err := iptables .NatAdd (forward .Protocol , forward .Port , forward .TargetIP , forward .TargetPort , forward . Interface , false ); err != nil {
253
255
return err
254
256
}
255
257
}
0 commit comments