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
|
package main
import (
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
)
func main() {
log.SetPrefix("gProxy: ")
fmt.Println("Starting gProxy...")
var localAddr string
var remoteAddr string
getArgs(&localAddr, &remoteAddr)
addr, err := net.ResolveTCPAddr("tcp", localAddr)
if err != nil {
panic(err)
}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
panic(err)
}
fmt.Println("Listening for connections...")
for {
conn, err := listener.AcceptTCP()
if err != nil {
panic(err)
}
go proxyConn(conn, remoteAddr)
}
}
func proxyConn(conn *net.TCPConn, remoteAddr string) {
fmt.Println("New connection from", conn.RemoteAddr())
defer conn.Close()
rAddr, err := net.ResolveTCPAddr("tcp", remoteAddr)
if err != nil {
panic(err)
}
rConn, err := net.DialTCP("tcp", nil, rAddr)
if err != nil {
panic(err)
}
defer rConn.Close()
go func() {
log.Println("waiting for data from client")
data := make([]byte, 1024)
for {
n, err := conn.Read(data)
if err != nil {
break
}
_, err = rConn.Write(data[:n])
if err != nil {
break
}
log.Printf("sent:\n%v\nend of send\n", hex.Dump(data[:n]))
}
}()
data := make([]byte, 1024)
for {
n, err := rConn.Read(data)
if err != nil {
break
}
_, err = conn.Write(data[:n])
if err != nil {
break
}
log.Printf("recv:\n%v\nend of recv\n", hex.Dump(data[:n]))
}
fmt.Println("Closed connection from", conn.RemoteAddr())
}
func getArgs(localAddr *string, remoteAddr *string) {
flag.StringVar(localAddr, "l", "0.0.0.0:9999", "local address")
flag.StringVar(remoteAddr, "r", "", "remote address")
var dbg = flag.Bool("d", false, "debug info")
flag.Parse()
if *remoteAddr == "" {
usage()
os.Exit(1)
}
if *dbg == false {
log.SetOutput(ioutil.Discard)
}
}
func usage() {
fmt.Println("./proxy -r [remote addr] (-d)")
}
|