File tree 2 files changed +33
-10
lines changed
2 files changed +33
-10
lines changed Original file line number Diff line number Diff line change @@ -481,17 +481,25 @@ func (c *Canal) prepareSyncer() error {
481
481
if strings .Contains (c .cfg .Addr , "/" ) {
482
482
cfg .Host = c .cfg .Addr
483
483
} else {
484
- seps := strings .Split (c .cfg .Addr , ":" )
485
- if len ( seps ) != 2 {
486
- return errors .Errorf ("invalid mysql addr format %s, must host:port" , c .cfg .Addr )
484
+ ipv6 := strings .Count (c .cfg .Addr , ":" ) > 1
485
+ if ipv6 && ! strings . ContainsAny ( c . cfg . Addr , "[]" ) {
486
+ return errors .Errorf ("invalid mysql ipv6 addr format %s, must [ host] :port" , c .cfg .Addr )
487
487
}
488
-
489
- port , err := strconv .ParseUint (seps [1 ], 10 , 16 )
488
+ lastSep := strings .LastIndex (c .cfg .Addr , ":" )
489
+ if ! ipv6 && lastSep == - 1 {
490
+ return errors .Errorf ("invalid mysql ipv4 addr format %s, must host:port" , c .cfg .Addr )
491
+ }
492
+ addr := strings .Trim (c .cfg .Addr [:lastSep ], "[]" )
493
+ ip := net .ParseIP (addr )
494
+ if ip == nil {
495
+ return errors .Errorf ("invalid mysql ip format %s" , addr )
496
+ }
497
+ port , err := strconv .ParseUint (c .cfg .Addr [lastSep + 1 :], 10 , 16 )
490
498
if err != nil {
491
499
return errors .Trace (err )
492
500
}
493
501
494
- cfg .Host = seps [ 0 ]
502
+ cfg .Host = addr
495
503
cfg .Port = uint16 (port )
496
504
}
497
505
Original file line number Diff line number Diff line change @@ -202,10 +202,25 @@ func (d *Dumper) Dump(w io.Writer) error {
202
202
if strings .Contains (d .Addr , "/" ) {
203
203
args = append (args , fmt .Sprintf ("--socket=%s" , d .Addr ))
204
204
} else {
205
- seps := strings .SplitN (d .Addr , ":" , 2 )
206
- args = append (args , fmt .Sprintf ("--host=%s" , seps [0 ]))
207
- if len (seps ) > 1 {
208
- args = append (args , fmt .Sprintf ("--port=%s" , seps [1 ]))
205
+ ipv6 := strings .Count (d .Addr , ":" ) > 1
206
+ lastSep := strings .LastIndex (d .Addr , ":" )
207
+ var host , port string
208
+ // without port
209
+ host = d .Addr
210
+ // ipv6 with port
211
+ if ipv6 && strings .ContainsAny (d .Addr , "[]" ) {
212
+ host = strings .Trim (d .Addr [:lastSep ], "[]" )
213
+ port = d .Addr [lastSep + 1 :]
214
+ }
215
+ // ipv4 with port
216
+ if ! ipv6 && lastSep != - 1 {
217
+ host = d .Addr [:lastSep ]
218
+ port = d .Addr [lastSep + 1 :]
219
+ }
220
+
221
+ args = append (args , fmt .Sprintf ("--host=%s" , host ))
222
+ if port != "" {
223
+ args = append (args , fmt .Sprintf ("--port=%s" , port ))
209
224
}
210
225
}
211
226
You can’t perform that action at this time.
0 commit comments