This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (80 loc) · 1.57 KB
/
main.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
package main
import (
"log"
"net"
"os"
"github.com/google/uuid"
)
var CONN_PORT = "8081"
var DEST_HOST = "localhost"
var DEST_PORT = "25565"
var CLIENTS = make(map[uuid.UUID]net.Conn)
func main() {
l, err := net.Listen("tcp", "0.0.0.0"+":"+CONN_PORT)
if err != nil {
panic(err)
}
log.Print("> edge-proxy started")
log.Printf("? listening on %s", CONN_PORT)
log.Printf("? forwarding to %s:%s", DEST_HOST, DEST_PORT)
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
panic(err)
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
uuid := uuid.New()
CLIENTS[uuid] = conn
log.Printf("[%s] connected (%s), %d clients connected", uuid, conn.RemoteAddr(), len(CLIENTS))
defer func() {
conn.Close()
delete(CLIENTS, uuid)
log.Printf("[%s] disconnected (%s), %d clients connected", uuid, conn.RemoteAddr(), len(CLIENTS))
}()
destConn, err := net.Dial("tcp", DEST_HOST+":"+DEST_PORT)
if err != nil {
conn.Close()
return
}
go func() {
defer func() {
conn.Close()
delete(CLIENTS, uuid)
destConn.Close()
}()
buf := make([]byte, 1024)
for {
n, err := conn.Read(buf)
if err != nil {
return
}
destConn.Write(buf[:n])
}
}()
buf := make([]byte, 1024)
for {
n, err := destConn.Read(buf)
if err != nil {
return
}
conn.Write(buf[:n])
}
}
func init() {
port := os.Getenv("PORT")
if port != "" {
CONN_PORT = port
}
dest := os.Getenv("MC_SERVER_HOST")
if dest != "" {
DEST_HOST = dest
}
dest = os.Getenv("MC_SERVER_PORT")
if dest != "" {
DEST_PORT = dest
}
}