Skip to content

Commit 7368eb9

Browse files
authored
Merge pull request #10 from wongoo/add-utils
add utilities: gxstrings.RegSplit & gxnet.GetLocalIP
2 parents 8be8485 + 71e7785 commit 7368eb9

5 files changed

Lines changed: 238 additions & 21 deletions

File tree

net/net.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package gxnet
19+
20+
import (
21+
"net"
22+
"strings"
23+
)
24+
25+
import (
26+
perrors "github.com/pkg/errors"
27+
)
28+
29+
var (
30+
privateBlocks []*net.IPNet
31+
)
32+
33+
func init() {
34+
for _, b := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} {
35+
if _, block, err := net.ParseCIDR(b); err == nil {
36+
privateBlocks = append(privateBlocks, block)
37+
}
38+
}
39+
}
40+
41+
func GetLocalIP() (string, error) {
42+
faces, err := net.Interfaces()
43+
if err != nil {
44+
return "", perrors.WithStack(err)
45+
}
46+
47+
var addr net.IP
48+
for _, face := range faces {
49+
if !isValidNetworkInterface(face) {
50+
continue
51+
}
52+
53+
addrs, err := face.Addrs()
54+
if err != nil {
55+
return "", perrors.WithStack(err)
56+
}
57+
58+
if ipv4, ok := getValidIPv4(addrs); ok {
59+
addr = ipv4
60+
if isPrivateIP(ipv4) {
61+
return ipv4.String(), nil
62+
}
63+
}
64+
}
65+
66+
if addr == nil {
67+
return "", perrors.Errorf("can not get local IP")
68+
}
69+
70+
return addr.String(), nil
71+
}
72+
73+
func isPrivateIP(ip net.IP) bool {
74+
for _, priv := range privateBlocks {
75+
if priv.Contains(ip) {
76+
return true
77+
}
78+
}
79+
return false
80+
}
81+
82+
func getValidIPv4(addrs []net.Addr) (net.IP, bool) {
83+
for _, addr := range addrs {
84+
var ip net.IP
85+
86+
switch v := addr.(type) {
87+
case *net.IPNet:
88+
ip = v.IP
89+
case *net.IPAddr:
90+
ip = v.IP
91+
}
92+
93+
if ip == nil || ip.IsLoopback() {
94+
continue
95+
}
96+
97+
ip = ip.To4()
98+
if ip == nil {
99+
// not an valid ipv4 address
100+
continue
101+
}
102+
103+
return ip, true
104+
}
105+
return nil, false
106+
}
107+
108+
func isValidNetworkInterface(face net.Interface) bool {
109+
if face.Flags&net.FlagUp == 0 {
110+
// interface down
111+
return false
112+
}
113+
114+
if face.Flags&net.FlagLoopback != 0 {
115+
// loopback interface
116+
return false
117+
}
118+
119+
if strings.Contains(strings.ToLower(face.Name), "docker") {
120+
return false
121+
}
122+
123+
return true
124+
}

net/net_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package gxnet
19+
20+
import (
21+
"testing"
22+
)
23+
24+
import (
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestGetLocalIP(t *testing.T) {
29+
ip, err := GetLocalIP()
30+
assert.NoError(t, err)
31+
t.Log(ip)
32+
}

strings/nil.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

strings/strings.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package gxstrings
19+
20+
import (
21+
"reflect"
22+
"regexp"
23+
)
24+
25+
func IsNil(i interface{}) bool {
26+
if i == nil {
27+
return true
28+
}
29+
30+
if reflect.ValueOf(i).IsNil() {
31+
return true
32+
}
33+
34+
return false
35+
}
36+
37+
func RegSplit(text string, regexSplit string) []string {
38+
reg := regexp.MustCompile(regexSplit)
39+
indexes := reg.FindAllStringIndex(text, -1)
40+
lastStart := 0
41+
result := make([]string, len(indexes)+1)
42+
for i, element := range indexes {
43+
result[i] = text[lastStart:element[0]]
44+
lastStart = element[1]
45+
}
46+
result[len(indexes)] = text[lastStart:]
47+
return result
48+
}

strings/strings_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package gxstrings
19+
20+
import (
21+
"testing"
22+
)
23+
24+
import (
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func Test_RegSplit(t *testing.T) {
29+
strings := RegSplit("dubbo://123.1.2.1;jsonrpc://127.0.0.1;registry://3.2.1.3?registry=zookeeper", "\\s*[;]+\\s*")
30+
assert.Len(t, strings, 3)
31+
assert.Equal(t, "dubbo://123.1.2.1", strings[0])
32+
assert.Equal(t, "jsonrpc://127.0.0.1", strings[1])
33+
assert.Equal(t, "registry://3.2.1.3?registry=zookeeper", strings[2])
34+
}

0 commit comments

Comments
 (0)