Skip to content

Commit

Permalink
disco: add PortScanOffset config
Browse files Browse the repository at this point in the history
  • Loading branch information
rkonfj committed Jun 1, 2024
1 parent e050b0e commit 95f057a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cmd/pgcli/vpn/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func init() {
Cmd.Flags().StringSlice("allowed-ip", []string{}, "declare IPs that can be routed/NATed by this machine (i.e. 192.168.0.0/24)")
Cmd.Flags().StringSlice("peer", []string{}, "specify peers instead of auto-discovery (pg://<peerID>?alias1=<ipv4>&alias2=<ipv6>)")

Cmd.Flags().Int("disco-port-scan-offset", -500, "scan ports offset when disco")
Cmd.Flags().Int("disco-port-scan-count", 2000, "scan ports count when disco")
Cmd.Flags().Int("disco-challenges-retry", 5, "ping challenges retry count when disco")
Cmd.Flags().Duration("disco-challenges-initial-interval", 200*time.Millisecond, "ping challenges initial interval when disco")
Expand All @@ -74,6 +75,10 @@ func run(cmd *cobra.Command, args []string) (err error) {
}

func createConfig(cmd *cobra.Command) (cfg Config, err error) {
cfg.DiscoPortScanOffset, err = cmd.Flags().GetInt("disco-port-scan-offset")
if err != nil {
return
}
cfg.DiscoPortScanCount, err = cmd.Flags().GetInt("disco-port-scan-count")
if err != nil {
return
Expand Down Expand Up @@ -131,6 +136,7 @@ func createConfig(cmd *cobra.Command) (cfg Config, err error) {

type Config struct {
iface.Config
DiscoPortScanOffset int
DiscoPortScanCount int
DiscoChallengesRetry int
DiscoChallengesInitialInterval time.Duration
Expand Down Expand Up @@ -164,6 +170,7 @@ func (v *P2PVPN) Run(ctx context.Context) error {

func (v *P2PVPN) listenPacketConn(ctx context.Context) (c net.PacketConn, err error) {
disco.SetModifyDiscoConfig(func(cfg *disco.DiscoConfig) {
cfg.PortScanOffset = v.Config.DiscoPortScanOffset
cfg.PortScanCount = v.Config.DiscoPortScanCount
cfg.ChallengesRetry = v.Config.DiscoChallengesRetry
cfg.ChallengesInitialInterval = v.Config.DiscoChallengesInitialInterval
Expand Down
2 changes: 2 additions & 0 deletions disco/disco.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ var (
)

var defaultDiscoConfig = DiscoConfig{
PortScanOffset: -500,
PortScanCount: 2000,
ChallengesRetry: 5,
ChallengesInitialInterval: 200 * time.Millisecond,
ChallengesBackoffRate: 1.65,
}

type DiscoConfig struct {
PortScanOffset int
PortScanCount int
ChallengesRetry int
ChallengesInitialInterval time.Duration
Expand Down
2 changes: 1 addition & 1 deletion disco/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (c *UDPConn) RunDiscoMessageSendLoop(peerID peer.ID, addr *net.UDPAddr) {

if ctx, ok := c.FindPeer(peerID); (!ok || !ctx.Ready()) && addr.IP.To4() != nil && !addr.IP.IsPrivate() {
slog.Info("[UDP] PortScanning", "peer", peerID, "addr", addr)
for port := addr.Port + 1; port <= addr.Port+defaultDiscoConfig.PortScanCount; port++ {
for port := addr.Port + defaultDiscoConfig.PortScanOffset; port <= addr.Port+defaultDiscoConfig.PortScanCount; port++ {
select {
case <-c.closedSig:
return
Expand Down

0 comments on commit 95f057a

Please sign in to comment.