Skip to content

Commit

Permalink
Use dnspool.Dial
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Jan 15, 2013
1 parent d3073dc commit a44ffcb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cmd/shadowsocks-httpget/httpget.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func get(connid int, uri, serverAddr string, rawAddr []byte, enctbl *ss.EncryptT
}
reqTime[reqDone] = time.Now().Sub(start)

if (reqDone+1)%1000 == 0 {
if (reqDone+1)%100 == 0 {
fmt.Printf("conn %d finished %d get requests\n", connid, reqDone+1)
}
}
Expand Down
32 changes: 3 additions & 29 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,7 @@ var errAddrType = errors.New("addr type not supported")

const dnsGoroutineNum = 64

func dial(hostPort string, isIP bool) (c net.Conn, err error) {
// return net.Dial("tcp", host)
if isIP {
return net.Dial("tcp", hostPort)
}

var addrs []string
var host, port string

if host, port, err = net.SplitHostPort(hostPort); err != nil {
log.Println("Internal error: host should always has port specified")
return
}
if addrs, err = dnspool.LookupHost(host); err != nil {
return
}
for _, ip := range addrs {
ipHost := net.JoinHostPort(ip, port)
if c, err = net.Dial("tcp", ipHost); err == nil {
return
}
}
return nil, err
}

func getRequest(conn *ss.Conn) (host string, extra []byte, isIP bool, err error) {
func getRequest(conn *ss.Conn) (host string, extra []byte, err error) {
const (
idType = 0 // address type index
idIP0 = 1 // ip addres start index
Expand Down Expand Up @@ -101,7 +76,6 @@ func getRequest(conn *ss.Conn) (host string, extra []byte, isIP bool, err error)
} else if buf[idType] == typeIPv4 {
addrIp := net.IPv4(buf[idIP0], buf[idIP0+1], buf[idIP0+2], buf[idIP0+3])
host = addrIp.String()
isIP = true
}
// parse port
port := binary.BigEndian.Uint16(buf[reqLen-2 : reqLen])
Expand Down Expand Up @@ -139,13 +113,13 @@ func handleConnection(conn *ss.Conn) {
conn.Close()
}()

host, extra, isIP, err := getRequest(conn)
host, extra, err := getRequest(conn)
if err != nil {
log.Println("error getting request:", err)
return
}
debug.Println("connecting", host)
remote, err := dial(host, isIP)
remote, err := dnspool.Dial(host)
if err != nil {
if ne, ok := err.(*net.OpError); ok && (ne.Err == syscall.EMFILE || ne.Err == syscall.ENFILE) {
// log too many open file error
Expand Down

0 comments on commit a44ffcb

Please sign in to comment.