Skip to content

Commit

Permalink
fix cannot login when use ipv6 address (#1516)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexustar authored Nov 1, 2023
1 parent 6c0c860 commit 49fda3d
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 32 deletions.
9 changes: 5 additions & 4 deletions cmd/tidb-dashboard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/signal"
"path"
"strconv"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -240,7 +241,7 @@ func main() {

loadDistroStringsRes()

listenAddr := fmt.Sprintf("%s:%d", cliConfig.ListenHost, cliConfig.ListenPort)
listenAddr := net.JoinHostPort(cliConfig.ListenHost, strconv.Itoa(cliConfig.ListenPort))
listener, err := net.Listen("tcp", listenAddr)
if err != nil {
log.Fatal("Dashboard server listen failed", zap.String("addr", listenAddr), zap.Error(err))
Expand Down Expand Up @@ -273,9 +274,9 @@ func main() {
mux.Handle(config.SwaggerPathPrefix, swaggerserver.Handler())

log.Info(fmt.Sprintf("Dashboard server is listening at %s", listenAddr))
log.Info(fmt.Sprintf("UI: http://%s:%d/dashboard/", cliConfig.ListenHost, cliConfig.ListenPort))
log.Info(fmt.Sprintf("API: http://%s:%d/dashboard/api/", cliConfig.ListenHost, cliConfig.ListenPort))
log.Info(fmt.Sprintf("Swagger: http://%s:%d/dashboard/api/swagger/", cliConfig.ListenHost, cliConfig.ListenPort))
log.Info(fmt.Sprintf("UI: http://%s/dashboard/", net.JoinHostPort(cliConfig.ListenHost, strconv.Itoa(cliConfig.ListenPort))))
log.Info(fmt.Sprintf("API: http://%s/dashboard/api/", net.JoinHostPort(cliConfig.ListenHost, strconv.Itoa(cliConfig.ListenPort))))
log.Info(fmt.Sprintf("Swagger: http://%s/dashboard/api/swagger/", net.JoinHostPort(cliConfig.ListenHost, strconv.Itoa(cliConfig.ListenPort))))

srv := &http.Server{Handler: mux} // nolint:gosec
var wg sync.WaitGroup
Expand Down
19 changes: 10 additions & 9 deletions pkg/apiserver/clusterinfo/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
package clusterinfo

import (
"fmt"
"net"
"sort"
"strconv"

"github.com/samber/lo"
"gorm.io/gorm"
Expand Down Expand Up @@ -83,8 +84,8 @@ func (s *Service) calculateStatistics(db *gorm.DB) (*ClusterStatistics, error) {
for _, i := range pdInfo {
globalHostsSet[i.IP] = struct{}{}
globalVersionsSet[i.Version] = struct{}{}
globalInfo.instances[fmt.Sprintf("%s:%d", i.IP, i.Port)] = struct{}{}
infoByIk["pd"].instances[fmt.Sprintf("%s:%d", i.IP, i.Port)] = struct{}{}
globalInfo.instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
infoByIk["pd"].instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
}
tikvInfo, tiFlashInfo, err := topology.FetchStoreTopology(s.params.PDClient)
if err != nil {
Expand All @@ -93,14 +94,14 @@ func (s *Service) calculateStatistics(db *gorm.DB) (*ClusterStatistics, error) {
for _, i := range tikvInfo {
globalHostsSet[i.IP] = struct{}{}
globalVersionsSet[i.Version] = struct{}{}
globalInfo.instances[fmt.Sprintf("%s:%d", i.IP, i.Port)] = struct{}{}
infoByIk["tikv"].instances[fmt.Sprintf("%s:%d", i.IP, i.Port)] = struct{}{}
globalInfo.instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
infoByIk["tikv"].instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
}
for _, i := range tiFlashInfo {
globalHostsSet[i.IP] = struct{}{}
globalVersionsSet[i.Version] = struct{}{}
globalInfo.instances[fmt.Sprintf("%s:%d", i.IP, i.Port)] = struct{}{}
infoByIk["tiflash"].instances[fmt.Sprintf("%s:%d", i.IP, i.Port)] = struct{}{}
globalInfo.instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
infoByIk["tiflash"].instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
}
tidbInfo, err := topology.FetchTiDBTopology(s.lifecycleCtx, s.params.EtcdClient)
if err != nil {
Expand All @@ -109,8 +110,8 @@ func (s *Service) calculateStatistics(db *gorm.DB) (*ClusterStatistics, error) {
for _, i := range tidbInfo {
globalHostsSet[i.IP] = struct{}{}
globalVersionsSet[i.Version] = struct{}{}
globalInfo.instances[fmt.Sprintf("%s:%d", i.IP, i.Port)] = struct{}{}
infoByIk["tidb"].instances[fmt.Sprintf("%s:%d", i.IP, i.Port)] = struct{}{}
globalInfo.instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
infoByIk["tidb"].instances[net.JoinHostPort(i.IP, strconv.Itoa(int(i.Port)))] = struct{}{}
}

// Fill from hardware info
Expand Down
8 changes: 5 additions & 3 deletions pkg/apiserver/configuration/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"context"
"encoding/json"
"fmt"
"net"
"sort"
"strconv"

"github.com/joomcode/errorx"
"go.etcd.io/etcd/clientv3"
Expand Down Expand Up @@ -105,7 +107,7 @@ func (s *Service) getConfigItemsFromPD() (map[string]interface{}, error) {
}

func (s *Service) getConfigItemsFromTiDBToChannel(tidb *topology.TiDBInfo, ch chan<- channelItem) {
displayAddress := fmt.Sprintf("%s:%d", tidb.IP, tidb.Port)
displayAddress := net.JoinHostPort(tidb.IP, strconv.Itoa(int(tidb.Port)))

r, err := s.getConfigItemsFromTiDB(tidb.IP, int(tidb.StatusPort))
if err != nil {
Expand All @@ -129,7 +131,7 @@ func (s *Service) getConfigItemsFromTiDB(host string, statusPort int) (map[strin
}

func (s *Service) getConfigItemsFromTiKVToChannel(tikv *topology.StoreInfo, ch chan<- channelItem) {
displayAddress := fmt.Sprintf("%s:%d", tikv.IP, tikv.Port)
displayAddress := net.JoinHostPort(tikv.IP, strconv.Itoa(int(tikv.Port)))

r, err := s.getConfigItemsFromTiKV(tikv.IP, int(tikv.StatusPort))
if err != nil {
Expand Down Expand Up @@ -335,7 +337,7 @@ func (s *Service) editConfig(db *gorm.DB, kind ItemKind, id string, newValue int
// TODO: What about tombstone stores?
_, err := s.params.TiKVClient.SendPostRequest(kvStore.IP, int(kvStore.StatusPort), "/config", bytes.NewBuffer(bodyJSON))
if err != nil {
failures = append(failures, ErrEditFailed.Wrap(err, "Failed to edit config for TiKV instance `%s:%d`", kvStore.IP, kvStore.Port))
failures = append(failures, ErrEditFailed.Wrap(err, "Failed to edit config for TiKV instance `%s`", net.JoinHostPort(kvStore.IP, strconv.Itoa(int(kvStore.Port)))))
}
}
if len(failures) == len(tikvInfo) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/apiserver/debugapi/endpoint/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
"regexp"
"strconv"

"go.etcd.io/etcd/clientv3"

Expand Down Expand Up @@ -192,7 +194,7 @@ func (p *ResolvedRequestPayload) SendRequestAndPipe(
}
req := httpClient.LR().
SetDebugTag("origin:debug_api").
SetTLSAwareBaseURL(fmt.Sprintf("http://%s:%d", p.host, p.port)).
SetTLSAwareBaseURL(fmt.Sprintf("http://%s", net.JoinHostPort(p.host, strconv.Itoa(p.port)))).
SetMethod(p.api.Method).
SetURL(p.path).
SetQueryParamsFromValues(p.queryValues)
Expand Down
3 changes: 2 additions & 1 deletion pkg/apiserver/logsearch/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"math"
"net"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -168,7 +169,7 @@ func (t *Task) SyncRun() {
secureOpt = grpc.WithTransportCredentials(creds)
}

conn, err := grpc.Dial(fmt.Sprintf("%s:%d", t.model.Target.IP, t.model.Target.Port),
conn, err := grpc.Dial(net.JoinHostPort(t.model.Target.IP, strconv.Itoa(t.model.Target.Port)),
secureOpt,
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(MaxRecvMsgSize)),
)
Expand Down
4 changes: 3 additions & 1 deletion pkg/apiserver/metrics/prom_resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/url"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -84,7 +86,7 @@ func (s *Service) resolveDeployedPromAddress() (string, error) {
if pi == nil {
return "", nil
}
return fmt.Sprintf("http://%s:%d", pi.IP, pi.Port), nil
return fmt.Sprintf("http://%s", net.JoinHostPort(pi.IP, strconv.Itoa(int(pi.Port)))), nil
}

// Resolve the final Prometheus address. When user has customized an address, this address is returned. Otherwise,
Expand Down
3 changes: 2 additions & 1 deletion pkg/apiserver/profiling/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "embed"
"fmt"
"io"
"net"
"os/exec"
"strconv"
"strings"
Expand Down Expand Up @@ -128,7 +129,7 @@ type pdFetcher struct {
}

func (f *pdFetcher) fetch(op *fetchOptions) ([]byte, error) {
baseURL := fmt.Sprintf("%s://%s:%d", f.statusAPIHTTPScheme, op.ip, op.port)
baseURL := fmt.Sprintf("%s://%s", f.statusAPIHTTPScheme, net.JoinHostPort(op.ip, strconv.Itoa(op.port)))
return f.client.
WithTimeout(maxProfilingTimeout).
WithBaseURL(baseURL).
Expand Down
4 changes: 3 additions & 1 deletion pkg/pd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"context"
"fmt"
"io"
"net"
"net/http"
"strconv"
"time"

"go.uber.org/fx"
Expand Down Expand Up @@ -56,7 +58,7 @@ func (c Client) WithBaseURL(baseURL string) *Client {
}

func (c Client) WithAddress(host string, port int) *Client {
c.baseURL = fmt.Sprintf("%s://%s:%d", c.httpScheme, host, port)
c.baseURL = fmt.Sprintf("%s://%s", c.httpScheme, net.JoinHostPort(host, strconv.Itoa(port)))
return &c
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/tidb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"net/http"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -90,18 +91,18 @@ func (c Client) WithStatusAPITimeout(timeout time.Duration) *Client {
}

func (c Client) WithStatusAPIAddress(host string, statusPort int) *Client {
c.statusAPIAddress = fmt.Sprintf("%s:%d", host, statusPort)
c.statusAPIAddress = net.JoinHostPort(host, strconv.Itoa(statusPort))
return &c
}

func (c Client) WithEnforcedStatusAPIAddress(host string, statusPort int) *Client {
c.enforceStatusAPIAddresss = true
c.statusAPIAddress = fmt.Sprintf("%s:%d", host, statusPort)
c.statusAPIAddress = net.JoinHostPort(host, strconv.Itoa(statusPort))
return &c
}

func (c Client) WithSQLAPIAddress(host string, sqlPort int) *Client {
c.sqlAPIAddress = fmt.Sprintf("%s:%d", host, sqlPort)
c.sqlAPIAddress = net.JoinHostPort(host, strconv.Itoa(sqlPort))
return &c
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/tidb/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"
"net"
"strconv"
"time"

"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -85,8 +86,8 @@ func (f *Forwarder) pollingForTiDB() {
tidbEndpoints := make(map[string]struct{}, len(allTiDB))
for _, server := range allTiDB {
if server.Status == topology.ComponentStatusUp {
tidbEndpoints[fmt.Sprintf("%s:%d", server.IP, server.Port)] = struct{}{}
statusEndpoints[fmt.Sprintf("%s:%d", server.IP, server.StatusPort)] = struct{}{}
tidbEndpoints[net.JoinHostPort(server.IP, strconv.Itoa(int(server.Port)))] = struct{}{}
statusEndpoints[net.JoinHostPort(server.IP, strconv.Itoa(int(server.StatusPort)))] = struct{}{}
}
}
f.sqlProxy.updateRemotes(tidbEndpoints)
Expand Down
6 changes: 4 additions & 2 deletions pkg/tiflash/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"context"
"fmt"
"io"
"net"
"net/http"
"strconv"
"time"

"go.uber.org/fx"
Expand Down Expand Up @@ -58,7 +60,7 @@ func (c Client) AddRequestHeader(key, value string) *Client {
}

func (c *Client) Get(host string, statusPort int, relativeURI string) (*httpc.Response, error) {
uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, relativeURI)
uri := fmt.Sprintf("%s://%s%s", c.httpScheme, net.JoinHostPort(host, strconv.Itoa(statusPort)), relativeURI)
return c.httpClient.WithTimeout(c.timeout).Send(c.lifecycleCtx, uri, http.MethodGet, nil, ErrFlashClientRequestFailed, distro.R().TiFlash)
}

Expand All @@ -71,6 +73,6 @@ func (c *Client) SendGetRequest(host string, statusPort int, relativeURI string)
}

func (c *Client) SendPostRequest(host string, statusPort int, relativeURI string, body io.Reader) ([]byte, error) {
uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, relativeURI)
uri := fmt.Sprintf("%s://%s%s", c.httpScheme, net.JoinHostPort(host, strconv.Itoa(statusPort)), relativeURI)
return c.httpClient.WithTimeout(c.timeout).SendRequest(c.lifecycleCtx, uri, http.MethodPost, body, ErrFlashClientRequestFailed, distro.R().TiFlash)
}
6 changes: 4 additions & 2 deletions pkg/tikv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"context"
"fmt"
"io"
"net"
"net/http"
"strconv"
"time"

"go.uber.org/fx"
Expand Down Expand Up @@ -58,7 +60,7 @@ func (c Client) AddRequestHeader(key, value string) *Client {
}

func (c *Client) Get(host string, statusPort int, relativeURI string) (*httpc.Response, error) {
uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, relativeURI)
uri := fmt.Sprintf("%s://%s%s", c.httpScheme, net.JoinHostPort(host, strconv.Itoa(statusPort)), relativeURI)
return c.httpClient.WithTimeout(c.timeout).Send(c.lifecycleCtx, uri, http.MethodGet, nil, ErrTiKVClientRequestFailed, distro.R().TiKV)
}

Expand All @@ -71,6 +73,6 @@ func (c *Client) SendGetRequest(host string, statusPort int, relativeURI string)
}

func (c *Client) SendPostRequest(host string, statusPort int, relativeURI string, body io.Reader) ([]byte, error) {
uri := fmt.Sprintf("%s://%s:%d%s", c.httpScheme, host, statusPort, relativeURI)
uri := fmt.Sprintf("%s://%s%s", c.httpScheme, net.JoinHostPort(host, strconv.Itoa(statusPort)), relativeURI)
return c.httpClient.WithTimeout(c.timeout).SendRequest(c.lifecycleCtx, uri, http.MethodPost, body, ErrTiKVClientRequestFailed, distro.R().TiKV)
}
5 changes: 3 additions & 2 deletions util/client/tidbclient/sql_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ package tidbclient
import (
"context"
"errors"
"fmt"
"net"
"strconv"
"time"

"github.com/VividCortex/mysqlerr"
Expand Down Expand Up @@ -50,7 +51,7 @@ func NewSQLClient(config SQLClientConfig) *SQLClient {
func (c *SQLClient) OpenConn(user string, pass string) (*gorm.DB, error) {
dsnConfig := mysql.NewConfig()
dsnConfig.Net = "tcp"
dsnConfig.Addr = fmt.Sprintf("%s:%d", c.config.Host, c.config.Port)
dsnConfig.Addr = net.JoinHostPort(c.config.Host, strconv.Itoa(c.config.Port))
dsnConfig.User = user
dsnConfig.Passwd = pass
dsnConfig.Timeout = time.Second * 60
Expand Down

0 comments on commit 49fda3d

Please sign in to comment.