Skip to content

Commit

Permalink
Fix #5, add ipv6 support @Release
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Jan 28, 2019
1 parent 2f3c98b commit c209031
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ kill:
sudo pkill -9 sower || true

client: build kill
sudo $(PWD)/sower -f /usr/local/etc/sower.toml
sudo $(PWD)/sower -f conf/sower.toml

server: build kill
$(PWD)/sower -n TCP -v 1

run: build kill
$(PWD)/sower -n TCP -v 1 &
sudo $(PWD)/sower -f /usr/local/etc/sower.toml &
sudo $(PWD)/sower -f conf/sower.toml &
@sleep 1
curl 127.0.0.1
@sleep 1
Expand Down
9 changes: 5 additions & 4 deletions dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const colon = byte(':')

func StartDNS(dnsServer, listenIP string) {
ip := net.ParseIP(listenIP)
suggest := &intelliSuggest{listenIP, 2 * time.Second, []string{":80", ":443"}}
suggest := &intelliSuggest{listenIP, 2 * time.Second, []string{"80", "443"}}
mem.DefaultCache = mem.New(time.Hour)
dnsServer = net.JoinHostPort(dnsServer, "53")

dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
// *Msg r has an TSIG record and it was validated
Expand All @@ -39,7 +40,7 @@ func StartDNS(dnsServer, listenIP string) {
matchAndServe(w, r, domain, listenIP, dnsServer, ip, suggest)
})

server := &dns.Server{Addr: listenIP + ":53", Net: "udp"}
server := &dns.Server{Addr: net.JoinHostPort(listenIP, "53"), Net: "udp"}
glog.Fatalln(server.ListenAndServe())
}

Expand All @@ -58,7 +59,7 @@ func matchAndServe(w dns.ResponseWriter, r *dns.Msg, domain, listenIP,
go mem.Remember(suggest, domain)
}

msg, err := dns.Exchange(r, dnsServer+":53")
msg, err := dns.Exchange(r, dnsServer)
if msg == nil { // expose any response except nil
glog.V(1).Infof("get dns of %s fail: %s", domain, err)
return
Expand All @@ -83,7 +84,7 @@ func (i *intelliSuggest) GetOne(domain interface{}) (iface interface{}, e error)

for _, port := range i.ports {
// give local dial a hand, make it not so easy to be added into suggestions
util.HTTPPing(addr+port, addr, i.timeout/4)
util.HTTPPing(addr+port, addr, i.timeout/5)
localCh := util.HTTPPing(addr+port, addr, i.timeout)
remoteCh := util.HTTPPing(i.listenIP+port, addr, i.timeout)

Expand Down
23 changes: 17 additions & 6 deletions dns/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ var (
)

func init() {
host, _, _ := net.SplitHostPort(conf.Conf.ServerAddr)

//first init
blockList = loadRules("block", conf.Conf.BlockList)
whiteList = loadRules("white", conf.Conf.WhiteList)
suggestList = loadRules("suggest", conf.Conf.BlockList)
whiteList = loadRules("white", conf.Conf.WhiteList)
whiteList.Add(host)

conf.OnRefreash = append(conf.OnRefreash, func() error {
blockList = loadRules("block", conf.Conf.BlockList)
whiteList = loadRules("white", conf.Conf.WhiteList)
suggestList = loadRules("suggest", conf.Conf.Suggestions)
whiteList = loadRules("white", conf.Conf.WhiteList)
whiteList.Add(host)
return nil
})
}
Expand All @@ -38,9 +42,16 @@ func loadRules(name string, list []string) *util.Node {
func localA(r *dns.Msg, domain string, localIP net.IP) *dns.Msg {
m := new(dns.Msg)
m.SetReply(r)
m.Answer = []dns.RR{&dns.A{
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 20},
A: localIP,
}}
if localIP.To4() != nil {
m.Answer = []dns.RR{&dns.A{
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 20},
A: localIP,
}}
} else {
m.Answer = []dns.RR{&dns.AAAA{
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 20},
AAAA: localIP,
}}
}
return m
}
15 changes: 6 additions & 9 deletions proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package proxy

import (
"net"
"strings"

"github.com/golang/glog"
"github.com/wweir/sower/proxy/kcp"
Expand Down Expand Up @@ -30,14 +29,12 @@ func NewClient(netType string) Client {
}

func StartClient(netType, server, cipher, password, listenIP string) {
connCh := listenLocal(listenIP, []string{":80", ":443"})
connCh := listenLocal(listenIP, []string{"80", "443"})
client := NewClient(netType)
if idx := strings.Index(server, ":"); idx > 0 {
ips, err := net.LookupIP(server[:idx])
if err != nil || len(ips) == 0 {
glog.Fatalln(err, ips)
}
server = ips[0].String() + server[idx:]
if addr, err := net.ResolveTCPAddr("tcp", server); err != nil {
glog.Fatalln(err)
} else {
server = addr.String()
}

glog.Infoln("Client started.")
Expand All @@ -61,7 +58,7 @@ func listenLocal(listenIP string, ports []string) <-chan net.Conn {
connCh := make(chan net.Conn, 10)
for i := range ports {
go func(port string) {
ln, err := net.Listen("tcp", listenIP+port)
ln, err := net.Listen("tcp", net.JoinHostPort(listenIP, port))
if err != nil {
glog.Fatalln(err)
}
Expand Down
5 changes: 5 additions & 0 deletions proxy/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (

func StartHttpProxy(netType, server, cipher, password, addr string) {
client := NewClient(netType)
if addr, err := net.ResolveTCPAddr("tcp", server); err != nil {
glog.Fatalln(err)
} else {
server = addr.String()
}

srv := &http.Server{
Addr: addr,
Expand Down

0 comments on commit c209031

Please sign in to comment.