Update deps

This commit is contained in:
Frank Denis 2024-04-25 12:45:52 +02:00
parent 249dba391d
commit f8ce22d9b9
48 changed files with 1288 additions and 596 deletions

View file

@ -11,7 +11,11 @@ import (
"strings"
)
const DefaultPort = 443
const (
DefaultPort = 443
DefaultDNSPort = 53
StampScheme = "sdns://"
)
type ServerInformalProperties uint64
@ -97,7 +101,10 @@ func NewServerStampFromString(stampStr string) (ServerStamp, error) {
if len(bin) < 1 {
return ServerStamp{}, errors.New("Stamp is too short")
}
if bin[0] == uint8(StampProtoTypeDNSCrypt) {
if bin[0] == uint8(StampProtoTypePlain) {
return newPlainDNSServerStamp(bin)
} else if bin[0] == uint8(StampProtoTypeDNSCrypt) {
return newDNSCryptServerStamp(bin)
} else if bin[0] == uint8(StampProtoTypeDoH) {
return newDoHServerStamp(bin)
@ -112,7 +119,7 @@ func NewServerStampFromString(stampStr string) (ServerStamp, error) {
}
func NewRelayAndServerStampFromString(stampStr string) (ServerStamp, ServerStamp, error) {
if !strings.HasPrefix(stampStr, "sdns://") {
if !strings.HasPrefix(stampStr, StampScheme) {
return ServerStamp{}, ServerStamp{}, errors.New("Stamps are expected to start with \"sdns://\"")
}
stampStr = stampStr[7:]
@ -120,11 +127,11 @@ func NewRelayAndServerStampFromString(stampStr string) (ServerStamp, ServerStamp
if len(parts) != 2 {
return ServerStamp{}, ServerStamp{}, errors.New("This is not a relay+server stamp")
}
relayStamp, err := NewServerStampFromString("sdns://" + parts[0])
relayStamp, err := NewServerStampFromString(StampScheme + parts[0])
if err != nil {
return ServerStamp{}, ServerStamp{}, err
}
serverStamp, err := NewServerStampFromString("sdns://" + parts[1])
serverStamp, err := NewServerStampFromString(StampScheme + parts[1])
if err != nil {
return ServerStamp{}, ServerStamp{}, err
}
@ -137,6 +144,49 @@ func NewRelayAndServerStampFromString(stampStr string) (ServerStamp, ServerStamp
return relayStamp, serverStamp, nil
}
// id(u8)=0x00 props 0x00 addrLen(1) serverAddr
func newPlainDNSServerStamp(bin []byte) (ServerStamp, error) {
stamp := ServerStamp{Proto: StampProtoTypePlain}
if len(bin) < 1+8+1+1 {
return stamp, errors.New("Stamp is too short")
}
stamp.Props = ServerInformalProperties(binary.LittleEndian.Uint64(bin[1:9]))
binLen := len(bin)
pos := 9
length := int(bin[pos])
if 1+length > binLen-pos {
return stamp, errors.New("Invalid stamp")
}
pos++
stamp.ServerAddrStr = string(bin[pos : pos+length])
pos += length
colIndex := strings.LastIndex(stamp.ServerAddrStr, ":")
bracketIndex := strings.LastIndex(stamp.ServerAddrStr, "]")
if colIndex < bracketIndex {
colIndex = -1
}
if colIndex < 0 {
colIndex = len(stamp.ServerAddrStr) // DefaultDNSPort
stamp.ServerAddrStr = fmt.Sprintf("%s:%d", stamp.ServerAddrStr, DefaultDNSPort)
}
if colIndex >= len(stamp.ServerAddrStr)-1 {
return stamp, errors.New("Invalid stamp (empty port)")
}
ipOnly := stamp.ServerAddrStr[:colIndex]
if err := validatePort(stamp.ServerAddrStr[colIndex+1:]); err != nil {
return stamp, errors.New("Invalid stamp (port range)")
}
if net.ParseIP(strings.TrimRight(strings.TrimLeft(ipOnly, "["), "]")) == nil {
return stamp, errors.New("Invalid stamp (IP address)")
}
if pos != binLen {
return stamp, errors.New("Invalid stamp (garbage after end)")
}
return stamp, nil
}
// id(u8)=0x01 props addrLen(1) serverAddr pkStrlen(1) pkStr providerNameLen(1) providerName
func newDNSCryptServerStamp(bin []byte) (ServerStamp, error) {
@ -169,8 +219,7 @@ func newDNSCryptServerStamp(bin []byte) (ServerStamp, error) {
return stamp, errors.New("Invalid stamp (empty port)")
}
ipOnly := stamp.ServerAddrStr[:colIndex]
portOnly := stamp.ServerAddrStr[colIndex+1:]
if _, err := strconv.ParseUint(portOnly, 10, 16); err != nil {
if err := validatePort(stamp.ServerAddrStr[colIndex+1:]); err != nil {
return stamp, errors.New("Invalid stamp (port range)")
}
if net.ParseIP(strings.TrimRight(strings.TrimLeft(ipOnly, "["), "]")) == nil {
@ -268,8 +317,7 @@ func newDoHServerStamp(bin []byte) (ServerStamp, error) {
return stamp, errors.New("Invalid stamp (empty port)")
}
ipOnly := stamp.ServerAddrStr[:colIndex]
portOnly := stamp.ServerAddrStr[colIndex+1:]
if _, err := strconv.ParseUint(portOnly, 10, 16); err != nil {
if err := validatePort(stamp.ServerAddrStr[colIndex+1:]); err != nil {
return stamp, errors.New("Invalid stamp (port range)")
}
if net.ParseIP(strings.TrimRight(strings.TrimLeft(ipOnly, "["), "]")) == nil {
@ -344,8 +392,7 @@ func newDNSCryptRelayStamp(bin []byte) (ServerStamp, error) {
return stamp, errors.New("Invalid stamp (empty port)")
}
ipOnly := stamp.ServerAddrStr[:colIndex]
portOnly := stamp.ServerAddrStr[colIndex+1:]
if _, err := strconv.ParseUint(portOnly, 10, 16); err != nil {
if err := validatePort(stamp.ServerAddrStr[colIndex+1:]); err != nil {
return stamp, errors.New("Invalid stamp (port range)")
}
if net.ParseIP(strings.TrimRight(strings.TrimLeft(ipOnly, "["), "]")) == nil {
@ -426,8 +473,7 @@ func newODoHRelayStamp(bin []byte) (ServerStamp, error) {
return stamp, errors.New("Invalid stamp (empty port)")
}
ipOnly := stamp.ServerAddrStr[:colIndex]
portOnly := stamp.ServerAddrStr[colIndex+1:]
if _, err := strconv.ParseUint(portOnly, 10, 16); err != nil {
if err := validatePort(stamp.ServerAddrStr[colIndex+1:]); err != nil {
return stamp, errors.New("Invalid stamp (port range)")
}
if net.ParseIP(strings.TrimRight(strings.TrimLeft(ipOnly, "["), "]")) == nil {
@ -438,8 +484,17 @@ func newODoHRelayStamp(bin []byte) (ServerStamp, error) {
return stamp, nil
}
func validatePort(port string) error {
if _, err := strconv.ParseUint(port, 10, 16); err != nil {
return errors.New("Invalid port")
}
return nil
}
func (stamp *ServerStamp) String() string {
if stamp.Proto == StampProtoTypeDNSCrypt {
if stamp.Proto == StampProtoTypePlain {
return stamp.plainStrng()
} else if stamp.Proto == StampProtoTypeDNSCrypt {
return stamp.dnsCryptString()
} else if stamp.Proto == StampProtoTypeDoH {
return stamp.dohString()
@ -453,6 +508,22 @@ func (stamp *ServerStamp) String() string {
panic("Unsupported protocol")
}
func (stamp *ServerStamp) plainStrng() string {
bin := make([]uint8, 9)
bin[0] = uint8(StampProtoTypePlain)
binary.LittleEndian.PutUint64(bin[1:9], uint64(stamp.Props))
serverAddrStr := stamp.ServerAddrStr
if strings.HasSuffix(serverAddrStr, ":"+strconv.Itoa(DefaultDNSPort)) {
serverAddrStr = serverAddrStr[:len(serverAddrStr)-1-len(strconv.Itoa(DefaultDNSPort))]
}
bin = append(bin, uint8(len(serverAddrStr)))
bin = append(bin, []uint8(serverAddrStr)...)
str := base64.RawURLEncoding.EncodeToString(bin)
return StampScheme + str
}
func (stamp *ServerStamp) dnsCryptString() string {
bin := make([]uint8, 9)
bin[0] = uint8(StampProtoTypeDNSCrypt)
@ -473,7 +544,7 @@ func (stamp *ServerStamp) dnsCryptString() string {
str := base64.RawURLEncoding.EncodeToString(bin)
return "sdns://" + str
return StampScheme + str
}
func (stamp *ServerStamp) dohString() string {
@ -510,7 +581,7 @@ func (stamp *ServerStamp) dohString() string {
str := base64.RawURLEncoding.EncodeToString(bin)
return "sdns://" + str
return StampScheme + str
}
func (stamp *ServerStamp) oDohTargetString() string {
@ -526,7 +597,7 @@ func (stamp *ServerStamp) oDohTargetString() string {
str := base64.RawURLEncoding.EncodeToString(bin)
return "sdns://" + str
return StampScheme + str
}
func (stamp *ServerStamp) dnsCryptRelayString() string {
@ -542,7 +613,7 @@ func (stamp *ServerStamp) dnsCryptRelayString() string {
str := base64.RawURLEncoding.EncodeToString(bin)
return "sdns://" + str
return StampScheme + str
}
func (stamp *ServerStamp) oDohRelayString() string {
@ -579,5 +650,5 @@ func (stamp *ServerStamp) oDohRelayString() string {
str := base64.RawURLEncoding.EncodeToString(bin)
return "sdns://" + str
return StampScheme + str
}