Skip to content

Commit

Permalink
Merge pull request #711 from mirokuratczyk/master
Browse files Browse the repository at this point in the history
Add HoldOffTunnelProtocol, HoldOffFrontingTunnel, HoldOffInproxy, and RestrictInproxy
  • Loading branch information
rod-hynes authored Nov 26, 2024
2 parents 97a2cdb + c128408 commit 15c34a0
Show file tree
Hide file tree
Showing 10 changed files with 404 additions and 73 deletions.
34 changes: 34 additions & 0 deletions psiphon/common/parameters/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ const (
HoldOffTunnelProtocols = "HoldOffTunnelProtocols"
HoldOffTunnelFrontingProviderIDs = "HoldOffTunnelFrontingProviderIDs"
HoldOffTunnelProbability = "HoldOffTunnelProbability"
HoldOffTunnelProtocolMinDuration = "HoldOffTunnelProtocolMinDuration"
HoldOffTunnelProtocolMaxDuration = "HoldOffTunnelProtocolMaxDuration"
HoldOffTunnelProtocolNames = "HoldOffTunnelProtocolNames"
HoldOffTunnelProtocolProbability = "HoldOffTunnelProtocolProbability"
HoldOffFrontingTunnelMinDuration = "HoldOffFrontingTunnelMinDuration"
HoldOffFrontingTunnelMaxDuration = "HoldOffFrontingTunnelMaxDuration"
HoldOffFrontingTunnelProviderIDs = "HoldOffFrontingTunnelProviderIDs"
HoldOffFrontingTunnelProbability = "HoldOffFrontingTunnelProbability"
RestrictFrontingProviderIDs = "RestrictFrontingProviderIDs"
RestrictFrontingProviderIDsServerProbability = "RestrictFrontingProviderIDsServerProbability"
RestrictFrontingProviderIDsClientProbability = "RestrictFrontingProviderIDsClientProbability"
Expand All @@ -325,6 +333,13 @@ const (
RestrictDirectProviderRegions = "RestrictDirectProviderRegions"
RestrictDirectProviderIDsServerProbability = "RestrictDirectProviderIDsServerProbability"
RestrictDirectProviderIDsClientProbability = "RestrictDirectProviderIDsClientProbability"
HoldOffInproxyTunnelMinDuration = "HoldOffInproxyTunnelMinDuration"
HoldOffInproxyTunnelMaxDuration = "HoldOffInproxyTunnelMaxDuration"
HoldOffInproxyTunnelProviderRegions = "HoldOffInproxyTunnelProviderRegions"
HoldOffInproxyTunnelProbability = "HoldOffInproxyTunnelProbability"
RestrictInproxyProviderRegions = "RestrictInproxyProviderRegions"
RestrictInproxyProviderIDsServerProbability = "RestrictInproxyProviderIDsServerProbability"
RestrictInproxyProviderIDsClientProbability = "RestrictInproxyProviderIDsClientProbability"
UpstreamProxyAllowAllServerEntrySources = "UpstreamProxyAllowAllServerEntrySources"
DestinationBytesMetricsASN = "DestinationBytesMetricsASN"
DestinationBytesMetricsASNs = "DestinationBytesMetricsASNs"
Expand Down Expand Up @@ -812,6 +827,16 @@ var defaultParameters = map[string]struct {
HoldOffTunnelFrontingProviderIDs: {value: []string{}},
HoldOffTunnelProbability: {value: 0.0, minimum: 0.0},

HoldOffTunnelProtocolMinDuration: {value: time.Duration(0), minimum: time.Duration(0)},
HoldOffTunnelProtocolMaxDuration: {value: time.Duration(0), minimum: time.Duration(0)},
HoldOffTunnelProtocolNames: {value: protocol.TunnelProtocols{}},
HoldOffTunnelProtocolProbability: {value: 0.0, minimum: 0.0},

HoldOffFrontingTunnelMinDuration: {value: time.Duration(0), minimum: time.Duration(0)},
HoldOffFrontingTunnelMaxDuration: {value: time.Duration(0), minimum: time.Duration(0)},
HoldOffFrontingTunnelProviderIDs: {value: []string{}},
HoldOffFrontingTunnelProbability: {value: 0.0, minimum: 0.0},

RestrictFrontingProviderIDs: {value: []string{}},
RestrictFrontingProviderIDsServerProbability: {value: 0.0, minimum: 0.0, flags: serverSideOnly},
RestrictFrontingProviderIDsClientProbability: {value: 0.0, minimum: 0.0},
Expand All @@ -825,6 +850,15 @@ var defaultParameters = map[string]struct {
RestrictDirectProviderIDsServerProbability: {value: 0.0, minimum: 0.0, flags: serverSideOnly},
RestrictDirectProviderIDsClientProbability: {value: 0.0, minimum: 0.0},

HoldOffInproxyTunnelMinDuration: {value: time.Duration(0), minimum: time.Duration(0)},
HoldOffInproxyTunnelMaxDuration: {value: time.Duration(0), minimum: time.Duration(0)},
HoldOffInproxyTunnelProviderRegions: {value: KeyStrings{}},
HoldOffInproxyTunnelProbability: {value: 0.0, minimum: 0.0},

RestrictInproxyProviderRegions: {value: KeyStrings{}},
RestrictInproxyProviderIDsServerProbability: {value: 0.0, minimum: 0.0, flags: serverSideOnly},
RestrictInproxyProviderIDsClientProbability: {value: 0.0, minimum: 0.0},

UpstreamProxyAllowAllServerEntrySources: {value: false},

DestinationBytesMetricsASN: {value: "", flags: serverSideOnly},
Expand Down
16 changes: 16 additions & 0 deletions psiphon/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,19 @@ func MergeContextCancel(ctx, cancelCtx context.Context) (context.Context, contex
cancel(context.Canceled)
}
}

// MaxDuration returns the maximum duration in durations or 0 if durations is
// empty.
func MaxDuration(durations ...time.Duration) time.Duration {
if len(durations) == 0 {
return 0
}

max := durations[0]
for _, d := range durations[1:] {
if d > max {
max = d
}
}
return max
}
169 changes: 135 additions & 34 deletions psiphon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,13 +872,19 @@ type Config struct {
ConjureSTUNServerAddresses []string
ConjureDTLSEmptyInitialPacketProbability *float64

// HoldOffTunnelMinDurationMilliseconds and other HoldOffTunnel fields are
// for testing purposes.
HoldOffTunnelMinDurationMilliseconds *int
HoldOffTunnelMaxDurationMilliseconds *int
HoldOffTunnelProtocols []string
HoldOffTunnelFrontingProviderIDs []string
HoldOffTunnelProbability *float64
// HoldOffTunnelProtocolMinDurationMilliseconds and other
// HoldOffTunnelProtocol fields are for testing purposes.
HoldOffTunnelProtocolMinDurationMilliseconds *int
HoldOffTunnelProtocolMaxDurationMilliseconds *int
HoldOffTunnelProtocolNames []string
HoldOffTunnelProtocolProbability *float64

// HoldOffFrontingTunnelMinDurationMilliseconds and other
// HoldOffFrontingTunnel fields are for testing purposes.
HoldOffFrontingTunnelMinDurationMilliseconds *int
HoldOffFrontingTunnelMaxDurationMilliseconds *int
HoldOffFrontingTunnelProviderIDs []string
HoldOffFrontingTunnelProbability *float64

// RestrictFrontingProviderIDs and other RestrictFrontingProviderIDs fields
// are for testing purposes.
Expand All @@ -897,6 +903,18 @@ type Config struct {
RestrictDirectProviderRegions map[string][]string
RestrictDirectProviderIDsClientProbability *float64

// HoldOffInproxyTunnelMinDurationMilliseconds and other HoldOffInproxy
// fields are for testing purposes.
HoldOffInproxyTunnelMinDurationMilliseconds *int
HoldOffInproxyTunnelMaxDurationMilliseconds *int
HoldOffInproxyTunnelProviderRegions map[string][]string
HoldOffInproxyTunnelProbability *float64

// RestrictInproxyProviderRegions and other RestrictInproxy fields are for
// testing purposes.
RestrictInproxyProviderRegions map[string][]string
RestrictInproxyProviderIDsClientProbability *float64

// UpstreamProxyAllowAllServerEntrySources is for testing purposes.
UpstreamProxyAllowAllServerEntrySources *bool

Expand Down Expand Up @@ -2202,24 +2220,36 @@ func (config *Config) makeConfigParameters() map[string]interface{} {
applyParameters[parameters.ConjureDTLSEmptyInitialPacketProbability] = *config.ConjureDTLSEmptyInitialPacketProbability
}

if config.HoldOffTunnelMinDurationMilliseconds != nil {
applyParameters[parameters.HoldOffTunnelMinDuration] = fmt.Sprintf("%dms", *config.HoldOffTunnelMinDurationMilliseconds)
if config.HoldOffTunnelProtocolMinDurationMilliseconds != nil {
applyParameters[parameters.HoldOffTunnelProtocolMinDuration] = fmt.Sprintf("%dms", *config.HoldOffTunnelProtocolMinDurationMilliseconds)
}

if config.HoldOffTunnelProtocolMaxDurationMilliseconds != nil {
applyParameters[parameters.HoldOffTunnelProtocolMaxDuration] = fmt.Sprintf("%dms", *config.HoldOffTunnelProtocolMaxDurationMilliseconds)
}

if config.HoldOffTunnelMaxDurationMilliseconds != nil {
applyParameters[parameters.HoldOffTunnelMaxDuration] = fmt.Sprintf("%dms", *config.HoldOffTunnelMaxDurationMilliseconds)
if len(config.HoldOffTunnelProtocolNames) > 0 {
applyParameters[parameters.HoldOffTunnelProtocolNames] = protocol.TunnelProtocols(config.HoldOffTunnelProtocolNames)
}

if len(config.HoldOffTunnelProtocols) > 0 {
applyParameters[parameters.HoldOffTunnelProtocols] = protocol.TunnelProtocols(config.HoldOffTunnelProtocols)
if config.HoldOffTunnelProtocolProbability != nil {
applyParameters[parameters.HoldOffTunnelProtocolProbability] = *config.HoldOffTunnelProtocolProbability
}

if len(config.HoldOffTunnelFrontingProviderIDs) > 0 {
applyParameters[parameters.HoldOffTunnelFrontingProviderIDs] = config.HoldOffTunnelFrontingProviderIDs
if config.HoldOffFrontingTunnelMinDurationMilliseconds != nil {
applyParameters[parameters.HoldOffFrontingTunnelMinDuration] = fmt.Sprintf("%dms", *config.HoldOffFrontingTunnelMinDurationMilliseconds)
}

if config.HoldOffTunnelProbability != nil {
applyParameters[parameters.HoldOffTunnelProbability] = *config.HoldOffTunnelProbability
if config.HoldOffFrontingTunnelMaxDurationMilliseconds != nil {
applyParameters[parameters.HoldOffFrontingTunnelMaxDuration] = fmt.Sprintf("%dms", *config.HoldOffFrontingTunnelMaxDurationMilliseconds)
}

if len(config.HoldOffFrontingTunnelProviderIDs) > 0 {
applyParameters[parameters.HoldOffFrontingTunnelProviderIDs] = config.HoldOffFrontingTunnelProviderIDs
}

if config.HoldOffFrontingTunnelProbability != nil {
applyParameters[parameters.HoldOffFrontingTunnelProbability] = *config.HoldOffFrontingTunnelProbability
}

if config.HoldOffDirectTunnelMinDurationMilliseconds != nil {
Expand Down Expand Up @@ -2254,6 +2284,22 @@ func (config *Config) makeConfigParameters() map[string]interface{} {
applyParameters[parameters.RestrictFrontingProviderIDsClientProbability] = *config.RestrictFrontingProviderIDsClientProbability
}

if config.HoldOffInproxyTunnelMinDurationMilliseconds != nil {
applyParameters[parameters.HoldOffInproxyTunnelMinDuration] = fmt.Sprintf("%dms", *config.HoldOffInproxyTunnelMinDurationMilliseconds)
}

if config.HoldOffInproxyTunnelMaxDurationMilliseconds != nil {
applyParameters[parameters.HoldOffInproxyTunnelMaxDuration] = fmt.Sprintf("%dms", *config.HoldOffInproxyTunnelMaxDurationMilliseconds)
}

if len(config.HoldOffInproxyTunnelProviderRegions) > 0 {
applyParameters[parameters.HoldOffInproxyTunnelProviderRegions] = parameters.KeyStrings(config.HoldOffInproxyTunnelProviderRegions)
}

if config.HoldOffInproxyTunnelProbability != nil {
applyParameters[parameters.HoldOffInproxyTunnelProbability] = *config.HoldOffInproxyTunnelProbability
}

if config.UpstreamProxyAllowAllServerEntrySources != nil {
applyParameters[parameters.UpstreamProxyAllowAllServerEntrySources] = *config.UpstreamProxyAllowAllServerEntrySources
}
Expand Down Expand Up @@ -3005,30 +3051,50 @@ func (config *Config) setDialParametersHash() {
}
}

if config.HoldOffTunnelMinDurationMilliseconds != nil {
hash.Write([]byte("HoldOffTunnelMinDurationMilliseconds"))
binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffTunnelMinDurationMilliseconds))
if config.HoldOffTunnelProtocolMinDurationMilliseconds != nil {
hash.Write([]byte("HoldOffTunnelProtocolMinDurationMilliseconds"))
binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffTunnelProtocolMinDurationMilliseconds))
}

if config.HoldOffTunnelMaxDurationMilliseconds != nil {
hash.Write([]byte("HoldOffTunnelMaxDurationMilliseconds"))
binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffTunnelMaxDurationMilliseconds))
if config.HoldOffTunnelProtocolMaxDurationMilliseconds != nil {
hash.Write([]byte("HoldOffTunnelProtocolMaxDurationMilliseconds"))
binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffTunnelProtocolMaxDurationMilliseconds))
}

if len(config.HoldOffTunnelProtocols) > 0 {
hash.Write([]byte("HoldOffTunnelProtocols"))
for _, protocol := range config.HoldOffTunnelProtocols {
if len(config.HoldOffTunnelProtocolNames) > 0 {
hash.Write([]byte("HoldOffTunnelProtocolNames"))
for _, protocol := range config.HoldOffTunnelProtocolNames {
hash.Write([]byte(protocol))
}
}

if len(config.HoldOffTunnelFrontingProviderIDs) > 0 {
hash.Write([]byte("HoldOffTunnelFrontingProviderIDs"))
for _, providerID := range config.HoldOffTunnelFrontingProviderIDs {
if config.HoldOffTunnelProtocolProbability != nil {
hash.Write([]byte("HoldOffTunnelProtocolProbability"))
binary.Write(hash, binary.LittleEndian, *config.HoldOffTunnelProtocolProbability)
}

if config.HoldOffFrontingTunnelMinDurationMilliseconds != nil {
hash.Write([]byte("HoldOffFrontingTunnelMinDurationMilliseconds"))
binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffFrontingTunnelMinDurationMilliseconds))
}

if config.HoldOffFrontingTunnelMaxDurationMilliseconds != nil {
hash.Write([]byte("HoldOffFrontingTunnelMaxDurationMilliseconds"))
binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffFrontingTunnelMaxDurationMilliseconds))
}

if len(config.HoldOffFrontingTunnelProviderIDs) > 0 {
hash.Write([]byte("HoldOffFrontingTunnelProviderIDs"))
for _, providerID := range config.HoldOffFrontingTunnelProviderIDs {
hash.Write([]byte(providerID))
}
}

if config.HoldOffFrontingTunnelProbability != nil {
hash.Write([]byte("HoldOffFrontingTunnelProbability"))
binary.Write(hash, binary.LittleEndian, *config.HoldOffFrontingTunnelProbability)
}

if config.HoldOffDirectTunnelProbability != nil {
hash.Write([]byte("HoldOffDirectTunnelProbability"))
binary.Write(hash, binary.LittleEndian, *config.HoldOffDirectTunnelProbability)
Expand All @@ -3054,11 +3120,6 @@ func (config *Config) setDialParametersHash() {
}
}

if config.HoldOffTunnelProbability != nil {
hash.Write([]byte("HoldOffTunnelProbability"))
binary.Write(hash, binary.LittleEndian, *config.HoldOffTunnelProbability)
}

if len(config.RestrictDirectProviderRegions) > 0 {
hash.Write([]byte("RestrictDirectProviderRegions"))
for providerID, regions := range config.RestrictDirectProviderRegions {
Expand Down Expand Up @@ -3086,6 +3147,46 @@ func (config *Config) setDialParametersHash() {
binary.Write(hash, binary.LittleEndian, *config.RestrictFrontingProviderIDsClientProbability)
}

if config.HoldOffInproxyTunnelProbability != nil {
hash.Write([]byte("HoldOffInproxyTunnelProbability"))
binary.Write(hash, binary.LittleEndian, *config.HoldOffInproxyTunnelProbability)
}

if config.HoldOffInproxyTunnelMinDurationMilliseconds != nil {
hash.Write([]byte("HoldOffInproxyTunnelMinDurationMilliseconds"))
binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffInproxyTunnelMinDurationMilliseconds))
}

if config.HoldOffInproxyTunnelMaxDurationMilliseconds != nil {
hash.Write([]byte("HoldOffInproxyTunnelMaxDurationMilliseconds"))
binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffInproxyTunnelMaxDurationMilliseconds))
}

if len(config.HoldOffInproxyTunnelProviderRegions) > 0 {
hash.Write([]byte("HoldOffInproxyTunnelProviderRegions"))
for providerID, regions := range config.HoldOffInproxyTunnelProviderRegions {
hash.Write([]byte(providerID))
for _, region := range regions {
hash.Write([]byte(region))
}
}
}

if len(config.RestrictInproxyProviderRegions) > 0 {
hash.Write([]byte("RestrictInproxyProviderRegions"))
for providerID, regions := range config.RestrictInproxyProviderRegions {
hash.Write([]byte(providerID))
for _, region := range regions {
hash.Write([]byte(region))
}
}
}

if config.RestrictInproxyProviderIDsClientProbability != nil {
hash.Write([]byte("RestrictInproxyProviderIDsClientProbability"))
binary.Write(hash, binary.LittleEndian, *config.RestrictInproxyProviderIDsClientProbability)
}

if config.UpstreamProxyAllowAllServerEntrySources != nil {
hash.Write([]byte("UpstreamProxyAllowAllServerEntrySources"))
binary.Write(hash, binary.LittleEndian, *config.UpstreamProxyAllowAllServerEntrySources)
Expand Down
Loading

0 comments on commit 15c34a0

Please sign in to comment.