Compare commits

...
2 Commits
Author SHA1 Message Date
wweir 9eaf27fdcc fix(dns): serve interface parser 2024-10-12 10:26:29 +08:00
wweir ea6c2a29ad feat: reduce dependency on host DNS configuration 2024-10-12 10:10:52 +08:00
2 changed files with 32 additions and 7 deletions
+13 -3
View File
@@ -37,7 +37,7 @@ var (
DNS struct {
Disable bool `default:"false" usage:"disable DNS proxy"`
Serve string `default:"127.0.0.1" usage:"dns server ip"`
Serve string `usage:"dns server ip"`
Serve6 string `usage:"dns server ipv6, eg: ::1"`
ServeIface string `usage:"use the IP in the net interface, if serve ip not setted. eg: eth0"`
Fallback string `default:"223.5.5.5" required:"true" usage:"fallback dns server"`
@@ -78,6 +78,16 @@ func init() {
if err := aconfig.LoaderFor(&conf, aconfig.Config{
AllowUnknownFields: true,
FileFlag: "f",
Files: []string{
"sower.hcl",
"sower.toml",
"sower.yaml",
"sower.yml",
"/etc/sower/sower.hcl",
"/etc/sower/sower.toml",
"/etc/sower/sower.yaml",
"/etc/sower/sower.yml",
},
FileDecoders: map[string]aconfig.FileDecoder{
".yml": aconfigyaml.New(),
".yaml": aconfigyaml.New(),
@@ -98,7 +108,7 @@ func init() {
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
log.InfoFatal(err).Str("iface", conf.DNS.ServeIface).
Msg("parse iface addr: " + addr.String())
Msg("parse iface IP: " + ip.String())
if ip.To4() != nil { // ipv4
if conf.DNS.Serve == "" {
@@ -126,7 +136,7 @@ func init() {
}
func main() {
proxyDial := GenProxyDial(conf.Remote.Type, conf.Remote.Addr, conf.Remote.Password)
proxyDial := GenProxyDial(conf.Remote.Type, conf.Remote.Addr, conf.Remote.Password, conf.DNS.Fallback)
r := router.NewRouter(conf.DNS.Serve, conf.DNS.Fallback, conf.Router.Country.MMDB, proxyDial)
r.BlockRule = suffixtree.NewNodeFromRules(conf.Router.Block.Rules...)
r.DirectRule = suffixtree.NewNodeFromRules(conf.Router.Direct.Rules...)
+19 -4
View File
@@ -2,6 +2,7 @@ package main
import (
"bufio"
"context"
"crypto/tls"
"net"
"net/http"
@@ -18,29 +19,43 @@ import (
"github.com/wweir/sower/transport/trojan"
)
func GenProxyDial(proxyType, proxyHost, proxyPassword string) router.ProxyDialFn {
func GenProxyDial(proxyType, proxyHost, proxyPassword, dnsFallback string) router.ProxyDialFn {
var proxy transport.Transport
var dialFn func() (net.Conn, error)
dialer := &net.Dialer{
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
c, err := d.DialContext(ctx, "udp", net.JoinHostPort(dnsFallback, "53"))
if err != nil {
log.Warn().Err(err).Msg("dial fallback dns failed, use default dns setting")
c, err = d.DialContext(ctx, network, address)
}
return c, err
},
},
}
switch conf.Remote.Type {
case "sower":
proxy = sower.New(conf.Remote.Password)
tlsCfg := &tls.Config{}
dialFn = func() (net.Conn, error) {
return tls.Dial("tcp", net.JoinHostPort(proxyHost, "443"), tlsCfg)
return tls.DialWithDialer(dialer, "tcp", net.JoinHostPort(proxyHost, "443"), tlsCfg)
}
case "trojan":
proxy = trojan.New(conf.Remote.Password)
tlsCfg := &tls.Config{}
dialFn = func() (net.Conn, error) {
return tls.Dial("tcp", net.JoinHostPort(proxyHost, "443"), tlsCfg)
return tls.DialWithDialer(dialer, "tcp", net.JoinHostPort(proxyHost, "443"), tlsCfg)
}
case "socks5":
proxy = socks5.New()
dialFn = func() (net.Conn, error) {
return net.Dial("tcp", proxyHost)
return dialer.Dial("tcp", proxyHost)
}
default: