Skip to content

Commit

Permalink
frpc: support ip4p
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Jul 22, 2023
1 parent 4fd6301 commit f5650d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
36 changes: 36 additions & 0 deletions client/ip4p.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 4 additions & 2 deletions client/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f5650d8

Please sign in to comment.