check and highlight advanced security params

This commit is contained in:
RomikB
2025-05-30 09:58:10 +02:00
parent e20bacac0f
commit 3f898ed0f5
4 changed files with 181 additions and 7 deletions

View File

@@ -78,7 +78,7 @@ func newEditDialog(owner walk.Form, tunnel *manager.Tunnel) (*EditDialog, error)
dlg.SetIcon(owner.Icon())
dlg.SetTitle(title)
dlg.SetLayout(layout)
dlg.SetMinMaxSize(walk.Size{500, 400}, walk.Size{0, 0})
dlg.SetMinMaxSize(walk.Size{500, 500}, walk.Size{0, 0})
if icon, err := loadSystemIcon("imageres", -114, 32); err == nil {
dlg.SetIcon(icon)
}
@@ -152,6 +152,7 @@ func newEditDialog(owner walk.Form, tunnel *manager.Tunnel) (*EditDialog, error)
dlg.SetCancelButton(cancelButton)
dlg.SetDefaultButton(dlg.saveButton)
dlg.syntaxEdit.TextChanged().Attach(dlg.onSyntaxEditTextChanged)
dlg.syntaxEdit.PrivateKeyChanged().Attach(dlg.onSyntaxEditPrivateKeyChanged)
dlg.syntaxEdit.BlockUntunneledTrafficStateChanged().Attach(dlg.onBlockUntunneledTrafficStateChanged)
dlg.syntaxEdit.SetText(dlg.config.ToWgQuick())
@@ -312,6 +313,10 @@ func (dlg *EditDialog) onSyntaxEditPrivateKeyChanged(privateKey string) {
}
}
func (dlg *EditDialog) onSyntaxEditTextChanged() {
dlg.saveButton.SetEnabled(dlg.syntaxEdit.HaveErrors())
}
func (dlg *EditDialog) onSaveButtonClicked() {
newName := dlg.nameEdit.Text()
if newName == "" {

View File

@@ -8,7 +8,12 @@
package syntax
import "unsafe"
import (
"strconv"
"unsafe"
"github.com/amnezia-vpn/amneziawg-go/device"
)
type highlight int
@@ -37,6 +42,7 @@ const (
highlightH2
highlightH3
highlightH4
highlightWarning
highlightError
)
@@ -578,15 +584,15 @@ func (hsa *highlightSpanArray) highlightValue(parent, s stringSpan, section fiel
case fieldAddress, fieldDNS, fieldAllowedIPs:
hsa.highlightMultivalue(parent, s, section)
case fieldJc:
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 128), highlightJc))
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 65_535), highlightJc))
case fieldJmin:
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 1280), highlightJmin))
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 65_535), highlightJmin))
case fieldJmax:
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 1280), highlightJmax))
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 65_535), highlightJmax))
case fieldS1:
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 150), highlightS1))
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 65_535), highlightS1))
case fieldS2:
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 150), highlightS2))
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 65_535), highlightS2))
case fieldH1:
hsa.append(parent.s, s, validateHighlight(s.isValidUint(false, 0, 2_147_483_647), highlightH1))
case fieldH2:
@@ -683,3 +689,135 @@ func highlightConfig(config string) []highlightSpan {
}
return ([]highlightSpan)(ret)
}
func highlightASecConfig(cfg string, spans []highlightSpan) {
const (
maxMTU = 1500
diffMTU = 80
)
var (
mtu = 0
jc = 0
jmin = 0
jmax = 0
s1 = 0
s2 = 0
h1 = 0
h2 = 0
h3 = 0
h4 = 0
)
var err error
for i := range spans {
span := &spans[i]
switch span.t {
case highlightError:
return
case highlightMTU:
if mtu, err = strconv.Atoi(cfg[span.s : span.s+span.len]); err != nil {
return
}
case highlightJc:
if jc, err = strconv.Atoi(cfg[span.s : span.s+span.len]); err != nil {
return
}
case highlightJmin:
if jmin, err = strconv.Atoi(cfg[span.s : span.s+span.len]); err != nil {
return
}
case highlightJmax:
if jmax, err = strconv.Atoi(cfg[span.s : span.s+span.len]); err != nil {
return
}
case highlightS1:
if s1, err = strconv.Atoi(cfg[span.s : span.s+span.len]); err != nil {
return
}
case highlightS2:
if s2, err = strconv.Atoi(cfg[span.s : span.s+span.len]); err != nil {
return
}
case highlightH1:
if h1, err = strconv.Atoi(cfg[span.s : span.s+span.len]); err != nil {
return
}
case highlightH2:
if h2, err = strconv.Atoi(cfg[span.s : span.s+span.len]); err != nil {
return
}
case highlightH3:
if h3, err = strconv.Atoi(cfg[span.s : span.s+span.len]); err != nil {
return
}
case highlightH4:
if h4, err = strconv.Atoi(cfg[span.s : span.s+span.len]); err != nil {
return
}
}
}
if mtu == 0 {
mtu = device.DefaultMTU
}
if h1 <= 4 {
h1 = 1
}
if h2 <= 4 {
h2 = 2
}
if h3 <= 4 {
h3 = 3
}
if h4 <= 4 {
h4 = 4
}
for i := range spans {
span := &spans[i]
switch span.t {
case highlightJc:
if jc > 128 {
span.t = highlightWarning
}
case highlightJmin:
if (jc != 0 || jmin != 0 || jmax != 0) && (jmin >= jmax || jmin >= mtu+diffMTU || jmin >= maxMTU) {
span.t = highlightWarning
}
case highlightJmax:
if (jc != 0 || jmin != 0 || jmax != 0) && (jmax <= jmin || jmax > mtu+diffMTU || jmax > maxMTU) {
span.t = highlightWarning
}
case highlightS1:
if s1+device.MessageInitiationSize == s2+device.MessageResponseSize {
span.t = highlightError
} else if s1 > mtu-device.MessageInitiationSize+diffMTU || s1 > maxMTU-device.MessageInitiationSize {
span.t = highlightWarning
}
case highlightS2:
if s1+device.MessageInitiationSize == s2+device.MessageResponseSize {
span.t = highlightError
} else if s2 > mtu-device.MessageResponseSize+diffMTU || s2 > maxMTU-device.MessageResponseSize {
span.t = highlightWarning
}
case highlightH1:
if h1 > 4 && (h1 == h2 || h1 == h3 || h1 == h4) {
span.t = highlightError
}
case highlightH2:
if h2 > 4 && (h2 == h1 || h2 == h3 || h2 == h4) {
span.t = highlightError
}
case highlightH3:
if h3 > 4 && (h3 == h1 || h3 == h2 || h3 == h4) {
span.t = highlightError
}
case highlightH4:
if h4 > 4 && (h4 == h1 || h4 == h2 || h4 == h3) {
span.t = highlightError
}
}
}
}

