diff --git a/client/ip4p.go b/client/ip4p.go new file mode 100644 index 00000000000..7e547dd6b4b --- /dev/null +++ b/client/ip4p.go @@ -0,0 +1,36 @@ +// Copyright 2022 hev, r@hev.cc +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package client + +import ( + "net" +) + +func lookupIP4P(addr string, port int) (string, int) { + ips, err := net.LookupIP(addr) + if err == nil { + for _, ip := range ips { + if len(ip) == 16 { + if ip[0] == 0x20 && ip[1] == 0x01 && + ip[2] == 0x00 && ip[3] == 0x00 { + addr = net.IPv4(ip[12], ip[13], ip[14], ip[15]).String() + port = int(ip[10])<<8 | int(ip[11]) + break + } + } + } + } + return addr, port +} diff --git a/client/service.go b/client/service.go index b5366b1e319..9c0ed56c5f6 100644 --- a/client/service.go +++ b/client/service.go @@ -378,9 +378,10 @@ func (cm *ConnectionManager) OpenConnection() error { } tlsConfig.NextProtos = []string{"frp"} + addr, port := lookupIP4P(cm.cfg.ServerAddr, cm.cfg.ServerPort) conn, err := quic.DialAddrContext( cm.ctx, - net.JoinHostPort(cm.cfg.ServerAddr, strconv.Itoa(cm.cfg.ServerPort)), + net.JoinHostPort(addr, strconv.Itoa(port)), tlsConfig, &quic.Config{ MaxIdleTimeout: time.Duration(cm.cfg.QUICMaxIdleTimeout) * time.Second, MaxIncomingStreams: int64(cm.cfg.QUICMaxIncomingStreams), @@ -491,9 +492,10 @@ func (cm *ConnectionManager) realConnect() (net.Conn, error) { libdial.WithProxy(proxyType, addr), libdial.WithProxyAuth(auth), ) + addr, port := lookupIP4P(cm.cfg.ServerAddr, cm.cfg.ServerPort) conn, err := libdial.DialContext( cm.ctx, - net.JoinHostPort(cm.cfg.ServerAddr, strconv.Itoa(cm.cfg.ServerPort)), + net.JoinHostPort(addr, strconv.Itoa(port)), dialOptions..., ) return conn, err