proxy: improve addr handling

This commit is contained in:
nadoo committed 2022-01-28 23:35:29 +08:00
1 parent e12642b47a
commit 7e7c7553cc
12 files changed
+99 -109

No files matched your search

+9 -7
View File
@@ -5,17 +5,19 @@ import (
stdlog "log"
)
// F is the main log function.
var F = func(string, ...any) {}
var verbose = false
// SetFlags sets the output flags for the logger.
func SetFlags(flag int) {
// Set sets the logger's verbose mode and output flags.
func Set(v bool, flag int) {
verbose = v
stdlog.SetFlags(flag)
}
// Debugf prints debug log.
func Debugf(f string, v ...any) {
stdlog.Output(2, fmt.Sprintf(f, v...))
// F prints debug log.
func F(f string, v ...any) {
if verbose {
stdlog.Output(2, fmt.Sprintf(f, v...))
}
}
// Print prints log.
+5 -4
View File
@@ -4,6 +4,7 @@ import (
"errors"
"io"
"net"
"net/netip"
"strconv"
)
@@ -139,16 +140,16 @@ func ParseAddr(s string) Addr {
if err != nil {
return nil
}
if ip := net.ParseIP(host); ip != nil {
if ip4 := ip.To4(); ip4 != nil {
if ip, err := netip.ParseAddr(host); err == nil {
if ip.Is4() {
addr = make([]byte, 1+net.IPv4len+2)
addr[0] = ATypIP4
copy(addr[1:], ip4)
} else {
addr = make([]byte, 1+net.IPv6len+2)
addr[0] = ATypIP6
copy(addr[1:], ip)
}
copy(addr[1:], ip.AsSlice())
} else {
if len(host) > 255 {
return nil