代码拉取完成,页面将自动刷新
// +build android linux darwin
/*
* Copyright (c) 2015, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package psiphon
import (
"context"
"errors"
"fmt"
"net"
"os"
"syscall"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
)
// LookupIP resolves a hostname. When BindToDevice is not required, it
// simply uses net.LookupIP.
// When BindToDevice is required, LookupIP explicitly creates a UDP
// socket, binds it to the device, and makes an explicit DNS request
// to the specified DNS resolver.
func LookupIP(ctx context.Context, host string, config *DialConfig) ([]net.IP, error) {
ip := net.ParseIP(host)
if ip != nil {
return []net.IP{ip}, nil
}
if config.DeviceBinder != nil {
dnsServer := config.DnsServerGetter.GetPrimaryDnsServer()
ips, err := bindLookupIP(ctx, host, dnsServer, config)
if err == nil {
if len(ips) == 0 {
err = errors.New("empty address list")
} else {
return ips, err
}
}
dnsServer = config.DnsServerGetter.GetSecondaryDnsServer()
if dnsServer == "" {
return ips, err
}
NoticeAlert("retry resolve host %s: %s", host, err)
return bindLookupIP(ctx, host, dnsServer, config)
}
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host)
if err != nil {
return nil, common.ContextError(err)
}
ips := make([]net.IP, len(addrs))
for i, addr := range addrs {
ips[i] = addr.IP
}
return ips, nil
}
// bindLookupIP implements the BindToDevice LookupIP case.
// To implement socket device binding, the lower-level syscall APIs are used.
func bindLookupIP(
ctx context.Context, host, dnsServer string, config *DialConfig) ([]net.IP, error) {
// config.DnsServerGetter.GetDnsServers() must return IP addresses
ipAddr := net.ParseIP(dnsServer)
if ipAddr == nil {
return nil, common.ContextError(errors.New("invalid IP address"))
}
// When configured, attempt to synthesize an IPv6 address from
// an IPv4 address for compatibility on DNS64/NAT64 networks.
// If synthesize fails, try the original address.
if config.IPv6Synthesizer != nil && ipAddr.To4() != nil {
synthesizedIPAddress := config.IPv6Synthesizer.IPv6Synthesize(dnsServer)
if synthesizedIPAddress != "" {
synthesizedAddr := net.ParseIP(synthesizedIPAddress)
if synthesizedAddr != nil {
ipAddr = synthesizedAddr
}
}
}
var ipv4 [4]byte
var ipv6 [16]byte
var domain int
// Get address type (IPv4 or IPv6)
if ipAddr.To4() != nil {
copy(ipv4[:], ipAddr.To4())
domain = syscall.AF_INET
} else if ipAddr.To16() != nil {
copy(ipv6[:], ipAddr.To16())
domain = syscall.AF_INET6
} else {
return nil, common.ContextError(fmt.Errorf("invalid IP address for dns server: %s", ipAddr.String()))
}
socketFd, err := syscall.Socket(domain, syscall.SOCK_DGRAM, 0)
if err != nil {
return nil, common.ContextError(err)
}
_, err = config.DeviceBinder.BindToDevice(socketFd)
if err != nil {
syscall.Close(socketFd)
return nil, common.ContextError(fmt.Errorf("BindToDevice failed: %s", err))
}
// Connect socket to the server's IP address
// Note: no timeout or interrupt for this connect, as it's a datagram socket
if domain == syscall.AF_INET {
sockAddr := syscall.SockaddrInet4{Addr: ipv4, Port: DNS_PORT}
err = syscall.Connect(socketFd, &sockAddr)
} else if domain == syscall.AF_INET6 {
sockAddr := syscall.SockaddrInet6{Addr: ipv6, Port: DNS_PORT}
err = syscall.Connect(socketFd, &sockAddr)
}
if err != nil {
syscall.Close(socketFd)
return nil, common.ContextError(err)
}
// Convert the syscall socket to a net.Conn, for use in the dns package
// This code block is from:
// https://github.com/golang/go/issues/6966
file := os.NewFile(uintptr(socketFd), "")
netConn, err := net.FileConn(file) // net.FileConn() dups socketFd
file.Close() // file.Close() closes socketFd
if err != nil {
return nil, common.ContextError(err)
}
type resolveIPResult struct {
ips []net.IP
err error
}
resultChannel := make(chan resolveIPResult)
go func() {
ips, _, err := ResolveIP(host, netConn)
netConn.Close()
resultChannel <- resolveIPResult{ips: ips, err: err}
}()
var result resolveIPResult
select {
case result = <-resultChannel:
case <-ctx.Done():
result.err = ctx.Err()
// Interrupt the goroutine
netConn.Close()
<-resultChannel
}
if result.err != nil {
return nil, common.ContextError(err)
}
return result.ips, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。