Skip to content

Commit

Permalink
feat: add dns.serve_6 flag
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed May 25, 2024
1 parent ac09663 commit c0f704a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
.vscode
.idea
**.env
/bin


**.mmdb
Expand Down
71 changes: 30 additions & 41 deletions cmd/sower/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"
"time"

"github.com/bobg/go-generics/v3/slices"
"github.com/cristalhq/aconfig"
"github.com/cristalhq/aconfig/aconfighcl"
"github.com/cristalhq/aconfig/aconfigtoml"
Expand All @@ -35,11 +34,10 @@ var (
}

DNS struct {
Disable bool `default:"false" usage:"disable DNS proxy"`
Serve string `usage:"Deprecated, use 'serve_ips' instead"`
ServeIps []string `usage:"dns server ip, eg: 127.0.0.1,::1"`
ServeIface string `usage:"dns server interface, eg: eth0"`
Fallback string `default:"223.5.5.5" required:"true" usage:"fallback dns server"`
Disable bool `default:"false" usage:"disable DNS proxy"`
Serve string `default:"127.0.0.1" required:"true" usage:"dns server ip"`
Serve6 string `usage:"dns server ipv6, eg: ::1"`
Fallback string `default:"223.5.5.5" required:"true" usage:"fallback dns server"`
}
Socks5 struct {
Disable bool `default:"false" usage:"disable sock5 proxy"`
Expand Down Expand Up @@ -89,25 +87,6 @@ func init() {
Msg("Load config")
}

if !slices.Contains(conf.DNS.ServeIps, conf.DNS.Serve) {
conf.DNS.ServeIps = append(conf.DNS.ServeIps, conf.DNS.Serve)
}
if conf.DNS.ServeIface != "" {
iface, err := net.InterfaceByName(conf.DNS.ServeIface)
log.DebugFatal(err).Str("iface", conf.DNS.ServeIface).Msg("get iface")
addrs, err := iface.Addrs()
log.InfoFatal(err).Str("iface", conf.DNS.ServeIface).Msg("get iface addrs")
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
log.InfoFatal(err).Str("iface", conf.DNS.ServeIface).
Msg("parse iface addr: " + addr.String())

if !slices.Contains(conf.DNS.ServeIps, ip.String()) {
conf.DNS.ServeIps = append(conf.DNS.ServeIps, ip.String())
}
}
}

conf.Router.Direct.Rules = append(conf.Router.Direct.Rules,
conf.Remote.Addr, "**.in-addr.arpa", "**.ip6.arpa")
log.Info().
Expand All @@ -127,22 +106,29 @@ func main() {

if conf.DNS.Disable {
log.Info().Msg("DNS proxy disabled")
return
}

for _, ip := range conf.DNS.ServeIps {
lnHTTP, err := net.Listen("tcp", net.JoinHostPort(ip, "80"))
log.DebugFatal(err).Str("listen_on", ip).Msg("listen port 80")
go ServeHTTP(lnHTTP, r)

lnHTTPS, err := net.Listen("tcp", net.JoinHostPort(ip, "443"))
log.DebugFatal(err).Str("listen_on", ip).Msg("listen port 443")
go ServeHTTPS(lnHTTPS, r)

go func(ip string) {
err := dns.ListenAndServe(net.JoinHostPort(ip, "53"), "udp", r)
log.InfoFatal(err).Str("listen_on", ip).Msg("serve dns")
}(ip)
} else {
ips := make([]string, 0, 2)
if strings.TrimSpace(conf.DNS.Serve) != "" {
ips = append(ips, conf.DNS.Serve)
}
if strings.TrimSpace(conf.DNS.Serve6) != "" {
ips = append(ips, conf.DNS.Serve6)
}
for _, ip := range ips {
lnHTTP, err := net.Listen("tcp", net.JoinHostPort(ip, "80"))
log.DebugFatal(err).Str("listen_on", ip).Msg("listen port 80")
go ServeHTTP(lnHTTP, r)

lnHTTPS, err := net.Listen("tcp", net.JoinHostPort(ip, "443"))
log.DebugFatal(err).Str("listen_on", ip).Msg("listen port 443")
go ServeHTTPS(lnHTTPS, r)

go func(ip string) {
err := dns.ListenAndServe(net.JoinHostPort(ip, "53"), "udp", r)
log.InfoFatal(err).Str("listen_on", ip).Msg("serve dns")
}(ip)
}
}

go func() {
Expand All @@ -152,7 +138,10 @@ func main() {
}

ln, err := net.Listen("tcp", conf.Socks5.Addr)
log.InfoFatal(err).Str("listen_on", conf.Socks5.Addr).Msg("listen SOCKS5 proxy")
if err != nil {
log.Fatal().Err(err).Msg("listen port")
}
log.Info().Msgf("SOCKS5 proxy listening on %s", conf.Socks5.Addr)
go ServeSocks5(ln, r)
}()

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.22
toolchain go1.22.3

require (
github.com/bobg/go-generics/v3 v3.4.0
github.com/cristalhq/aconfig v0.18.5
github.com/cristalhq/aconfig/aconfighcl v0.17.1
github.com/cristalhq/aconfig/aconfigtoml v0.17.1
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/bobg/go-generics/v3 v3.4.0 h1:XxTJxH843OknMgw//HGQXklJCZ0eacdt5EABfNcKFr8=
github.com/bobg/go-generics/v3 v3.4.0/go.mod h1:gCsHnnRz88zpXpdsWPyDmjg1tYQPmpbUQbM4MW8z9Jc=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cristalhq/aconfig v0.17.0/go.mod h1:NXaRp+1e6bkO4dJn+wZ71xyaihMDYPtCSvEhMTm/H3E=
github.com/cristalhq/aconfig v0.18.5 h1:QqXH/Gy2c4QUQJTV2BN8UAuL/rqZ3IwhvxeC8OgzquA=
Expand All @@ -16,8 +14,6 @@ github.com/cristalhq/aconfig/aconfigyaml v0.17.1/go.mod h1:5DTsjHkvQ6hfbyxfG32ro
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/krolaw/dhcp4 v0.0.0-20190909130307-a50d88189771 h1:t2c2B9g1ZVhMYduqmANSEGVD3/1WlsrEYNPtVoFlENk=
Expand All @@ -30,8 +26,6 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0=
github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/miekg/dns v1.1.59 h1:C9EXc/UToRwKLhK5wKU/I4QVsBUc8kE6MkHBkeypWZs=
github.com/miekg/dns v1.1.59/go.mod h1:nZpewl5p6IvctfgrckopVx2OlSEHPRO/U4SYkRklrEk=
github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrzN7IgKZc=
Expand Down

0 comments on commit c0f704a

Please sign in to comment.