Skip to content

Commit 595ae9b

Browse files
committed
feat: use net.SplitHostPort to parse ip with ports
1 parent 17e43ca commit 595ae9b

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

canal/canal.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -481,26 +481,17 @@ func (c *Canal) prepareSyncer() error {
481481
if strings.Contains(c.cfg.Addr, "/") {
482482
cfg.Host = c.cfg.Addr
483483
} else {
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-
}
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)
484+
host, port, err := net.SplitHostPort(c.cfg.Addr)
485+
if err != nil {
486+
return errors.Errorf("invalid mysql addr format %s, must host:port", c.cfg.Addr)
496487
}
497-
port, err := strconv.ParseUint(c.cfg.Addr[lastSep+1:], 10, 16)
488+
portNumber, err := strconv.ParseUint(port, 10, 16)
498489
if err != nil {
499490
return errors.Trace(err)
500491
}
501492

502-
cfg.Host = addr
503-
cfg.Port = uint16(port)
493+
cfg.Host = host
494+
cfg.Port = uint16(portNumber)
504495
}
505496

506497
c.syncer = replication.NewBinlogSyncer(cfg)

0 commit comments

Comments
 (0)