Skip to content

Commit 17e43ca

Browse files
committed
feat(canal): support ipv6 address
1 parent 5acb569 commit 17e43ca

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
@@ -202,10 +202,25 @@ func (d *Dumper) Dump(w io.Writer) error {
202202
if strings.Contains(d.Addr, "/") {
203203
args = append(args, fmt.Sprintf("--socket=%s", d.Addr))
204204
} 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))
209224
}
210225
}
211226

0 commit comments

Comments
 (0)