Skip to content

Commit

Permalink
Fix CNAME
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna committed Jan 19, 2024
1 parent bcd4b0e commit bf0128e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
71 changes: 71 additions & 0 deletions x/smart/cname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2024 Jigsaw Operations LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package smart

/*
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
*/
import "C"

import (
"context"
"fmt"
"unsafe"
)

func lookupCNAME(ctx context.Context, domain string) (string, error) {
type result struct {
cname string
err error
}

results := make(chan result)
go func() {
cname, err := lookupCNAMEBlocking(domain)
results <- result{cname, err}
}()

select {
case r := <-results:
return r.cname, r.err
case <-ctx.Done():
return "", ctx.Err()
}
}

func lookupCNAMEBlocking(host string) (string, error) {
var hints C.struct_addrinfo
var result *C.struct_addrinfo

chost := C.CString(host)
defer C.free(unsafe.Pointer(chost))

hints.ai_family = C.AF_UNSPEC
hints.ai_flags = C.AI_CANONNAME

// Call getaddrinfo
res := C.getaddrinfo(chost, nil, &hints, &result)
if res != 0 {
return "", fmt.Errorf("getaddrinfo error: %s", C.GoString(C.gai_strerror(res)))
}
defer C.freeaddrinfo(result)

// Extract canonical name
cname := C.GoString(result.ai_canonname)
return cname, nil
}
2 changes: 1 addition & 1 deletion x/smart/stream_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func fingerprint(pd transport.PacketDialer, sd transport.StreamDialer, testDomai

func evaluateNetResolver(ctx context.Context, resolver *net.Resolver, testDomain string) ([]net.IP, error) {
requestDomain := mixCase(testDomain)
_, err := resolver.LookupCNAME(ctx, requestDomain)
_, err := lookupCNAME(ctx, requestDomain)
if err != nil {
return nil, fmt.Errorf("could not get cname: %w", err)
}
Expand Down

0 comments on commit bf0128e

Please sign in to comment.