View File

@@ -23,6 +23,7 @@ type SyntaxEdit struct {
irich *win.IRichEditOle
idoc *win.ITextDocument
lastBlockState BlockState
haveErrors bool
yheight int
highlightGuard uint32
textChangedPublisher walk.EventPublisher
@@ -72,6 +73,10 @@ func (se *SyntaxEdit) SetText(text string) (err error) {
return
}
func (se *SyntaxEdit) HaveErrors() bool {
return se.haveErrors
}
func (se *SyntaxEdit) TextChanged() *walk.Event {
return se.textChangedPublisher.Event()
}
@@ -105,6 +110,16 @@ var stylemap = map[highlight]spanStyle{
highlightComment: {color: win.RGB(0x53, 0x65, 0x79), effects: win.CFE_ITALIC},
highlightDelimiter: {color: win.RGB(0x00, 0x00, 0x00)},
highlightCmd: {color: win.RGB(0x63, 0x75, 0x89)},
highlightJc: {color: win.RGB(0x64, 0x38, 0x20)},
highlightJmin: {color: win.RGB(0x64, 0x38, 0x20)},
highlightJmax: {color: win.RGB(0x64, 0x38, 0x20)},
highlightS1: {color: win.RGB(0x64, 0x38, 0x20)},
highlightS2: {color: win.RGB(0x64, 0x38, 0x20)},
highlightH1: {color: win.RGB(0x64, 0x38, 0x20)},
highlightH2: {color: win.RGB(0x64, 0x38, 0x20)},
highlightH3: {color: win.RGB(0x64, 0x38, 0x20)},
highlightH4: {color: win.RGB(0x64, 0x38, 0x20)},
highlightWarning: {color: win.RGB(0xC4, 0x1A, 0x16), effects: win.CFE_UNDERLINE},
highlightError: {color: win.RGB(0xC4, 0x1A, 0x16), effects: win.CFE_UNDERLINE},
}
@@ -191,6 +206,18 @@ done:
}
}
func (se *SyntaxEdit) evaluateHaveErrors(spans []highlightSpan) {
for i := range spans {
span := &spans[i]
switch span.t {
case highlightError:
se.haveErrors = false
return
}
}
se.haveErrors = true
}
func (se *SyntaxEdit) highlightText() error {
if !atomic.CompareAndSwapUint32(&se.highlightGuard, 0, 1) {
return nil
@@ -220,7 +247,9 @@ func (se *SyntaxEdit) highlightText() error {
cfg := strings.Replace(string(msg[:msgCount]), "\r", "\n", -1)
spans := highlightConfig(cfg)
highlightASecConfig(cfg, spans)
se.evaluateUntunneledBlocking(cfg, spans)
se.evaluateHaveErrors(spans)
se.idoc.Undo(win.TomSuspend, nil)
win.SendMessage(hWnd, win.EM_SETEVENTMASK, 0, 0)

View File

@@ -69,6 +69,7 @@ func NewTunnelsPage() (*TunnelsPage, error) {
vlayout.SetAlignment(walk.AlignHNearVCenter)
vlayout.SetMargins(walk.Margins{})
tp.currentTunnelContainer.SetLayout(vlayout)
hlayout.SetStretchFactor(tp.currentTunnelContainer, 100)
if tp.fillerContainer, err = walk.NewComposite(tp); err != nil {
return nil, err
@@ -78,6 +79,7 @@ func NewTunnelsPage() (*TunnelsPage, error) {
vlayout.SetAlignment(walk.AlignHCenterVCenter)
vlayout.SetMargins(walk.Margins{})
tp.fillerContainer.SetLayout(vlayout)
hlayout.SetStretchFactor(tp.fillerContainer, 100)
fillerButtonContainer, _ := walk.NewComposite(tp.fillerContainer)
hlayout = walk.NewHBoxLayout()