Skip to content

Commit 1f807fc

Browse files
committed
feat: upgrade goVirtualHost v1.3.0
1 parent 5e1d29a commit 1f807fc

File tree

17 files changed

+727
-207
lines changed

17 files changed

+727
-207
lines changed

src/goVirtualHost/const.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package goVirtualHost
2+
3+
const tcp46 = "tcp"
4+
const tcp4 = "tcp4"
5+
const tcp6 = "tcp6"
6+
const unix = "unix"
7+
8+
const ip4ver = 4
9+
const ip6ver = 6
10+
11+
const httpUrl = "http://"
12+
const httpsUrl = "https://"

src/goVirtualHost/errorwrapper.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package goVirtualHost
2+
3+
type errorWrapper struct {
4+
parent error
5+
message string
6+
}
7+
8+
func (wrapper errorWrapper) Error() string {
9+
return wrapper.message
10+
}
11+
12+
func (wrapper errorWrapper) Unwrap() error {
13+
return wrapper.parent
14+
}
15+
16+
func wrapError(parent error, message string) errorWrapper {
17+
return errorWrapper{parent, message}
18+
}

src/goVirtualHost/hostInfo.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,25 @@ package goVirtualHost
33
import "crypto/tls"
44

55
func (info *HostInfo) toParam(listen string, useTLS bool) *param {
6-
proto, addr := splitListen(listen, false)
6+
proto, ip, port := splitListen(listen, false)
77
var cert *tls.Certificate
88
if useTLS {
99
cert = info.Cert
1010
}
1111

1212
param := &param{
13-
proto: proto,
14-
addr: addr,
15-
useTLS: useTLS,
16-
cert: cert,
17-
handler: info.Handler,
13+
proto: proto,
14+
ip: ip,
15+
port: port,
16+
useTLS: useTLS,
17+
cert: cert,
1818
}
1919

2020
return param
2121
}
2222

