Skip to content

Commit e8647b6

Browse files
committed
cfg:
- Added the SemiLastValid selector! - Added support for the Percent/PercentRoundRobin selectors - Python support in tools/config.py - Reworked the Group switch code. - Fixed a bug where Jitter values supplied in the command line could only be ints. - Fixed a bug where specifying the '-p' print arg but NOT specifiying the JSON '-j' arg causes JSON output still instead of the standard string output. pipe: - Removed some code that cause a double 'CloseHandle' call when closing the Pipe Listener on Windows.
1 parent 2eb9601 commit e8647b6

File tree

5 files changed

+176
-28
lines changed

5 files changed

+176
-28
lines changed

c2/cfg/convert.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ func (c Config) next(i int) int {
3737
switch cBit(c[i]) {
3838
case WrapHex, WrapZlib, WrapGzip, WrapBase64:
3939
fallthrough
40-
case SelectorLastValid, SelectorRoundRobin, SelectorRandom, SelectorSemiRandom, SelectorSemiRoundRobin:
40+
case SelectorLastValid, SelectorRoundRobin, SelectorRandom, SelectorSemiRandom, SelectorSemiRoundRobin, SelectorSemiLastValid:
4141
fallthrough
4242
case Separator, ConnectTCP, ConnectTLS, ConnectUDP, ConnectICMP, ConnectPipe, ConnectTLSNoVerify, TransformB64:
4343
return i + 1
44-
case valIP, valB64Shift, valJitter, valWeight, valTLSx:
44+
case valIP, valB64Shift, valJitter, valWeight, valTLSx, valSelectorPercent, valSelectorPercentRoundRobin:
4545
return i + 2
4646
case valCBK, valWorkHours:
4747
return i + 6
@@ -233,6 +233,10 @@ loop:
233233
if i+1 >= n {
234234
return -1, xerr.Wrap("weight", ErrInvalidSetting)
235235
}
236+
case valSelectorPercent, valSelectorPercentRoundRobin:
237+
if i+1 >= n {
238+
return -1, xerr.Wrap("select-precent", ErrInvalidSetting)
239+
}
236240
case valKillDate:
237241
if i+8 >= n {
238242
return -1, xerr.Wrap("killdate", ErrInvalidSetting)
@@ -244,7 +248,7 @@ loop:
244248
if c[i+2] > 23 || c[i+3] > 59 || c[i+4] > 23 || c[i+5] > 59 {
245249
return -1, xerr.Wrap("workhours", ErrInvalidSetting)
246250
}
247-
case SelectorRoundRobin, SelectorLastValid, SelectorRandom, SelectorSemiRandom, SelectorSemiRoundRobin:
251+
case SelectorRoundRobin, SelectorLastValid, SelectorRandom, SelectorSemiRandom, SelectorSemiRoundRobin, SelectorSemiLastValid:
248252
case ConnectTCP, ConnectTLS, ConnectUDP, ConnectICMP, ConnectPipe, ConnectTLSNoVerify:
249253
if p {
250254
return -1, ErrMultipleConnections
@@ -474,8 +478,8 @@ loop:
474478
} else {
475479
p.work = &WorkHours{Days: c[i+1], StartHour: c[i+2], StartMin: c[i+3], EndHour: c[i+4], EndMin: c[i+5]}
476480
}
477-
case SelectorRandom, SelectorRoundRobin, SelectorLastValid, SelectorSemiRandom, SelectorSemiRoundRobin:
478-
z = int8(c[i] - byte(SelectorLastValid))
481+
case SelectorRandom, SelectorRoundRobin, SelectorLastValid, SelectorSemiRandom, SelectorSemiRoundRobin, SelectorSemiLastValid:
482+
z = int8(c[i])
479483
case ConnectTCP:
480484
if p.conn != nil {
481485
return nil, -1, -1, xerr.Wrap("tcp", ErrMultipleConnections)

c2/cfg/group.go

+30-9
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ const (
6666
// Takes effect only if there are multiple Groups in this Config.
6767
// This value is GLOBAL and can be present in any Group!
6868
SelectorSemiRandom = cBit(0xAE)
69+
// SelectorSemiLastValid is a selection that will keep using the last
70+
// Group unless it fails or a random precentage threashold is met (25%).
71+
// On a failure, the first call or a random change, this will act similar to
72+
// 'SelectorRoundRobin'.
73+
//
74+
// Takes effect only if there are multiple Groups in this Config.
75+
// This value is GLOBAL and can be present in any Group!
76+
SelectorSemiLastValid = cBit(0xA7)
77+
)
78+
79+
const (
80+
valSelectorPercent = cBit(0xA8)
81+
valSelectorPercentRoundRobin = cBit(0xA9)
6982
)
7083

7184
// Group is a struct that allows for using multiple connections for a single
@@ -153,25 +166,33 @@ func (g *Group) Less(i, j int) bool {
153166
//
154167
// Static Profile variants may always return 'false' to prevent allocations.
155168
func (g *Group) Switch(e bool) bool {
156-
if (g.cur != nil && !e && g.sel == 0) || len(g.entries) == 0 {
157-
return false
158-
}
159-
if g.sel == 0 && !e && g.cur != nil { // redundent?
169+
// NOTE(dij): Rust sets the first Group on creation, but Go does not, so
170+
// if g.cur == nil, it means we haven't set it yet.
171+
if len(g.entries) == 0 {
160172
return false
161173
}
162-
if g.cur != nil && (g.sel == 3 || g.sel == 4) && util.FastRandN(4) != 0 {
163-
return false
174+
if g.cur != nil {
175+
if !e && g.sel == uint8(SelectorLastValid) {
176+
return false
177+
}
178+
if !e && g.sel == uint8(SelectorSemiLastValid) && util.FastRandN(4) != 0 {
179+
return false
180+
}
181+
if (g.sel == uint8(SelectorSemiRandom) || g.sel == uint8(SelectorSemiRoundRobin)) && util.FastRandN(4) != 0 {
182+
return false
183+
}
184+
// selectorPercent stuff here.
164185
}
165-
if g.lock.Lock(); g.sel == 2 || g.sel == 4 {
186+
switch g.lock.Lock(); {
187+
case g.sel == uint8(SelectorRandom) || g.sel == uint8(SelectorSemiRandom):
166188
if n := g.entries[util.FastRandN(len(g.entries))]; g.cur != n {
167189
g.cur = n
168190
g.lock.Unlock()
169191
return true
170192
}
171193
g.lock.Unlock()
172194
return false
173-
}
174-
if g.cur == nil {
195+
case g.cur == nil:
175196
g.cur = g.entries[0]
176197
g.lock.Unlock()
177198
return true

c2/cfg/z_json.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package cfg
2121

22+
// NOTE(dij): Deprecation Planned!
23+
2224
import (
2325
"encoding/base64"
2426
"encoding/json"
@@ -65,6 +67,12 @@ func (c cBit) String() string {
6567
return "select-semi-round-robin"
6668
case SelectorSemiRandom:
6769
return "select-semi-random"
70+
case SelectorSemiLastValid:
71+
return "select-semi-last"
72+
case valSelectorPercent:
73+
return "select-percent"
74+
case valSelectorPercentRoundRobin:
75+
return "select-percent-round-robin"
6876
case ConnectTCP:
6977
return "tcp"
7078
case ConnectTLS:
@@ -138,6 +146,12 @@ func bitFromName(s string) cBit {
138146
return SelectorSemiRoundRobin
139147
case "select-semi-random":
140148
return SelectorSemiRandom
149+
case "select-semi-last":
150+
return SelectorSemiLastValid
151+
case "select-percent":
152+
return valSelectorPercent
153+
case "select-percent-round-robin":
154+
return valSelectorPercentRoundRobin
141155
case "tcp":
142156
return ConnectTCP
143157
case "tls":
@@ -285,7 +299,7 @@ func (c Config) MarshalJSON() ([]byte, error) {
285299
switch z = false; x {
286300
case WrapHex, WrapZlib, WrapGzip, WrapBase64:
287301
fallthrough
288-
case SelectorLastValid, SelectorRoundRobin, SelectorRandom, SelectorSemiRandom, SelectorSemiRoundRobin:
302+
case SelectorLastValid, SelectorRoundRobin, SelectorRandom, SelectorSemiRandom, SelectorSemiRoundRobin, SelectorSemiLastValid:
289303
fallthrough
290304
case ConnectTCP, ConnectTLS, ConnectUDP, ConnectICMP, ConnectPipe, ConnectTLSNoVerify:
291305
fallthrough
@@ -341,7 +355,7 @@ func (c Config) MarshalJSON() ([]byte, error) {
341355
)
342356
}
343357
b.WriteByte('}')
344-
case valJitter, valWeight, valIP, valTLSx, valB64Shift:
358+
case valJitter, valWeight, valIP, valTLSx, valB64Shift, valSelectorPercent, valSelectorPercentRoundRobin:
345359
if i+1 >= n {
346360
return nil, ErrInvalidSetting
347361
}
@@ -570,7 +584,7 @@ func (c *Config) UnmarshalJSON(b []byte) error {
570584
return ErrInvalidSetting
571585
case WrapHex, WrapZlib, WrapGzip, WrapBase64:
572586
fallthrough
573-
case SelectorLastValid, SelectorRoundRobin, SelectorRandom, SelectorSemiRandom, SelectorSemiRoundRobin:
587+
case SelectorLastValid, SelectorRoundRobin, SelectorRandom, SelectorSemiRandom, SelectorSemiRoundRobin, SelectorSemiLastValid:
574588
fallthrough
575589
case ConnectTCP, ConnectTLS, ConnectUDP, ConnectICMP, ConnectPipe, ConnectTLSNoVerify:
576590
fallthrough
@@ -632,6 +646,18 @@ func (c *Config) UnmarshalJSON(b []byte) error {
632646
byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v),
633647
})
634648
}
649+
case valSelectorPercent:
650+
var v uint8
651+
if err := json.Unmarshal(m[i].Args, &v); err != nil {
652+
return xerr.Wrap("selector-percent", err)
653+
}
654+
r = append(r, cBytes{byte(valSelectorPercent), v})
655+
case valSelectorPercentRoundRobin:
656+
var v uint8
657+
if err := json.Unmarshal(m[i].Args, &v); err != nil {
658+
return xerr.Wrap("selector-percent-round-robin", err)
659+
}
660+
r = append(r, cBytes{byte(valSelectorPercentRoundRobin), v})
635661
case valWorkHours:
636662
var z mapper
637663
if err := json.Unmarshal(m[i].Args, &z); err != nil {

com/pipe/pipe_windows.go

-5
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ func (l *Listener) Close() error {
133133
bugtrack.Track("com.(*Listener).Close(): CancelIoEx error: %s!", err.Error())
134134
}
135135
}
136-
if err = winapi.CloseHandle(l.overlap.Event); err != nil {
137-
if bugtrack.Enabled {
138-
bugtrack.Track("com.(*Listener).Close(): CloseHandle(event) error: %s!", err.Error())
139-
}
140-
}
141136
if l.active > 0 { // Extra check as it can be reset here sometimes.
142137
if err = winapi.CloseHandle(l.active); err != nil {
143138
if bugtrack.Enabled {

0 commit comments

Comments
 (0)