-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcp.go
77 lines (70 loc) · 1.45 KB
/
tcp.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
package tsproxy
import (
"io"
"log"
"net"
"sync"
)
type TailscaleTCPProxyServer struct {
server *TailscaleProxyServer
}
func NewTailscaleTCPProxyServer(server *TailscaleProxyServer) Server {
return &TailscaleTCPProxyServer{
server: server,
}
}
func (tps *TailscaleTCPProxyServer) Serve(ln net.Listener) error {
for {
select {
case <-tps.server.ctx.Done():
break
default:
conn, err := ln.Accept()
if err != nil {
log.Printf("error/accept: %s", err.Error())
continue
}
log.Printf("got tcp conn")
go handleTCPConn(tps.server, conn, nil)
}
}
}
var bufferPool = sync.Pool{
New: func() interface{} {
// TODO maybe different buffer size?
// benchmark pls
return make([]byte, 1<<15)
},
}
func handleTCPConn(server *TailscaleProxyServer, c1 net.Conn, c2 net.Conn) {
var err error
if c2 == nil {
c2, err = server.Dial("whatever", "whatever")
if err != nil {
log.Print(err)
c1.Close()
log.Printf("disconnected %v", c1.RemoteAddr())
return
}
}
first := make(chan<- struct{}, 1)
cp := func(dst net.Conn, src net.Conn) {
buf := bufferPool.Get().([]byte)
defer bufferPool.Put(buf)
// TODO use splice on linux
// TODO needs some timeout to prevent torshammer ddos
_, err := io.CopyBuffer(dst, src, buf)
select {
case first <- struct{}{}:
if err != nil {
log.Print(err)
}
dst.Close()
src.Close()
log.Printf("disconnected %v", c1.RemoteAddr())
default:
}
}
go cp(c1, c2)
cp(c2, c1)
}