23-
func (info *HostInfo) toParams() params {
24-
params := params{}
25-
26-
hostNames := normalizeHostNames(info.HostNames)
23+
func (info *HostInfo) parse() (hostNames []string, params params) {
24+
hostNames = normalizeHostNames(info.HostNames)
2725

2826
for _, listen := range info.Listens {
2927
param := info.toParam(listen, info.Cert != nil)
@@ -43,5 +41,5 @@ func (info *HostInfo) toParams() params {
4341
params = append(params, param)
4442
}
4543

46-
return params
44+
return
4745
}

src/goVirtualHost/ipaddr.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package goVirtualHost
2+
3+
import (
4+
"errors"
5+
"net"
6+
)
7+
8+
var unknownIPVersion = errors.New("unknown IP version")
9+
10+
func isPrivateIPv4(netIPv4 net.IP) bool {
11+
return netIPv4[0] == 10 ||
12+
(netIPv4[0] == 172 && netIPv4[1]&0xf0 == 16) ||
13+
(netIPv4[0] == 192 && netIPv4[1] == 168)
14+
}
15+
16+
func isPrivateIPv6(netIPv6 net.IP) bool {
17+
return netIPv6[0]&0xfe == 0xfc
18+
}
19+
20+
func newIPAddr(netIP net.IP) (*ipAddr, error) {
21+
var version int
22+
var isNonPrivate bool
23+
24+
if netIPv4 := netIP.To4(); netIPv4 != nil {
25+
version = ip4ver
26+
isNonPrivate = !isPrivateIPv4(netIPv4)
27+
} else if netIPv6 := netIP.To16(); netIPv6 != nil {
28+
version = ip6ver
29+
isNonPrivate = !isPrivateIPv6(netIPv6)
30+
} else {
31+
return nil, unknownIPVersion
32+
}
33+
34+
instance := &ipAddr{
35+
netIP: netIP,
36+
version: version,
37+
isGlobalUnicast: netIP.IsGlobalUnicast(),
38+
isLinkLocalUnicast: netIP.IsLinkLocalUnicast(),
39+
isNonPrivate: isNonPrivate,
40+
isNonLoopback: !netIP.IsLoopback(),
41+
}
42+
return instance, nil
43+
}
44+
45+
func (addr *ipAddr) String() string {
46+
if addr.version == ip6ver {
47+
return "[" + addr.netIP.String() + "]"
48+
} else {
49+
return addr.netIP.String()
50+
}
51+
}

src/goVirtualHost/ipaddr_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package goVirtualHost
2+
3+
import (
4+
"net"
5+
"testing"
6+
)
7+
8+
func TestIsPrivateIPv4(t *testing.T) {
9+
var ip net.IP
10+
11+
ip = net.IPv4(10, 1, 2, 3).To4()
12+
if !isPrivateIPv4(ip) {
13+
t.Error()
14+
}
15+
16+
ip = net.IPv4(172, 16, 2, 3).To4()
17+
if !isPrivateIPv4(ip) {
18+
t.Error()
19+
}
20+
21+
ip = net.IPv4(172, 17, 2, 3).To4()
22+
if !isPrivateIPv4(ip) {
23+
t.Error()
24+
}
25+
26+
ip = net.IPv4(172, 30, 2, 3).To4()
27+
if !isPrivateIPv4(ip) {
28+
t.Error()
29+
}
30+
31+
ip = net.IPv4(172, 31, 2, 3).To4()
32+
if !isPrivateIPv4(ip) {
33+
t.Error()
34+
}
35+
36+
ip = net.IPv4(192, 168, 4, 5).To4()
37+
if !isPrivateIPv4(ip) {
38+
t.Error()
39+
}
40+
41+
ip = net.IPv4(8, 8, 8, 8).To4()
42+
if isPrivateIPv4(ip) {
43+
t.Error()
44+
}
45+
}
46+
47+
func TestIsPrivateIPv6(t *testing.T) {
48+
var ip net.IP
49+
50+
ip = net.IP{0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
51+
if !isPrivateIPv6(ip) {
52+
t.Error()
53+
}
54+
55+
ip = net.IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
56+
if isPrivateIPv6(ip) {
57+
t.Error()
58+
}
59+
}

src/goVirtualHost/ipaddrs.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package goVirtualHost
2+
3+
func (addrs ipAddrs) Len() int {
4+
return len(addrs)
5+
}
6+
7+
func (addrs ipAddrs) Less(i, j int) bool {
8+
addr1 := addrs[i]
9+
addr2 := addrs[j]
10+
11+
if addr1.isGlobalUnicast != addr2.isGlobalUnicast {
12+
return addr1.isGlobalUnicast
13+
}
14+
15+
if addr1.isLinkLocalUnicast != addr2.isLinkLocalUnicast {
16+
return addr1.isLinkLocalUnicast
17+
}
18+
19+
if addr1.isNonPrivate != addr2.isNonPrivate {
20+
return addr1.isNonPrivate
21+
}
22+
23+
if addr1.isNonLoopback != addr2.isNonLoopback {
24+
return addr1.isNonLoopback
25+
}
26+
27+
if addr1.version != addr2.version {
28+
return addr1.version == ip4ver
29+
}
30+
31+
return i < j
32+
}
33+
34+
func (addrs ipAddrs) Swap(i, j int) {
35+
addrs[i], addrs[j] = addrs[j], addrs[i]
36+
}
37+
38+
func (addrs ipAddrs) String() []string {
39+
results := make([]string, len(addrs))
40+
for i := range addrs {
41+
results[i] = addrs[i].String()
42+
}
43+
return results
44+
}

src/goVirtualHost/listener.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@ import (
55
"os"
66
)
77

8-
func newListener(proto, addr string) *listener {
8+
func newListener(proto, ip, port string) *listener {
99
listener := &listener{
1010
proto: proto,
11-
addr: addr,
11+
ip: ip,
12+
port: port,
1213
}
1314

1415
return listener
1516
}
1617

1718
func (listener *listener) open() error {
19+
addr := listener.ip + listener.port
1820
if listener.proto == "unix" {
19-
sockInfo, _ := os.Lstat(listener.addr)
21+
sockInfo, _ := os.Lstat(addr)
2022
if sockInfo != nil && (sockInfo.Mode()&os.ModeSocket != 0) {
21-
os.Remove(listener.addr)
23+
os.Remove(addr)
2224
}
2325
}
2426

25-
netListener, err := net.Listen(listener.proto, listener.addr)
27+
netListener, err := net.Listen(listener.proto, addr)
2628
listener.netListener = netListener
2729

2830
if listener.proto == "unix" && err == nil {
29-
os.Chmod(listener.addr, 0777)
31+
os.Chmod(addr, 0777)
3032
}
3133

3234
return err

src/goVirtualHost/listeners.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package goVirtualHost
22

3-
func (listeners listeners) find(proto, addr string) *listener {
3+
func (listeners listeners) find(proto, ip, port string) *listener {
44
for _, l := range listeners {
5-
if l.proto == proto && l.addr == addr {
5+
if l.proto == proto && l.ip == ip && l.port == port {
66
return l
77
}
88
}

src/goVirtualHost/param.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package goVirtualHost
22

3-
import "fmt"
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var CertificateNotFound = errors.New("certificate not found for TLS listens")
49

510
func (param *param) hasHostNames(checkHostNames []string) bool {
611
if len(param.hostNames) == 0 || len(checkHostNames) == 0 {
@@ -19,7 +24,7 @@ func (param *param) hasHostNames(checkHostNames []string) bool {
1924

2025
func (param *param) validate() (errs []error) {
2126
if param.useTLS && param.cert == nil {
22-
err := fmt.Errorf("certificate not found for TLS listens: %+v", param)
27+
err := wrapError(CertificateNotFound, fmt.Sprintf("certificate not found for TLS listens: %+v", param))
2328
errs = append(errs, err)
2429
}
2530

src/goVirtualHost/param_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package goVirtualHost
2+
3+
import (
4+
"crypto/tls"
5+
"errors"
6+
"testing"
7+
)
8+
9+
func TestParamValidate(t *testing.T) {
10+
var p *param
11+
var errs []error
12+
13+
p = &param{
14+
proto: "tcp",
15+
ip: "",
16+
port: "80",
17+
}
18+
errs = p.validate()
19+
if len(errs) > 0 {
20+
t.Error()
21+
}
22+
23+
p.useTLS = true
24+
errs = p.validate()
25+
if len(errs) == 0 {
26+
t.Error()
27+
} else if !errors.Is(errs[0], CertificateNotFound) {
28+
t.Error()
29+
}
30+
31+
p.cert = &tls.Certificate{}
32+
errs = p.validate()
33+
if len(errs) > 0 {
34+
t.Error()
35+
}
36+
}

0 commit comments

Comments
 (0)