Skip to content

Commit

Permalink
config: assign host and port
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Nov 9, 2023
1 parent 9aaf625 commit f24a4fc
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions intra/settings/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (
"golang.org/x/net/proxy"
)

var (
errDnsOptArg = errors.New("dnsopt: invalid arg")
)

// TODO: These modes could be covered by bit-flags instead.

// DNSModeNone does not redirect DNS queries sent to the tunnel.
Expand Down Expand Up @@ -63,10 +67,6 @@ const NICID = 0x01

var Debug bool = false

func (tm *TunMode) L3() string {
return L3(tm.IpMode)
}

func L3(engine int) string {
switch engine {
case Ns46:
Expand All @@ -90,29 +90,8 @@ type TunMode struct {
IpMode int
}

// DNSOptions define https or socks5 proxy options
type DNSOptions struct {
ipp string
hostport string
hostips string
}

func (d *DNSOptions) String() string {
return d.Addr()
}

func (d *DNSOptions) Addr() string {
if len(d.ipp) > 0 {
return d.ipp
}
if len(d.hostport) > 0 {
return d.hostport
}
return ""
}

func (d *DNSOptions) ResolvedAddrs() string {
return d.hostips
func (tm *TunMode) L3() string {
return L3(tm.IpMode)
}

// SetMode re-assigns d to DNSMode, b to BlockMode,
Expand Down Expand Up @@ -152,6 +131,31 @@ func DefaultTunMode() *TunMode {
}
}

// DNSOptions define https or socks5 proxy options
type DNSOptions struct {
ipp string
hostport string
hostips string
}

func (d *DNSOptions) String() string {
return d.Addr()
}

func (d *DNSOptions) Addr() string {
if len(d.ipp) > 0 {
return d.ipp
}
if len(d.hostport) > 0 {
return d.hostport
}
return ""
}

func (d *DNSOptions) ResolvedAddrs() string {
return d.hostips
}

// Parse ip and port; where ip can be either ip:port or ip
func addrport(ip string, port string) (ipp netip.AddrPort, err error) {
var ipaddr netip.Addr
Expand Down Expand Up @@ -194,17 +198,21 @@ func NewDNSOptionsFromNetIp(ipp netip.AddrPort) (*DNSOptions, error) {
}

func NewDNSOptionsFromHostname(hostname, ipcsv string) (*DNSOptions, error) {
domain, port, err := net.SplitHostPort(hostname)
if err != nil {
return nil, err
if len(hostname) <= 0 {
return nil, errDnsOptArg
}

domain, port, _ := net.SplitHostPort(hostname)
if len(domain) <= 0 {
domain = hostname
}
if len(port) == 0 {
port = "53"
}

return &DNSOptions{
hostport: net.JoinHostPort(domain, port),
hostips: ipcsv,
hostips: ipcsv, // may be empty
}, nil
}

Expand Down

0 comments on commit f24a4fc

Please sign in to comment.