feat: Added TOR address validation #149

This commit is contained in:
Christian Ditaputratama 2024-11-06 20:21:15 +07:00
parent df161f831a
commit 3f5c2b9905
No known key found for this signature in database
GPG key ID: 31D3D06D77950979

View file

@ -8,6 +8,7 @@ import (
"log/slog" "log/slog"
"math" "math"
"net" "net"
"regexp"
"slices" "slices"
"strings" "strings"
"time" "time"
@ -221,8 +222,8 @@ func (r *moneroRepo) Add(protocol string, hostname string, port uint) error {
ipAddr = hostIp.String() ipAddr = hostIp.String()
ips = ip.SliceToString(hostIps) ips = ip.SliceToString(hostIps)
} else { } else {
if strings.HasPrefix(hostname, "http://") || strings.HasPrefix(hostname, "https://") { if !validTorHostname(hostname) {
return errors.New("Don't start hostname with http:// or https://, just put your hostname") return errors.New("Invalid TOR v3 .onion hostname")
} }
} }
@ -295,6 +296,12 @@ func (r *moneroRepo) Add(protocol string, hostname string, port uint) error {
return nil return nil
} }
// Checks if a given hostname is a valid TOR v3 .onion address
// TOR v3 .onion addresses are 56 characters of base32 followed by ".onion"
func validTorHostname(hostname string) bool {
return regexp.MustCompile(`^[a-z2-7]{56}\.onion$`).MatchString(hostname)
}
func (r *moneroRepo) Delete(id uint) error { func (r *moneroRepo) Delete(id uint) error {
if _, err := r.db.Exec(`DELETE FROM tbl_node WHERE id = ?`, id); err != nil { if _, err := r.db.Exec(`DELETE FROM tbl_node WHERE id = ?`, id); err != nil {
return err return err