-
Notifications
You must be signed in to change notification settings - Fork 0
/
frwd.go
154 lines (132 loc) · 3 KB
/
frwd.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
146
147
148
149
150
151
152
153
154
package main
import (
"flag"
"fmt"
"io"
"log"
"net"
"os"
"regexp"
)
type connectionData struct {
err error
n int64
}
var version_ = []int{0, 1, 1}
var udp = flag.Bool("u", false, "forward UDP")
var verbose = flag.Bool("v", false, "verbose")
var filter = flag.String("filter", "", "Regex connection filter (TCP only)")
var refilter *regexp.Regexp = nil
func usage() {
fmt.Fprintf(os.Stderr, "frwd version %d.%d.%d\n", version_[0], version_[1], version_[2])
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] [local-ip]:<local-port> [target-ip]:<target-port>\n", os.Args[0])
flag.PrintDefaults()
os.Exit(3)
}
func info(format string, a ...interface{}) {
if *verbose {
log.Printf("INFO "+format, a...)
}
}
func fail(format string, a ...interface{}) {
log.Printf("ERROR "+format, a...)
}
func copy(i net.Conn, o net.Conn, ev chan connectionData) {
n, err := io.Copy(i, o)
ev <- connectionData{err: err, n: n}
}
func tcpForward(inc net.Conn, outadr string) {
remote := inc.RemoteAddr()
if refilter != nil && !refilter.MatchString(remote.String()) {
info("Rejecting TCP connection from %v...\n", remote)
inc.Close()
return
} else {
info("TCP connection from %v...\n", remote)
}
inadr := inc.LocalAddr().String()
outc, err := net.Dial("tcp", outadr)
if err != nil {
fail("failed to open %s -> %s: %v\n", inadr, outadr, err)
inc.Close()
} else {
info("Connected %s -> %s\n", inadr, outadr)
ev := make(chan connectionData)
go copy(inc, outc, ev)
go copy(outc, inc, ev)
cd1 := <-ev
if cd1.err != nil {
fail("%v\n", err)
}
inc.Close()
outc.Close()
cd2 := <-ev // wait for the other endpoint
close(ev)
info("TCP %s -> %s, data %d/%d\n", inadr, outadr, cd1.n, cd2.n)
}
}
func tcpServe(inadr, outadr string) error {
in, err := net.Listen("tcp", inadr)
if err != nil {
return err
}
for {
conn, err := in.Accept()
if err != nil {
fail("incoming failed: %v\n", err)
} else {
go tcpForward(conn, outadr)
}
}
}
func udpServe(inadrstr, outadrstr string) error {
inadr, err := net.ResolveUDPAddr("udp", inadrstr)
if err != nil {
return err
}
outadr, err := net.ResolveUDPAddr("udp", outadrstr)
if err != nil {
return err
}
buffer := make([]byte, 2048)
conn, err := net.ListenUDP("udp", inadr)
if err != nil {
return err
}
defer conn.Close()
for {
n, adrin, err := conn.ReadFromUDP(buffer)
if err != nil {
fail("when receiving UDP: %v\n", err)
} else {
info("Connection from %v...\n", adrin)
_, err = conn.WriteToUDP(buffer[:n], outadr)
if err != nil {
fail("when sending UDP: %v\n", err)
}
info("TCP %v -> %v, data %d\n", adrin, outadr, n)
}
}
}
func main() {
var err error
if flag.Parse(); flag.NArg() != 2 {
usage()
}
if *filter != "" {
refilter, err = regexp.Compile(*filter)
if err != nil {
log.Fatal(err)
}
info("Using filter %v\n", refilter)
}
inadr, outadr := flag.Arg(0), flag.Arg(1)
if *udp {
err = udpServe(inadr, outadr)
} else {
err = tcpServe(inadr, outadr)
}
if err != nil {
log.Fatal(err)
}
}