mirror of
https://github.com/openlibrecommunity/olcrtc.git
synced 2026-06-03 10:59:45 +00:00
(feature): check if port is valid
This commit is contained in:
24
ui/config.go
24
ui/config.go
@@ -2,8 +2,10 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -15,6 +17,18 @@ type Config struct {
|
||||
ConferenceID string `json:"conference_id"`
|
||||
}
|
||||
|
||||
func isValidPort(portStr string) bool {
|
||||
portStr = strings.TrimSpace(portStr)
|
||||
if portStr == "" {
|
||||
return false
|
||||
}
|
||||
port, err := strconv.Atoi(portStr)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return port > 0 && port <= 65535
|
||||
}
|
||||
|
||||
func (p *Program) getConfigPath() string {
|
||||
dir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
@@ -53,6 +67,10 @@ func (p *Program) loadConfig() *Config {
|
||||
return cfg
|
||||
}
|
||||
cfg.ConferenceID = strings.ReplaceAll(cfg.ConferenceID, " ", "")
|
||||
if !isValidPort(cfg.SocksPort) {
|
||||
log("WARNING: Invalid port in config, using default: 1080")
|
||||
cfg.SocksPort = "1080"
|
||||
}
|
||||
log("Config loaded successfully")
|
||||
return cfg
|
||||
}
|
||||
@@ -62,6 +80,12 @@ func (p *Program) saveConfig(dns, encryptionKey, socksPort, conferenceID string)
|
||||
|
||||
conferenceID = strings.ReplaceAll(conferenceID, " ", "")
|
||||
|
||||
if !isValidPort(socksPort) {
|
||||
log("ERROR: Invalid port: %s", socksPort)
|
||||
p.showError(fmt.Errorf("invalid port: must be between 1 and 65535"))
|
||||
return
|
||||
}
|
||||
|
||||
p.Config = &Config{
|
||||
DNS: dns,
|
||||
EncryptionKey: encryptionKey,
|
||||
|
||||
Reference in New Issue
Block a user