Skip to content

Commit fed6f08

Browse files
committed
optimize.
1 parent 995f56d commit fed6f08

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

controller/proxy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var (
3333
},
3434
redir: func(host string) (proxyI.Server, error) {
3535
if runtime.GOOS == "windows" {
36-
return nil, fmt.Errorf("Redir not support windows")
36+
return nil, fmt.Errorf("redir not support windows")
3737
}
3838
return proxyI.NewTCPServer(host, redirserver.RedirHandle())
3939
},

net/match/cidr.go

+15-17
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ type Cidr struct {
1717
}
1818

1919
func ipv4toInt(ip net.IP) string {
20-
return fmt.Sprintf("%032b", binary.BigEndian.Uint32(ip.To4()))
20+
return fmt.Sprintf("%032b", binary.BigEndian.Uint32(ip)) // there ip is ip.To4()
2121
}
2222

2323
func ipv6toInt(ip net.IP) string {
24-
if ip == nil {
25-
return ""
26-
}
2724
// from http://golang.org/pkg/net/#pkg-constants
2825
// IPv6len = 16
29-
return fmt.Sprintf("%0128b", big.NewInt(0).SetBytes(ip.To16()))
26+
return fmt.Sprintf("%0128b", big.NewInt(0).SetBytes(ip)) // there ip is ip.To16()
3027
}
3128

3229
// InsetOneCIDR Insert one CIDR to cidr matcher
@@ -36,11 +33,11 @@ func (c *Cidr) Insert(cidr string, mark interface{}) error {
3633
return err
3734
}
3835
maskSize, _ := ipNet.Mask.Size()
39-
40-
if ipNet.IP.To4() != nil {
41-
c.v4CidrTrie.Insert(ipv4toInt(ipNet.IP)[:maskSize], mark)
36+
x := ipNet.IP.To4()
37+
if x != nil {
38+
c.v4CidrTrie.Insert(ipv4toInt(x)[:maskSize], mark)
4239
} else {
43-
c.v6CidrTrie.Insert(ipv6toInt(ipNet.IP)[:maskSize], mark)
40+
c.v6CidrTrie.Insert(ipv6toInt(ipNet.IP.To16())[:maskSize], mark)
4441
}
4542
return nil
4643
}
@@ -64,14 +61,15 @@ func (c *Cidr) singleSearch(ip string) (ok bool, mark interface{}) {
6461

6562
// MatchWithTrie match ip with trie
6663
func (c *Cidr) Search(ip string) (isMatch bool, mark interface{}) {
67-
ip2 := net.ParseIP(ip)
68-
if ip2 == nil {
64+
iP := net.ParseIP(ip)
65+
if iP == nil {
6966
return false, nil
7067
}
71-
if ip2.To4() != nil {
72-
return c.v4CidrTrie.Search(ipv4toInt(ip2))
68+
x := iP.To4()
69+
if iP.To4() != nil {
70+
return c.v4CidrTrie.Search(ipv4toInt(x))
7371
} else {
74-
return c.v6CidrTrie.Search(ipv6toInt(ip2))
72+
return c.v6CidrTrie.Search(ipv6toInt(iP.To16()))
7573
}
7674
}
7775

@@ -102,7 +100,7 @@ type cidrNode struct {
102100
// Insert insert node to tree
103101
func (t *Trie) Insert(str string, mark interface{}) {
104102
nodeTemp := t.root
105-
for i := 0; i < len(str); i++ {
103+
for i := range str {
106104
// 1 byte is 49
107105
if str[i] == 49 {
108106
if nodeTemp.right == nil {
@@ -128,8 +126,8 @@ func (t *Trie) Insert(str string, mark interface{}) {
128126

129127
// Search search from trie tree
130128
func (t *Trie) Search(str string) (isMatch bool, mark interface{}) {
131-
nodeTemp, l := t.root, len(str)
132-
for i := 0; i < l; i++ {
129+
nodeTemp := t.root
130+
for i := range str {
133131
if str[i] == 49 {
134132
nodeTemp = nodeTemp.right
135133
}

net/match/domain.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ func (d *Domain) insert(root *domainNode, mark interface{}, domain []string) {
5353
}
5454
}
5555

56-
func (d *Domain) SearchFlip(domain string) (isMatcher bool, mark interface{}) {
56+
func (d *Domain) SearchFlip(domain string) (ok bool, mark interface{}) {
5757
domainDiv := strings.Split(domain, ".")
58-
isMatcher, mark = d.search(d.root, domainDiv)
59-
if isMatcher {
60-
return isMatcher, mark
58+
ok, mark = d.search(d.root, domainDiv)
59+
if ok {
60+
return ok, mark
6161
}
6262
return d.search(d.wildcardRoot, domainDiv)
6363
}

net/proxy/shadowsocks/client/shadowsocksclient.go

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type shadowsocks struct {
2727
pluginFunc func(conn net.Conn) net.Conn
2828

2929
cache []net.IP
30+
ip bool
3031
}
3132

3233
func NewShadowsocks(cipherName string, password string, server, port string, plugin, pluginOpt string) (*shadowsocks, error) {
@@ -41,6 +42,7 @@ func NewShadowsocks(cipherName string, password string, server, port string, plu
4142
plugin: strings.ToUpper(plugin),
4243
pluginOpt: pluginOpt,
4344
cache: []net.IP{},
45+
ip: net.ParseIP(server) != nil,
4446
}
4547
switch strings.ToLower(plugin) {
4648
case OBFS:
@@ -82,6 +84,9 @@ func (s *shadowsocks) Conn(host string) (conn net.Conn, err error) {
8284
}
8385

8486
func (s *shadowsocks) getTCPConn() (net.Conn, error) {
87+
if s.ip {
88+
return net.Dial("tcp", net.JoinHostPort(s.server, s.port))
89+
}
8590
conn, err := s.tcpDial()
8691
if err == nil {
8792
return conn, err

net/proxy/shadowsocksr/client/ssrclient.go

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Shadowsocksr struct {
2929
protocolData interface{}
3030

3131
cache []net.IP
32+
ip bool
3233
}
3334

3435
func NewShadowsocksrClient(host, port, method, password, obfs, obfsParam, protocol, protocolParam string) (ssr *Shadowsocksr, err error) {
@@ -41,6 +42,9 @@ func NewShadowsocksrClient(host, port, method, password, obfs, obfsParam, protoc
4142
obfsParam: obfsParam,
4243
protocol: protocol,
4344
protocolParam: protocolParam,
45+
46+
cache: []net.IP{},
47+
ip: net.ParseIP(host) != nil,
4448
}
4549
s.protocolData = new(Protocol.AuthData)
4650
return s, nil
@@ -113,6 +117,9 @@ func (s *Shadowsocksr) Conn(addr string) (net.Conn, error) {
113117
}
114118

115119
func (s *Shadowsocksr) getTCPConn() (net.Conn, error) {
120+
if s.ip {
121+
return net.Dial("tcp", net.JoinHostPort(s.host, s.port))
122+
}
116123
conn, err := s.tcpDial()
117124
if err == nil {
118125
return conn, err

0 commit comments

Comments
 (0)