forked from shawwwn/Gole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
145 lines (123 loc) · 3.02 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package gole
import (
"bytes"
"fmt"
"io"
"net"
"os"
"github.com/xtaci/smux"
"golang.org/x/net/ipv4"
)
func perror(a ...interface{}) {
fmt.Fprintln(os.Stderr, a...)
}
func contains(s string, ss []string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}
func bound(val int, low int, high int) int {
if val < low {
val = low
} else if val > high {
val = high
}
return val
}
func PrintDbgf(format string, a ...interface{}) (n int, err error) {
if g_verbose {
return fmt.Printf(format, a...)
}
return 0, nil
}
// Forward data between @conn and @stream util one of them
// calls close() or error out.
func conn2stream(conn net.Conn, stream *smux.Stream) {
var n_recv = make(chan int64, 1)
var n_send = make(chan int64, 1)
var copyIO = func(dst, src io.ReadWriteCloser, count chan int64) {
defer src.Close()
defer dst.Close()
buf := make([]byte, 4096)
c, err := io.CopyBuffer(dst, src, buf)
count <- c
if err != nil {
return
}
}
go copyIO(stream, conn, n_send)
go copyIO(conn, stream, n_recv)
PrintDbgf("stream close(%d): send:%d, recv:%d\n", stream.ID(), <-n_send, <-n_recv)
}
// Forward data between @conn and @stream util one of them
// calls close() or error out.
func stream2conn(stream *smux.Stream, conn net.Conn) {
var n_recv = make(chan int64, 1)
var n_send = make(chan int64, 1)
var copyIO = func(dst, src io.ReadWriteCloser, count chan int64) {
defer src.Close()
defer dst.Close()
buf := make([]byte, 4096)
c, err := io.CopyBuffer(dst, src, buf)
count <- c
if err != nil {
return
}
}
go copyIO(stream, conn, n_send)
go copyIO(conn, stream, n_recv)
PrintDbgf("stream close(%d): send:%d, recv:%d\n", stream.ID(), <-n_send, <-n_recv)
}
func conn2conn(fwd_conn net.Conn, conn net.Conn) {
var n_recv = make(chan int64, 1)
var n_send = make(chan int64, 1)
var copyIO = func(dst, src io.ReadWriteCloser, count chan int64) {
defer src.Close()
defer dst.Close()
c, err := io.Copy(dst, src)
count <- c
if err != nil {
fmt.Println(err)
return
}
}
go copyIO(conn, fwd_conn, n_recv)
go copyIO(fwd_conn, conn, n_send)
fmt.Printf("stream close: send:%d, recv:%d\n", <-n_send, <-n_recv)
}
// check if two UDPAddr is equal
func UDPAddrEqual(a, b *net.UDPAddr) bool {
return a.Port == b.Port && bytes.Equal(a.IP, b.IP) && a.Zone == b.Zone
}
// setDSCP for EConn and EPacketConn
func SetDSCP(conn net.Conn, dscp int) error {
switch nc := conn.(type) {
case *EConnXor:
return ipv4.NewConn(nc.Conn()).SetTOS(dscp << 2)
case *EPacketConnXor:
return ipv4.NewConn(nc.Conn()).SetTOS(dscp << 2)
}
return ipv4.NewConn(conn).SetTOS(dscp << 2)
}
// convert an interface name/hostname/url/ip to an ip address
func parseIP(address string) net.IP {
var addrs []net.Addr
itf, err := net.InterfaceByName(address)
if err != nil {
goto End
}
addrs, err = itf.Addrs()
if err != nil {
goto End
}
return addrs[0].(*net.IPNet).IP
End:
ipaddr, err := net.ResolveIPAddr("ip", address)
if err != nil {
return net.ParseIP(address)
}
return ipaddr.IP
}