forked from bepass-org/bepass-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay.go
163 lines (138 loc) · 3.21 KB
/
relay.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
155
156
157
158
159
160
161
162
163
// Relay is a package that provides functionality for relaying network traffic.
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net"
"net/netip"
"strings"
)
const BUFFER_SIZE = 256 * 1024
type Server struct {
host string
port string
}
type Client struct {
conn net.Conn
}
type Config struct {
Host string
Port string
}
func New(config *Config) *Server {
return &Server{
host: config.Host,
port: config.Port,
}
}
func (server *Server) Run() {
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%s", server.host, server.port))
if err != nil {
log.Fatal(err)
}
defer func() {
_ = listener.Close()
}()
for {
conn, err := listener.Accept()
if err != nil {
log.Fatal(err)
}
src, err := netip.ParseAddrPort(conn.RemoteAddr().String())
if err != nil {
log.Printf("unable to parse host %v", conn.RemoteAddr())
_ = conn.Close()
continue
}
// Check if srcIP is in the whitelist
if !connFilter.isSourceAllowed(src.Addr()) {
log.Printf("blocked connection from: %v", src)
conn.Close()
continue
}
go (&Client{conn: conn}).handleRequest()
}
}
func (client *Client) handleRequest() {
defer func() {
_ = client.conn.Close()
}()
reader := bufio.NewReader(client.conn)
header, _ := reader.ReadBytes(byte(13))
if len(header) < 1 {
return
}
inputHeader := strings.Split(string(header[:len(header)-1]), "@")
if len(inputHeader) < 2 {
return
}
network := "tcp"
if inputHeader[0] == "udp" {
network = "udp"
}
address := strings.Replace(inputHeader[1], "$", ":", -1)
if strings.Contains(address, "temp-mail.org") {
return
}
dh, _, err := net.SplitHostPort(address)
if err != nil {
return
}
// check if ip is not blocked
blockFlag := false
addr, err := netip.ParseAddr(dh)
if err != nil {
// the host may not be an IP, try to resolve it
ips, err := net.LookupIP(dh)
if err != nil {
return
}
// parse the first IP and use it
addr, _ = netip.AddrFromSlice(ips[0])
}
// If the address is invalid or not allowed as a destination, set the block flag.
blockFlag = !addr.IsValid() || !connFilter.isDestinationAllowed(addr)
if blockFlag {
log.Printf("destination host is blocked: %s\n", address)
return
}
if network == "udp" {
handleUDPOverTCP(client.conn, address)
return
}
// transmit data
log.Printf("%s Dialing to %s...\n", network, address)
rConn, err := net.Dial(network, address)
if err != nil {
log.Println(fmt.Errorf("failed to connect to socket: %v", err))
return
}
defer func() {
_ = rConn.Close()
}()
// transmit data
go Copy(client.conn, rConn)
Copy(rConn, client.conn)
}
// Copy reads from src and writes to dst until either EOF is reached on src or
// an error occurs. It returns the number of bytes copied and any error
// encountered. Copy uses a fixed-size buffer to efficiently copy data between
// the source and destination.
func Copy(src io.Reader, dst io.Writer) {
buf := make([]byte, BUFFER_SIZE)
_, err := io.CopyBuffer(dst, src, buf[:cap(buf)])
if err != nil {
fmt.Println(err)
}
}
func main() {
var config Config
flag.StringVar(&config.Host, "b", "0.0.0.0", "Server IP address")
flag.StringVar(&config.Port, "p", "6666", "Server Port number")
flag.Parse()
server := New(&config)
server.Run()
}