Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client/dial.go: support multiple remote ips #980

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions client/dial.go
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug on line 45

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after splitting comma, the port range has not been splitted with std.ParseMultiPort

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"crypto/rand"
"encoding/binary"
"fmt"
mrand "math/rand"
"net"
"strings"

"github.com/pkg/errors"
kcp "github.com/xtaci/kcp-go/v5"
Expand All @@ -36,18 +38,24 @@ import (

// dial connects to the remote address
func dial(config *Config, block kcp.BlockCrypt) (*kcp.UDPSession, error) {
mp, err := std.ParseMultiPort(config.RemoteAddr)
if err != nil {
return nil, err
}
var remoteAddr string
if strings.Contains(config.RemoteAddr, ",") {
parts := strings.Split(config.RemoteAddr, ",")
remoteAddr = parts[mrand.Intn(len(parts))]
} else {
mp, err := std.ParseMultiPort(config.RemoteAddr)
if err != nil {
return nil, err
}

// generate a random port
var randport uint64
err = binary.Read(rand.Reader, binary.LittleEndian, &randport)
if err != nil {
return nil, err
// generate a random port
var randport uint64
err = binary.Read(rand.Reader, binary.LittleEndian, &randport)
if err != nil {
return nil, err
}
remoteAddr = fmt.Sprintf("%v:%v", mp.Host, uint64(mp.MinPort)+randport%uint64(mp.MaxPort-mp.MinPort+1))
}
remoteAddr := fmt.Sprintf("%v:%v", mp.Host, uint64(mp.MinPort)+randport%uint64(mp.MaxPort-mp.MinPort+1))

// emulate TCP connection
if config.TCP {
Expand Down