Skip to content

Commit 6f8b774

Browse files
committed
feat(canal): support ipv6 address
1 parent f230d5f commit 6f8b774

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

canal/canal.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,25 @@ func (c *Canal) prepareSyncer() error {
481481
if strings.Contains(c.cfg.Addr, "/") {
482482
cfg.Host = c.cfg.Addr
483483
} 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)
487487
}
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)
490498
if err != nil {
491499
return errors.Trace(err)
492500
}
493501

494-
cfg.Host = seps[0]
502+
cfg.Host = addr
495503
cfg.Port = uint16(port)
496504
}
497505

dump/dumper.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,25 @@ func (d *Dumper) Dump(w io.Writer) error {
212212
if strings.Contains(d.Addr, "/") {
213213
args = append(args, fmt.Sprintf("--socket=%s", d.Addr))
214214
} else {
215-
seps := strings.SplitN(d.Addr, ":", 2)
216-
args = append(args, fmt.Sprintf("--host=%s", seps[0]))
217-
if len(seps) > 1 {
218-
args = append(args, fmt.Sprintf("--port=%s", seps[1]))
215+
ipv6 := strings.Count(d.Addr, ":") > 1
216+
lastSep := strings.LastIndex(d.Addr, ":")
217+
var host, port string
218+
// without port
219+
host = d.Addr
220+
// ipv6 with port
221+
if ipv6 && strings.ContainsAny(d.Addr, "[]") {
222+
host = strings.Trim(d.Addr[:lastSep], "[]")
223+
port = d.Addr[lastSep+1:]
224+
}
225+
// ipv4 with port
226+
if !ipv6 && lastSep != -1 {
227+
host = d.Addr[:lastSep]
228+
port = d.Addr[lastSep+1:]
229+
}
230+
231+
args = append(args, fmt.Sprintf("--host=%s", host))
232+
if port != "" {
233+
args = append(args, fmt.Sprintf("--port=%s", port))
219234
}
220235
}
221236

0 commit comments

Comments
 (0)